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