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