1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/toolbar.cpp
3 // Purpose: implementation of wxToolBar for wxUniversal
4 // Author: Robert Roebling, Vadim Zeitlin (universalization)
8 // Copyright: (c) 2001 Robert Roebling,
9 // (c) 2002 SciTech Software, Inc. (www.scitechsoft.com)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
30 #include "wx/toolbar.h"
41 #include "wx/univ/renderer.h"
43 // ----------------------------------------------------------------------------
44 // wxStdToolbarInputHandler: translates SPACE and ENTER keys and the left mouse
45 // click into button press/release actions
46 // ----------------------------------------------------------------------------
48 class WXDLLEXPORT wxStdToolbarInputHandler
: public wxStdInputHandler
51 wxStdToolbarInputHandler(wxInputHandler
*inphand
);
53 virtual bool HandleKey(wxInputConsumer
*consumer
,
54 const wxKeyEvent
& event
,
56 virtual bool HandleMouse(wxInputConsumer
*consumer
,
57 const wxMouseEvent
& event
);
58 virtual bool HandleMouseMove(wxInputConsumer
*consumer
, const wxMouseEvent
& event
);
59 virtual bool HandleFocus(wxInputConsumer
*consumer
, const wxFocusEvent
& event
);
60 virtual bool HandleActivation(wxInputConsumer
*consumer
, bool activated
);
63 wxWindow
*m_winCapture
;
64 wxToolBarToolBase
*m_toolCapture
;
65 wxToolBarToolBase
*m_toolLast
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // value meaning that m_widthSeparator is not initialized
73 static const wxCoord INVALID_WIDTH
= wxDefaultCoord
;
75 // ----------------------------------------------------------------------------
76 // wxToolBarTool: our implementation of wxToolBarToolBase
77 // ----------------------------------------------------------------------------
79 class WXDLLEXPORT wxToolBarTool
: public wxToolBarToolBase
82 wxToolBarTool(wxToolBar
*tbar
,
84 const wxString
& label
,
85 const wxBitmap
& bmpNormal
,
86 const wxBitmap
& bmpDisabled
,
89 const wxString
& shortHelp
,
90 const wxString
& longHelp
)
91 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
92 clientData
, shortHelp
, longHelp
)
101 m_isInverted
= false;
103 // mouse not here yet
104 m_underMouse
= false;
107 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
108 : wxToolBarToolBase(tbar
, control
, label
)
112 m_y
= wxDefaultCoord
;
117 m_isInverted
= false;
119 // mouse not here yet
120 m_underMouse
= false;
123 // is this tool pressed, even temporarily? (this is different from being
124 // permanently toggled which is what IsToggled() returns)
125 bool IsPressed() const
126 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
128 // are we temporarily pressed/unpressed?
129 bool IsInverted() const { return m_isInverted
; }
131 // press the tool temporarily by inverting its toggle state
132 void Invert() { m_isInverted
= !m_isInverted
; }
135 void SetUnderMouse( bool under
= true ) { m_underMouse
= under
; }
136 bool IsUnderMouse() { return m_underMouse
; }
139 // the tool position (for controls)
146 // true if the tool is pressed
149 // true if the tool is under the mouse
153 // ============================================================================
154 // wxToolBar implementation
155 // ============================================================================
157 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
159 // ----------------------------------------------------------------------------
160 // wxToolBar creation
161 // ----------------------------------------------------------------------------
163 void wxToolBar::Init()
166 m_needsLayout
= false;
168 // unknown widths for the tools and separators
169 m_widthSeparator
= INVALID_WIDTH
;
174 wxRenderer
*renderer
= GetRenderer();
176 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
177 SetMargins(renderer
->GetToolBarMargin());
180 bool wxToolBar::Create(wxWindow
*parent
,
185 const wxString
& name
)
187 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
188 wxDefaultValidator
, name
) )
195 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
197 SetInitialSize(size
);
202 wxToolBar::~wxToolBar()
204 // Make sure the toolbar is removed from the parent.
208 void wxToolBar::SetMargins(int x
, int y
)
210 // This required for similar visual effects under
211 // native platforms and wxUniv.
212 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
215 // ----------------------------------------------------------------------------
216 // wxToolBar tool-related methods
217 // ----------------------------------------------------------------------------
219 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
221 // check the "other" direction first: it must be inside the toolbar or we
222 // don't risk finding anything
225 if ( x
< 0 || x
> m_maxWidth
)
228 // we always use x, even for a vertical toolbar, this makes the code
234 if ( y
< 0 || y
> m_maxHeight
)
238 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
240 node
= node
->GetNext() )
242 wxToolBarToolBase
*tool
= node
->GetData();
243 wxRect rectTool
= GetToolRect(tool
);
245 wxCoord startTool
, endTool
;
246 GetRectLimits(rectTool
, &startTool
, &endTool
);
248 if ( x
>= startTool
&& x
<= endTool
)
250 // don't return the separators from here, they don't accept any
252 return tool
->IsSeparator() ? NULL
: tool
;
259 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
261 wxToolBarToolBase
*tool
= FindById(id
);
263 wxCHECK_RET( tool
, wxT("SetToolShortHelp: no such tool") );
265 tool
->SetShortHelp(help
);
268 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
269 wxToolBarToolBase
* WXUNUSED(tool
))
271 // recalculate the toolbar geometry before redrawing it the next time
272 m_needsLayout
= true;
274 // and ensure that we indeed are going to redraw
280 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
281 wxToolBarToolBase
* WXUNUSED(tool
))
284 m_needsLayout
= true;
291 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
294 // created disabled-state bitmap on demand
295 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
297 wxImage
image(tool
->GetNormalBitmap().ConvertToImage());
299 tool
->SetDisabledBitmap(image
.ConvertToGreyscale());
301 #endif // wxUSE_IMAGE
306 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
308 // note that if we're called the tool did change state (the base class
309 // checks for it), so it's not necessary to check for this again here
313 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
318 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
319 const wxString
& label
,
320 const wxBitmap
& bmpNormal
,
321 const wxBitmap
& bmpDisabled
,
323 wxObject
*clientData
,
324 const wxString
& shortHelp
,
325 const wxString
& longHelp
)
327 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
328 clientData
, shortHelp
, longHelp
);
332 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
334 return new wxToolBarTool(this, control
, label
);
337 // ----------------------------------------------------------------------------
338 // wxToolBar geometry
339 // ----------------------------------------------------------------------------
341 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
343 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
347 wxCHECK_MSG( tool
, rect
, wxT("GetToolRect: NULL tool") );
349 // ensure that we always have the valid tool position
352 wxConstCast(this, wxToolBar
)->DoLayout();
355 rect
.x
= tool
->m_x
- m_xMargin
;
356 rect
.y
= tool
->m_y
- m_yMargin
;
360 if (tool
->IsButton())
362 if(!HasFlag(wxTB_TEXT
))
364 rect
.width
= m_defaultWidth
;
365 rect
.height
= m_defaultHeight
;
369 rect
.width
= m_defaultWidth
+
370 GetFont().GetPointSize() * tool
->GetLabel().length();
371 rect
.height
= m_defaultHeight
;
374 else if (tool
->IsSeparator())
376 rect
.width
= m_defaultWidth
;
377 rect
.height
= m_widthSeparator
;
381 rect
.width
= tool
->m_width
;
382 rect
.height
= tool
->m_height
;
387 if (tool
->IsButton())
389 if(!HasFlag(wxTB_TEXT
))
391 rect
.width
= m_defaultWidth
;
392 rect
.height
= m_defaultHeight
;
396 rect
.width
= m_defaultWidth
+
397 GetFont().GetPointSize() * tool
->GetLabel().length();
398 rect
.height
= m_defaultHeight
;
401 else if (tool
->IsSeparator())
403 rect
.width
= m_widthSeparator
;
404 rect
.height
= m_defaultHeight
;
408 rect
.width
= tool
->m_width
;
409 rect
.height
= tool
->m_height
;
413 rect
.width
+= 2*m_xMargin
;
414 rect
.height
+= 2*m_yMargin
;
419 bool wxToolBar::Realize()
421 if ( !wxToolBarBase::Realize() )
424 m_needsLayout
= true;
427 // the first item in the radio group is checked by default to be consistent
428 // with wxGTK and the menu radio items
429 int radioGroupCount
= 0;
431 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
433 node
= node
->GetNext() )
435 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
437 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
443 bool toggle
= !radioGroupCount
++;
444 if ( tool
->Toggle(toggle
) )
446 DoToggleTool(tool
, toggle
);
450 SetInitialSize(wxDefaultSize
);
455 void wxToolBar::SetWindowStyleFlag( long style
)
457 wxToolBarBase::SetWindowStyleFlag(style
);
459 m_needsLayout
= true;
464 void wxToolBar::DoLayout()
466 wxASSERT_MSG( m_needsLayout
, wxT("why are we called?") );
468 m_needsLayout
= false;
470 wxCoord x
= m_xMargin
,
473 wxCoord widthTool
= 0, maxWidthTool
= 0;
474 wxCoord heightTool
= 0, maxHeightTool
= 0;
475 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
476 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
478 // calculate the positions of all elements
479 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
481 node
= node
->GetNext() )
483 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
488 // TODO ugly number fiddling
489 if (tool
->IsButton())
493 widthTool
= m_defaultHeight
;
494 heightTool
= m_defaultWidth
;
495 if(HasFlag(wxTB_TEXT
))
496 heightTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
500 widthTool
= m_defaultWidth
;
501 if(HasFlag(wxTB_TEXT
))
502 widthTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
504 heightTool
= m_defaultHeight
;
507 if(widthTool
> maxWidthTool
) // Record max width of tool
509 maxWidthTool
= widthTool
;
512 if(heightTool
> maxHeightTool
) // Record max width of tool
514 maxHeightTool
= heightTool
;
519 else if (tool
->IsSeparator())
521 *pCur
+= m_widthSeparator
;
523 else if (!IsVertical()) // horizontal control
525 wxControl
*control
= tool
->GetControl();
526 wxSize size
= control
->GetSize();
527 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
528 tool
->m_width
= size
.x
;
529 tool
->m_height
= size
.y
;
531 *pCur
+= tool
->m_width
;
536 // calculate the total toolbar size
539 if(!HasFlag(wxTB_TEXT
))
541 xMin
= m_defaultWidth
+ 2*m_xMargin
;
542 yMin
= m_defaultHeight
+ 2*m_yMargin
;
548 xMin
= heightTool
+ 2*m_xMargin
;
549 yMin
= widthTool
+ 2*m_xMargin
;
553 xMin
= maxWidthTool
+ 2*m_xMargin
;
554 yMin
= heightTool
+ 2*m_xMargin
;
558 m_maxWidth
= x
< xMin
? xMin
: x
;
559 m_maxHeight
= y
< yMin
? yMin
: y
;
562 wxSize
wxToolBar::DoGetBestClientSize() const
564 return wxSize(m_maxWidth
, m_maxHeight
);
567 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
569 int old_width
, old_height
;
570 GetSize(&old_width
, &old_height
);
572 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
574 // Correct width and height if needed.
575 if ( width
== wxDefaultCoord
|| height
== wxDefaultCoord
)
577 int tmp_width
, tmp_height
;
578 GetSize(&tmp_width
, &tmp_height
);
580 if ( width
== wxDefaultCoord
)
582 if ( height
== wxDefaultCoord
)
586 // We must refresh the frame size when the toolbar changes size
587 // otherwise the toolbar can be shown incorrectly
588 if ( old_width
!= width
|| old_height
!= height
)
590 SendSizeEventToParent();
594 // ----------------------------------------------------------------------------
596 // ----------------------------------------------------------------------------
598 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
600 RefreshRect(GetToolRect(tool
));
603 void wxToolBar::GetRectLimits(const wxRect
& rect
,
607 wxCHECK_RET( start
&& end
, wxT("NULL pointer in GetRectLimits") );
611 *start
= rect
.GetTop();
612 *end
= rect
.GetBottom();
616 *start
= rect
.GetLeft();
617 *end
= rect
.GetRight();
621 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
623 // prepare the variables used below
624 wxDC
& dc
= renderer
->GetDC();
625 wxRenderer
*rend
= renderer
->GetRenderer();
626 dc
.SetFont(GetFont());
628 // draw the border separating us from the menubar (if there is no menubar
629 // we probably shouldn't draw it?)
632 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
635 // get the update rect and its limits depending on the orientation
636 wxRect rectUpdate
= GetUpdateClientRect();
638 GetRectLimits(rectUpdate
, &start
, &end
);
640 // and redraw all the tools intersecting it
641 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
643 node
= node
->GetNext() )
645 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
646 wxRect rectTool
= GetToolRect(tool
);
647 wxCoord startTool
, endTool
;
648 GetRectLimits(rectTool
, &startTool
, &endTool
);
650 if ( endTool
< start
)
652 // we're still to the left of the area to redraw
656 if ( startTool
> end
)
658 // we're beyond the area to redraw, nothing left to do
662 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
664 // Draw separators only in flat mode
668 // deal with the flags
671 if ( tool
->IsEnabled() )
673 // The toolbars without wxTB_FLAT don't react to the mouse hovering
674 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
675 flags
|= wxCONTROL_CURRENT
;
677 else // disabled tool
679 flags
|= wxCONTROL_DISABLED
;
682 //if ( tool == m_toolCaptured )
683 // flags |= wxCONTROL_FOCUSED;
685 if ( tool
->IsPressed() )
686 flags
= wxCONTROL_PRESSED
;
690 if ( !tool
->IsSeparator() )
692 label
= tool
->GetLabel();
693 bitmap
= tool
->GetBitmap();
695 if ( !bitmap
.IsOk() )
697 // it's better not to draw anything than to assert inside
698 // drawing code as this results in an almost guaranteed crash
699 // as we're likely to be called by a paint event handler and so
700 // the assert is going to be triggered again and again and ...
704 //else: leave both the label and the bitmap invalid to draw a separator
706 if ( !tool
->IsControl() )
708 int tbStyle
= HasFlag(wxTB_VERTICAL
) ? wxTB_VERTICAL
: wxTB_HORIZONTAL
;
709 if ( HasFlag(wxTB_TEXT
) )
710 tbStyle
|= wxTB_TEXT
;
712 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
,
713 tool
->GetStyle(), tbStyle
);
717 wxControl
*control
= tool
->GetControl();
718 control
->Move(tool
->m_x
, tool
->m_y
);
723 // ----------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
727 bool wxToolBar::PerformAction(const wxControlAction
& action
,
729 const wxString
& strArg
)
731 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
735 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
737 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
739 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
741 // Set mouse leave toolbar button range (If still in the range,
742 // toolbar button would get focus again
743 PerformAction( wxACTION_TOOLBAR_LEAVE
, numArg
);
745 else if ( action
== wxACTION_TOOLBAR_PRESS
)
747 wxLogTrace(wxT("toolbar"), wxT("Button '%s' pressed."), tool
->GetShortHelp().c_str());
753 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
755 wxLogTrace(wxT("toolbar"), wxT("Button '%s' released."), tool
->GetShortHelp().c_str());
757 wxASSERT_MSG( tool
->IsInverted(), wxT("release unpressed button?") );
759 if(tool
->IsInverted())
766 else if ( action
== wxACTION_TOOLBAR_CLICK
)
769 if ( tool
->CanBeToggled() )
771 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
773 UnToggleRadioGroup(tool
);
783 isToggled
= tool
->IsToggled();
785 else // simple non-checkable tool
789 OnLeftClick( tool
->GetId(), isToggled
);
791 else if ( action
== wxACTION_TOOLBAR_ENTER
)
793 wxCHECK_MSG( tool
, false, wxT("no tool to enter?") );
795 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
797 tool
->SetUnderMouse( true );
799 if ( !tool
->IsToggled() )
803 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
805 wxCHECK_MSG( tool
, false, wxT("no tool to leave?") );
807 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
809 tool
->SetUnderMouse( false );
811 if ( !tool
->IsToggled() )
816 return wxControl::PerformAction(action
, numArg
, strArg
);
822 wxInputHandler
*wxToolBar::GetStdInputHandler(wxInputHandler
*handlerDef
)
824 static wxStdToolbarInputHandler
s_handler(handlerDef
);
829 // ============================================================================
830 // wxStdToolbarInputHandler implementation
831 // ============================================================================
833 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
834 : wxStdInputHandler(handler
)
837 m_toolCapture
= NULL
;
841 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
842 const wxKeyEvent
& event
,
845 // TODO: when we have a current button we should allow the arrow
847 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
850 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
851 const wxMouseEvent
& event
)
853 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
854 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
856 if ( event
.Button(1) )
859 if ( event
.LeftDown() || event
.LeftDClick() )
861 if ( !tool
|| !tool
->IsEnabled() )
865 m_winCapture
->CaptureMouse();
867 m_toolCapture
= tool
;
869 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
873 else if ( event
.LeftUp() )
877 m_winCapture
->ReleaseMouse();
883 if ( tool
== m_toolCapture
)
884 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
886 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
889 m_toolCapture
= NULL
;
893 //else: don't do anything special about the double click
896 return wxStdInputHandler::HandleMouse(consumer
, event
);
899 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
900 const wxMouseEvent
& event
)
902 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
904 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
907 if ( event
.Leaving() )
909 // We cannot possibly be over a tool when
910 // leaving the toolbar
915 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
920 // During capture we only care of the captured tool
921 if (tool
&& (tool
!= m_toolCapture
))
924 if (tool
== m_toolLast
)
928 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
930 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
936 if (tool
== m_toolLast
)
941 // Leave old tool if any
942 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
947 // Enter new tool if any
948 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
960 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
961 const wxFocusEvent
& WXUNUSED(event
))
965 // We shouldn't be left with a highlighted button
966 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
972 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
975 if (m_toolCapture
&& !activated
)
977 // We shouldn't be left with a highlighted button
978 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
984 #endif // wxUSE_TOOLBAR