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