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
) )
193 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
200 wxToolBar::~wxToolBar()
202 // Make sure the toolbar is removed from the parent.
206 void wxToolBar::SetMargins(int x
, int y
)
208 // This required for similar visual effects under
209 // native platforms and wxUniv.
210 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
213 // ----------------------------------------------------------------------------
214 // wxToolBar tool-related methods
215 // ----------------------------------------------------------------------------
217 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
219 // check the "other" direction first: it must be inside the toolbar or we
220 // don't risk finding anything
223 if ( x
< 0 || x
> m_maxWidth
)
226 // we always use x, even for a vertical toolbar, this makes the code
232 if ( y
< 0 || y
> m_maxHeight
)
236 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
238 node
= node
->GetNext() )
240 wxToolBarToolBase
*tool
= node
->GetData();
241 wxRect rectTool
= GetToolRect(tool
);
243 wxCoord startTool
, endTool
;
244 GetRectLimits(rectTool
, &startTool
, &endTool
);
246 if ( x
>= startTool
&& x
<= endTool
)
248 // don't return the separators from here, they don't accept any
250 return tool
->IsSeparator() ? NULL
: tool
;
257 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
259 wxToolBarToolBase
*tool
= FindById(id
);
261 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
263 tool
->SetShortHelp(help
);
266 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
267 wxToolBarToolBase
* WXUNUSED(tool
))
269 // recalculate the toolbar geometry before redrawing it the next time
270 m_needsLayout
= true;
272 // and ensure that we indeed are going to redraw
278 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
279 wxToolBarToolBase
* WXUNUSED(tool
))
282 m_needsLayout
= true;
289 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
292 // created disabled-state bitmap on demand
293 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
295 wxImage
image(tool
->GetNormalBitmap().ConvertToImage());
297 tool
->SetDisabledBitmap(image
.ConvertToGreyscale());
299 #endif // wxUSE_IMAGE
304 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
306 // note that if we're called the tool did change state (the base class
307 // checks for it), so it's not necessary to check for this again here
311 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
316 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
317 const wxString
& label
,
318 const wxBitmap
& bmpNormal
,
319 const wxBitmap
& bmpDisabled
,
321 wxObject
*clientData
,
322 const wxString
& shortHelp
,
323 const wxString
& longHelp
)
325 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
326 clientData
, shortHelp
, longHelp
);
329 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
331 return new wxToolBarTool(this, control
);
334 // ----------------------------------------------------------------------------
335 // wxToolBar geometry
336 // ----------------------------------------------------------------------------
338 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
340 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
344 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
346 // ensure that we always have the valid tool position
349 wxConstCast(this, wxToolBar
)->DoLayout();
352 rect
.x
= tool
->m_x
- m_xMargin
;
353 rect
.y
= tool
->m_y
- m_yMargin
;
357 if (tool
->IsButton())
359 if(!HasFlag(wxTB_TEXT
))
361 rect
.width
= m_defaultWidth
;
362 rect
.height
= m_defaultHeight
;
366 rect
.width
= m_defaultWidth
+
367 GetFont().GetPointSize() * tool
->GetLabel().length();
368 rect
.height
= m_defaultHeight
;
371 else if (tool
->IsSeparator())
373 rect
.width
= m_defaultWidth
;
374 rect
.height
= m_widthSeparator
;
378 rect
.width
= tool
->m_width
;
379 rect
.height
= tool
->m_height
;
384 if (tool
->IsButton())
386 if(!HasFlag(wxTB_TEXT
))
388 rect
.width
= m_defaultWidth
;
389 rect
.height
= m_defaultHeight
;
393 rect
.width
= m_defaultWidth
+
394 GetFont().GetPointSize() * tool
->GetLabel().length();
395 rect
.height
= m_defaultHeight
;
398 else if (tool
->IsSeparator())
400 rect
.width
= m_widthSeparator
;
401 rect
.height
= m_defaultHeight
;
405 rect
.width
= tool
->m_width
;
406 rect
.height
= tool
->m_height
;
410 rect
.width
+= 2*m_xMargin
;
411 rect
.height
+= 2*m_yMargin
;
416 bool wxToolBar::Realize()
418 if ( !wxToolBarBase::Realize() )
421 m_needsLayout
= true;
424 SetBestSize(wxDefaultSize
);
429 void wxToolBar::SetWindowStyleFlag( long style
)
431 wxToolBarBase::SetWindowStyleFlag(style
);
433 m_needsLayout
= true;
438 void wxToolBar::DoLayout()
440 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
442 m_needsLayout
= false;
444 wxCoord x
= m_xMargin
,
447 wxCoord widthTool
= 0, maxWidthTool
= 0;
448 wxCoord heightTool
= 0, maxHeightTool
= 0;
449 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
450 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
452 // calculate the positions of all elements
453 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
455 node
= node
->GetNext() )
457 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
462 // TODO ugly number fiddling
463 if (tool
->IsButton())
467 widthTool
= m_defaultHeight
;
468 heightTool
= m_defaultWidth
;
469 if(HasFlag(wxTB_TEXT
))
470 heightTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
474 widthTool
= m_defaultWidth
;
475 if(HasFlag(wxTB_TEXT
))
476 widthTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
478 heightTool
= m_defaultHeight
;
481 if(widthTool
> maxWidthTool
) // Record max width of tool
483 maxWidthTool
= widthTool
;
486 if(heightTool
> maxHeightTool
) // Record max width of tool
488 maxHeightTool
= heightTool
;
493 else if (tool
->IsSeparator())
495 *pCur
+= m_widthSeparator
;
497 else if (!IsVertical()) // horizontal control
499 wxControl
*control
= tool
->GetControl();
500 wxSize size
= control
->GetSize();
501 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
502 tool
->m_width
= size
.x
;
503 tool
->m_height
= size
.y
;
505 *pCur
+= tool
->m_width
;
510 // calculate the total toolbar size
513 if(!HasFlag(wxTB_TEXT
))
515 xMin
= m_defaultWidth
+ 2*m_xMargin
;
516 yMin
= m_defaultHeight
+ 2*m_yMargin
;
522 xMin
= heightTool
+ 2*m_xMargin
;
523 yMin
= widthTool
+ 2*m_xMargin
;
527 xMin
= maxWidthTool
+ 2*m_xMargin
;
528 yMin
= heightTool
+ 2*m_xMargin
;
532 m_maxWidth
= x
< xMin
? xMin
: x
;
533 m_maxHeight
= y
< yMin
? yMin
: y
;
536 wxSize
wxToolBar::DoGetBestClientSize() const
538 return wxSize(m_maxWidth
, m_maxHeight
);
541 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
543 int old_width
, old_height
;
544 GetSize(&old_width
, &old_height
);
546 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
548 // Correct width and height if needed.
549 if ( width
== wxDefaultCoord
|| height
== wxDefaultCoord
)
551 int tmp_width
, tmp_height
;
552 GetSize(&tmp_width
, &tmp_height
);
554 if ( width
== wxDefaultCoord
)
556 if ( height
== wxDefaultCoord
)
560 // We must refresh the frame size when the toolbar changes size
561 // otherwise the toolbar can be shown incorrectly
562 if ( old_width
!= width
|| old_height
!= height
)
564 // But before we send the size event check it
565 // we have a frame that is not being deleted.
566 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
567 if ( frame
&& !frame
->IsBeingDeleted() )
569 frame
->SendSizeEvent();
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
580 RefreshRect(GetToolRect(tool
));
583 void wxToolBar::GetRectLimits(const wxRect
& rect
,
587 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
591 *start
= rect
.GetTop();
592 *end
= rect
.GetBottom();
596 *start
= rect
.GetLeft();
597 *end
= rect
.GetRight();
601 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
603 // prepare the variables used below
604 wxDC
& dc
= renderer
->GetDC();
605 wxRenderer
*rend
= renderer
->GetRenderer();
606 dc
.SetFont(GetFont());
608 // draw the border separating us from the menubar (if there is no menubar
609 // we probably shouldn't draw it?)
612 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
615 // get the update rect and its limits depending on the orientation
616 wxRect rectUpdate
= GetUpdateClientRect();
618 GetRectLimits(rectUpdate
, &start
, &end
);
620 // and redraw all the tools intersecting it
621 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
623 node
= node
->GetNext() )
625 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
626 wxRect rectTool
= GetToolRect(tool
);
627 wxCoord startTool
, endTool
;
628 GetRectLimits(rectTool
, &startTool
, &endTool
);
630 if ( endTool
< start
)
632 // we're still to the left of the area to redraw
636 if ( startTool
> end
)
638 // we're beyond the area to redraw, nothing left to do
642 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
644 // Draw separators only in flat mode
648 // deal with the flags
651 if ( tool
->IsEnabled() )
653 // The toolbars without wxTB_FLAT don't react to the mouse hovering
654 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
655 flags
|= wxCONTROL_CURRENT
;
657 else // disabled tool
659 flags
|= wxCONTROL_DISABLED
;
662 //if ( tool == m_toolCaptured )
663 // flags |= wxCONTROL_FOCUSED;
665 if ( tool
->IsPressed() )
666 flags
= wxCONTROL_PRESSED
;
670 if ( !tool
->IsSeparator() )
672 label
= tool
->GetLabel();
673 bitmap
= tool
->GetBitmap();
675 //else: leave both the label and the bitmap invalid to draw a separator
677 if ( !tool
->IsControl() )
680 if(HasFlag(wxTB_TEXT
))
682 tbStyle
|= wxTB_TEXT
;
685 if(HasFlag(wxTB_VERTICAL
))
687 tbStyle
|= wxTB_VERTICAL
;
691 tbStyle
|= wxTB_HORIZONTAL
;
693 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, tool
->GetStyle(), tbStyle
);
697 wxControl
*control
= tool
->GetControl();
698 control
->Move(tool
->m_x
, tool
->m_y
);
703 // ----------------------------------------------------------------------------
705 // ----------------------------------------------------------------------------
707 bool wxToolBar::PerformAction(const wxControlAction
& action
,
709 const wxString
& strArg
)
711 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
715 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
717 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
719 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
721 // Write by Danny Raynor to change state again.
722 // Check button still pressed or not
723 if ( tool
->CanBeToggled() && tool
->IsToggled() )
728 if( tool
->IsInverted() )
730 PerformAction( wxACTION_TOOLBAR_RELEASE
, numArg
);
733 // Set mouse leave toolbar button range (If still in the range,
734 // toolbar button would get focus again
735 PerformAction( wxACTION_TOOLBAR_LEAVE
, numArg
);
737 else if ( action
== wxACTION_TOOLBAR_PRESS
)
739 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
745 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
747 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
749 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
755 else if ( action
== wxACTION_TOOLBAR_CLICK
)
758 if ( tool
->CanBeToggled() )
764 isToggled
= tool
->IsToggled();
766 else // simple non-checkable tool
770 OnLeftClick( tool
->GetId(), isToggled
);
772 else if ( action
== wxACTION_TOOLBAR_ENTER
)
774 wxCHECK_MSG( tool
, false, _T("no tool to enter?") );
776 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
778 tool
->SetUnderMouse( true );
780 if ( !tool
->IsToggled() )
784 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
786 wxCHECK_MSG( tool
, false, _T("no tool to leave?") );
788 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
790 tool
->SetUnderMouse( false );
792 if ( !tool
->IsToggled() )
797 return wxControl::PerformAction(action
, numArg
, strArg
);
803 wxInputHandler
*wxToolBar::GetStdInputHandler(wxInputHandler
*handlerDef
)
805 static wxStdToolbarInputHandler
s_handler(handlerDef
);
810 // ============================================================================
811 // wxStdToolbarInputHandler implementation
812 // ============================================================================
814 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
815 : wxStdInputHandler(handler
)
818 m_toolCapture
= NULL
;
822 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
823 const wxKeyEvent
& event
,
826 // TODO: when we have a current button we should allow the arrow
828 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
831 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
832 const wxMouseEvent
& event
)
834 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
835 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
837 if ( event
.Button(1) )
840 if ( event
.LeftDown() || event
.LeftDClick() )
842 if ( !tool
|| !tool
->IsEnabled() )
846 m_winCapture
->CaptureMouse();
848 m_toolCapture
= tool
;
850 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
854 else if ( event
.LeftUp() )
858 m_winCapture
->ReleaseMouse();
864 if ( tool
== m_toolCapture
)
865 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
867 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
870 m_toolCapture
= NULL
;
874 //else: don't do anything special about the double click
877 return wxStdInputHandler::HandleMouse(consumer
, event
);
880 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
881 const wxMouseEvent
& event
)
883 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
885 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
888 if ( event
.Leaving() )
890 // We cannot possibly be over a tool when
891 // leaving the toolbar
896 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
901 // During capture we only care of the captured tool
902 if (tool
&& (tool
!= m_toolCapture
))
905 if (tool
== m_toolLast
)
909 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
911 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
917 if (tool
== m_toolLast
)
922 // Leave old tool if any
923 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
928 // Enter new tool if any
929 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
941 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
942 const wxFocusEvent
& WXUNUSED(event
))
946 // We shouldn't be left with a highlighted button
947 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
953 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
956 if (m_toolCapture
&& !activated
)
958 // We shouldn't be left with a highlighted button
959 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
965 #endif // wxUSE_TOOLBAR