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
)
108 : wxToolBarToolBase(tbar
, control
)
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
, _T("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
);
331 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
333 return new wxToolBarTool(this, control
);
336 // ----------------------------------------------------------------------------
337 // wxToolBar geometry
338 // ----------------------------------------------------------------------------
340 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
342 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
346 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
348 // ensure that we always have the valid tool position
351 wxConstCast(this, wxToolBar
)->DoLayout();
354 rect
.x
= tool
->m_x
- m_xMargin
;
355 rect
.y
= tool
->m_y
- m_yMargin
;
359 if (tool
->IsButton())
361 if(!HasFlag(wxTB_TEXT
))
363 rect
.width
= m_defaultWidth
;
364 rect
.height
= m_defaultHeight
;
368 rect
.width
= m_defaultWidth
+
369 GetFont().GetPointSize() * tool
->GetLabel().length();
370 rect
.height
= m_defaultHeight
;
373 else if (tool
->IsSeparator())
375 rect
.width
= m_defaultWidth
;
376 rect
.height
= m_widthSeparator
;
380 rect
.width
= tool
->m_width
;
381 rect
.height
= tool
->m_height
;
386 if (tool
->IsButton())
388 if(!HasFlag(wxTB_TEXT
))
390 rect
.width
= m_defaultWidth
;
391 rect
.height
= m_defaultHeight
;
395 rect
.width
= m_defaultWidth
+
396 GetFont().GetPointSize() * tool
->GetLabel().length();
397 rect
.height
= m_defaultHeight
;
400 else if (tool
->IsSeparator())
402 rect
.width
= m_widthSeparator
;
403 rect
.height
= m_defaultHeight
;
407 rect
.width
= tool
->m_width
;
408 rect
.height
= tool
->m_height
;
412 rect
.width
+= 2*m_xMargin
;
413 rect
.height
+= 2*m_yMargin
;
418 bool wxToolBar::Realize()
420 if ( !wxToolBarBase::Realize() )
423 m_needsLayout
= true;
426 SetInitialSize(wxDefaultSize
);
431 void wxToolBar::SetWindowStyleFlag( long style
)
433 wxToolBarBase::SetWindowStyleFlag(style
);
435 m_needsLayout
= true;
440 void wxToolBar::DoLayout()
442 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
444 m_needsLayout
= false;
446 wxCoord x
= m_xMargin
,
449 wxCoord widthTool
= 0, maxWidthTool
= 0;
450 wxCoord heightTool
= 0, maxHeightTool
= 0;
451 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
452 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
454 // calculate the positions of all elements
455 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
457 node
= node
->GetNext() )
459 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
464 // TODO ugly number fiddling
465 if (tool
->IsButton())
469 widthTool
= m_defaultHeight
;
470 heightTool
= m_defaultWidth
;
471 if(HasFlag(wxTB_TEXT
))
472 heightTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
476 widthTool
= m_defaultWidth
;
477 if(HasFlag(wxTB_TEXT
))
478 widthTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
480 heightTool
= m_defaultHeight
;
483 if(widthTool
> maxWidthTool
) // Record max width of tool
485 maxWidthTool
= widthTool
;
488 if(heightTool
> maxHeightTool
) // Record max width of tool
490 maxHeightTool
= heightTool
;
495 else if (tool
->IsSeparator())
497 *pCur
+= m_widthSeparator
;
499 else if (!IsVertical()) // horizontal control
501 wxControl
*control
= tool
->GetControl();
502 wxSize size
= control
->GetSize();
503 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
504 tool
->m_width
= size
.x
;
505 tool
->m_height
= size
.y
;
507 *pCur
+= tool
->m_width
;
512 // calculate the total toolbar size
515 if(!HasFlag(wxTB_TEXT
))
517 xMin
= m_defaultWidth
+ 2*m_xMargin
;
518 yMin
= m_defaultHeight
+ 2*m_yMargin
;
524 xMin
= heightTool
+ 2*m_xMargin
;
525 yMin
= widthTool
+ 2*m_xMargin
;
529 xMin
= maxWidthTool
+ 2*m_xMargin
;
530 yMin
= heightTool
+ 2*m_xMargin
;
534 m_maxWidth
= x
< xMin
? xMin
: x
;
535 m_maxHeight
= y
< yMin
? yMin
: y
;
538 wxSize
wxToolBar::DoGetBestClientSize() const
540 return wxSize(m_maxWidth
, m_maxHeight
);
543 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
545 int old_width
, old_height
;
546 GetSize(&old_width
, &old_height
);
548 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
550 // Correct width and height if needed.
551 if ( width
== wxDefaultCoord
|| height
== wxDefaultCoord
)
553 int tmp_width
, tmp_height
;
554 GetSize(&tmp_width
, &tmp_height
);
556 if ( width
== wxDefaultCoord
)
558 if ( height
== wxDefaultCoord
)
562 // We must refresh the frame size when the toolbar changes size
563 // otherwise the toolbar can be shown incorrectly
564 if ( old_width
!= width
|| old_height
!= height
)
566 // But before we send the size event check it
567 // we have a frame that is not being deleted.
568 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
569 if ( frame
&& !frame
->IsBeingDeleted() )
571 frame
->SendSizeEvent();
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
580 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
582 RefreshRect(GetToolRect(tool
));
585 void wxToolBar::GetRectLimits(const wxRect
& rect
,
589 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
593 *start
= rect
.GetTop();
594 *end
= rect
.GetBottom();
598 *start
= rect
.GetLeft();
599 *end
= rect
.GetRight();
603 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
605 // prepare the variables used below
606 wxDC
& dc
= renderer
->GetDC();
607 wxRenderer
*rend
= renderer
->GetRenderer();
608 dc
.SetFont(GetFont());
610 // draw the border separating us from the menubar (if there is no menubar
611 // we probably shouldn't draw it?)
614 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
617 // get the update rect and its limits depending on the orientation
618 wxRect rectUpdate
= GetUpdateClientRect();
620 GetRectLimits(rectUpdate
, &start
, &end
);
622 // and redraw all the tools intersecting it
623 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
625 node
= node
->GetNext() )
627 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
628 wxRect rectTool
= GetToolRect(tool
);
629 wxCoord startTool
, endTool
;
630 GetRectLimits(rectTool
, &startTool
, &endTool
);
632 if ( endTool
< start
)
634 // we're still to the left of the area to redraw
638 if ( startTool
> end
)
640 // we're beyond the area to redraw, nothing left to do
644 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
646 // Draw separators only in flat mode
650 // deal with the flags
653 if ( tool
->IsEnabled() )
655 // The toolbars without wxTB_FLAT don't react to the mouse hovering
656 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
657 flags
|= wxCONTROL_CURRENT
;
659 else // disabled tool
661 flags
|= wxCONTROL_DISABLED
;
664 //if ( tool == m_toolCaptured )
665 // flags |= wxCONTROL_FOCUSED;
667 if ( tool
->IsPressed() )
668 flags
= wxCONTROL_PRESSED
;
672 if ( !tool
->IsSeparator() )
674 label
= tool
->GetLabel();
675 bitmap
= tool
->GetBitmap();
677 //else: leave both the label and the bitmap invalid to draw a separator
679 if ( !tool
->IsControl() )
682 if(HasFlag(wxTB_TEXT
))
684 tbStyle
|= wxTB_TEXT
;
687 if(HasFlag(wxTB_VERTICAL
))
689 tbStyle
|= wxTB_VERTICAL
;
693 tbStyle
|= wxTB_HORIZONTAL
;
695 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, tool
->GetStyle(), tbStyle
);
699 wxControl
*control
= tool
->GetControl();
700 control
->Move(tool
->m_x
, tool
->m_y
);
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 bool wxToolBar::PerformAction(const wxControlAction
& action
,
711 const wxString
& strArg
)
713 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
717 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
719 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
721 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
723 // Write by Danny Raynor to change state again.
724 // Check button still pressed or not
725 if ( tool
->CanBeToggled() && tool
->IsToggled() )
730 if( tool
->IsInverted() )
732 PerformAction( wxACTION_TOOLBAR_RELEASE
, numArg
);
735 // Set mouse leave toolbar button range (If still in the range,
736 // toolbar button would get focus again
737 PerformAction( wxACTION_TOOLBAR_LEAVE
, numArg
);
739 else if ( action
== wxACTION_TOOLBAR_PRESS
)
741 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
747 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
749 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
751 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
757 else if ( action
== wxACTION_TOOLBAR_CLICK
)
760 if ( tool
->CanBeToggled() )
766 isToggled
= tool
->IsToggled();
768 else // simple non-checkable tool
772 OnLeftClick( tool
->GetId(), isToggled
);
774 else if ( action
== wxACTION_TOOLBAR_ENTER
)
776 wxCHECK_MSG( tool
, false, _T("no tool to enter?") );
778 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
780 tool
->SetUnderMouse( true );
782 if ( !tool
->IsToggled() )
786 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
788 wxCHECK_MSG( tool
, false, _T("no tool to leave?") );
790 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
792 tool
->SetUnderMouse( false );
794 if ( !tool
->IsToggled() )
799 return wxControl::PerformAction(action
, numArg
, strArg
);
805 wxInputHandler
*wxToolBar::GetStdInputHandler(wxInputHandler
*handlerDef
)
807 static wxStdToolbarInputHandler
s_handler(handlerDef
);
812 // ============================================================================
813 // wxStdToolbarInputHandler implementation
814 // ============================================================================
816 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
817 : wxStdInputHandler(handler
)
820 m_toolCapture
= NULL
;
824 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
825 const wxKeyEvent
& event
,
828 // TODO: when we have a current button we should allow the arrow
830 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
833 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
834 const wxMouseEvent
& event
)
836 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
837 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
839 if ( event
.Button(1) )
842 if ( event
.LeftDown() || event
.LeftDClick() )
844 if ( !tool
|| !tool
->IsEnabled() )
848 m_winCapture
->CaptureMouse();
850 m_toolCapture
= tool
;
852 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
856 else if ( event
.LeftUp() )
860 m_winCapture
->ReleaseMouse();
866 if ( tool
== m_toolCapture
)
867 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
869 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
872 m_toolCapture
= NULL
;
876 //else: don't do anything special about the double click
879 return wxStdInputHandler::HandleMouse(consumer
, event
);
882 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
883 const wxMouseEvent
& event
)
885 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
887 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
890 if ( event
.Leaving() )
892 // We cannot possibly be over a tool when
893 // leaving the toolbar
898 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
903 // During capture we only care of the captured tool
904 if (tool
&& (tool
!= m_toolCapture
))
907 if (tool
== m_toolLast
)
911 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
913 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
919 if (tool
== m_toolLast
)
924 // Leave old tool if any
925 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
930 // Enter new tool if any
931 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
943 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
944 const wxFocusEvent
& WXUNUSED(event
))
948 // We shouldn't be left with a highlighted button
949 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
955 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
958 if (m_toolCapture
&& !activated
)
960 // We shouldn't be left with a highlighted button
961 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
967 #endif // wxUSE_TOOLBAR