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
, wxControl
*control
)
70 : wxToolBarToolBase(tbar
, control
)
72 m_x
= m_y
= wxDefaultCoord
;
78 wxBitmapButton
* GetButton() const { return m_button
; }
79 void SetButton(wxBitmapButton
* button
) { m_button
= button
; }
82 // the tool position (for controls)
89 // the control representing the button
90 wxBitmapButton
* m_button
;
93 // ============================================================================
94 // wxButtonToolBar implementation
95 // ============================================================================
97 IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar
, wxControl
)
99 BEGIN_EVENT_TABLE(wxButtonToolBar
, wxControl
)
100 EVT_BUTTON(wxID_ANY
, wxButtonToolBar::OnCommand
)
101 EVT_PAINT(wxButtonToolBar::OnPaint
)
102 EVT_LEFT_UP(wxButtonToolBar::OnLeftUp
)
105 // ----------------------------------------------------------------------------
106 // wxButtonToolBar creation
107 // ----------------------------------------------------------------------------
109 void wxButtonToolBar::Init()
112 m_needsLayout
= false;
114 // unknown widths for the tools and separators
115 m_widthSeparator
= wxDefaultCoord
;
117 m_maxWidth
= m_maxHeight
= 0;
126 bool wxButtonToolBar::Create(wxWindow
*parent
,
131 const wxString
& name
)
133 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
134 wxDefaultValidator
, name
) )
139 // wxColour lightBackground(244, 244, 244);
141 wxFont
font(wxSMALL_FONT
->GetPointSize(), wxNORMAL_FONT
->GetFamily(), wxNORMAL_FONT
->GetStyle(), wxNORMAL
);
144 // Calculate the label height if necessary
145 if (GetWindowStyle() & wxTB_TEXT
)
150 dc
.GetTextExtent(wxT("X"), & w
, & h
);
156 wxButtonToolBar::~wxButtonToolBar()
160 // ----------------------------------------------------------------------------
161 // wxButtonToolBar tool-related methods
162 // ----------------------------------------------------------------------------
164 wxToolBarToolBase
*wxButtonToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
166 // check the "other" direction first: it must be inside the toolbar or we
167 // don't risk finding anything
170 if ( x
< 0 || x
> m_maxWidth
)
173 // we always use x, even for a vertical toolbar, this makes the code
179 if ( y
< 0 || y
> m_maxHeight
)
183 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
185 node
= node
->GetNext() )
187 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
188 wxRect rectTool
= GetToolRect(tool
);
190 wxCoord startTool
, endTool
;
191 GetRectLimits(rectTool
, &startTool
, &endTool
);
193 if ( x
>= startTool
&& x
<= endTool
)
195 // don't return the separators from here, they don't accept any
197 return tool
->IsSeparator() ? NULL
: tool
;
204 void wxButtonToolBar::GetRectLimits(const wxRect
& rect
,
208 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
212 *start
= rect
.GetTop();
213 *end
= rect
.GetBottom();
217 *start
= rect
.GetLeft();
218 *end
= rect
.GetRight();
223 void wxButtonToolBar::SetToolShortHelp(int id
, const wxString
& help
)
225 wxToolBarToolBase
*tool
= FindById(id
);
227 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
229 // TODO: set tooltip/short help
230 tool
->SetShortHelp(help
);
233 bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos
),
234 wxToolBarToolBase
* WXUNUSED(tool
))
239 bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
240 wxToolBarToolBase
* WXUNUSED(tool
))
246 void wxButtonToolBar::DoEnableTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(enable
))
251 void wxButtonToolBar::DoToggleTool(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
256 void wxButtonToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
261 wxToolBarToolBase
*wxButtonToolBar::CreateTool(int id
,
262 const wxString
& label
,
263 const wxBitmap
& bmpNormal
,
264 const wxBitmap
& bmpDisabled
,
266 wxObject
*clientData
,
267 const wxString
& shortHelp
,
268 const wxString
& longHelp
)
270 return new wxButtonToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
271 clientData
, shortHelp
, longHelp
);
274 wxToolBarToolBase
*wxButtonToolBar::CreateTool(wxControl
*control
)
276 return new wxButtonToolBarTool(this, control
);
279 // ----------------------------------------------------------------------------
280 // wxButtonToolBar geometry
281 // ----------------------------------------------------------------------------
283 wxRect
wxButtonToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
285 const wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*)toolBase
;
289 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
291 // ensure that we always have the valid tool position
294 wxConstCast(this, wxButtonToolBar
)->DoLayout();
297 rect
.x
= tool
->m_x
- (m_toolPacking
/2);
302 if (tool
->IsButton())
304 rect
.width
= m_defaultWidth
;
305 rect
.height
= m_defaultHeight
;
306 if (tool
->GetButton())
307 rect
.SetSize(wxSize(tool
->m_width
, tool
->m_height
));
309 else if (tool
->IsSeparator())
311 rect
.width
= m_defaultWidth
;
312 rect
.height
= m_widthSeparator
;
316 rect
.width
= tool
->m_width
;
317 rect
.height
= tool
->m_height
;
322 if (tool
->IsButton())
324 rect
.width
= m_defaultWidth
;
325 rect
.height
= m_defaultHeight
;
326 if (tool
->GetButton())
327 rect
.SetSize(wxSize(tool
->m_width
, tool
->m_height
));
329 else if (tool
->IsSeparator())
331 rect
.width
= m_widthSeparator
;
332 rect
.height
= m_defaultHeight
;
336 rect
.width
= tool
->m_width
;
337 rect
.height
= tool
->m_height
;
341 rect
.width
+= m_toolPacking
;
346 bool wxButtonToolBar::Realize()
348 if ( !wxToolBarBase::Realize() )
351 m_needsLayout
= true;
354 SetBestSize(wxSize(m_maxWidth
, m_maxHeight
));
359 void wxButtonToolBar::DoLayout()
361 m_needsLayout
= false;
363 wxCoord x
= m_xMargin
,
368 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
369 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
370 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
372 // calculate the positions of all elements
373 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
375 node
= node
->GetNext() )
377 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
382 if (tool
->IsButton())
384 if (!tool
->GetButton())
386 wxBitmapButton
* bmpButton
= new wxBitmapButton(this, tool
->GetId(), tool
->GetNormalBitmap(), wxPoint(tool
->m_x
, tool
->m_y
), wxDefaultSize
,
387 wxBU_AUTODRAW
|wxBORDER_NONE
);
388 if (!tool
->GetShortHelp().empty())
389 bmpButton
->SetLabel(tool
->GetShortHelp());
391 tool
->SetButton(bmpButton
);
395 tool
->GetButton()->Move(wxPoint(tool
->m_x
, tool
->m_y
));
399 if (tool
->GetButton())
401 wxSize sz
= tool
->GetButton()->GetSize();
404 if (m_labelHeight
> 0)
406 sz
.y
+= (m_labelHeight
+ m_labelMargin
);
408 if (!tool
->GetShortHelp().empty())
411 dc
.SetFont(GetFont());
413 dc
.GetTextExtent(tool
->GetShortHelp(), & tw
, & th
);
415 // If the label is bigger than the icon, the label width
416 // becomes the new tool width, and we need to centre the
417 // the bitmap in this box.
420 int newX
= int(tool
->m_x
+ (tw
- sz
.x
)/2.0);
421 tool
->GetButton()->Move(newX
, tool
->m_y
);
427 maxHeight
= wxMax(maxHeight
, sz
.y
);
429 tool
->m_width
= sz
.x
;
430 tool
->m_height
= sz
.y
;
434 *pCur
+= (w
+ GetToolPacking());
436 else if (tool
->IsSeparator())
438 *pCur
+= m_widthSeparator
;
440 else if (!IsVertical()) // horizontal control
442 wxControl
*control
= tool
->GetControl();
443 wxSize size
= control
->GetSize();
444 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
445 tool
->m_width
= size
.x
;
446 tool
->m_height
= size
.y
;
448 *pCur
+= tool
->m_width
;
450 maxHeight
= wxMax(maxHeight
, size
.y
);
455 // calculate the total toolbar size
456 m_maxWidth
= x
+ 2*m_xMargin
;
457 m_maxHeight
= maxHeight
+ 2*m_yMargin
;
459 if ((GetWindowStyle() & wxTB_NODIVIDER
) == 0)
464 wxSize
wxButtonToolBar::DoGetBestClientSize() const
466 return wxSize(m_maxWidth
, m_maxHeight
);
469 // receives button commands
470 void wxButtonToolBar::OnCommand(wxCommandEvent
& event
)
472 wxButtonToolBarTool
* tool
= (wxButtonToolBarTool
*) FindById(event
.GetId());
479 if (tool
->CanBeToggled())
480 tool
->Toggle(tool
->IsToggled());
482 // TODO: handle toggle items
483 OnLeftClick(event
.GetId(), false);
485 if (tool
->GetKind() == wxITEM_RADIO
)
486 UnToggleRadioGroup(tool
);
488 if (tool
->CanBeToggled())
493 void wxButtonToolBar::OnPaint(wxPaintEvent
& event
)
497 dc
.SetFont(GetFont());
498 dc
.SetBackgroundMode(wxTRANSPARENT
);
500 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
502 node
= node
->GetNext() )
504 wxButtonToolBarTool
*tool
= (wxButtonToolBarTool
*) node
->GetData();
505 wxRect rectTool
= GetToolRect(tool
);
506 if (tool
->IsToggled())
508 wxRect backgroundRect
= rectTool
;
509 backgroundRect
.y
= -1; backgroundRect
.height
= GetClientSize().y
+ 1;
510 wxBrush
brush(wxColour(219, 219, 219));
511 wxPen
pen(wxColour(159, 159, 159));
514 dc
.DrawRectangle(backgroundRect
);
517 if (m_labelHeight
> 0 && !tool
->GetShortHelp().empty())
520 dc
.GetTextExtent(tool
->GetShortHelp(), & tw
, & th
);
523 dc
.DrawText(tool
->GetShortHelp(), x
, tool
->m_y
+ tool
->GetButton()->GetSize().y
+ m_labelMargin
);
527 if ((GetWindowStyle() & wxTB_NODIVIDER
) == 0)
529 wxPen
pen(wxColour(159, 159, 159));
532 int y1
= GetClientSize().y
-1;
533 int x2
= GetClientSize().x
;
535 dc
.DrawLine(x1
, y1
, x2
, y2
);
539 // detects mouse clicks outside buttons
540 void wxButtonToolBar::OnLeftUp(wxMouseEvent
& event
)
542 if (m_labelHeight
> 0)
544 wxButtonToolBarTool
* tool
= (wxButtonToolBarTool
*) FindToolForPosition(event
.GetX(), event
.GetY());
545 if (tool
&& tool
->GetButton() && (event
.GetY() > (tool
->m_y
+ tool
->GetButton()->GetSize().y
)))
547 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, tool
->GetId());
548 event
.SetEventObject(tool
->GetButton());
549 if (!ProcessEvent(event
))
555 #endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON