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 // ----------------------------------------------------------------------------
22 #pragma implementation "univtoolbar.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
39 #include "wx/univ/renderer.h"
42 #include "wx/toolbar.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // value meaning that m_widthSeparator is not initialized
51 static const wxCoord INVALID_WIDTH
= -1;
53 // ----------------------------------------------------------------------------
54 // wxToolBarTool: our implementation of wxToolBarToolBase
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxToolBarTool
: public wxToolBarToolBase
60 wxToolBarTool(wxToolBar
*tbar
,
62 const wxString
& label
,
63 const wxBitmap
& bmpNormal
,
64 const wxBitmap
& bmpDisabled
,
67 const wxString
& shortHelp
,
68 const wxString
& longHelp
)
69 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
70 clientData
, shortHelp
, longHelp
)
85 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
86 : wxToolBarToolBase(tbar
, control
)
101 // is this tool pressed, even temporarily? (this is different from being
102 // permanently toggled which is what IsToggled() returns)
103 bool IsPressed() const
104 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
106 // are we temporarily pressed/unpressed?
107 bool IsInverted() const { return m_isInverted
; }
109 // press the tool temporarily by inverting its toggle state
110 void Invert() { m_isInverted
= !m_isInverted
; }
113 void SetUnderMouse( bool under
= TRUE
) { m_underMouse
= under
; }
114 bool IsUnderMouse() { return m_underMouse
; }
117 // the tool position (for controls)
124 // TRUE if the tool is pressed
127 // TRUE if the tool is under the mouse
131 // ============================================================================
132 // wxToolBar implementation
133 // ============================================================================
135 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
137 // ----------------------------------------------------------------------------
138 // wxToolBar creation
139 // ----------------------------------------------------------------------------
141 void wxToolBar::Init()
144 m_needsLayout
= FALSE
;
146 // unknown widths for the tools and separators
147 m_widthSeparator
= INVALID_WIDTH
;
152 wxRenderer
*renderer
= GetRenderer();
154 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
155 SetMargins(renderer
->GetToolBarMargin());
158 bool wxToolBar::Create(wxWindow
*parent
,
163 const wxString
& name
)
165 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
166 wxDefaultValidator
, name
) )
171 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
178 wxToolBar::~wxToolBar()
180 // Make sure the toolbar is removed from the parent.
184 void wxToolBar::SetMargins(int x
, int y
)
186 // This required for similar visual effects under
187 // native platforms and wxUniv.
188 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
191 // ----------------------------------------------------------------------------
192 // wxToolBar tool-related methods
193 // ----------------------------------------------------------------------------
195 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
197 // check the "other" direction first: it must be inside the toolbar or we
198 // don't risk finding anything
201 if ( x
< 0 || x
> m_maxWidth
)
204 // we always use x, even for a vertical toolbar, this makes the code
210 if ( y
< 0 || y
> m_maxHeight
)
214 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
216 node
= node
->GetNext() )
218 wxToolBarToolBase
*tool
= node
->GetData();
219 wxRect rectTool
= GetToolRect(tool
);
221 wxCoord startTool
, endTool
;
222 GetRectLimits(rectTool
, &startTool
, &endTool
);
224 if ( x
>= startTool
&& x
<= endTool
)
226 // don't return the separators from here, they don't accept any
228 return tool
->IsSeparator() ? NULL
: tool
;
235 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
237 wxToolBarToolBase
*tool
= FindById(id
);
239 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
241 tool
->SetShortHelp(help
);
244 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
245 wxToolBarToolBase
* WXUNUSED(tool
))
247 // recalculate the toolbar geometry before redrawing it the next time
248 m_needsLayout
= TRUE
;
250 // and ensure that we indeed are going to redraw
256 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
257 wxToolBarToolBase
* WXUNUSED(tool
))
260 m_needsLayout
= TRUE
;
267 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
269 // created disabled-state bitmap on demand
270 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
272 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
274 // TODO: don't hardcode 180
275 unsigned char bg_red
= 180;
276 unsigned char bg_green
= 180;
277 unsigned char bg_blue
= 180;
279 unsigned char mask_red
= image
.GetMaskRed();
280 unsigned char mask_green
= image
.GetMaskGreen();
281 unsigned char mask_blue
= image
.GetMaskBlue();
283 bool has_mask
= image
.HasMask();
286 for (y
= 0; y
< image
.GetHeight(); y
++)
288 for (x
= 0; x
< image
.GetWidth(); x
++)
290 unsigned char red
= image
.GetRed(x
,y
);
291 unsigned char green
= image
.GetGreen(x
,y
);
292 unsigned char blue
= image
.GetBlue(x
,y
);
293 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
295 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
296 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
297 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
298 image
.SetRGB( x
, y
, red
, green
, blue
);
303 for (y
= 0; y
< image
.GetHeight(); y
++)
305 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
307 unsigned char red
= image
.GetRed(x
,y
);
308 unsigned char green
= image
.GetGreen(x
,y
);
309 unsigned char blue
= image
.GetBlue(x
,y
);
310 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
312 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
313 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
314 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
315 image
.SetRGB( x
, y
, red
, green
, blue
);
320 tool
->SetDisabledBitmap(image
);
326 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
328 // note that if we're called the tool did change state (the base class
329 // checks for it), so it's not necessary to check for this again here
333 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
338 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
339 const wxString
& label
,
340 const wxBitmap
& bmpNormal
,
341 const wxBitmap
& bmpDisabled
,
343 wxObject
*clientData
,
344 const wxString
& shortHelp
,
345 const wxString
& longHelp
)
347 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
348 clientData
, shortHelp
, longHelp
);
351 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
353 return new wxToolBarTool(this, control
);
356 // ----------------------------------------------------------------------------
357 // wxToolBar geometry
358 // ----------------------------------------------------------------------------
360 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
362 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
366 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
368 // ensure that we always have the valid tool position
371 wxConstCast(this, wxToolBar
)->DoLayout();
374 rect
.x
= tool
->m_x
- m_xMargin
;
375 rect
.y
= tool
->m_y
- m_yMargin
;
379 if (tool
->IsButton())
381 rect
.width
= m_defaultWidth
;
382 rect
.height
= m_defaultHeight
;
384 else if (tool
->IsSeparator())
386 rect
.width
= m_defaultWidth
;
387 rect
.height
= m_widthSeparator
;
391 rect
.width
= tool
->m_width
;
392 rect
.height
= tool
->m_height
;
397 if (tool
->IsButton())
399 rect
.width
= m_defaultWidth
;
400 rect
.height
= m_defaultHeight
;
402 else if (tool
->IsSeparator())
404 rect
.width
= m_widthSeparator
;
405 rect
.height
= m_defaultHeight
;
409 rect
.width
= tool
->m_width
;
410 rect
.height
= tool
->m_height
;
414 rect
.width
+= 2*m_xMargin
;
415 rect
.height
+= 2*m_yMargin
;
420 bool wxToolBar::Realize()
422 if ( !wxToolBarBase::Realize() )
425 m_needsLayout
= TRUE
;
428 SetBestSize(wxDefaultSize
);
433 void wxToolBar::DoLayout()
435 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
437 m_needsLayout
= FALSE
;
439 wxCoord x
= m_xMargin
,
442 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
443 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
,
444 *pCur
= IsVertical() ? &y
: &x
;
446 // calculate the positions of all elements
447 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
449 node
= node
->GetNext() )
451 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
456 // TODO ugly number fiddling
457 if (tool
->IsButton())
461 else if (tool
->IsSeparator())
463 *pCur
+= m_widthSeparator
;
465 else if (!IsVertical()) // horizontal control
467 wxControl
*control
= tool
->GetControl();
468 wxSize size
= control
->GetSize();
469 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
470 tool
->m_width
= size
.x
;
471 tool
->m_height
= size
.y
;
473 *pCur
+= tool
->m_width
;
478 // calculate the total toolbar size
479 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
480 yMin
= m_defaultHeight
+ 2*m_yMargin
;
482 m_maxWidth
= x
< xMin
? xMin
: x
;
483 m_maxHeight
= y
< yMin
? yMin
: y
;
486 wxSize
wxToolBar::DoGetBestClientSize() const
488 return wxSize(m_maxWidth
, m_maxHeight
);
491 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
493 int old_width
, old_height
;
494 GetSize(&old_width
, &old_height
);
496 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
498 // Correct width and height if needed.
499 if ( width
== -1 || height
== -1 )
501 int tmp_width
, tmp_height
;
502 GetSize(&tmp_width
, &tmp_height
);
510 // We must refresh the frame size when the toolbar changes size
511 // otherwise the toolbar can be shown incorrectly
512 if ( old_width
!= width
|| old_height
!= height
)
514 // But before we send the size event check it
515 // we have a frame that is not being deleted.
516 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
517 if ( frame
&& !frame
->IsBeingDeleted() )
519 frame
->SendSizeEvent();
524 // ----------------------------------------------------------------------------
526 // ----------------------------------------------------------------------------
528 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
530 RefreshRect(GetToolRect(tool
));
533 void wxToolBar::GetRectLimits(const wxRect
& rect
,
537 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
541 *start
= rect
.GetTop();
542 *end
= rect
.GetBottom();
546 *start
= rect
.GetLeft();
547 *end
= rect
.GetRight();
551 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
553 // prepare the variables used below
554 wxDC
& dc
= renderer
->GetDC();
555 wxRenderer
*rend
= renderer
->GetRenderer();
556 // dc.SetFont(GetFont()); -- uncomment when we support labels
558 // draw the border separating us from the menubar (if there is no menubar
559 // we probably shouldn't draw it?)
562 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
565 // get the update rect and its limits depending on the orientation
566 wxRect rectUpdate
= GetUpdateClientRect();
568 GetRectLimits(rectUpdate
, &start
, &end
);
570 // and redraw all the tools intersecting it
571 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
573 node
= node
->GetNext() )
575 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
576 wxRect rectTool
= GetToolRect(tool
);
577 wxCoord startTool
, endTool
;
578 GetRectLimits(rectTool
, &startTool
, &endTool
);
580 if ( endTool
< start
)
582 // we're still to the left of the area to redraw
586 if ( startTool
> end
)
588 // we're beyond the area to redraw, nothing left to do
592 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
594 // Draw seperators only in flat mode
598 // deal with the flags
601 if ( tool
->IsEnabled() )
603 // The toolbars without wxTB_FLAT don't react to the mouse hovering
604 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
605 flags
|= wxCONTROL_CURRENT
;
607 else // disabled tool
609 flags
|= wxCONTROL_DISABLED
;
612 //if ( tool == m_toolCaptured )
613 // flags |= wxCONTROL_FOCUSED;
615 if ( tool
->IsPressed() )
616 flags
= wxCONTROL_PRESSED
;
620 if ( !tool
->IsSeparator() )
622 // label = tool->GetLabel();
623 bitmap
= tool
->GetBitmap();
625 //else: leave both the label and the bitmap invalid to draw a separator
627 if ( !tool
->IsControl() )
629 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, tool
->GetStyle());
633 wxControl
*control
= tool
->GetControl();
634 control
->Move(tool
->m_x
, tool
->m_y
);
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 bool wxToolBar::PerformAction(const wxControlAction
& action
,
645 const wxString
& strArg
)
647 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
649 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
651 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
653 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
655 else if ( action
== wxACTION_TOOLBAR_PRESS
)
657 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
663 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
665 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
667 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
673 else if ( action
== wxACTION_TOOLBAR_CLICK
)
676 if ( tool
->CanBeToggled() )
682 isToggled
= tool
->IsToggled();
684 else // simple non-checkable tool
688 OnLeftClick( tool
->GetId(), isToggled
);
690 else if ( action
== wxACTION_TOOLBAR_ENTER
)
692 wxCHECK_MSG( tool
, FALSE
, _T("no tool to enter?") );
694 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
696 tool
->SetUnderMouse( TRUE
);
698 if ( !tool
->IsToggled() )
702 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
704 wxCHECK_MSG( tool
, FALSE
, _T("no tool to leave?") );
706 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
708 tool
->SetUnderMouse( FALSE
);
710 if ( !tool
->IsToggled() )
715 return wxControl::PerformAction(action
, numArg
, strArg
);
720 // ============================================================================
721 // wxStdToolbarInputHandler implementation
722 // ============================================================================
724 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
725 : wxStdInputHandler(handler
)
728 m_toolCapture
= NULL
;
732 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
733 const wxKeyEvent
& event
,
736 // TODO: when we have a current button we should allow the arrow
738 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
741 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
742 const wxMouseEvent
& event
)
744 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
745 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
747 if ( event
.Button(1) )
750 if ( event
.LeftDown() || event
.LeftDClick() )
752 if ( !tool
|| !tool
->IsEnabled() )
756 m_winCapture
->CaptureMouse();
758 m_toolCapture
= tool
;
760 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
764 else if ( event
.LeftUp() )
768 m_winCapture
->ReleaseMouse();
774 if ( tool
== m_toolCapture
)
775 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
777 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
780 m_toolCapture
= NULL
;
784 //else: don't do anything special about the double click
787 return wxStdInputHandler::HandleMouse(consumer
, event
);
790 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
791 const wxMouseEvent
& event
)
793 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
795 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
798 if ( event
.Leaving() )
800 // We cannot possibly be over a tool when
801 // leaving the toolbar
806 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
811 // During capture we only care of the captured tool
812 if (tool
&& (tool
!= m_toolCapture
))
815 if (tool
== m_toolLast
)
819 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
821 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
827 if (tool
== m_toolLast
)
832 // Leave old tool if any
833 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
838 // Enter new tool if any
839 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
851 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
852 const wxFocusEvent
& event
)
856 // We shouldn't be left with a highlighted button
857 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
863 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
866 if (m_toolCapture
&& !activated
)
868 // We shouldn't be left with a highlighted button
869 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
875 #endif // wxUSE_TOOLBAR