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 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // value meaning that m_widthSeparator is not initialized
48 static const wxCoord INVALID_WIDTH
= wxDefaultCoord
;
50 // ----------------------------------------------------------------------------
51 // wxToolBarTool: our implementation of wxToolBarToolBase
52 // ----------------------------------------------------------------------------
54 class WXDLLEXPORT wxToolBarTool
: public wxToolBarToolBase
57 wxToolBarTool(wxToolBar
*tbar
,
59 const wxString
& label
,
60 const wxBitmap
& bmpNormal
,
61 const wxBitmap
& bmpDisabled
,
64 const wxString
& shortHelp
,
65 const wxString
& longHelp
)
66 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
67 clientData
, shortHelp
, longHelp
)
82 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
83 : wxToolBarToolBase(tbar
, control
)
98 // is this tool pressed, even temporarily? (this is different from being
99 // permanently toggled which is what IsToggled() returns)
100 bool IsPressed() const
101 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
103 // are we temporarily pressed/unpressed?
104 bool IsInverted() const { return m_isInverted
; }
106 // press the tool temporarily by inverting its toggle state
107 void Invert() { m_isInverted
= !m_isInverted
; }
110 void SetUnderMouse( bool under
= true ) { m_underMouse
= under
; }
111 bool IsUnderMouse() { return m_underMouse
; }
114 // the tool position (for controls)
121 // true if the tool is pressed
124 // true if the tool is under the mouse
128 // ============================================================================
129 // wxToolBar implementation
130 // ============================================================================
132 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
134 // ----------------------------------------------------------------------------
135 // wxToolBar creation
136 // ----------------------------------------------------------------------------
138 void wxToolBar::Init()
141 m_needsLayout
= false;
143 // unknown widths for the tools and separators
144 m_widthSeparator
= INVALID_WIDTH
;
149 wxRenderer
*renderer
= GetRenderer();
151 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
152 SetMargins(renderer
->GetToolBarMargin());
155 bool wxToolBar::Create(wxWindow
*parent
,
160 const wxString
& name
)
162 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
163 wxDefaultValidator
, name
) )
168 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
175 wxToolBar::~wxToolBar()
177 // Make sure the toolbar is removed from the parent.
181 void wxToolBar::SetMargins(int x
, int y
)
183 // This required for similar visual effects under
184 // native platforms and wxUniv.
185 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
188 // ----------------------------------------------------------------------------
189 // wxToolBar tool-related methods
190 // ----------------------------------------------------------------------------
192 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
194 // check the "other" direction first: it must be inside the toolbar or we
195 // don't risk finding anything
198 if ( x
< 0 || x
> m_maxWidth
)
201 // we always use x, even for a vertical toolbar, this makes the code
207 if ( y
< 0 || y
> m_maxHeight
)
211 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
213 node
= node
->GetNext() )
215 wxToolBarToolBase
*tool
= node
->GetData();
216 wxRect rectTool
= GetToolRect(tool
);
218 wxCoord startTool
, endTool
;
219 GetRectLimits(rectTool
, &startTool
, &endTool
);
221 if ( x
>= startTool
&& x
<= endTool
)
223 // don't return the separators from here, they don't accept any
225 return tool
->IsSeparator() ? NULL
: tool
;
232 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
234 wxToolBarToolBase
*tool
= FindById(id
);
236 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
238 tool
->SetShortHelp(help
);
241 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
242 wxToolBarToolBase
* WXUNUSED(tool
))
244 // recalculate the toolbar geometry before redrawing it the next time
245 m_needsLayout
= true;
247 // and ensure that we indeed are going to redraw
253 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
254 wxToolBarToolBase
* WXUNUSED(tool
))
257 m_needsLayout
= true;
264 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
266 // created disabled-state bitmap on demand
267 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
269 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
271 // TODO: don't hardcode 180
272 unsigned char bg_red
= 180;
273 unsigned char bg_green
= 180;
274 unsigned char bg_blue
= 180;
276 unsigned char mask_red
= image
.GetMaskRed();
277 unsigned char mask_green
= image
.GetMaskGreen();
278 unsigned char mask_blue
= image
.GetMaskBlue();
280 bool has_mask
= image
.HasMask();
283 for (y
= 0; y
< image
.GetHeight(); y
++)
285 for (x
= 0; x
< image
.GetWidth(); x
++)
287 unsigned char red
= image
.GetRed(x
,y
);
288 unsigned char green
= image
.GetGreen(x
,y
);
289 unsigned char blue
= image
.GetBlue(x
,y
);
290 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
292 red
= (unsigned char)((((wxInt32
) red
- bg_red
) >> 1) + bg_red
);
293 green
= (unsigned char)((((wxInt32
) green
- bg_green
) >> 1) + bg_green
);
294 blue
= (unsigned char)((((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
);
295 image
.SetRGB( x
, y
, red
, green
, blue
);
300 for (y
= 0; y
< image
.GetHeight(); y
++)
302 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
304 unsigned char red
= image
.GetRed(x
,y
);
305 unsigned char green
= image
.GetGreen(x
,y
);
306 unsigned char blue
= image
.GetBlue(x
,y
);
307 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
309 red
= (unsigned char)((((wxInt32
) red
- bg_red
) >> 1) + bg_red
);
310 green
= (unsigned char)((((wxInt32
) green
- bg_green
) >> 1) + bg_green
);
311 blue
= (unsigned char)((((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
);
312 image
.SetRGB( x
, y
, red
, green
, blue
);
317 tool
->SetDisabledBitmap(image
);
323 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
325 // note that if we're called the tool did change state (the base class
326 // checks for it), so it's not necessary to check for this again here
330 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
335 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
336 const wxString
& label
,
337 const wxBitmap
& bmpNormal
,
338 const wxBitmap
& bmpDisabled
,
340 wxObject
*clientData
,
341 const wxString
& shortHelp
,
342 const wxString
& longHelp
)
344 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
345 clientData
, shortHelp
, longHelp
);
348 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
350 return new wxToolBarTool(this, control
);
353 // ----------------------------------------------------------------------------
354 // wxToolBar geometry
355 // ----------------------------------------------------------------------------
357 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
359 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
363 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
365 // ensure that we always have the valid tool position
368 wxConstCast(this, wxToolBar
)->DoLayout();
371 rect
.x
= tool
->m_x
- m_xMargin
;
372 rect
.y
= tool
->m_y
- m_yMargin
;
376 if (tool
->IsButton())
378 if(!HasFlag(wxTB_TEXT
))
380 rect
.width
= m_defaultWidth
;
381 rect
.height
= m_defaultHeight
;
385 rect
.width
= m_defaultWidth
+
386 GetFont().GetPointSize() * tool
->GetLabel().length();
387 rect
.height
= m_defaultHeight
;
390 else if (tool
->IsSeparator())
392 rect
.width
= m_defaultWidth
;
393 rect
.height
= m_widthSeparator
;
397 rect
.width
= tool
->m_width
;
398 rect
.height
= tool
->m_height
;
403 if (tool
->IsButton())
405 if(!HasFlag(wxTB_TEXT
))
407 rect
.width
= m_defaultWidth
;
408 rect
.height
= m_defaultHeight
;
412 rect
.width
= m_defaultWidth
+
413 GetFont().GetPointSize() * tool
->GetLabel().length();
414 rect
.height
= m_defaultHeight
;
417 else if (tool
->IsSeparator())
419 rect
.width
= m_widthSeparator
;
420 rect
.height
= m_defaultHeight
;
424 rect
.width
= tool
->m_width
;
425 rect
.height
= tool
->m_height
;
429 rect
.width
+= 2*m_xMargin
;
430 rect
.height
+= 2*m_yMargin
;
435 bool wxToolBar::Realize()
437 if ( !wxToolBarBase::Realize() )
440 m_needsLayout
= true;
443 SetBestSize(wxDefaultSize
);
448 void wxToolBar::SetWindowStyleFlag( long style
)
450 wxToolBarBase::SetWindowStyleFlag(style
);
452 m_needsLayout
= true;
457 void wxToolBar::DoLayout()
459 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
461 m_needsLayout
= false;
463 wxCoord x
= m_xMargin
,
466 wxCoord widthTool
= 0, maxWidthTool
= 0;
467 wxCoord heightTool
= 0, maxHeightTool
= 0;
468 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
469 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
471 // calculate the positions of all elements
472 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
474 node
= node
->GetNext() )
476 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
481 // TODO ugly number fiddling
482 if (tool
->IsButton())
486 widthTool
= m_defaultHeight
;
487 heightTool
= m_defaultWidth
;
488 if(HasFlag(wxTB_TEXT
))
489 heightTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
493 widthTool
= m_defaultWidth
;
494 if(HasFlag(wxTB_TEXT
))
495 widthTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
497 heightTool
= m_defaultHeight
;
500 if(widthTool
> maxWidthTool
) // Record max width of tool
502 maxWidthTool
= widthTool
;
505 if(heightTool
> maxHeightTool
) // Record max width of tool
507 maxHeightTool
= heightTool
;
512 else if (tool
->IsSeparator())
514 *pCur
+= m_widthSeparator
;
516 else if (!IsVertical()) // horizontal control
518 wxControl
*control
= tool
->GetControl();
519 wxSize size
= control
->GetSize();
520 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
521 tool
->m_width
= size
.x
;
522 tool
->m_height
= size
.y
;
524 *pCur
+= tool
->m_width
;
529 // calculate the total toolbar size
532 if(!HasFlag(wxTB_TEXT
))
534 xMin
= m_defaultWidth
+ 2*m_xMargin
;
535 yMin
= m_defaultHeight
+ 2*m_yMargin
;
541 xMin
= heightTool
+ 2*m_xMargin
;
542 yMin
= widthTool
+ 2*m_xMargin
;
546 xMin
= maxWidthTool
+ 2*m_xMargin
;
547 yMin
= heightTool
+ 2*m_xMargin
;
551 m_maxWidth
= x
< xMin
? xMin
: x
;
552 m_maxHeight
= y
< yMin
? yMin
: y
;
555 wxSize
wxToolBar::DoGetBestClientSize() const
557 return wxSize(m_maxWidth
, m_maxHeight
);
560 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
562 int old_width
, old_height
;
563 GetSize(&old_width
, &old_height
);
565 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
567 // Correct width and height if needed.
568 if ( width
== wxDefaultCoord
|| height
== wxDefaultCoord
)
570 int tmp_width
, tmp_height
;
571 GetSize(&tmp_width
, &tmp_height
);
573 if ( width
== wxDefaultCoord
)
575 if ( height
== wxDefaultCoord
)
579 // We must refresh the frame size when the toolbar changes size
580 // otherwise the toolbar can be shown incorrectly
581 if ( old_width
!= width
|| old_height
!= height
)
583 // But before we send the size event check it
584 // we have a frame that is not being deleted.
585 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
586 if ( frame
&& !frame
->IsBeingDeleted() )
588 frame
->SendSizeEvent();
593 // ----------------------------------------------------------------------------
595 // ----------------------------------------------------------------------------
597 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
599 RefreshRect(GetToolRect(tool
));
602 void wxToolBar::GetRectLimits(const wxRect
& rect
,
606 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
610 *start
= rect
.GetTop();
611 *end
= rect
.GetBottom();
615 *start
= rect
.GetLeft();
616 *end
= rect
.GetRight();
620 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
622 // prepare the variables used below
623 wxDC
& dc
= renderer
->GetDC();
624 wxRenderer
*rend
= renderer
->GetRenderer();
625 dc
.SetFont(GetFont());
627 // draw the border separating us from the menubar (if there is no menubar
628 // we probably shouldn't draw it?)
631 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
634 // get the update rect and its limits depending on the orientation
635 wxRect rectUpdate
= GetUpdateClientRect();
637 GetRectLimits(rectUpdate
, &start
, &end
);
639 // and redraw all the tools intersecting it
640 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
642 node
= node
->GetNext() )
644 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
645 wxRect rectTool
= GetToolRect(tool
);
646 wxCoord startTool
, endTool
;
647 GetRectLimits(rectTool
, &startTool
, &endTool
);
649 if ( endTool
< start
)
651 // we're still to the left of the area to redraw
655 if ( startTool
> end
)
657 // we're beyond the area to redraw, nothing left to do
661 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
663 // Draw separators only in flat mode
667 // deal with the flags
670 if ( tool
->IsEnabled() )
672 // The toolbars without wxTB_FLAT don't react to the mouse hovering
673 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
674 flags
|= wxCONTROL_CURRENT
;
676 else // disabled tool
678 flags
|= wxCONTROL_DISABLED
;
681 //if ( tool == m_toolCaptured )
682 // flags |= wxCONTROL_FOCUSED;
684 if ( tool
->IsPressed() )
685 flags
= wxCONTROL_PRESSED
;
689 if ( !tool
->IsSeparator() )
691 label
= tool
->GetLabel();
692 bitmap
= tool
->GetBitmap();
694 //else: leave both the label and the bitmap invalid to draw a separator
696 if ( !tool
->IsControl() )
699 if(HasFlag(wxTB_TEXT
))
701 tbStyle
|= wxTB_TEXT
;
704 if(HasFlag(wxTB_VERTICAL
))
706 tbStyle
|= wxTB_VERTICAL
;
710 tbStyle
|= wxTB_HORIZONTAL
;
712 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, tool
->GetStyle(), tbStyle
);
716 wxControl
*control
= tool
->GetControl();
717 control
->Move(tool
->m_x
, tool
->m_y
);
722 // ----------------------------------------------------------------------------
724 // ----------------------------------------------------------------------------
726 bool wxToolBar::PerformAction(const wxControlAction
& action
,
728 const wxString
& strArg
)
730 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
734 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
736 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
738 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
740 // Write by Danny Raynor to change state again.
741 // Check button still pressed or not
742 if ( tool
->CanBeToggled() && tool
->IsToggled() )
747 if( tool
->IsInverted() )
749 PerformAction( wxACTION_TOOLBAR_RELEASE
, numArg
);
752 // Set mouse leave toolbar button range (If still in the range,
753 // toolbar button would get focus again
754 PerformAction( wxACTION_TOOLBAR_LEAVE
, numArg
);
756 else if ( action
== wxACTION_TOOLBAR_PRESS
)
758 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
764 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
766 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
768 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
774 else if ( action
== wxACTION_TOOLBAR_CLICK
)
777 if ( tool
->CanBeToggled() )
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, _T("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, _T("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
);
821 // ============================================================================
822 // wxStdToolbarInputHandler implementation
823 // ============================================================================
825 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
826 : wxStdInputHandler(handler
)
829 m_toolCapture
= NULL
;
833 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
834 const wxKeyEvent
& event
,
837 // TODO: when we have a current button we should allow the arrow
839 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
842 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
843 const wxMouseEvent
& event
)
845 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
846 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
848 if ( event
.Button(1) )
851 if ( event
.LeftDown() || event
.LeftDClick() )
853 if ( !tool
|| !tool
->IsEnabled() )
857 m_winCapture
->CaptureMouse();
859 m_toolCapture
= tool
;
861 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
865 else if ( event
.LeftUp() )
869 m_winCapture
->ReleaseMouse();
875 if ( tool
== m_toolCapture
)
876 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
878 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
881 m_toolCapture
= NULL
;
885 //else: don't do anything special about the double click
888 return wxStdInputHandler::HandleMouse(consumer
, event
);
891 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
892 const wxMouseEvent
& event
)
894 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
896 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
899 if ( event
.Leaving() )
901 // We cannot possibly be over a tool when
902 // leaving the toolbar
907 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
912 // During capture we only care of the captured tool
913 if (tool
&& (tool
!= m_toolCapture
))
916 if (tool
== m_toolLast
)
920 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
922 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
928 if (tool
== m_toolLast
)
933 // Leave old tool if any
934 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
939 // Enter new tool if any
940 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
952 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
953 const wxFocusEvent
& WXUNUSED(event
))
957 // We shouldn't be left with a highlighted button
958 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
964 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
967 if (m_toolCapture
&& !activated
)
969 // We shouldn't be left with a highlighted button
970 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
976 #endif // wxUSE_TOOLBAR