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
, _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
);
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
, _T("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
, _T("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 // But before we send the size event check it
591 // we have a frame that is not being deleted.
592 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
593 if ( frame
&& !frame
->IsBeingDeleted() )
595 frame
->SendSizeEvent();
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
604 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
606 RefreshRect(GetToolRect(tool
));
609 void wxToolBar::GetRectLimits(const wxRect
& rect
,
613 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
617 *start
= rect
.GetTop();
618 *end
= rect
.GetBottom();
622 *start
= rect
.GetLeft();
623 *end
= rect
.GetRight();
627 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
629 // prepare the variables used below
630 wxDC
& dc
= renderer
->GetDC();
631 wxRenderer
*rend
= renderer
->GetRenderer();
632 dc
.SetFont(GetFont());
634 // draw the border separating us from the menubar (if there is no menubar
635 // we probably shouldn't draw it?)
638 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
641 // get the update rect and its limits depending on the orientation
642 wxRect rectUpdate
= GetUpdateClientRect();
644 GetRectLimits(rectUpdate
, &start
, &end
);
646 // and redraw all the tools intersecting it
647 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
649 node
= node
->GetNext() )
651 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
652 wxRect rectTool
= GetToolRect(tool
);
653 wxCoord startTool
, endTool
;
654 GetRectLimits(rectTool
, &startTool
, &endTool
);
656 if ( endTool
< start
)
658 // we're still to the left of the area to redraw
662 if ( startTool
> end
)
664 // we're beyond the area to redraw, nothing left to do
668 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
670 // Draw separators only in flat mode
674 // deal with the flags
677 if ( tool
->IsEnabled() )
679 // The toolbars without wxTB_FLAT don't react to the mouse hovering
680 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
681 flags
|= wxCONTROL_CURRENT
;
683 else // disabled tool
685 flags
|= wxCONTROL_DISABLED
;
688 //if ( tool == m_toolCaptured )
689 // flags |= wxCONTROL_FOCUSED;
691 if ( tool
->IsPressed() )
692 flags
= wxCONTROL_PRESSED
;
696 if ( !tool
->IsSeparator() )
698 label
= tool
->GetLabel();
699 bitmap
= tool
->GetBitmap();
701 //else: leave both the label and the bitmap invalid to draw a separator
703 if ( !tool
->IsControl() )
706 if(HasFlag(wxTB_TEXT
))
708 tbStyle
|= wxTB_TEXT
;
711 if(HasFlag(wxTB_VERTICAL
))
713 tbStyle
|= wxTB_VERTICAL
;
717 tbStyle
|= wxTB_HORIZONTAL
;
719 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, tool
->GetStyle(), tbStyle
);
723 wxControl
*control
= tool
->GetControl();
724 control
->Move(tool
->m_x
, tool
->m_y
);
729 // ----------------------------------------------------------------------------
731 // ----------------------------------------------------------------------------
733 bool wxToolBar::PerformAction(const wxControlAction
& action
,
735 const wxString
& strArg
)
737 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
741 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
743 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
745 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
747 // Set mouse leave toolbar button range (If still in the range,
748 // toolbar button would get focus again
749 PerformAction( wxACTION_TOOLBAR_LEAVE
, numArg
);
751 else if ( action
== wxACTION_TOOLBAR_PRESS
)
753 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
759 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
761 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
763 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
765 if(tool
->IsInverted())
772 else if ( action
== wxACTION_TOOLBAR_CLICK
)
775 if ( tool
->CanBeToggled() )
777 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
779 UnToggleRadioGroup(tool
);
789 isToggled
= tool
->IsToggled();
791 else // simple non-checkable tool
795 OnLeftClick( tool
->GetId(), isToggled
);
797 else if ( action
== wxACTION_TOOLBAR_ENTER
)
799 wxCHECK_MSG( tool
, false, _T("no tool to enter?") );
801 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
803 tool
->SetUnderMouse( true );
805 if ( !tool
->IsToggled() )
809 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
811 wxCHECK_MSG( tool
, false, _T("no tool to leave?") );
813 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
815 tool
->SetUnderMouse( false );
817 if ( !tool
->IsToggled() )
822 return wxControl::PerformAction(action
, numArg
, strArg
);
828 wxInputHandler
*wxToolBar::GetStdInputHandler(wxInputHandler
*handlerDef
)
830 static wxStdToolbarInputHandler
s_handler(handlerDef
);
835 // ============================================================================
836 // wxStdToolbarInputHandler implementation
837 // ============================================================================
839 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
840 : wxStdInputHandler(handler
)
843 m_toolCapture
= NULL
;
847 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
848 const wxKeyEvent
& event
,
851 // TODO: when we have a current button we should allow the arrow
853 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
856 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
857 const wxMouseEvent
& event
)
859 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
860 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
862 if ( event
.Button(1) )
865 if ( event
.LeftDown() || event
.LeftDClick() )
867 if ( !tool
|| !tool
->IsEnabled() )
871 m_winCapture
->CaptureMouse();
873 m_toolCapture
= tool
;
875 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
879 else if ( event
.LeftUp() )
883 m_winCapture
->ReleaseMouse();
889 if ( tool
== m_toolCapture
)
890 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
892 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
895 m_toolCapture
= NULL
;
899 //else: don't do anything special about the double click
902 return wxStdInputHandler::HandleMouse(consumer
, event
);
905 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
906 const wxMouseEvent
& event
)
908 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
910 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
913 if ( event
.Leaving() )
915 // We cannot possibly be over a tool when
916 // leaving the toolbar
921 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
926 // During capture we only care of the captured tool
927 if (tool
&& (tool
!= m_toolCapture
))
930 if (tool
== m_toolLast
)
934 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
936 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
942 if (tool
== m_toolLast
)
947 // Leave old tool if any
948 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
953 // Enter new tool if any
954 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
966 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
967 const wxFocusEvent
& WXUNUSED(event
))
971 // We shouldn't be left with a highlighted button
972 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
978 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
981 if (m_toolCapture
&& !activated
)
983 // We shouldn't be left with a highlighted button
984 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
990 #endif // wxUSE_TOOLBAR