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