]> git.saurik.com Git - wxWidgets.git/blame - src/univ/toolbar.cpp
Use std::isfinite() for wxFinite() for C++11 compilers.
[wxWidgets.git] / src / univ / toolbar.cpp
CommitLineData
c08a4f00
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/univ/toolbar.cpp
3216dbf5
VZ
3// Purpose: implementation of wxToolBar for wxUniversal
4// Author: Robert Roebling, Vadim Zeitlin (universalization)
5// Modified by:
6// Created: 20.02.02
3216dbf5
VZ
7// Copyright: (c) 2001 Robert Roebling,
8// (c) 2002 SciTech Software, Inc. (www.scitechsoft.com)
65571936 9// Licence: wxWindows licence
c08a4f00
RR
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c08a4f00
RR
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
6a317e61
VZ
27#if wxUSE_TOOLBAR
28
e4db172a
WS
29#include "wx/toolbar.h"
30
c08a4f00
RR
31#ifndef WX_PRECOMP
32 #include "wx/utils.h"
33 #include "wx/app.h"
e4db172a 34 #include "wx/log.h"
76b49cf4 35 #include "wx/frame.h"
370efbe7 36 #include "wx/dc.h"
155ecd4c 37 #include "wx/image.h"
c08a4f00
RR
38#endif
39
a9b33d6b
VZ
40#include "wx/univ/renderer.h"
41
9467bdb7
VZ
42// ----------------------------------------------------------------------------
43// wxStdToolbarInputHandler: translates SPACE and ENTER keys and the left mouse
44// click into button press/release actions
45// ----------------------------------------------------------------------------
46
47class WXDLLEXPORT wxStdToolbarInputHandler : public wxStdInputHandler
48{
49public:
50 wxStdToolbarInputHandler(wxInputHandler *inphand);
51
52 virtual bool HandleKey(wxInputConsumer *consumer,
53 const wxKeyEvent& event,
54 bool pressed);
55 virtual bool HandleMouse(wxInputConsumer *consumer,
56 const wxMouseEvent& event);
57 virtual bool HandleMouseMove(wxInputConsumer *consumer, const wxMouseEvent& event);
58 virtual bool HandleFocus(wxInputConsumer *consumer, const wxFocusEvent& event);
59 virtual bool HandleActivation(wxInputConsumer *consumer, bool activated);
60
61private:
62 wxWindow *m_winCapture;
63 wxToolBarToolBase *m_toolCapture;
64 wxToolBarToolBase *m_toolLast;
65};
66
3216dbf5
VZ
67// ----------------------------------------------------------------------------
68// constants
69// ----------------------------------------------------------------------------
70
71// value meaning that m_widthSeparator is not initialized
a290fa5a 72static const wxCoord INVALID_WIDTH = wxDefaultCoord;
3216dbf5
VZ
73
74// ----------------------------------------------------------------------------
75// wxToolBarTool: our implementation of wxToolBarToolBase
76// ----------------------------------------------------------------------------
77
78class WXDLLEXPORT wxToolBarTool : public wxToolBarToolBase
16c9a425 79{
3216dbf5 80public:
d448aec3
VZ
81 wxToolBarTool(wxToolBar *tbar,
82 int id,
83 const wxString& label,
84 const wxBitmap& bmpNormal,
85 const wxBitmap& bmpDisabled,
86 wxItemKind kind,
87 wxObject *clientData,
88 const wxString& shortHelp,
89 const wxString& longHelp)
90 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
91 clientData, shortHelp, longHelp)
3216dbf5
VZ
92 {
93 // no position yet
94 m_x =
a290fa5a 95 m_y = wxDefaultCoord;
a8f4cabe
JS
96 m_width =
97 m_height = 0;
98
99 // not pressed yet
a290fa5a 100 m_isInverted = false;
32b13913 101
a8f4cabe 102 // mouse not here yet
a290fa5a 103 m_underMouse = false;
a8f4cabe
JS
104 }
105
cdb11cb9
VZ
106 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
107 : wxToolBarToolBase(tbar, control, label)
a8f4cabe
JS
108 {
109 // no position yet
110 m_x =
a290fa5a 111 m_y = wxDefaultCoord;
a8f4cabe
JS
112 m_width =
113 m_height = 0;
5a73d082
VZ
114
115 // not pressed yet
a290fa5a 116 m_isInverted = false;
32b13913 117
34d26f42 118 // mouse not here yet
a290fa5a 119 m_underMouse = false;
3216dbf5
VZ
120 }
121
5a73d082
VZ
122 // is this tool pressed, even temporarily? (this is different from being
123 // permanently toggled which is what IsToggled() returns)
124 bool IsPressed() const
125 { return CanBeToggled() ? IsToggled() != m_isInverted : m_isInverted; }
126
127 // are we temporarily pressed/unpressed?
128 bool IsInverted() const { return m_isInverted; }
129
130 // press the tool temporarily by inverting its toggle state
131 void Invert() { m_isInverted = !m_isInverted; }
32b13913 132
34d26f42 133 // Set underMouse
a290fa5a 134 void SetUnderMouse( bool under = true ) { m_underMouse = under; }
34d26f42 135 bool IsUnderMouse() { return m_underMouse; }
5a73d082 136
3216dbf5 137public:
a8f4cabe
JS
138 // the tool position (for controls)
139 wxCoord m_x;
140 wxCoord m_y;
141 wxCoord m_width;
142 wxCoord m_height;
5a73d082
VZ
143
144private:
a290fa5a 145 // true if the tool is pressed
5a73d082 146 bool m_isInverted;
32b13913 147
a290fa5a 148 // true if the tool is under the mouse
34d26f42 149 bool m_underMouse;
3216dbf5
VZ
150};
151
152// ============================================================================
153// wxToolBar implementation
154// ============================================================================
155
412e0d47 156IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
3216dbf5
VZ
157
158// ----------------------------------------------------------------------------
159// wxToolBar creation
160// ----------------------------------------------------------------------------
c08a4f00
RR
161
162void wxToolBar::Init()
163{
3216dbf5 164 // no tools yet
a290fa5a 165 m_needsLayout = false;
3216dbf5
VZ
166
167 // unknown widths for the tools and separators
168 m_widthSeparator = INVALID_WIDTH;
169
170 m_maxWidth =
16c9a425
JS
171 m_maxHeight = 0;
172
3216dbf5
VZ
173 wxRenderer *renderer = GetRenderer();
174
175 SetToolBitmapSize(renderer->GetToolBarButtonSize(&m_widthSeparator));
176 SetMargins(renderer->GetToolBarMargin());
c08a4f00
RR
177}
178
3216dbf5
VZ
179bool wxToolBar::Create(wxWindow *parent,
180 wxWindowID id,
181 const wxPoint& pos,
182 const wxSize& size,
183 long style,
184 const wxString& name)
185{
186 if ( !wxToolBarBase::Create(parent, id, pos, size, style,
187 wxDefaultValidator, name) )
188 {
a290fa5a 189 return false;
3216dbf5
VZ
190 }
191
d408730c
VZ
192 FixupStyle();
193
3216dbf5
VZ
194 CreateInputHandler(wxINP_HANDLER_TOOLBAR);
195
170acdc9 196 SetInitialSize(size);
3216dbf5 197
a290fa5a 198 return true;
3216dbf5
VZ
199}
200
201wxToolBar::~wxToolBar()
202{
dda4f6c0
JS
203 // Make sure the toolbar is removed from the parent.
204 SetSize(0,0);
3216dbf5
VZ
205}
206
34d26f42
RR
207void wxToolBar::SetMargins(int x, int y)
208{
209 // This required for similar visual effects under
210 // native platforms and wxUniv.
72726d27 211 wxToolBarBase::SetMargins( x + 3, y + 3 );
34d26f42
RR
212}
213
3216dbf5
VZ
214// ----------------------------------------------------------------------------
215// wxToolBar tool-related methods
216// ----------------------------------------------------------------------------
217
c08a4f00
RR
218wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
219{
3216dbf5
VZ
220 // check the "other" direction first: it must be inside the toolbar or we
221 // don't risk finding anything
222 if ( IsVertical() )
223 {
224 if ( x < 0 || x > m_maxWidth )
225 return NULL;
226
227 // we always use x, even for a vertical toolbar, this makes the code
228 // below simpler
229 x = y;
230 }
231 else // horizontal
232 {
233 if ( y < 0 || y > m_maxHeight )
234 return NULL;
235 }
236
ac32ba44 237 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
3216dbf5
VZ
238 node;
239 node = node->GetNext() )
240 {
241 wxToolBarToolBase *tool = node->GetData();
242 wxRect rectTool = GetToolRect(tool);
243
244 wxCoord startTool, endTool;
245 GetRectLimits(rectTool, &startTool, &endTool);
246
247 if ( x >= startTool && x <= endTool )
248 {
249 // don't return the separators from here, they don't accept any
250 // input anyhow
251 return tool->IsSeparator() ? NULL : tool;
252 }
253 }
254
c08a4f00
RR
255 return NULL;
256}
257
3216dbf5 258void wxToolBar::SetToolShortHelp(int id, const wxString& help)
c08a4f00 259{
3216dbf5
VZ
260 wxToolBarToolBase *tool = FindById(id);
261
9a83f860 262 wxCHECK_RET( tool, wxT("SetToolShortHelp: no such tool") );
3216dbf5
VZ
263
264 tool->SetShortHelp(help);
c08a4f00
RR
265}
266
3216dbf5
VZ
267bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
268 wxToolBarToolBase * WXUNUSED(tool))
c08a4f00 269{
3216dbf5 270 // recalculate the toolbar geometry before redrawing it the next time
a290fa5a 271 m_needsLayout = true;
3216dbf5
VZ
272
273 // and ensure that we indeed are going to redraw
274 Refresh();
275
a290fa5a 276 return true;
c08a4f00
RR
277}
278
3216dbf5
VZ
279bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos),
280 wxToolBarToolBase * WXUNUSED(tool))
c08a4f00 281{
3216dbf5 282 // as above
a290fa5a 283 m_needsLayout = true;
3216dbf5
VZ
284
285 Refresh();
286
a290fa5a 287 return true;
c08a4f00
RR
288}
289
290void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
291{
10ab355c 292#if wxUSE_IMAGE
3216dbf5 293 // created disabled-state bitmap on demand
a1b806b9 294 if ( !enable && !tool->GetDisabledBitmap().IsOk() )
c229e50d 295 {
10ab355c 296 wxImage image(tool->GetNormalBitmap().ConvertToImage());
c229e50d 297
10ab355c 298 tool->SetDisabledBitmap(image.ConvertToGreyscale());
c229e50d 299 }
10ab355c 300#endif // wxUSE_IMAGE
3216dbf5
VZ
301
302 RefreshTool(tool);
c08a4f00
RR
303}
304
3216dbf5 305void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
c08a4f00 306{
3216dbf5
VZ
307 // note that if we're called the tool did change state (the base class
308 // checks for it), so it's not necessary to check for this again here
309 RefreshTool(tool);
c08a4f00
RR
310}
311
3216dbf5 312void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
c08a4f00 313{
3216dbf5 314 RefreshTool(tool);
c08a4f00
RR
315}
316
317wxToolBarToolBase *wxToolBar::CreateTool(int id,
d448aec3
VZ
318 const wxString& label,
319 const wxBitmap& bmpNormal,
320 const wxBitmap& bmpDisabled,
321 wxItemKind kind,
3216dbf5 322 wxObject *clientData,
d448aec3
VZ
323 const wxString& shortHelp,
324 const wxString& longHelp)
c08a4f00 325{
d448aec3
VZ
326 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
327 clientData, shortHelp, longHelp);
c08a4f00 328}
3216dbf5 329
cdb11cb9
VZ
330wxToolBarToolBase *
331wxToolBar::CreateTool(wxControl *control, const wxString& label)
c08a4f00 332{
cdb11cb9 333 return new wxToolBarTool(this, control, label);
c08a4f00
RR
334}
335
3216dbf5
VZ
336// ----------------------------------------------------------------------------
337// wxToolBar geometry
338// ----------------------------------------------------------------------------
c08a4f00 339
3216dbf5 340wxRect wxToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
c08a4f00 341{
3216dbf5
VZ
342 const wxToolBarTool *tool = (wxToolBarTool *)toolBase;
343
344 wxRect rect;
345
9a83f860 346 wxCHECK_MSG( tool, rect, wxT("GetToolRect: NULL tool") );
3216dbf5
VZ
347
348 // ensure that we always have the valid tool position
349 if ( m_needsLayout )
bb312b54 350 {
3216dbf5 351 wxConstCast(this, wxToolBar)->DoLayout();
bb312b54 352 }
3216dbf5
VZ
353
354 rect.x = tool->m_x - m_xMargin;
355 rect.y = tool->m_y - m_yMargin;
356
357 if ( IsVertical() )
c08a4f00 358 {
a290fa5a 359 if (tool->IsButton())
a8f4cabe 360 {
370efbe7
WS
361 if(!HasFlag(wxTB_TEXT))
362 {
363 rect.width = m_defaultWidth;
364 rect.height = m_defaultHeight;
365 }
366 else
367 {
368 rect.width = m_defaultWidth +
369 GetFont().GetPointSize() * tool->GetLabel().length();
370 rect.height = m_defaultHeight;
371 }
a8f4cabe
JS
372 }
373 else if (tool->IsSeparator())
374 {
375 rect.width = m_defaultWidth;
376 rect.height = m_widthSeparator;
377 }
378 else // control
379 {
380 rect.width = tool->m_width;
381 rect.height = tool->m_height;
382 }
c08a4f00 383 }
3216dbf5 384 else // horizontal
c08a4f00 385 {
a8f4cabe
JS
386 if (tool->IsButton())
387 {
370efbe7
WS
388 if(!HasFlag(wxTB_TEXT))
389 {
390 rect.width = m_defaultWidth;
391 rect.height = m_defaultHeight;
392 }
393 else
394 {
395 rect.width = m_defaultWidth +
396 GetFont().GetPointSize() * tool->GetLabel().length();
397 rect.height = m_defaultHeight;
398 }
a8f4cabe
JS
399 }
400 else if (tool->IsSeparator())
401 {
402 rect.width = m_widthSeparator;
403 rect.height = m_defaultHeight;
404 }
405 else // control
406 {
407 rect.width = tool->m_width;
408 rect.height = tool->m_height;
409 }
c08a4f00 410 }
3216dbf5
VZ
411
412 rect.width += 2*m_xMargin;
413 rect.height += 2*m_yMargin;
414
415 return rect;
c08a4f00
RR
416}
417
3216dbf5 418bool wxToolBar::Realize()
c08a4f00 419{
3216dbf5 420 if ( !wxToolBarBase::Realize() )
a290fa5a 421 return false;
3216dbf5 422
a290fa5a 423 m_needsLayout = true;
3216dbf5
VZ
424 DoLayout();
425
5c680882
VZ
426 // the first item in the radio group is checked by default to be consistent
427 // with wxGTK and the menu radio items
428 int radioGroupCount = 0;
429
430 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
431 node;
432 node = node->GetNext() )
433 {
434 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
435
436 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
437 {
438 radioGroupCount = 0;
439 continue;
440 }
441
442 bool toggle = !radioGroupCount++;
443 if ( tool->Toggle(toggle) )
444 {
445 DoToggleTool(tool, toggle);
446 }
447 }
448
170acdc9 449 SetInitialSize(wxDefaultSize);
3216dbf5 450
a290fa5a 451 return true;
3216dbf5
VZ
452}
453
370efbe7
WS
454void wxToolBar::SetWindowStyleFlag( long style )
455{
456 wxToolBarBase::SetWindowStyleFlag(style);
457
458 m_needsLayout = true;
459
460 Refresh();
461}
462
3216dbf5
VZ
463void wxToolBar::DoLayout()
464{
9a83f860 465 wxASSERT_MSG( m_needsLayout, wxT("why are we called?") );
3216dbf5 466
a290fa5a 467 m_needsLayout = false;
3216dbf5
VZ
468
469 wxCoord x = m_xMargin,
470 y = m_yMargin;
471
370efbe7
WS
472 wxCoord widthTool = 0, maxWidthTool = 0;
473 wxCoord heightTool = 0, maxHeightTool = 0;
32b13913
WS
474 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin;
475 wxCoord *pCur = IsVertical() ? &y : &x;
3216dbf5
VZ
476
477 // calculate the positions of all elements
ac32ba44 478 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
c08a4f00
RR
479 node;
480 node = node->GetNext() )
481 {
3216dbf5
VZ
482 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
483
484 tool->m_x = x;
485 tool->m_y = y;
486
72726d27 487 // TODO ugly number fiddling
a8f4cabe
JS
488 if (tool->IsButton())
489 {
370efbe7
WS
490 if (IsVertical())
491 {
492 widthTool = m_defaultHeight;
493 heightTool = m_defaultWidth;
494 if(HasFlag(wxTB_TEXT))
495 heightTool += GetFont().GetPointSize() * tool->GetLabel().length();
496 }
497 else
498 {
499 widthTool = m_defaultWidth;
500 if(HasFlag(wxTB_TEXT))
501 widthTool += GetFont().GetPointSize() * tool->GetLabel().length();
502
503 heightTool = m_defaultHeight;
504 }
505
506 if(widthTool > maxWidthTool) // Record max width of tool
507 {
508 maxWidthTool = widthTool;
509 }
510
511 if(heightTool > maxHeightTool) // Record max width of tool
512 {
513 maxHeightTool = heightTool;
514 }
515
a8f4cabe
JS
516 *pCur += widthTool;
517 }
518 else if (tool->IsSeparator())
519 {
520 *pCur += m_widthSeparator;
521 }
522 else if (!IsVertical()) // horizontal control
523 {
524 wxControl *control = tool->GetControl();
525 wxSize size = control->GetSize();
526 tool->m_y += (m_defaultHeight - size.y)/2;
527 tool->m_width = size.x;
528 tool->m_height = size.y;
529
530 *pCur += tool->m_width;
531 }
532 *pCur += margin;
c08a4f00 533 }
3216dbf5
VZ
534
535 // calculate the total toolbar size
370efbe7
WS
536 wxCoord xMin, yMin;
537
538 if(!HasFlag(wxTB_TEXT))
539 {
540 xMin = m_defaultWidth + 2*m_xMargin;
541 yMin = m_defaultHeight + 2*m_yMargin;
542 }
543 else
544 {
545 if (IsVertical())
546 {
547 xMin = heightTool + 2*m_xMargin;
548 yMin = widthTool + 2*m_xMargin;
549 }
550 else
551 {
552 xMin = maxWidthTool + 2*m_xMargin;
553 yMin = heightTool + 2*m_xMargin;
554 }
555 }
3216dbf5
VZ
556
557 m_maxWidth = x < xMin ? xMin : x;
558 m_maxHeight = y < yMin ? yMin : y;
c08a4f00
RR
559}
560
3216dbf5 561wxSize wxToolBar::DoGetBestClientSize() const
c08a4f00 562{
3216dbf5
VZ
563 return wxSize(m_maxWidth, m_maxHeight);
564}
565
dda4f6c0
JS
566void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
567{
568 int old_width, old_height;
569 GetSize(&old_width, &old_height);
570
571 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
32b13913 572
dda4f6c0 573 // Correct width and height if needed.
a290fa5a 574 if ( width == wxDefaultCoord || height == wxDefaultCoord )
dda4f6c0
JS
575 {
576 int tmp_width, tmp_height;
577 GetSize(&tmp_width, &tmp_height);
578
a290fa5a 579 if ( width == wxDefaultCoord )
dda4f6c0 580 width = tmp_width;
a290fa5a 581 if ( height == wxDefaultCoord )
dda4f6c0
JS
582 height = tmp_height;
583 }
32b13913 584
dda4f6c0
JS
585 // We must refresh the frame size when the toolbar changes size
586 // otherwise the toolbar can be shown incorrectly
587 if ( old_width != width || old_height != height )
588 {
0dba08dd 589 SendSizeEventToParent();
dda4f6c0
JS
590 }
591}
592
3216dbf5
VZ
593// ----------------------------------------------------------------------------
594// wxToolBar drawing
595// ----------------------------------------------------------------------------
c08a4f00 596
3216dbf5
VZ
597void wxToolBar::RefreshTool(wxToolBarToolBase *tool)
598{
599 RefreshRect(GetToolRect(tool));
600}
601
602void wxToolBar::GetRectLimits(const wxRect& rect,
603 wxCoord *start,
604 wxCoord *end) const
605{
9a83f860 606 wxCHECK_RET( start && end, wxT("NULL pointer in GetRectLimits") );
3216dbf5
VZ
607
608 if ( IsVertical() )
16c9a425 609 {
3216dbf5
VZ
610 *start = rect.GetTop();
611 *end = rect.GetBottom();
16c9a425 612 }
3216dbf5 613 else // horizontal
16c9a425 614 {
3216dbf5
VZ
615 *start = rect.GetLeft();
616 *end = rect.GetRight();
16c9a425 617 }
3216dbf5 618}
c08a4f00 619
3216dbf5
VZ
620void wxToolBar::DoDraw(wxControlRenderer *renderer)
621{
622 // prepare the variables used below
623 wxDC& dc = renderer->GetDC();
624 wxRenderer *rend = renderer->GetRenderer();
370efbe7 625 dc.SetFont(GetFont());
3216dbf5
VZ
626
627 // draw the border separating us from the menubar (if there is no menubar
628 // we probably shouldn't draw it?)
629 if ( !IsVertical() )
630 {
631 rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x);
632 }
633
634 // get the update rect and its limits depending on the orientation
635 wxRect rectUpdate = GetUpdateClientRect();
636 wxCoord start, end;
637 GetRectLimits(rectUpdate, &start, &end);
638
639 // and redraw all the tools intersecting it
ac32ba44 640 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
c08a4f00
RR
641 node;
642 node = node->GetNext() )
643 {
34d26f42 644 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
3216dbf5
VZ
645 wxRect rectTool = GetToolRect(tool);
646 wxCoord startTool, endTool;
647 GetRectLimits(rectTool, &startTool, &endTool);
648
649 if ( endTool < start )
c08a4f00 650 {
3216dbf5
VZ
651 // we're still to the left of the area to redraw
652 continue;
16c9a425 653 }
3216dbf5
VZ
654
655 if ( startTool > end )
16c9a425 656 {
3216dbf5
VZ
657 // we're beyond the area to redraw, nothing left to do
658 break;
659 }
32b13913 660
72726d27
RR
661 if (tool->IsSeparator() && !HasFlag(wxTB_FLAT))
662 {
d6922577 663 // Draw separators only in flat mode
72726d27
RR
664 continue;
665 }
32b13913 666
3216dbf5
VZ
667 // deal with the flags
668 int flags = 0;
669
670 if ( tool->IsEnabled() )
671 {
34d26f42
RR
672 // The toolbars without wxTB_FLAT don't react to the mouse hovering
673 if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() )
3216dbf5
VZ
674 flags |= wxCONTROL_CURRENT;
675 }
676 else // disabled tool
677 {
678 flags |= wxCONTROL_DISABLED;
c08a4f00 679 }
3216dbf5 680
34d26f42
RR
681 //if ( tool == m_toolCaptured )
682 // flags |= wxCONTROL_FOCUSED;
5a73d082 683
34d26f42
RR
684 if ( tool->IsPressed() )
685 flags = wxCONTROL_PRESSED;
3216dbf5
VZ
686
687 wxString label;
688 wxBitmap bitmap;
689 if ( !tool->IsSeparator() )
690 {
370efbe7 691 label = tool->GetLabel();
3216dbf5 692 bitmap = tool->GetBitmap();
57c6f0fe
VZ
693
694 if ( !bitmap.IsOk() )
695 {
696 // it's better not to draw anything than to assert inside
697 // drawing code as this results in an almost guaranteed crash
698 // as we're likely to be called by a paint event handler and so
699 // the assert is going to be triggered again and again and ...
700 continue;
701 }
3216dbf5
VZ
702 }
703 //else: leave both the label and the bitmap invalid to draw a separator
704
a8f4cabe
JS
705 if ( !tool->IsControl() )
706 {
57c6f0fe
VZ
707 int tbStyle = HasFlag(wxTB_VERTICAL) ? wxTB_VERTICAL : wxTB_HORIZONTAL;
708 if ( HasFlag(wxTB_TEXT) )
370efbe7 709 tbStyle |= wxTB_TEXT;
370efbe7 710
57c6f0fe
VZ
711 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags,
712 tool->GetStyle(), tbStyle);
a8f4cabe
JS
713 }
714 else // control
715 {
716 wxControl *control = tool->GetControl();
717 control->Move(tool->m_x, tool->m_y);
718 }
16c9a425 719 }
3216dbf5 720}
16c9a425 721
3216dbf5
VZ
722// ----------------------------------------------------------------------------
723// wxToolBar actions
724// ----------------------------------------------------------------------------
725
34d26f42
RR
726bool wxToolBar::PerformAction(const wxControlAction& action,
727 long numArg,
728 const wxString& strArg)
bb312b54 729{
34d26f42 730 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
23219626
JS
731 if (!tool)
732 return false;
32b13913 733
34d26f42
RR
734 if ( action == wxACTION_TOOLBAR_TOGGLE )
735 {
736 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
bb312b54 737
34d26f42 738 PerformAction( wxACTION_BUTTON_CLICK, numArg );
e4db172a 739
c4709ea5
JS
740 // Set mouse leave toolbar button range (If still in the range,
741 // toolbar button would get focus again
742 PerformAction( wxACTION_TOOLBAR_LEAVE, numArg );
34d26f42
RR
743 }
744 else if ( action == wxACTION_TOOLBAR_PRESS )
745 {
9a83f860 746 wxLogTrace(wxT("toolbar"), wxT("Button '%s' pressed."), tool->GetShortHelp().c_str());
32b13913 747
34d26f42 748 tool->Invert();
3216dbf5 749
34d26f42
RR
750 RefreshTool( tool );
751 }
752 else if ( action == wxACTION_TOOLBAR_RELEASE )
5a73d082 753 {
9a83f860 754 wxLogTrace(wxT("toolbar"), wxT("Button '%s' released."), tool->GetShortHelp().c_str());
5a73d082 755
9a83f860 756 wxASSERT_MSG( tool->IsInverted(), wxT("release unpressed button?") );
32b13913 757
5c680882
VZ
758 if(tool->IsInverted())
759 {
760 tool->Invert();
761 }
5a73d082 762
34d26f42 763 RefreshTool( tool );
5a73d082 764 }
34d26f42 765 else if ( action == wxACTION_TOOLBAR_CLICK )
5a73d082 766 {
34d26f42
RR
767 bool isToggled;
768 if ( tool->CanBeToggled() )
769 {
5c680882
VZ
770 if ( tool->IsButton() && tool->GetKind() == wxITEM_RADIO )
771 {
772 UnToggleRadioGroup(tool);
773 tool->Toggle(true);
774 }
775 else
776 {
777 tool->Toggle();
778 }
5a73d082 779
34d26f42 780 RefreshTool( tool );
bb312b54 781
34d26f42
RR
782 isToggled = tool->IsToggled();
783 }
784 else // simple non-checkable tool
785 {
a290fa5a 786 isToggled = false;
34d26f42
RR
787 }
788 OnLeftClick( tool->GetId(), isToggled );
789 }
3216dbf5 790 else if ( action == wxACTION_TOOLBAR_ENTER )
c08a4f00 791 {
9a83f860 792 wxCHECK_MSG( tool, false, wxT("no tool to enter?") );
32b13913 793
34d26f42 794 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
c08a4f00 795 {
a290fa5a 796 tool->SetUnderMouse( true );
32b13913 797
34d26f42
RR
798 if ( !tool->IsToggled() )
799 RefreshTool( tool );
c08a4f00
RR
800 }
801 }
3216dbf5 802 else if ( action == wxACTION_TOOLBAR_LEAVE )
bb312b54 803 {
9a83f860 804 wxCHECK_MSG( tool, false, wxT("no tool to leave?") );
32b13913 805
34d26f42 806 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
bb312b54 807 {
a290fa5a 808 tool->SetUnderMouse( false );
32b13913 809
34d26f42
RR
810 if ( !tool->IsToggled() )
811 RefreshTool( tool );
bb312b54 812 }
c08a4f00 813 }
3216dbf5
VZ
814 else
815 return wxControl::PerformAction(action, numArg, strArg);
816
a290fa5a 817 return true;
3216dbf5
VZ
818}
819
9467bdb7
VZ
820/* static */
821wxInputHandler *wxToolBar::GetStdInputHandler(wxInputHandler *handlerDef)
822{
823 static wxStdToolbarInputHandler s_handler(handlerDef);
824
825 return &s_handler;
826}
827
3216dbf5
VZ
828// ============================================================================
829// wxStdToolbarInputHandler implementation
830// ============================================================================
831
832wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
34d26f42 833 : wxStdInputHandler(handler)
3216dbf5 834{
34d26f42
RR
835 m_winCapture = NULL;
836 m_toolCapture = NULL;
837 m_toolLast = NULL;
3216dbf5
VZ
838}
839
840bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
841 const wxKeyEvent& event,
842 bool pressed)
843{
844 // TODO: when we have a current button we should allow the arrow
845 // keys to move it
846 return wxStdInputHandler::HandleKey(consumer, event, pressed);
847}
848
4e89ceb1
VZ
849bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
850 const wxMouseEvent& event)
851{
4e89ceb1
VZ
852 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
853 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
854
34d26f42
RR
855 if ( event.Button(1) )
856 {
34d26f42
RR
857
858 if ( event.LeftDown() || event.LeftDClick() )
859 {
72726d27 860 if ( !tool || !tool->IsEnabled() )
a290fa5a 861 return true;
32b13913 862
34d26f42
RR
863 m_winCapture = tbar;
864 m_winCapture->CaptureMouse();
32b13913 865
34d26f42
RR
866 m_toolCapture = tool;
867
868 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
869
a290fa5a 870 return true;
34d26f42
RR
871 }
872 else if ( event.LeftUp() )
873 {
874 if ( m_winCapture )
875 {
876 m_winCapture->ReleaseMouse();
877 m_winCapture = NULL;
878 }
879
2b5f62a0
VZ
880 if (m_toolCapture)
881 {
882 if ( tool == m_toolCapture )
883 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
884 else
885 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
886 }
32b13913 887
34d26f42 888 m_toolCapture = NULL;
32b13913 889
a290fa5a 890 return true;
34d26f42
RR
891 }
892 //else: don't do anything special about the double click
893 }
894
895 return wxStdInputHandler::HandleMouse(consumer, event);
4e89ceb1
VZ
896}
897
3216dbf5
VZ
898bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
899 const wxMouseEvent& event)
900{
34d26f42 901 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
c08a4f00 902 {
34d26f42 903 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
32b13913 904
34d26f42 905 wxToolBarTool *tool;
3216dbf5 906 if ( event.Leaving() )
c08a4f00 907 {
34d26f42
RR
908 // We cannot possibly be over a tool when
909 // leaving the toolbar
3216dbf5 910 tool = NULL;
c08a4f00 911 }
3216dbf5 912 else
c08a4f00 913 {
34d26f42 914 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
c08a4f00 915 }
32b13913 916
72726d27 917 if (m_toolCapture)
34d26f42 918 {
72726d27
RR
919 // During capture we only care of the captured tool
920 if (tool && (tool != m_toolCapture))
921 tool = NULL;
32b13913 922
72726d27 923 if (tool == m_toolLast)
a290fa5a 924 return true;
32b13913 925
72726d27
RR
926 if (tool)
927 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
928 else
929 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
32b13913 930
72726d27 931 m_toolLast = tool;
34d26f42 932 }
72726d27 933 else
34d26f42 934 {
72726d27 935 if (tool == m_toolLast)
a290fa5a 936 return true;
32b13913 937
72726d27
RR
938 if (m_toolLast)
939 {
940 // Leave old tool if any
941 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
942 }
32b13913 943
72726d27
RR
944 if (tool)
945 {
946 // Enter new tool if any
947 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
948 }
32b13913 949
34d26f42 950 m_toolLast = tool;
34d26f42 951 }
32b13913 952
a290fa5a 953 return true;
bb312b54 954 }
3216dbf5 955
a290fa5a 956 return false;
3216dbf5
VZ
957}
958
959bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
61fef19b 960 const wxFocusEvent& WXUNUSED(event))
3216dbf5 961{
32b13913 962 if ( m_toolCapture )
34d26f42
RR
963 {
964 // We shouldn't be left with a highlighted button
965 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
966 }
3216dbf5 967
a290fa5a 968 return true;
3216dbf5
VZ
969}
970
971bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
972 bool activated)
973{
34d26f42
RR
974 if (m_toolCapture && !activated)
975 {
976 // We shouldn't be left with a highlighted button
977 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
978 }
3216dbf5 979
a290fa5a 980 return true;
c08a4f00
RR
981}
982
6a317e61 983#endif // wxUSE_TOOLBAR