]> git.saurik.com Git - wxWidgets.git/blob - src/generic/buttonbar.cpp
fix compilation both for Robert's cross-compiler and for all normal compilers by...
[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, 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
81 public:
82 // the tool position (for controls)
83 wxCoord m_x;
84 wxCoord m_y;
85 wxCoord m_width;
86 wxCoord m_height;
87
88 private:
89 // the control representing the button
90 wxBitmapButton* m_button;
91 };
92
93 // ============================================================================
94 // wxButtonToolBar implementation
95 // ============================================================================
96
97 IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar, wxControl)
98
99 BEGIN_EVENT_TABLE(wxButtonToolBar, wxControl)
100 EVT_BUTTON(wxID_ANY, wxButtonToolBar::OnCommand)
101 EVT_PAINT(wxButtonToolBar::OnPaint)
102 EVT_LEFT_UP(wxButtonToolBar::OnLeftUp)
103 END_EVENT_TABLE()
104
105 // ----------------------------------------------------------------------------
106 // wxButtonToolBar creation
107 // ----------------------------------------------------------------------------
108
109 void 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
117 m_maxWidth = m_maxHeight = 0;
118
119 m_labelMargin = 2;
120 m_labelHeight = 0;
121
122 SetMargins(8, 2);
123 SetToolPacking(8);
124 }
125
126 bool 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
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 }
153 return true;
154 }
155
156 wxButtonToolBar::~wxButtonToolBar()
157 {
158 }
159
160 // ----------------------------------------------------------------------------
161 // wxButtonToolBar tool-related methods
162 // ----------------------------------------------------------------------------
163
164 wxToolBarToolBase *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
204 void 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
223 void 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
233 bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos),
234 wxToolBarToolBase * WXUNUSED(tool))
235 {
236 return true;
237 }
238
239 bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos),
240 wxToolBarToolBase * WXUNUSED(tool))
241 {
242 // TODO
243 return true;
244 }
245
246 void wxButtonToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(enable))
247 {
248 // TODO
249 }
250
251 void wxButtonToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
252 {
253 // TODO
254 }
255
256 void wxButtonToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
257 {
258 // TODO
259 }
260
261 wxToolBarToolBase *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
274 wxToolBarToolBase *wxButtonToolBar::CreateTool(wxControl *control)
275 {
276 return new wxButtonToolBarTool(this, control);
277 }
278
279 // ----------------------------------------------------------------------------
280 // wxButtonToolBar geometry
281 // ----------------------------------------------------------------------------
282
283 wxRect 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
297 rect.x = tool->m_x - (m_toolPacking/2);
298 rect.y = tool->m_y;
299
300 if ( IsVertical() )
301 {
302 if (tool->IsButton())
303 {
304 rect.width = m_defaultWidth;
305 rect.height = m_defaultHeight;
306 if (tool->GetButton())
307 rect.SetSize(wxSize(tool->m_width, tool->m_height));
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())
327 rect.SetSize(wxSize(tool->m_width, tool->m_height));
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
341 rect.width += m_toolPacking;
342
343 return rect;
344 }
345
346 bool wxButtonToolBar::Realize()
347 {
348 if ( !wxToolBarBase::Realize() )
349 return false;
350
351 m_needsLayout = true;
352 DoLayout();
353
354 SetBestSize(wxSize(m_maxWidth, m_maxHeight));
355
356 return true;
357 }
358
359 void 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;
381
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,
387 wxBU_AUTODRAW|wxBORDER_NONE);
388 if (!tool->GetShortHelp().empty())
389 bmpButton->SetLabel(tool->GetShortHelp());
390
391 tool->SetButton(bmpButton);
392 }
393 else
394 {
395 tool->GetButton()->Move(wxPoint(tool->m_x, tool->m_y));
396 }
397
398 int w = widthTool;
399 if (tool->GetButton())
400 {
401 wxSize sz = tool->GetButton()->GetSize();
402 w = sz.x;
403
404 if (m_labelHeight > 0)
405 {
406 sz.y += (m_labelHeight + m_labelMargin);
407
408 if (!tool->GetShortHelp().empty())
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
427 maxHeight = wxMax(maxHeight, sz.y);
428
429 tool->m_width = sz.x;
430 tool->m_height = sz.y;
431 w = sz.x;
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;
458
459 if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
460 m_maxHeight += 2;
461
462 }
463
464 wxSize wxButtonToolBar::DoGetBestClientSize() const
465 {
466 return wxSize(m_maxWidth, m_maxHeight);
467 }
468
469 // receives button commands
470 void wxButtonToolBar::OnCommand(wxCommandEvent& event)
471 {
472 wxButtonToolBarTool* tool = (wxButtonToolBarTool*) FindById(event.GetId());
473 if (!tool)
474 {
475 event.Skip();
476 return;
477 }
478
479 if (tool->CanBeToggled())
480 tool->Toggle(tool->IsToggled());
481
482 // TODO: handle toggle items
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
493 void wxButtonToolBar::OnPaint(wxPaintEvent& event)
494 {
495 wxPaintDC dc(this);
496
497 dc.SetFont(GetFont());
498 dc.SetBackgroundMode(wxTRANSPARENT);
499
500 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
501 node;
502 node = node->GetNext() )
503 {
504 wxButtonToolBarTool *tool = (wxButtonToolBarTool*) node->GetData();
505 wxRect rectTool = GetToolRect(tool);
506 if (tool->IsToggled())
507 {
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));
512 dc.SetBrush(brush);
513 dc.SetPen(pen);
514 dc.DrawRectangle(backgroundRect);
515 }
516
517 if (m_labelHeight > 0 && !tool->GetShortHelp().empty())
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);
524 }
525 }
526
527 if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
528 {
529 wxPen pen(wxColour(159, 159, 159));
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 }
537 }
538
539 // detects mouse clicks outside buttons
540 void 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
555 #endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON