]> git.saurik.com Git - wxWidgets.git/blame - src/generic/buttonbar.cpp
Disable sorting of generic wxDataViewCtrl while it is frozen.
[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,
e4db172a 9// SciTech Software, Inc.
64d3ed17
JS
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 30
e4db172a
WS
31#include "wx/generic/buttonbar.h"
32
64d3ed17
JS
33#ifndef WX_PRECOMP
34 #include "wx/utils.h"
35 #include "wx/app.h"
e4db172a 36 #include "wx/log.h"
76b49cf4 37 #include "wx/frame.h"
ed4b0fdc 38 #include "wx/dcclient.h"
9eddec69 39 #include "wx/settings.h"
155ecd4c 40 #include "wx/image.h"
64d3ed17
JS
41#endif
42
64d3ed17
JS
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;
e4db172a 65
64d3ed17
JS
66 m_button = NULL;
67 }
68
cdb11cb9
VZ
69 wxButtonToolBarTool(wxButtonToolBar *tbar,
70 wxControl *control,
71 const wxString& label)
72 : wxToolBarToolBase(tbar, control, label)
64d3ed17
JS
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
83public:
84 // the tool position (for controls)
85 wxCoord m_x;
86 wxCoord m_y;
87 wxCoord m_width;
88 wxCoord m_height;
89
90private:
91 // the control representing the button
92 wxBitmapButton* m_button;
93};
94
95// ============================================================================
96// wxButtonToolBar implementation
97// ============================================================================
98
99IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar, wxControl)
100
101BEGIN_EVENT_TABLE(wxButtonToolBar, wxControl)
102 EVT_BUTTON(wxID_ANY, wxButtonToolBar::OnCommand)
77631b1d 103 EVT_PAINT(wxButtonToolBar::OnPaint)
fac6eaec 104 EVT_LEFT_UP(wxButtonToolBar::OnLeftUp)
64d3ed17
JS
105END_EVENT_TABLE()
106
107// ----------------------------------------------------------------------------
108// wxButtonToolBar creation
109// ----------------------------------------------------------------------------
110
111void 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
fac6eaec 119 m_maxWidth = m_maxHeight = 0;
77631b1d 120
fac6eaec
JS
121 m_labelMargin = 2;
122 m_labelHeight = 0;
e4db172a 123
006591de 124 SetMargins(8, 2);
fac6eaec 125 SetToolPacking(8);
64d3ed17
JS
126}
127
128bool 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
fac6eaec
JS
141 // wxColour lightBackground(244, 244, 244);
142
9aa727fa
VZ
143 wxFont font(wxSMALL_FONT->GetPointSize(),
144 wxNORMAL_FONT->GetFamily(),
145 wxNORMAL_FONT->GetStyle(),
146 wxFONTWEIGHT_NORMAL);
fac6eaec
JS
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 }
64d3ed17
JS
158 return true;
159}
160
161wxButtonToolBar::~wxButtonToolBar()
162{
163}
164
165// ----------------------------------------------------------------------------
166// wxButtonToolBar tool-related methods
167// ----------------------------------------------------------------------------
168
169wxToolBarToolBase *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
209void wxButtonToolBar::GetRectLimits(const wxRect& rect,
210 wxCoord *start,
211 wxCoord *end) const
212{
9a83f860 213 wxCHECK_RET( start && end, wxT("NULL pointer in GetRectLimits") );
64d3ed17
JS
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
228void wxButtonToolBar::SetToolShortHelp(int id, const wxString& help)
229{
230 wxToolBarToolBase *tool = FindById(id);
231
9a83f860 232 wxCHECK_RET( tool, wxT("SetToolShortHelp: no such tool") );
64d3ed17
JS
233
234 // TODO: set tooltip/short help
235 tool->SetShortHelp(help);
236}
237
238bool wxButtonToolBar::DoInsertTool(size_t WXUNUSED(pos),
239 wxToolBarToolBase * WXUNUSED(tool))
240{
241 return true;
242}
243
244bool wxButtonToolBar::DoDeleteTool(size_t WXUNUSED(pos),
245 wxToolBarToolBase * WXUNUSED(tool))
246{
247 // TODO
248 return true;
249}
250
251void wxButtonToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(enable))
252{
253 // TODO
254}
255
256void wxButtonToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
257{
258 // TODO
259}
260
261void wxButtonToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
262{
263 // TODO
264}
265
266wxToolBarToolBase *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
cdb11cb9
VZ
279wxToolBarToolBase *wxButtonToolBar::CreateTool(wxControl *control,
280 const wxString& label)
64d3ed17 281{
cdb11cb9 282 return new wxButtonToolBarTool(this, control, label);
64d3ed17
JS
283}
284
285// ----------------------------------------------------------------------------
286// wxButtonToolBar geometry
287// ----------------------------------------------------------------------------
288
289wxRect wxButtonToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
290{
291 const wxButtonToolBarTool *tool = (wxButtonToolBarTool *)toolBase;
292
293 wxRect rect;
294
9a83f860 295 wxCHECK_MSG( tool, rect, wxT("GetToolRect: NULL tool") );
64d3ed17
JS
296
297 // ensure that we always have the valid tool position
298 if ( m_needsLayout )
299 {
300 wxConstCast(this, wxButtonToolBar)->DoLayout();
301 }
302
fac6eaec
JS
303 rect.x = tool->m_x - (m_toolPacking/2);
304 rect.y = tool->m_y;
64d3ed17
JS
305
306 if ( IsVertical() )
307 {
308 if (tool->IsButton())
309 {
310 rect.width = m_defaultWidth;
311 rect.height = m_defaultHeight;
312 if (tool->GetButton())
fac6eaec 313 rect.SetSize(wxSize(tool->m_width, tool->m_height));
64d3ed17
JS
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())
fac6eaec 333 rect.SetSize(wxSize(tool->m_width, tool->m_height));
64d3ed17
JS
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
fac6eaec 347 rect.width += m_toolPacking;
64d3ed17
JS
348
349 return rect;
350}
351
352bool wxButtonToolBar::Realize()
353{
354 if ( !wxToolBarBase::Realize() )
355 return false;
e4db172a 356
64d3ed17
JS
357 m_needsLayout = true;
358 DoLayout();
e4db172a 359
170acdc9 360 SetInitialSize(wxSize(m_maxWidth, m_maxHeight));
64d3ed17
JS
361
362 return true;
363}
364
365void 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;
e4db172a 387
64d3ed17
JS
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,
fac6eaec 393 wxBU_AUTODRAW|wxBORDER_NONE);
76b49cf4 394 if (!tool->GetShortHelp().empty())
fac6eaec 395 bmpButton->SetLabel(tool->GetShortHelp());
e4db172a 396
64d3ed17
JS
397 tool->SetButton(bmpButton);
398 }
399 else
400 {
401 tool->GetButton()->Move(wxPoint(tool->m_x, tool->m_y));
402 }
e4db172a 403
64d3ed17
JS
404 int w = widthTool;
405 if (tool->GetButton())
406 {
407 wxSize sz = tool->GetButton()->GetSize();
408 w = sz.x;
409
fac6eaec
JS
410 if (m_labelHeight > 0)
411 {
412 sz.y += (m_labelHeight + m_labelMargin);
413
76b49cf4 414 if (!tool->GetShortHelp().empty())
fac6eaec
JS
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
64d3ed17 433 maxHeight = wxMax(maxHeight, sz.y);
fac6eaec
JS
434
435 tool->m_width = sz.x;
436 tool->m_height = sz.y;
437 w = sz.x;
64d3ed17
JS
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;
77631b1d
JS
464
465 if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
466 m_maxHeight += 2;
467
64d3ed17
JS
468}
469
470wxSize wxButtonToolBar::DoGetBestClientSize() const
471{
472 return wxSize(m_maxWidth, m_maxHeight);
473}
474
475// receives button commands
476void wxButtonToolBar::OnCommand(wxCommandEvent& event)
477{
478 wxButtonToolBarTool* tool = (wxButtonToolBarTool*) FindById(event.GetId());
479 if (!tool)
480 {
481 event.Skip();
482 return;
483 }
484
77631b1d
JS
485 if (tool->CanBeToggled())
486 tool->Toggle(tool->IsToggled());
487
64d3ed17 488 // TODO: handle toggle items
77631b1d
JS
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
ff654490 499void wxButtonToolBar::OnPaint(wxPaintEvent& WXUNUSED(event))
77631b1d
JS
500{
501 wxPaintDC dc(this);
502
fac6eaec
JS
503 dc.SetFont(GetFont());
504 dc.SetBackgroundMode(wxTRANSPARENT);
505
77631b1d
JS
506 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
507 node;
508 node = node->GetNext() )
509 {
510 wxButtonToolBarTool *tool = (wxButtonToolBarTool*) node->GetData();
fac6eaec 511 wxRect rectTool = GetToolRect(tool);
77631b1d
JS
512 if (tool->IsToggled())
513 {
fac6eaec
JS
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));
77631b1d
JS
518 dc.SetBrush(brush);
519 dc.SetPen(pen);
fac6eaec
JS
520 dc.DrawRectangle(backgroundRect);
521 }
522
76b49cf4 523 if (m_labelHeight > 0 && !tool->GetShortHelp().empty())
fac6eaec
JS
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);
77631b1d
JS
530 }
531 }
532
533 if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
534 {
fac6eaec 535 wxPen pen(wxColour(159, 159, 159));
77631b1d
JS
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 }
64d3ed17
JS
543}
544
fac6eaec
JS
545// detects mouse clicks outside buttons
546void 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_COMMAND_BUTTON_CLICKED, tool->GetId());
554 event.SetEventObject(tool->GetButton());
eafd76b0 555 if (!GetEventHandler()->ProcessEvent(event))
fac6eaec
JS
556 event.Skip();
557 }
558 }
559}
560
64d3ed17 561#endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON