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"
39 #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 rect
.width
= m_defaultWidth
;
379 rect
.height
= m_defaultHeight
;
381 else if (tool
->IsSeparator())
383 rect
.width
= m_defaultWidth
;
384 rect
.height
= m_widthSeparator
;
388 rect
.width
= tool
->m_width
;
389 rect
.height
= tool
->m_height
;
394 if (tool
->IsButton())
396 rect
.width
= m_defaultWidth
;
397 rect
.height
= m_defaultHeight
;
399 else if (tool
->IsSeparator())
401 rect
.width
= m_widthSeparator
;
402 rect
.height
= m_defaultHeight
;
406 rect
.width
= tool
->m_width
;
407 rect
.height
= tool
->m_height
;
411 rect
.width
+= 2*m_xMargin
;
412 rect
.height
+= 2*m_yMargin
;
417 bool wxToolBar::Realize()
419 if ( !wxToolBarBase::Realize() )
422 m_needsLayout
= true;
425 SetBestSize(wxDefaultSize
);
430 void wxToolBar::DoLayout()
432 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
434 m_needsLayout
= false;
436 wxCoord x
= m_xMargin
,
439 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
440 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
441 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
443 // calculate the positions of all elements
444 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
446 node
= node
->GetNext() )
448 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
453 // TODO ugly number fiddling
454 if (tool
->IsButton())
458 else if (tool
->IsSeparator())
460 *pCur
+= m_widthSeparator
;
462 else if (!IsVertical()) // horizontal control
464 wxControl
*control
= tool
->GetControl();
465 wxSize size
= control
->GetSize();
466 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
467 tool
->m_width
= size
.x
;
468 tool
->m_height
= size
.y
;
470 *pCur
+= tool
->m_width
;
475 // calculate the total toolbar size
476 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
477 yMin
= m_defaultHeight
+ 2*m_yMargin
;
479 m_maxWidth
= x
< xMin
? xMin
: x
;
480 m_maxHeight
= y
< yMin
? yMin
: y
;
483 wxSize
wxToolBar::DoGetBestClientSize() const
485 return wxSize(m_maxWidth
, m_maxHeight
);
488 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
490 int old_width
, old_height
;
491 GetSize(&old_width
, &old_height
);
493 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
495 // Correct width and height if needed.
496 if ( width
== wxDefaultCoord
|| height
== wxDefaultCoord
)
498 int tmp_width
, tmp_height
;
499 GetSize(&tmp_width
, &tmp_height
);
501 if ( width
== wxDefaultCoord
)
503 if ( height
== wxDefaultCoord
)
507 // We must refresh the frame size when the toolbar changes size
508 // otherwise the toolbar can be shown incorrectly
509 if ( old_width
!= width
|| old_height
!= height
)
511 // But before we send the size event check it
512 // we have a frame that is not being deleted.
513 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
514 if ( frame
&& !frame
->IsBeingDeleted() )
516 frame
->SendSizeEvent();
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
525 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
527 RefreshRect(GetToolRect(tool
));
530 void wxToolBar::GetRectLimits(const wxRect
& rect
,
534 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
538 *start
= rect
.GetTop();
539 *end
= rect
.GetBottom();
543 *start
= rect
.GetLeft();
544 *end
= rect
.GetRight();
548 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
550 // prepare the variables used below
551 wxDC
& dc
= renderer
->GetDC();
552 wxRenderer
*rend
= renderer
->GetRenderer();
553 // dc.SetFont(GetFont()); -- uncomment when we support labels
555 // draw the border separating us from the menubar (if there is no menubar
556 // we probably shouldn't draw it?)
559 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
562 // get the update rect and its limits depending on the orientation
563 wxRect rectUpdate
= GetUpdateClientRect();
565 GetRectLimits(rectUpdate
, &start
, &end
);
567 // and redraw all the tools intersecting it
568 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
570 node
= node
->GetNext() )
572 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
573 wxRect rectTool
= GetToolRect(tool
);
574 wxCoord startTool
, endTool
;
575 GetRectLimits(rectTool
, &startTool
, &endTool
);
577 if ( endTool
< start
)
579 // we're still to the left of the area to redraw
583 if ( startTool
> end
)
585 // we're beyond the area to redraw, nothing left to do
589 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
591 // Draw separators only in flat mode
595 // deal with the flags
598 if ( tool
->IsEnabled() )
600 // The toolbars without wxTB_FLAT don't react to the mouse hovering
601 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
602 flags
|= wxCONTROL_CURRENT
;
604 else // disabled tool
606 flags
|= wxCONTROL_DISABLED
;
609 //if ( tool == m_toolCaptured )
610 // flags |= wxCONTROL_FOCUSED;
612 if ( tool
->IsPressed() )
613 flags
= wxCONTROL_PRESSED
;
617 if ( !tool
->IsSeparator() )
619 // label = tool->GetLabel();
620 bitmap
= tool
->GetBitmap();
622 //else: leave both the label and the bitmap invalid to draw a separator
624 if ( !tool
->IsControl() )
626 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, tool
->GetStyle());
630 wxControl
*control
= tool
->GetControl();
631 control
->Move(tool
->m_x
, tool
->m_y
);
636 // ----------------------------------------------------------------------------
638 // ----------------------------------------------------------------------------
640 bool wxToolBar::PerformAction(const wxControlAction
& action
,
642 const wxString
& strArg
)
644 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
648 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
650 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
652 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
654 // Write by Danny Raynor to change state again.
655 // Check button still pressed or not
656 if ( tool
->CanBeToggled() && tool
->IsToggled() )
661 if( tool
->IsInverted() )
663 PerformAction( wxACTION_TOOLBAR_RELEASE
, numArg
);
666 // Set mouse leave toolbar button range (If still in the range,
667 // toolbar button would get focus again
668 PerformAction( wxACTION_TOOLBAR_LEAVE
, numArg
);
670 else if ( action
== wxACTION_TOOLBAR_PRESS
)
672 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
678 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
680 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
682 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
688 else if ( action
== wxACTION_TOOLBAR_CLICK
)
691 if ( tool
->CanBeToggled() )
697 isToggled
= tool
->IsToggled();
699 else // simple non-checkable tool
703 OnLeftClick( tool
->GetId(), isToggled
);
705 else if ( action
== wxACTION_TOOLBAR_ENTER
)
707 wxCHECK_MSG( tool
, false, _T("no tool to enter?") );
709 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
711 tool
->SetUnderMouse( true );
713 if ( !tool
->IsToggled() )
717 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
719 wxCHECK_MSG( tool
, false, _T("no tool to leave?") );
721 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
723 tool
->SetUnderMouse( false );
725 if ( !tool
->IsToggled() )
730 return wxControl::PerformAction(action
, numArg
, strArg
);
735 // ============================================================================
736 // wxStdToolbarInputHandler implementation
737 // ============================================================================
739 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
740 : wxStdInputHandler(handler
)
743 m_toolCapture
= NULL
;
747 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
748 const wxKeyEvent
& event
,
751 // TODO: when we have a current button we should allow the arrow
753 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
756 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
757 const wxMouseEvent
& event
)
759 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
760 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
762 if ( event
.Button(1) )
765 if ( event
.LeftDown() || event
.LeftDClick() )
767 if ( !tool
|| !tool
->IsEnabled() )
771 m_winCapture
->CaptureMouse();
773 m_toolCapture
= tool
;
775 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
779 else if ( event
.LeftUp() )
783 m_winCapture
->ReleaseMouse();
789 if ( tool
== m_toolCapture
)
790 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
792 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
795 m_toolCapture
= NULL
;
799 //else: don't do anything special about the double click
802 return wxStdInputHandler::HandleMouse(consumer
, event
);
805 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
806 const wxMouseEvent
& event
)
808 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
810 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
813 if ( event
.Leaving() )
815 // We cannot possibly be over a tool when
816 // leaving the toolbar
821 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
826 // During capture we only care of the captured tool
827 if (tool
&& (tool
!= m_toolCapture
))
830 if (tool
== m_toolLast
)
834 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
836 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
842 if (tool
== m_toolLast
)
847 // Leave old tool if any
848 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
853 // Enter new tool if any
854 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
866 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
867 const wxFocusEvent
& WXUNUSED(event
))
871 // We shouldn't be left with a highlighted button
872 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
878 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
881 if (m_toolCapture
&& !activated
)
883 // We shouldn't be left with a highlighted button
884 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
890 #endif // wxUSE_TOOLBAR