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
31 #include "wx/generic/buttonbar.h"
38 #include "wx/dcclient.h"
39 #include "wx/settings.h"
44 // ----------------------------------------------------------------------------
45 // wxButtonToolBarTool: our implementation of wxToolBarToolBase
46 // ----------------------------------------------------------------------------
48 class WXDLLEXPORT wxButtonToolBarTool
: public wxToolBarToolBase
51 wxButtonToolBarTool(wxButtonToolBar
*tbar
,
53 const wxString
& label
,
54 const wxBitmap
& bmpNormal
,
55 const wxBitmap
& bmpDisabled
,
58 const wxString
& shortHelp
,
59 const wxString
& longHelp
)
60 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
61 clientData
, shortHelp
, longHelp
)
63 m_x
= m_y
= wxDefaultCoord
;
70 wxButtonToolBarTool(wxButtonToolBar
*tbar
, wxControl
*control
)
71 : wxToolBarToolBase(tbar
, control
)
73 m_x
= m_y
= wxDefaultCoord
;
79 wxBitmapButton
* GetButton() const { return m_button
; }
80 void SetButton(wxBitmapButton
* button
) { m_button
= button
; }
83 // the tool position (for controls)
90 // the control representing the button
91 wxBitmapButton
* m_button
;
94 // ============================================================================
95 // wxButtonToolBar implementation
96 // ============================================================================
98 IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar
, wxControl
)
100 BEGIN_EVENT_TABLE(wxButtonToolBar
, wxControl
)
101 EVT_BUTTON(wxID_ANY
, wxButtonToolBar::OnCommand
)
102 EVT_PAINT(wxButtonToolBar::OnPaint
)
103 EVT_LEFT_UP(wxButtonToolBar::OnLeftUp
)
106 // ----------------------------------------------------------------------------
107 // wxButtonToolBar creation
108 // ----------------------------------------------------------------------------
110 void wxButtonToolBar::Init()
113 m_needsLayout
= false;
115 // unknown widths for the tools and separators
116 m_widthSeparator
= wxDefaultCoord
;
118 m_maxWidth
= m_maxHeight
= 0;
127 bool wxButtonToolBar::Create(wxWindow
*parent
,
132 const wxString
& name
)
134 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
135 wxDefaultValidator
, name
) )
140 // wxColour lightBackground(244, 244, 244);
142 wxFont
font(wxSMALL_FONT
->GetPointSize(), wxNORMAL_FONT
->GetFamily(), wxNORMAL_FONT
->GetStyle(), wxNORMAL
);
145 // Calculate the label height if necessary
146 if (GetWindowStyle() & wxTB_TEXT
)
151 dc
.GetTextExtent(wxT("X"), & w
, & h
);
157 wxButtonToolBar::~wxButtonToolBar()
161 // ----------------------------------------------------------------------------
162 // wxButtonToolBar tool-related methods
163 // ----------------------------------------------------------------------------
165 wxToolBarToolBase
*wxButtonToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
167 // check the "other" direction first: it must be inside the toolbar or we
168 // don't risk finding anything
171 if ( x
< 0 || x
> m_maxWidth
)
174 // we always use x, even for a vertical toolbar, this makes the code
180 if ( y
< 0 || y
> m_maxHeight
)
184 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
186 node
= node
->GetNext() )
188 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
189 wxRect rectTool
= GetToolRect(tool
);
191 wxCoord startTool
, endTool
;
192 GetRectLimits(rectTool
, &startTool
, &endTool
);
194 if ( x
>= startTool
&& x
<= endTool
)
196 // don't return the separators from here, they don't accept any
198 return tool
->IsSeparator() ? NULL
: tool
;
205 void wxButtonToolBar::GetRectLimits(const wxRect
& rect
,
209 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
213 *start
= rect
.GetTop();
214 *end
= rect
.GetBottom();
218 *start
= rect
.GetLeft();
219 *end
= rect
.GetRight();
224 void wxButtonToolBar::SetToolShortHelp(int id
, const wxString
& help
)
226 wxToolBarToolBase
*tool
= FindById(id
);
228 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
230 // TODO: set tooltip/short help
231 tool
->SetShortHelp(help
);
234 bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos
),
235 wxToolBarToolBase
* WXUNUSED(tool
))
240 bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
241 wxToolBarToolBase
* WXUNUSED(tool
))
247 void wxButtonToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(enable
))
252 void wxButtonToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
257 void wxButtonToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
262 wxToolBarToolBase
*wxButtonToolBar::CreateTool(int id
,
263 const wxString
& label
,
264 const wxBitmap
& bmpNormal
,
265 const wxBitmap
& bmpDisabled
,
267 wxObject
*clientData
,
268 const wxString
& shortHelp
,
269 const wxString
& longHelp
)
271 return new wxButtonToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
272 clientData
, shortHelp
, longHelp
);
275 wxToolBarToolBase
*wxButtonToolBar::CreateTool(wxControl
*control
)
277 return new wxButtonToolBarTool(this, control
);
280 // ----------------------------------------------------------------------------
281 // wxButtonToolBar geometry
282 // ----------------------------------------------------------------------------
284 wxRect
wxButtonToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
286 const wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*)toolBase
;
290 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
292 // ensure that we always have the valid tool position
295 wxConstCast(this, wxButtonToolBar
)->DoLayout();
298 rect
.x
= tool
->m_x
- (m_toolPacking
/2);
303 if (tool
->IsButton())
305 rect
.width
= m_defaultWidth
;
306 rect
.height
= m_defaultHeight
;
307 if (tool
->GetButton())
308 rect
.SetSize(wxSize(tool
->m_width
, tool
->m_height
));
310 else if (tool
->IsSeparator())
312 rect
.width
= m_defaultWidth
;
313 rect
.height
= m_widthSeparator
;
317 rect
.width
= tool
->m_width
;
318 rect
.height
= tool
->m_height
;
323 if (tool
->IsButton())
325 rect
.width
= m_defaultWidth
;
326 rect
.height
= m_defaultHeight
;
327 if (tool
->GetButton())
328 rect
.SetSize(wxSize(tool
->m_width
, tool
->m_height
));
330 else if (tool
->IsSeparator())
332 rect
.width
= m_widthSeparator
;
333 rect
.height
= m_defaultHeight
;
337 rect
.width
= tool
->m_width
;
338 rect
.height
= tool
->m_height
;
342 rect
.width
+= m_toolPacking
;
347 bool wxButtonToolBar::Realize()
349 if ( !wxToolBarBase::Realize() )
352 m_needsLayout
= true;
355 SetBestSize(wxSize(m_maxWidth
, m_maxHeight
));
360 void wxButtonToolBar::DoLayout()
362 m_needsLayout
= false;
364 wxCoord x
= m_xMargin
,
369 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
370 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
371 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
373 // calculate the positions of all elements
374 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
376 node
= node
->GetNext() )
378 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
383 if (tool
->IsButton())
385 if (!tool
->GetButton())
387 wxBitmapButton
* bmpButton
= new wxBitmapButton(this, tool
->GetId(), tool
->GetNormalBitmap(), wxPoint(tool
->m_x
, tool
->m_y
), wxDefaultSize
,
388 wxBU_AUTODRAW
|wxBORDER_NONE
);
389 if (!tool
->GetShortHelp().empty())
390 bmpButton
->SetLabel(tool
->GetShortHelp());
392 tool
->SetButton(bmpButton
);
396 tool
->GetButton()->Move(wxPoint(tool
->m_x
, tool
->m_y
));
400 if (tool
->GetButton())
402 wxSize sz
= tool
->GetButton()->GetSize();
405 if (m_labelHeight
> 0)
407 sz
.y
+= (m_labelHeight
+ m_labelMargin
);
409 if (!tool
->GetShortHelp().empty())
412 dc
.SetFont(GetFont());
414 dc
.GetTextExtent(tool
->GetShortHelp(), & tw
, & th
);
416 // If the label is bigger than the icon, the label width
417 // becomes the new tool width, and we need to centre the
418 // the bitmap in this box.
421 int newX
= int(tool
->m_x
+ (tw
- sz
.x
)/2.0);
422 tool
->GetButton()->Move(newX
, tool
->m_y
);
428 maxHeight
= wxMax(maxHeight
, sz
.y
);
430 tool
->m_width
= sz
.x
;
431 tool
->m_height
= sz
.y
;
435 *pCur
+= (w
+ GetToolPacking());
437 else if (tool
->IsSeparator())
439 *pCur
+= m_widthSeparator
;
441 else if (!IsVertical()) // horizontal control
443 wxControl
*control
= tool
->GetControl();
444 wxSize size
= control
->GetSize();
445 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
446 tool
->m_width
= size
.x
;
447 tool
->m_height
= size
.y
;
449 *pCur
+= tool
->m_width
;
451 maxHeight
= wxMax(maxHeight
, size
.y
);
456 // calculate the total toolbar size
457 m_maxWidth
= x
+ 2*m_xMargin
;
458 m_maxHeight
= maxHeight
+ 2*m_yMargin
;
460 if ((GetWindowStyle() & wxTB_NODIVIDER
) == 0)
465 wxSize
wxButtonToolBar::DoGetBestClientSize() const
467 return wxSize(m_maxWidth
, m_maxHeight
);
470 // receives button commands
471 void wxButtonToolBar::OnCommand(wxCommandEvent
& event
)
473 wxButtonToolBarTool
* tool
= (wxButtonToolBarTool
*) FindById(event
.GetId());
480 if (tool
->CanBeToggled())
481 tool
->Toggle(tool
->IsToggled());
483 // TODO: handle toggle items
484 OnLeftClick(event
.GetId(), false);
486 if (tool
->GetKind() == wxITEM_RADIO
)
487 UnToggleRadioGroup(tool
);
489 if (tool
->CanBeToggled())
494 void wxButtonToolBar::OnPaint(wxPaintEvent
& event
)
498 dc
.SetFont(GetFont());
499 dc
.SetBackgroundMode(wxTRANSPARENT
);
501 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
503 node
= node
->GetNext() )
505 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
506 wxRect rectTool
= GetToolRect(tool
);
507 if (tool
->IsToggled())
509 wxRect backgroundRect
= rectTool
;
510 backgroundRect
.y
= -1; backgroundRect
.height
= GetClientSize().y
+ 1;
511 wxBrush
brush(wxColour(219, 219, 219));
512 wxPen
pen(wxColour(159, 159, 159));
515 dc
.DrawRectangle(backgroundRect
);
518 if (m_labelHeight
> 0 && !tool
->GetShortHelp().empty())
521 dc
.GetTextExtent(tool
->GetShortHelp(), & tw
, & th
);
524 dc
.DrawText(tool
->GetShortHelp(), x
, tool
->m_y
+ tool
->GetButton()->GetSize().y
+ m_labelMargin
);
528 if ((GetWindowStyle() & wxTB_NODIVIDER
) == 0)
530 wxPen
pen(wxColour(159, 159, 159));
533 int y1
= GetClientSize().y
-1;
534 int x2
= GetClientSize().x
;
536 dc
.DrawLine(x1
, y1
, x2
, y2
);
540 // detects mouse clicks outside buttons
541 void wxButtonToolBar::OnLeftUp(wxMouseEvent
& event
)
543 if (m_labelHeight
> 0)
545 wxButtonToolBarTool
* tool
= (wxButtonToolBarTool
*) FindToolForPosition(event
.GetX(), event
.GetY());
546 if (tool
&& tool
->GetButton() && (event
.GetY() > (tool
->m_y
+ tool
->GetButton()->GetSize().y
)))
548 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, tool
->GetId());
549 event
.SetEventObject(tool
->GetButton());
550 if (!ProcessEvent(event
))
556 #endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON