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"
43 // ----------------------------------------------------------------------------
44 // wxButtonToolBarTool: our implementation of wxToolBarToolBase
45 // ----------------------------------------------------------------------------
47 class WXDLLEXPORT wxButtonToolBarTool
: public wxToolBarToolBase
50 wxButtonToolBarTool(wxButtonToolBar
*tbar
,
52 const wxString
& label
,
53 const wxBitmap
& bmpNormal
,
54 const wxBitmap
& bmpDisabled
,
57 const wxString
& shortHelp
,
58 const wxString
& longHelp
)
59 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
60 clientData
, shortHelp
, longHelp
)
62 m_x
= m_y
= wxDefaultCoord
;
69 wxButtonToolBarTool(wxButtonToolBar
*tbar
,
71 const wxString
& label
)
72 : wxToolBarToolBase(tbar
, control
, label
)
74 m_x
= m_y
= wxDefaultCoord
;
80 wxBitmapButton
* GetButton() const { return m_button
; }
81 void SetButton(wxBitmapButton
* button
) { m_button
= button
; }
84 // the tool position (for controls)
91 // the control representing the button
92 wxBitmapButton
* m_button
;
95 // ============================================================================
96 // wxButtonToolBar implementation
97 // ============================================================================
99 IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar
, wxControl
)
101 BEGIN_EVENT_TABLE(wxButtonToolBar
, wxControl
)
102 EVT_BUTTON(wxID_ANY
, wxButtonToolBar::OnCommand
)
103 EVT_PAINT(wxButtonToolBar::OnPaint
)
104 EVT_LEFT_UP(wxButtonToolBar::OnLeftUp
)
107 // ----------------------------------------------------------------------------
108 // wxButtonToolBar creation
109 // ----------------------------------------------------------------------------
111 void wxButtonToolBar::Init()
114 m_needsLayout
= false;
116 // unknown widths for the tools and separators
117 m_widthSeparator
= wxDefaultCoord
;
119 m_maxWidth
= m_maxHeight
= 0;
128 bool wxButtonToolBar::Create(wxWindow
*parent
,
133 const wxString
& name
)
135 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
136 wxDefaultValidator
, name
) )
141 // wxColour lightBackground(244, 244, 244);
143 wxFont
font(wxSMALL_FONT
->GetPointSize(),
144 wxNORMAL_FONT
->GetFamily(),
145 wxNORMAL_FONT
->GetStyle(),
146 wxFONTWEIGHT_NORMAL
);
149 // Calculate the label height if necessary
150 if (GetWindowStyle() & wxTB_TEXT
)
155 dc
.GetTextExtent(wxT("X"), & w
, & h
);
161 wxButtonToolBar::~wxButtonToolBar()
165 // ----------------------------------------------------------------------------
166 // wxButtonToolBar tool-related methods
167 // ----------------------------------------------------------------------------
169 wxToolBarToolBase
*wxButtonToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
171 // check the "other" direction first: it must be inside the toolbar or we
172 // don't risk finding anything
175 if ( x
< 0 || x
> m_maxWidth
)
178 // we always use x, even for a vertical toolbar, this makes the code
184 if ( y
< 0 || y
> m_maxHeight
)
188 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
190 node
= node
->GetNext() )
192 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
193 wxRect rectTool
= GetToolRect(tool
);
195 wxCoord startTool
, endTool
;
196 GetRectLimits(rectTool
, &startTool
, &endTool
);
198 if ( x
>= startTool
&& x
<= endTool
)
200 // don't return the separators from here, they don't accept any
202 return tool
->IsSeparator() ? NULL
: tool
;
209 void wxButtonToolBar::GetRectLimits(const wxRect
& rect
,
213 wxCHECK_RET( start
&& end
, wxT("NULL pointer in GetRectLimits") );
217 *start
= rect
.GetTop();
218 *end
= rect
.GetBottom();
222 *start
= rect
.GetLeft();
223 *end
= rect
.GetRight();
228 void wxButtonToolBar::SetToolShortHelp(int id
, const wxString
& help
)
230 wxToolBarToolBase
*tool
= FindById(id
);
232 wxCHECK_RET( tool
, wxT("SetToolShortHelp: no such tool") );
234 // TODO: set tooltip/short help
235 tool
->SetShortHelp(help
);
238 bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos
),
239 wxToolBarToolBase
* WXUNUSED(tool
))
244 bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
245 wxToolBarToolBase
* WXUNUSED(tool
))
251 void wxButtonToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(enable
))
256 void wxButtonToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
261 void wxButtonToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
266 wxToolBarToolBase
*wxButtonToolBar::CreateTool(int id
,
267 const wxString
& label
,
268 const wxBitmap
& bmpNormal
,
269 const wxBitmap
& bmpDisabled
,
271 wxObject
*clientData
,
272 const wxString
& shortHelp
,
273 const wxString
& longHelp
)
275 return new wxButtonToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
276 clientData
, shortHelp
, longHelp
);
279 wxToolBarToolBase
*wxButtonToolBar::CreateTool(wxControl
*control
,
280 const wxString
& label
)
282 return new wxButtonToolBarTool(this, control
, label
);
285 // ----------------------------------------------------------------------------
286 // wxButtonToolBar geometry
287 // ----------------------------------------------------------------------------
289 wxRect
wxButtonToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
291 const wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*)toolBase
;
295 wxCHECK_MSG( tool
, rect
, wxT("GetToolRect: NULL tool") );
297 // ensure that we always have the valid tool position
300 wxConstCast(this, wxButtonToolBar
)->DoLayout();
303 rect
.x
= tool
->m_x
- (m_toolPacking
/2);
308 if (tool
->IsButton())
310 rect
.width
= m_defaultWidth
;
311 rect
.height
= m_defaultHeight
;
312 if (tool
->GetButton())
313 rect
.SetSize(wxSize(tool
->m_width
, tool
->m_height
));
315 else if (tool
->IsSeparator())
317 rect
.width
= m_defaultWidth
;
318 rect
.height
= m_widthSeparator
;
322 rect
.width
= tool
->m_width
;
323 rect
.height
= tool
->m_height
;
328 if (tool
->IsButton())
330 rect
.width
= m_defaultWidth
;
331 rect
.height
= m_defaultHeight
;
332 if (tool
->GetButton())
333 rect
.SetSize(wxSize(tool
->m_width
, tool
->m_height
));
335 else if (tool
->IsSeparator())
337 rect
.width
= m_widthSeparator
;
338 rect
.height
= m_defaultHeight
;
342 rect
.width
= tool
->m_width
;
343 rect
.height
= tool
->m_height
;
347 rect
.width
+= m_toolPacking
;
352 bool wxButtonToolBar::Realize()
354 if ( !wxToolBarBase::Realize() )
357 m_needsLayout
= true;
360 SetInitialSize(wxSize(m_maxWidth
, m_maxHeight
));
365 void wxButtonToolBar::DoLayout()
367 m_needsLayout
= false;
369 wxCoord x
= m_xMargin
,
374 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
375 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
376 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
378 // calculate the positions of all elements
379 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
381 node
= node
->GetNext() )
383 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
388 if (tool
->IsButton())
390 if (!tool
->GetButton())
392 wxBitmapButton
* bmpButton
= new wxBitmapButton(this, tool
->GetId(), tool
->GetNormalBitmap(), wxPoint(tool
->m_x
, tool
->m_y
), wxDefaultSize
,
393 wxBU_AUTODRAW
|wxBORDER_NONE
);
394 if (!tool
->GetShortHelp().empty())
395 bmpButton
->SetLabel(tool
->GetShortHelp());
397 tool
->SetButton(bmpButton
);
401 tool
->GetButton()->Move(wxPoint(tool
->m_x
, tool
->m_y
));
405 if (tool
->GetButton())
407 wxSize sz
= tool
->GetButton()->GetSize();
410 if (m_labelHeight
> 0)
412 sz
.y
+= (m_labelHeight
+ m_labelMargin
);
414 if (!tool
->GetShortHelp().empty())
417 dc
.SetFont(GetFont());
419 dc
.GetTextExtent(tool
->GetShortHelp(), & tw
, & th
);
421 // If the label is bigger than the icon, the label width
422 // becomes the new tool width, and we need to centre the
423 // the bitmap in this box.
426 int newX
= int(tool
->m_x
+ (tw
- sz
.x
)/2.0);
427 tool
->GetButton()->Move(newX
, tool
->m_y
);
433 maxHeight
= wxMax(maxHeight
, sz
.y
);
435 tool
->m_width
= sz
.x
;
436 tool
->m_height
= sz
.y
;
440 *pCur
+= (w
+ GetToolPacking());
442 else if (tool
->IsSeparator())
444 *pCur
+= m_widthSeparator
;
446 else if (!IsVertical()) // horizontal control
448 wxControl
*control
= tool
->GetControl();
449 wxSize size
= control
->GetSize();
450 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
451 tool
->m_width
= size
.x
;
452 tool
->m_height
= size
.y
;
454 *pCur
+= tool
->m_width
;
456 maxHeight
= wxMax(maxHeight
, size
.y
);
461 // calculate the total toolbar size
462 m_maxWidth
= x
+ 2*m_xMargin
;
463 m_maxHeight
= maxHeight
+ 2*m_yMargin
;
465 if ((GetWindowStyle() & wxTB_NODIVIDER
) == 0)
470 wxSize
wxButtonToolBar::DoGetBestClientSize() const
472 return wxSize(m_maxWidth
, m_maxHeight
);
475 // receives button commands
476 void wxButtonToolBar::OnCommand(wxCommandEvent
& event
)
478 wxButtonToolBarTool
* tool
= (wxButtonToolBarTool
*) FindById(event
.GetId());
485 if (tool
->CanBeToggled())
486 tool
->Toggle(tool
->IsToggled());
488 // TODO: handle toggle items
489 OnLeftClick(event
.GetId(), false);
491 if (tool
->GetKind() == wxITEM_RADIO
)
492 UnToggleRadioGroup(tool
);
494 if (tool
->CanBeToggled())
499 void wxButtonToolBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
503 dc
.SetFont(GetFont());
504 dc
.SetBackgroundMode(wxTRANSPARENT
);
506 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
508 node
= node
->GetNext() )
510 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
511 wxRect rectTool
= GetToolRect(tool
);
512 if (tool
->IsToggled())
514 wxRect backgroundRect
= rectTool
;
515 backgroundRect
.y
= -1; backgroundRect
.height
= GetClientSize().y
+ 1;
516 wxBrush
brush(wxColour(219, 219, 219));
517 wxPen
pen(wxColour(159, 159, 159));
520 dc
.DrawRectangle(backgroundRect
);
523 if (m_labelHeight
> 0 && !tool
->GetShortHelp().empty())
526 dc
.GetTextExtent(tool
->GetShortHelp(), & tw
, & th
);
529 dc
.DrawText(tool
->GetShortHelp(), x
, tool
->m_y
+ tool
->GetButton()->GetSize().y
+ m_labelMargin
);
533 if ((GetWindowStyle() & wxTB_NODIVIDER
) == 0)
535 wxPen
pen(wxColour(159, 159, 159));
538 int y1
= GetClientSize().y
-1;
539 int x2
= GetClientSize().x
;
541 dc
.DrawLine(x1
, y1
, x2
, y2
);
545 // detects mouse clicks outside buttons
546 void wxButtonToolBar::OnLeftUp(wxMouseEvent
& event
)
548 if (m_labelHeight
> 0)
550 wxButtonToolBarTool
* tool
= (wxButtonToolBarTool
*) FindToolForPosition(event
.GetX(), event
.GetY());
551 if (tool
&& tool
->GetButton() && (event
.GetY() > (tool
->m_y
+ tool
->GetButton()->GetSize().y
)))
553 wxCommandEvent
event(wxEVT_BUTTON
, tool
->GetId());
554 event
.SetEventObject(tool
->GetButton());
555 if (!GetEventHandler()->ProcessEvent(event
))
561 #endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON