]> git.saurik.com Git - wxWidgets.git/blame - src/generic/buttonbar.cpp
Don't generate any events from wxSpinCtrl and wxSpinCtrlDouble methods.
[wxWidgets.git] / src / generic / buttonbar.cpp
CommitLineData
64d3ed17
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/buttonbar.cpp
3// Purpose: wxButtonToolBar implementation
4// Author: Julian Smart, after Robert Roebling, Vadim Zeitlin, SciTech
5// Modified by:
6// Created: 2006-04-13
64d3ed17 7// Copyright: (c) Julian Smart, Robert Roebling, Vadim Zeitlin,
e4db172a 8// SciTech Software, Inc.
64d3ed17
JS
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
b887dc7b
JS
27// Currently, only for Mac as a toolbar replacement.
28#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
64d3ed17 29
e4db172a
WS
30#include "wx/generic/buttonbar.h"
31
64d3ed17
JS
32#ifndef WX_PRECOMP
33 #include "wx/utils.h"
34 #include "wx/app.h"
e4db172a 35 #include "wx/log.h"
76b49cf4 36 #include "wx/frame.h"
ed4b0fdc 37 #include "wx/dcclient.h"
9eddec69 38 #include "wx/settings.h"
155ecd4c 39 #include "wx/image.h"
64d3ed17
JS
40#endif
41
64d3ed17
JS
42// ----------------------------------------------------------------------------
43// wxButtonToolBarTool: our implementation of wxToolBarToolBase
44// ----------------------------------------------------------------------------
45
46class WXDLLEXPORT wxButtonToolBarTool : public wxToolBarToolBase
47{
48public:
49 wxButtonToolBarTool(wxButtonToolBar *tbar,
50 int id,
51 const wxString& label,
52 const wxBitmap& bmpNormal,
53 const wxBitmap& bmpDisabled,
54 wxItemKind kind,
55 wxObject *clientData,
56 const wxString& shortHelp,
57 const wxString& longHelp)
58 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
59 clientData, shortHelp, longHelp)
60 {
61 m_x = m_y = wxDefaultCoord;
62 m_width =
63 m_height = 0;
e4db172a 64
64d3ed17
JS
65 m_button = NULL;
66 }
67
cdb11cb9
VZ
68 wxButtonToolBarTool(wxButtonToolBar *tbar,
69 wxControl *control,
70 const wxString& label)
71 : wxToolBarToolBase(tbar, control, label)
64d3ed17
JS
72 {
73 m_x = m_y = wxDefaultCoord;
74 m_width =
75 m_height = 0;
76 m_button = NULL;
77 }
78
79 wxBitmapButton* GetButton() const { return m_button; }
80 void SetButton(wxBitmapButton* button) { m_button = button; }
81
82public:
83 // the tool position (for controls)
84 wxCoord m_x;
85 wxCoord m_y;
86 wxCoord m_width;
87 wxCoord m_height;
88
89private:
90 // the control representing the button
91 wxBitmapButton* m_button;
92};
93
94// ============================================================================
95// wxButtonToolBar implementation
96// ============================================================================
97
98IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar, wxControl)
99
100BEGIN_EVENT_TABLE(wxButtonToolBar, wxControl)
101 EVT_BUTTON(wxID_ANY, wxButtonToolBar::OnCommand)
77631b1d 102 EVT_PAINT(wxButtonToolBar::OnPaint)
fac6eaec 103 EVT_LEFT_UP(wxButtonToolBar::OnLeftUp)
64d3ed17
JS
104END_EVENT_TABLE()
105
106// ----------------------------------------------------------------------------
107// wxButtonToolBar creation
108// ----------------------------------------------------------------------------
109
110void wxButtonToolBar::Init()
111{
112 // no tools yet
113 m_needsLayout = false;
114
115 // unknown widths for the tools and separators
116 m_widthSeparator = wxDefaultCoord;
117
fac6eaec 118 m_maxWidth = m_maxHeight = 0;
77631b1d 119
fac6eaec
JS
120 m_labelMargin = 2;
121 m_labelHeight = 0;
e4db172a 122
006591de 123 SetMargins(8, 2);
fac6eaec 124 SetToolPacking(8);
64d3ed17
JS
125}
126
127bool wxButtonToolBar::Create(wxWindow *parent,
128 wxWindowID id,
129 const wxPoint& pos,
130 const wxSize& size,
131 long style,
132 const wxString& name)
133{
134 if ( !wxToolBarBase::Create(parent, id, pos, size, style,
135 wxDefaultValidator, name) )
136 {
137 return false;
138 }
139
fac6eaec
JS
140 // wxColour lightBackground(244, 244, 244);
141
9aa727fa
VZ
142 wxFont font(wxSMALL_FONT->GetPointSize(),
143 wxNORMAL_FONT->GetFamily(),
144 wxNORMAL_FONT->GetStyle(),
145 wxFONTWEIGHT_NORMAL);
fac6eaec
JS
146 SetFont(font);
147
148 // Calculate the label height if necessary
149 if (GetWindowStyle() & wxTB_TEXT)
150 {
151 wxClientDC dc(this);
152 dc.SetFont(font);
153 int w, h;
154 dc.GetTextExtent(wxT("X"), & w, & h);
155 m_labelHeight = h;
156 }
64d3ed17
JS
157 return true;
158}
159
160wxButtonToolBar::~wxButtonToolBar()
161{
162}
163
164// ----------------------------------------------------------------------------
165// wxButtonToolBar tool-related methods
166// ----------------------------------------------------------------------------
167
168wxToolBarToolBase *wxButtonToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
169{
170 // check the "other" direction first: it must be inside the toolbar or we
171 // don't risk finding anything
172 if ( IsVertical() )
173 {
174 if ( x < 0 || x > m_maxWidth )
175 return NULL;
176
177 // we always use x, even for a vertical toolbar, this makes the code
178 // below simpler
179 x = y;
180 }
181 else // horizontal
182 {
183 if ( y < 0 || y > m_maxHeight )
184 return NULL;
185 }
186
187 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
188 node;
189 node = node->GetNext() )
190 {
191 wxButtonToolBarTool *tool = (wxButtonToolBarTool*) node->GetData();
192 wxRect rectTool = GetToolRect(tool);
193
194 wxCoord startTool, endTool;
195 GetRectLimits(rectTool, &startTool, &endTool);
196
197 if ( x >= startTool && x <= endTool )
198 {
199 // don't return the separators from here, they don't accept any
200 // input anyhow
201 return tool->IsSeparator() ? NULL : tool;
202 }
203 }
204
205 return NULL;
206}
207
208void wxButtonToolBar::GetRectLimits(const wxRect& rect,
209 wxCoord *start,
210 wxCoord *end) const
211{
9a83f860 212 wxCHECK_RET( start && end, wxT("NULL pointer in GetRectLimits") );
64d3ed17
JS
213
214 if ( IsVertical() )
215 {
216 *start = rect.GetTop();
217 *end = rect.GetBottom();
218 }
219 else // horizontal
220 {
221 *start = rect.GetLeft();
222 *end = rect.GetRight();
223 }
224}
225
226
227void wxButtonToolBar::SetToolShortHelp(int id, const wxString& help)
228{
229 wxToolBarToolBase *tool = FindById(id);
230
9a83f860 231 wxCHECK_RET( tool, wxT("SetToolShortHelp: no such tool") );
64d3ed17
JS
232
233 // TODO: set tooltip/short help
234 tool->SetShortHelp(help);
235}
236
237bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos),
238 wxToolBarToolBase * WXUNUSED(tool))
239{
240 return true;
241}
242
243bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos),
244 wxToolBarToolBase * WXUNUSED(tool))
245{
246 // TODO
247 return true;
248}
249
250void wxButtonToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(enable))
251{
252 // TODO
253}
254
255void wxButtonToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
256{
257 // TODO
258}
259
260void wxButtonToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
261{
262 // TODO
263}
264
265wxToolBarToolBase *wxButtonToolBar::CreateTool(int id,
266 const wxString& label,
267 const wxBitmap& bmpNormal,
268 const wxBitmap& bmpDisabled,
269 wxItemKind kind,
270 wxObject *clientData,
271 const wxString& shortHelp,
272 const wxString& longHelp)
273{
274 return new wxButtonToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
275 clientData, shortHelp, longHelp);
276}
277
cdb11cb9
VZ
278wxToolBarToolBase *wxButtonToolBar::CreateTool(wxControl *control,
279 const wxString& label)
64d3ed17 280{
cdb11cb9 281 return new wxButtonToolBarTool(this, control, label);
64d3ed17
JS
282}
283
284// ----------------------------------------------------------------------------
285// wxButtonToolBar geometry
286// ----------------------------------------------------------------------------
287
288wxRect wxButtonToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
289{
290 const wxButtonToolBarTool *tool = (wxButtonToolBarTool *)toolBase;
291
292 wxRect rect;
293
9a83f860 294 wxCHECK_MSG( tool, rect, wxT("GetToolRect: NULL tool") );
64d3ed17
JS
295
296 // ensure that we always have the valid tool position
297 if ( m_needsLayout )
298 {
299 wxConstCast(this, wxButtonToolBar)->DoLayout();
300 }
301
fac6eaec
JS
302 rect.x = tool->m_x - (m_toolPacking/2);
303 rect.y = tool->m_y;
64d3ed17
JS
304
305 if ( IsVertical() )
306 {
307 if (tool->IsButton())
308 {
309 rect.width = m_defaultWidth;
310 rect.height = m_defaultHeight;
311 if (tool->GetButton())
fac6eaec 312 rect.SetSize(wxSize(tool->m_width, tool->m_height));
64d3ed17
JS
313 }
314 else if (tool->IsSeparator())
315 {
316 rect.width = m_defaultWidth;
317 rect.height = m_widthSeparator;
318 }
319 else // control
320 {
321 rect.width = tool->m_width;
322 rect.height = tool->m_height;
323 }
324 }
325 else // horizontal
326 {
327 if (tool->IsButton())
328 {
329 rect.width = m_defaultWidth;
330 rect.height = m_defaultHeight;
331 if (tool->GetButton())
fac6eaec 332 rect.SetSize(wxSize(tool->m_width, tool->m_height));
64d3ed17
JS
333 }
334 else if (tool->IsSeparator())
335 {
336 rect.width = m_widthSeparator;
337 rect.height = m_defaultHeight;
338 }
339 else // control
340 {
341 rect.width = tool->m_width;
342 rect.height = tool->m_height;
343 }
344 }
345
fac6eaec 346 rect.width += m_toolPacking;
64d3ed17
JS
347
348 return rect;
349}
350
351bool wxButtonToolBar::Realize()
352{
353 if ( !wxToolBarBase::Realize() )
354 return false;
e4db172a 355
64d3ed17
JS
356 m_needsLayout = true;
357 DoLayout();
e4db172a 358
170acdc9 359 SetInitialSize(wxSize(m_maxWidth, m_maxHeight));
64d3ed17
JS
360
361 return true;
362}
363
364void wxButtonToolBar::DoLayout()
365{
366 m_needsLayout = false;
367
368 wxCoord x = m_xMargin,
369 y = m_yMargin;
370
371 int maxHeight = 0;
372
373 const wxCoord widthTool = IsVertical() ? m_defaultHeight : m_defaultWidth;
374 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin;
375 wxCoord *pCur = IsVertical() ? &y : &x;
376
377 // calculate the positions of all elements
378 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
379 node;
380 node = node->GetNext() )
381 {
382 wxButtonToolBarTool *tool = (wxButtonToolBarTool *) node->GetData();
383
384 tool->m_x = x;
385 tool->m_y = y;
e4db172a 386
64d3ed17
JS
387 if (tool->IsButton())
388 {
389 if (!tool->GetButton())
390 {
391 wxBitmapButton* bmpButton = new wxBitmapButton(this, tool->GetId(), tool->GetNormalBitmap(), wxPoint(tool->m_x, tool->m_y), wxDefaultSize,
fac6eaec 392 wxBU_AUTODRAW|wxBORDER_NONE);
76b49cf4 393 if (!tool->GetShortHelp().empty())
fac6eaec 394 bmpButton->SetLabel(tool->GetShortHelp());
e4db172a 395
64d3ed17
JS
396 tool->SetButton(bmpButton);
397 }
398 else
399 {
400 tool->GetButton()->Move(wxPoint(tool->m_x, tool->m_y));
401 }
e4db172a 402
64d3ed17
JS
403 int w = widthTool;
404 if (tool->GetButton())
405 {
406 wxSize sz = tool->GetButton()->GetSize();
407 w = sz.x;
408
fac6eaec
JS
409 if (m_labelHeight > 0)
410 {
411 sz.y += (m_labelHeight + m_labelMargin);
412
76b49cf4 413 if (!tool->GetShortHelp().empty())
fac6eaec
JS
414 {
415 wxClientDC dc(this);
416 dc.SetFont(GetFont());
417 int tw, th;
418 dc.GetTextExtent(tool->GetShortHelp(), & tw, & th);
419
420 // If the label is bigger than the icon, the label width
421 // becomes the new tool width, and we need to centre the
422 // the bitmap in this box.
423 if (tw > sz.x)
424 {
425 int newX = int(tool->m_x + (tw - sz.x)/2.0);
426 tool->GetButton()->Move(newX, tool->m_y);
427 sz.x = tw;
428 }
429 }
430 }
431
64d3ed17 432 maxHeight = wxMax(maxHeight, sz.y);
fac6eaec
JS
433
434 tool->m_width = sz.x;
435 tool->m_height = sz.y;
436 w = sz.x;
64d3ed17
JS
437 }
438
439 *pCur += (w + GetToolPacking());
440 }
441 else if (tool->IsSeparator())
442 {
443 *pCur += m_widthSeparator;
444 }
445 else if (!IsVertical()) // horizontal control
446 {
447 wxControl *control = tool->GetControl();
448 wxSize size = control->GetSize();
449 tool->m_y += (m_defaultHeight - size.y)/2;
450 tool->m_width = size.x;
451 tool->m_height = size.y;
452
453 *pCur += tool->m_width;
454
455 maxHeight = wxMax(maxHeight, size.y);
456 }
457 *pCur += margin;
458 }
459
460 // calculate the total toolbar size
461 m_maxWidth = x + 2*m_xMargin;
462 m_maxHeight = maxHeight + 2*m_yMargin;
77631b1d
JS
463
464 if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
465 m_maxHeight += 2;
466
64d3ed17
JS
467}
468
469wxSize wxButtonToolBar::DoGetBestClientSize() const
470{
471 return wxSize(m_maxWidth, m_maxHeight);
472}
473
474// receives button commands
475void wxButtonToolBar::OnCommand(wxCommandEvent& event)
476{
477 wxButtonToolBarTool* tool = (wxButtonToolBarTool*) FindById(event.GetId());
478 if (!tool)
479 {
480 event.Skip();
481 return;
482 }
483
77631b1d
JS
484 if (tool->CanBeToggled())
485 tool->Toggle(tool->IsToggled());
486
64d3ed17 487 // TODO: handle toggle items
77631b1d
JS
488 OnLeftClick(event.GetId(), false);
489
490 if (tool->GetKind() == wxITEM_RADIO)
491 UnToggleRadioGroup(tool);
492
493 if (tool->CanBeToggled())
494 Refresh();
495}
496
497// paints a border
ff654490 498void wxButtonToolBar::OnPaint(wxPaintEvent& WXUNUSED(event))
77631b1d
JS
499{
500 wxPaintDC dc(this);
501
fac6eaec
JS
502 dc.SetFont(GetFont());
503 dc.SetBackgroundMode(wxTRANSPARENT);
504
77631b1d
JS
505 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
506 node;
507 node = node->GetNext() )
508 {
509 wxButtonToolBarTool *tool = (wxButtonToolBarTool*) node->GetData();
fac6eaec 510 wxRect rectTool = GetToolRect(tool);
77631b1d
JS
511 if (tool->IsToggled())
512 {
fac6eaec
JS
513 wxRect backgroundRect = rectTool;
514 backgroundRect.y = -1; backgroundRect.height = GetClientSize().y + 1;
515 wxBrush brush(wxColour(219, 219, 219));
516 wxPen pen(wxColour(159, 159, 159));
77631b1d
JS
517 dc.SetBrush(brush);
518 dc.SetPen(pen);
fac6eaec
JS
519 dc.DrawRectangle(backgroundRect);
520 }
521
76b49cf4 522 if (m_labelHeight > 0 && !tool->GetShortHelp().empty())
fac6eaec
JS
523 {
524 int tw, th;
525 dc.GetTextExtent(tool->GetShortHelp(), & tw, & th);
526
527 int x = tool->m_x;
528 dc.DrawText(tool->GetShortHelp(), x, tool->m_y + tool->GetButton()->GetSize().y + m_labelMargin);
77631b1d
JS
529 }
530 }
531
532 if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
533 {
fac6eaec 534 wxPen pen(wxColour(159, 159, 159));
77631b1d
JS
535 dc.SetPen(pen);
536 int x1 = 0;
537 int y1 = GetClientSize().y-1;
538 int x2 = GetClientSize().x;
539 int y2 = y1;
540 dc.DrawLine(x1, y1, x2, y2);
541 }
64d3ed17
JS
542}
543
fac6eaec
JS
544// detects mouse clicks outside buttons
545void wxButtonToolBar::OnLeftUp(wxMouseEvent& event)
546{
547 if (m_labelHeight > 0)
548 {
549 wxButtonToolBarTool* tool = (wxButtonToolBarTool*) FindToolForPosition(event.GetX(), event.GetY());
550 if (tool && tool->GetButton() && (event.GetY() > (tool->m_y + tool->GetButton()->GetSize().y)))
551 {
ce7fe42e 552 wxCommandEvent event(wxEVT_BUTTON, tool->GetId());
fac6eaec 553 event.SetEventObject(tool->GetButton());
eafd76b0 554 if (!GetEventHandler()->ProcessEvent(event))
fac6eaec
JS
555 event.Skip();
556 }
557 }
558}
559
64d3ed17 560#endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON