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