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"
40 #include "wx/univ/renderer.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // value meaning that m_widthSeparator is not initialized
49 static const wxCoord INVALID_WIDTH
= wxDefaultCoord
;
51 // ----------------------------------------------------------------------------
52 // wxToolBarTool: our implementation of wxToolBarToolBase
53 // ----------------------------------------------------------------------------
55 class WXDLLEXPORT wxToolBarTool
: public wxToolBarToolBase
58 wxToolBarTool(wxToolBar
*tbar
,
60 const wxString
& label
,
61 const wxBitmap
& bmpNormal
,
62 const wxBitmap
& bmpDisabled
,
65 const wxString
& shortHelp
,
66 const wxString
& longHelp
)
67 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
68 clientData
, shortHelp
, longHelp
)
83 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
84 : wxToolBarToolBase(tbar
, control
)
99 // is this tool pressed, even temporarily? (this is different from being
100 // permanently toggled which is what IsToggled() returns)
101 bool IsPressed() const
102 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
104 // are we temporarily pressed/unpressed?
105 bool IsInverted() const { return m_isInverted
; }
107 // press the tool temporarily by inverting its toggle state
108 void Invert() { m_isInverted
= !m_isInverted
; }
111 void SetUnderMouse( bool under
= true ) { m_underMouse
= under
; }
112 bool IsUnderMouse() { return m_underMouse
; }
115 // the tool position (for controls)
122 // true if the tool is pressed
125 // true if the tool is under the mouse
129 // ============================================================================
130 // wxToolBar implementation
131 // ============================================================================
133 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
135 // ----------------------------------------------------------------------------
136 // wxToolBar creation
137 // ----------------------------------------------------------------------------
139 void wxToolBar::Init()
142 m_needsLayout
= false;
144 // unknown widths for the tools and separators
145 m_widthSeparator
= INVALID_WIDTH
;
150 wxRenderer
*renderer
= GetRenderer();
152 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
153 SetMargins(renderer
->GetToolBarMargin());
156 bool wxToolBar::Create(wxWindow
*parent
,
161 const wxString
& name
)
163 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
164 wxDefaultValidator
, name
) )
169 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
176 wxToolBar::~wxToolBar()
178 // Make sure the toolbar is removed from the parent.
182 void wxToolBar::SetMargins(int x
, int y
)
184 // This required for similar visual effects under
185 // native platforms and wxUniv.
186 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
189 // ----------------------------------------------------------------------------
190 // wxToolBar tool-related methods
191 // ----------------------------------------------------------------------------
193 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
195 // check the "other" direction first: it must be inside the toolbar or we
196 // don't risk finding anything
199 if ( x
< 0 || x
> m_maxWidth
)
202 // we always use x, even for a vertical toolbar, this makes the code
208 if ( y
< 0 || y
> m_maxHeight
)
212 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
214 node
= node
->GetNext() )
216 wxToolBarToolBase
*tool
= node
->GetData();
217 wxRect rectTool
= GetToolRect(tool
);
219 wxCoord startTool
, endTool
;
220 GetRectLimits(rectTool
, &startTool
, &endTool
);
222 if ( x
>= startTool
&& x
<= endTool
)
224 // don't return the separators from here, they don't accept any
226 return tool
->IsSeparator() ? NULL
: tool
;
233 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
235 wxToolBarToolBase
*tool
= FindById(id
);
237 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
239 tool
->SetShortHelp(help
);
242 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
243 wxToolBarToolBase
* WXUNUSED(tool
))
245 // recalculate the toolbar geometry before redrawing it the next time
246 m_needsLayout
= true;
248 // and ensure that we indeed are going to redraw
254 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
255 wxToolBarToolBase
* WXUNUSED(tool
))
258 m_needsLayout
= true;
265 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
267 // created disabled-state bitmap on demand
268 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
270 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
272 // TODO: don't hardcode 180
273 unsigned char bg_red
= 180;
274 unsigned char bg_green
= 180;
275 unsigned char bg_blue
= 180;
277 unsigned char mask_red
= image
.GetMaskRed();
278 unsigned char mask_green
= image
.GetMaskGreen();
279 unsigned char mask_blue
= image
.GetMaskBlue();
281 bool has_mask
= image
.HasMask();
284 for (y
= 0; y
< image
.GetHeight(); y
++)
286 for (x
= 0; x
< image
.GetWidth(); x
++)
288 unsigned char red
= image
.GetRed(x
,y
);
289 unsigned char green
= image
.GetGreen(x
,y
);
290 unsigned char blue
= image
.GetBlue(x
,y
);
291 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
293 red
= (unsigned char)((((wxInt32
) red
- bg_red
) >> 1) + bg_red
);
294 green
= (unsigned char)((((wxInt32
) green
- bg_green
) >> 1) + bg_green
);
295 blue
= (unsigned char)((((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
);
296 image
.SetRGB( x
, y
, red
, green
, blue
);
301 for (y
= 0; y
< image
.GetHeight(); y
++)
303 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
305 unsigned char red
= image
.GetRed(x
,y
);
306 unsigned char green
= image
.GetGreen(x
,y
);
307 unsigned char blue
= image
.GetBlue(x
,y
);
308 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
310 red
= (unsigned char)((((wxInt32
) red
- bg_red
) >> 1) + bg_red
);
311 green
= (unsigned char)((((wxInt32
) green
- bg_green
) >> 1) + bg_green
);
312 blue
= (unsigned char)((((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
);
313 image
.SetRGB( x
, y
, red
, green
, blue
);
318 tool
->SetDisabledBitmap(image
);
324 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
326 // note that if we're called the tool did change state (the base class
327 // checks for it), so it's not necessary to check for this again here
331 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
336 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
337 const wxString
& label
,
338 const wxBitmap
& bmpNormal
,
339 const wxBitmap
& bmpDisabled
,
341 wxObject
*clientData
,
342 const wxString
& shortHelp
,
343 const wxString
& longHelp
)
345 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
346 clientData
, shortHelp
, longHelp
);
349 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
351 return new wxToolBarTool(this, control
);
354 // ----------------------------------------------------------------------------
355 // wxToolBar geometry
356 // ----------------------------------------------------------------------------
358 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
360 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
364 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
366 // ensure that we always have the valid tool position
369 wxConstCast(this, wxToolBar
)->DoLayout();
372 rect
.x
= tool
->m_x
- m_xMargin
;
373 rect
.y
= tool
->m_y
- m_yMargin
;
377 if (tool
->IsButton())
379 if(!HasFlag(wxTB_TEXT
))
381 rect
.width
= m_defaultWidth
;
382 rect
.height
= m_defaultHeight
;
386 rect
.width
= m_defaultWidth
+
387 GetFont().GetPointSize() * tool
->GetLabel().length();
388 rect
.height
= m_defaultHeight
;
391 else if (tool
->IsSeparator())
393 rect
.width
= m_defaultWidth
;
394 rect
.height
= m_widthSeparator
;
398 rect
.width
= tool
->m_width
;
399 rect
.height
= tool
->m_height
;
404 if (tool
->IsButton())
406 if(!HasFlag(wxTB_TEXT
))
408 rect
.width
= m_defaultWidth
;
409 rect
.height
= m_defaultHeight
;
413 rect
.width
= m_defaultWidth
+
414 GetFont().GetPointSize() * tool
->GetLabel().length();
415 rect
.height
= m_defaultHeight
;
418 else if (tool
->IsSeparator())
420 rect
.width
= m_widthSeparator
;
421 rect
.height
= m_defaultHeight
;
425 rect
.width
= tool
->m_width
;
426 rect
.height
= tool
->m_height
;
430 rect
.width
+= 2*m_xMargin
;
431 rect
.height
+= 2*m_yMargin
;
436 bool wxToolBar::Realize()
438 if ( !wxToolBarBase::Realize() )
441 m_needsLayout
= true;
444 SetBestSize(wxDefaultSize
);
449 void wxToolBar::SetWindowStyleFlag( long style
)
451 wxToolBarBase::SetWindowStyleFlag(style
);
453 m_needsLayout
= true;
458 void wxToolBar::DoLayout()
460 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
462 m_needsLayout
= false;
464 wxCoord x
= m_xMargin
,
467 wxCoord widthTool
= 0, maxWidthTool
= 0;
468 wxCoord heightTool
= 0, maxHeightTool
= 0;
469 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
470 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
472 // calculate the positions of all elements
473 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
475 node
= node
->GetNext() )
477 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
482 // TODO ugly number fiddling
483 if (tool
->IsButton())
487 widthTool
= m_defaultHeight
;
488 heightTool
= m_defaultWidth
;
489 if(HasFlag(wxTB_TEXT
))
490 heightTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
494 widthTool
= m_defaultWidth
;
495 if(HasFlag(wxTB_TEXT
))
496 widthTool
+= GetFont().GetPointSize() * tool
->GetLabel().length();
498 heightTool
= m_defaultHeight
;
501 if(widthTool
> maxWidthTool
) // Record max width of tool
503 maxWidthTool
= widthTool
;
506 if(heightTool
> maxHeightTool
) // Record max width of tool
508 maxHeightTool
= heightTool
;
513 else if (tool
->IsSeparator())
515 *pCur
+= m_widthSeparator
;
517 else if (!IsVertical()) // horizontal control
519 wxControl
*control
= tool
->GetControl();
520 wxSize size
= control
->GetSize();
521 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
522 tool
->m_width
= size
.x
;
523 tool
->m_height
= size
.y
;
525 *pCur
+= tool
->m_width
;
530 // calculate the total toolbar size
533 if(!HasFlag(wxTB_TEXT
))
535 xMin
= m_defaultWidth
+ 2*m_xMargin
;
536 yMin
= m_defaultHeight
+ 2*m_yMargin
;
542 xMin
= heightTool
+ 2*m_xMargin
;
543 yMin
= widthTool
+ 2*m_xMargin
;
547 xMin
= maxWidthTool
+ 2*m_xMargin
;
548 yMin
= heightTool
+ 2*m_xMargin
;
552 m_maxWidth
= x
< xMin
? xMin
: x
;
553 m_maxHeight
= y
< yMin
? yMin
: y
;
556 wxSize
wxToolBar::DoGetBestClientSize() const
558 return wxSize(m_maxWidth
, m_maxHeight
);
561 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
563 int old_width
, old_height
;
564 GetSize(&old_width
, &old_height
);
566 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
568 // Correct width and height if needed.
569 if ( width
== wxDefaultCoord
|| height
== wxDefaultCoord
)
571 int tmp_width
, tmp_height
;
572 GetSize(&tmp_width
, &tmp_height
);
574 if ( width
== wxDefaultCoord
)
576 if ( height
== wxDefaultCoord
)
580 // We must refresh the frame size when the toolbar changes size
581 // otherwise the toolbar can be shown incorrectly
582 if ( old_width
!= width
|| old_height
!= height
)
584 // But before we send the size event check it
585 // we have a frame that is not being deleted.
586 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
587 if ( frame
&& !frame
->IsBeingDeleted() )
589 frame
->SendSizeEvent();
594 // ----------------------------------------------------------------------------
596 // ----------------------------------------------------------------------------
598 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
600 RefreshRect(GetToolRect(tool
));
603 void wxToolBar::GetRectLimits(const wxRect
& rect
,
607 wxCHECK_RET( start
&& end
, _T("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 //else: leave both the label and the bitmap invalid to draw a separator
697 if ( !tool
->IsControl() )
700 if(HasFlag(wxTB_TEXT
))
702 tbStyle
|= wxTB_TEXT
;
705 if(HasFlag(wxTB_VERTICAL
))
707 tbStyle
|= wxTB_VERTICAL
;
711 tbStyle
|= wxTB_HORIZONTAL
;
713 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, 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 // Write by Danny Raynor to change state again.
742 // Check button still pressed or not
743 if ( tool
->CanBeToggled() && tool
->IsToggled() )
748 if( tool
->IsInverted() )
750 PerformAction( wxACTION_TOOLBAR_RELEASE
, numArg
);
753 // Set mouse leave toolbar button range (If still in the range,
754 // toolbar button would get focus again
755 PerformAction( wxACTION_TOOLBAR_LEAVE
, numArg
);
757 else if ( action
== wxACTION_TOOLBAR_PRESS
)
759 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
765 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
767 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
769 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
775 else if ( action
== wxACTION_TOOLBAR_CLICK
)
778 if ( tool
->CanBeToggled() )
784 isToggled
= tool
->IsToggled();
786 else // simple non-checkable tool
790 OnLeftClick( tool
->GetId(), isToggled
);
792 else if ( action
== wxACTION_TOOLBAR_ENTER
)
794 wxCHECK_MSG( tool
, false, _T("no tool to enter?") );
796 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
798 tool
->SetUnderMouse( true );
800 if ( !tool
->IsToggled() )
804 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
806 wxCHECK_MSG( tool
, false, _T("no tool to leave?") );
808 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
810 tool
->SetUnderMouse( false );
812 if ( !tool
->IsToggled() )
817 return wxControl::PerformAction(action
, numArg
, strArg
);
822 // ============================================================================
823 // wxStdToolbarInputHandler implementation
824 // ============================================================================
826 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
827 : wxStdInputHandler(handler
)
830 m_toolCapture
= NULL
;
834 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
835 const wxKeyEvent
& event
,
838 // TODO: when we have a current button we should allow the arrow
840 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
843 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
844 const wxMouseEvent
& event
)
846 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
847 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
849 if ( event
.Button(1) )
852 if ( event
.LeftDown() || event
.LeftDClick() )
854 if ( !tool
|| !tool
->IsEnabled() )
858 m_winCapture
->CaptureMouse();
860 m_toolCapture
= tool
;
862 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
866 else if ( event
.LeftUp() )
870 m_winCapture
->ReleaseMouse();
876 if ( tool
== m_toolCapture
)
877 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
879 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
882 m_toolCapture
= NULL
;
886 //else: don't do anything special about the double click
889 return wxStdInputHandler::HandleMouse(consumer
, event
);
892 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
893 const wxMouseEvent
& event
)
895 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
897 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
900 if ( event
.Leaving() )
902 // We cannot possibly be over a tool when
903 // leaving the toolbar
908 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
913 // During capture we only care of the captured tool
914 if (tool
&& (tool
!= m_toolCapture
))
917 if (tool
== m_toolLast
)
921 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
923 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
929 if (tool
== m_toolLast
)
934 // Leave old tool if any
935 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
940 // Enter new tool if any
941 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
953 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
954 const wxFocusEvent
& WXUNUSED(event
))
958 // We shouldn't be left with a highlighted button
959 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
965 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
968 if (m_toolCapture
&& !activated
)
970 // We shouldn't be left with a highlighted button
971 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
977 #endif // wxUSE_TOOLBAR