]> git.saurik.com Git - wxWidgets.git/blame - src/generic/buttonbar.cpp
Reversed accidental commit
[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,
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
b887dc7b
JS
28// Currently, only for Mac as a toolbar replacement.
29#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
64d3ed17
JS
30
31#ifndef WX_PRECOMP
32 #include "wx/utils.h"
33 #include "wx/app.h"
34#endif
35
36#include "wx/generic/buttonbar.h"
37#include "wx/frame.h"
38#include "wx/image.h"
39#include "wx/log.h"
77631b1d
JS
40#include "wx/settings.h"
41#include "wx/dcclient.h"
64d3ed17
JS
42
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;
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
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)
64d3ed17
JS
102END_EVENT_TABLE()
103
104// ----------------------------------------------------------------------------
105// wxButtonToolBar creation
106// ----------------------------------------------------------------------------
107
108void wxButtonToolBar::Init()
109{
110 // no tools yet
111 m_needsLayout = false;
112
113 // unknown widths for the tools and separators
114 m_widthSeparator = wxDefaultCoord;
115
116 m_maxWidth =
77631b1d
JS
117 m_maxHeight = 0;
118
119 SetMargins(8, 4);
120 SetToolPacking(5);
64d3ed17
JS
121}
122
123bool wxButtonToolBar::Create(wxWindow *parent,
124 wxWindowID id,
125 const wxPoint& pos,
126 const wxSize& size,
127 long style,
128 const wxString& name)
129{
130 if ( !wxToolBarBase::Create(parent, id, pos, size, style,
131 wxDefaultValidator, name) )
132 {
133 return false;
134 }
135
77631b1d
JS
136 // TODO: get the correct colour from the system
137 wxColour lightBackground(240, 240, 240);
138 SetBackgroundColour(lightBackground);
64d3ed17
JS
139 return true;
140}
141
142wxButtonToolBar::~wxButtonToolBar()
143{
144}
145
146// ----------------------------------------------------------------------------
147// wxButtonToolBar tool-related methods
148// ----------------------------------------------------------------------------
149
150wxToolBarToolBase *wxButtonToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
151{
152 // check the "other" direction first: it must be inside the toolbar or we
153 // don't risk finding anything
154 if ( IsVertical() )
155 {
156 if ( x < 0 || x > m_maxWidth )
157 return NULL;
158
159 // we always use x, even for a vertical toolbar, this makes the code
160 // below simpler
161 x = y;
162 }
163 else // horizontal
164 {
165 if ( y < 0 || y > m_maxHeight )
166 return NULL;
167 }
168
169 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
170 node;
171 node = node->GetNext() )
172 {
173 wxButtonToolBarTool *tool = (wxButtonToolBarTool*) node->GetData();
174 wxRect rectTool = GetToolRect(tool);
175
176 wxCoord startTool, endTool;
177 GetRectLimits(rectTool, &startTool, &endTool);
178
179 if ( x >= startTool && x <= endTool )
180 {
181 // don't return the separators from here, they don't accept any
182 // input anyhow
183 return tool->IsSeparator() ? NULL : tool;
184 }
185 }
186
187 return NULL;
188}
189
190void wxButtonToolBar::GetRectLimits(const wxRect& rect,
191 wxCoord *start,
192 wxCoord *end) const
193{
194 wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") );
195
196 if ( IsVertical() )
197 {
198 *start = rect.GetTop();
199 *end = rect.GetBottom();
200 }
201 else // horizontal
202 {
203 *start = rect.GetLeft();
204 *end = rect.GetRight();
205 }
206}
207
208
209void wxButtonToolBar::SetToolShortHelp(int id, const wxString& help)
210{
211 wxToolBarToolBase *tool = FindById(id);
212
213 wxCHECK_RET( tool, _T("SetToolShortHelp: no such tool") );
214
215 // TODO: set tooltip/short help
216 tool->SetShortHelp(help);
217}
218
219bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos),
220 wxToolBarToolBase * WXUNUSED(tool))
221{
222 return true;
223}
224
225bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos),
226 wxToolBarToolBase * WXUNUSED(tool))
227{
228 // TODO
229 return true;
230}
231
232void wxButtonToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(enable))
233{
234 // TODO
235}
236
237void wxButtonToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
238{
239 // TODO
240}
241
242void wxButtonToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
243{
244 // TODO
245}
246
247wxToolBarToolBase *wxButtonToolBar::CreateTool(int id,
248 const wxString& label,
249 const wxBitmap& bmpNormal,
250 const wxBitmap& bmpDisabled,
251 wxItemKind kind,
252 wxObject *clientData,
253 const wxString& shortHelp,
254 const wxString& longHelp)
255{
256 return new wxButtonToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
257 clientData, shortHelp, longHelp);
258}
259
260wxToolBarToolBase *wxButtonToolBar::CreateTool(wxControl *control)
261{
262 return new wxButtonToolBarTool(this, control);
263}
264
265// ----------------------------------------------------------------------------
266// wxButtonToolBar geometry
267// ----------------------------------------------------------------------------
268
269wxRect wxButtonToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
270{
271 const wxButtonToolBarTool *tool = (wxButtonToolBarTool *)toolBase;
272
273 wxRect rect;
274
275 wxCHECK_MSG( tool, rect, _T("GetToolRect: NULL tool") );
276
277 // ensure that we always have the valid tool position
278 if ( m_needsLayout )
279 {
280 wxConstCast(this, wxButtonToolBar)->DoLayout();
281 }
282
283 rect.x = tool->m_x - m_xMargin;
284 rect.y = tool->m_y - m_yMargin;
285
286 if ( IsVertical() )
287 {
288 if (tool->IsButton())
289 {
290 rect.width = m_defaultWidth;
291 rect.height = m_defaultHeight;
292 if (tool->GetButton())
293 rect.SetSize(tool->GetButton()->GetSize());
294 }
295 else if (tool->IsSeparator())
296 {
297 rect.width = m_defaultWidth;
298 rect.height = m_widthSeparator;
299 }
300 else // control
301 {
302 rect.width = tool->m_width;
303 rect.height = tool->m_height;
304 }
305 }
306 else // horizontal
307 {
308 if (tool->IsButton())
309 {
310 rect.width = m_defaultWidth;
311 rect.height = m_defaultHeight;
312 if (tool->GetButton())
313 rect.SetSize(tool->GetButton()->GetSize());
314 }
315 else if (tool->IsSeparator())
316 {
317 rect.width = m_widthSeparator;
318 rect.height = m_defaultHeight;
319 }
320 else // control
321 {
322 rect.width = tool->m_width;
323 rect.height = tool->m_height;
324 }
325 }
326
327 rect.width += 2*m_xMargin;
328 rect.height += 2*m_yMargin;
329
330 return rect;
331}
332
333bool wxButtonToolBar::Realize()
334{
335 if ( !wxToolBarBase::Realize() )
336 return false;
337
338 m_needsLayout = true;
339 DoLayout();
340
341 SetBestSize(wxSize(m_maxWidth, m_maxHeight));
342
343 return true;
344}
345
346void wxButtonToolBar::DoLayout()
347{
348 m_needsLayout = false;
349
350 wxCoord x = m_xMargin,
351 y = m_yMargin;
352
353 int maxHeight = 0;
354
355 const wxCoord widthTool = IsVertical() ? m_defaultHeight : m_defaultWidth;
356 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin;
357 wxCoord *pCur = IsVertical() ? &y : &x;
358
359 // calculate the positions of all elements
360 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
361 node;
362 node = node->GetNext() )
363 {
364 wxButtonToolBarTool *tool = (wxButtonToolBarTool *) node->GetData();
365
366 tool->m_x = x;
367 tool->m_y = y;
368
369 if (tool->IsButton())
370 {
371 if (!tool->GetButton())
372 {
373 wxBitmapButton* bmpButton = new wxBitmapButton(this, tool->GetId(), tool->GetNormalBitmap(), wxPoint(tool->m_x, tool->m_y), wxDefaultSize,
77631b1d 374 wxBU_AUTODRAW|wxBORDER_NONE);
64d3ed17
JS
375
376 tool->SetButton(bmpButton);
377 }
378 else
379 {
380 tool->GetButton()->Move(wxPoint(tool->m_x, tool->m_y));
381 }
382
383 int w = widthTool;
384 if (tool->GetButton())
385 {
386 wxSize sz = tool->GetButton()->GetSize();
387 w = sz.x;
388
389 maxHeight = wxMax(maxHeight, sz.y);
390 }
391
392 *pCur += (w + GetToolPacking());
393 }
394 else if (tool->IsSeparator())
395 {
396 *pCur += m_widthSeparator;
397 }
398 else if (!IsVertical()) // horizontal control
399 {
400 wxControl *control = tool->GetControl();
401 wxSize size = control->GetSize();
402 tool->m_y += (m_defaultHeight - size.y)/2;
403 tool->m_width = size.x;
404 tool->m_height = size.y;
405
406 *pCur += tool->m_width;
407
408 maxHeight = wxMax(maxHeight, size.y);
409 }
410 *pCur += margin;
411 }
412
413 // calculate the total toolbar size
414 m_maxWidth = x + 2*m_xMargin;
415 m_maxHeight = maxHeight + 2*m_yMargin;
77631b1d
JS
416
417 if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
418 m_maxHeight += 2;
419
64d3ed17
JS
420}
421
422wxSize wxButtonToolBar::DoGetBestClientSize() const
423{
424 return wxSize(m_maxWidth, m_maxHeight);
425}
426
427// receives button commands
428void wxButtonToolBar::OnCommand(wxCommandEvent& event)
429{
430 wxButtonToolBarTool* tool = (wxButtonToolBarTool*) FindById(event.GetId());
431 if (!tool)
432 {
433 event.Skip();
434 return;
435 }
436
77631b1d
JS
437 if (tool->CanBeToggled())
438 tool->Toggle(tool->IsToggled());
439
64d3ed17 440 // TODO: handle toggle items
77631b1d
JS
441 OnLeftClick(event.GetId(), false);
442
443 if (tool->GetKind() == wxITEM_RADIO)
444 UnToggleRadioGroup(tool);
445
446 if (tool->CanBeToggled())
447 Refresh();
448}
449
450// paints a border
451void wxButtonToolBar::OnPaint(wxPaintEvent& event)
452{
453 wxPaintDC dc(this);
454
455 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
456 node;
457 node = node->GetNext() )
458 {
459 wxButtonToolBarTool *tool = (wxButtonToolBarTool*) node->GetData();
460 if (tool->IsToggled())
461 {
462 wxRect rectTool = GetToolRect(tool);
463 rectTool.y = 0; rectTool.height = GetClientSize().y;
464 wxBrush brush(wxColour(220, 220, 220));
465 wxPen pen(*wxLIGHT_GREY);
466 dc.SetBrush(brush);
467 dc.SetPen(pen);
468 dc.DrawRectangle(rectTool);
469 }
470 }
471
472 if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
473 {
474 wxPen pen(*wxLIGHT_GREY);
475 dc.SetPen(pen);
476 int x1 = 0;
477 int y1 = GetClientSize().y-1;
478 int x2 = GetClientSize().x;
479 int y2 = y1;
480 dc.DrawLine(x1, y1, x2, y2);
481 }
64d3ed17
JS
482}
483
484#endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON
485