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
)
83 // is this tool pressed, even temporarily? (this is different from being
84 // permanently toggled which is what IsToggled() returns)
85 bool IsPressed() const
86 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
88 // are we temporarily pressed/unpressed?
89 bool IsInverted() const { return m_isInverted
; }
91 // press the tool temporarily by inverting its toggle state
92 void Invert() { m_isInverted
= !m_isInverted
; }
95 void SetUnderMouse( bool under
= TRUE
) { m_underMouse
= under
; }
96 bool IsUnderMouse() { return m_underMouse
; }
99 // the tool position (the size is known by the toolbar itself)
104 // TRUE if the tool is pressed
107 // TRUE if the tool is under the mouse
111 // ============================================================================
112 // wxToolBar implementation
113 // ============================================================================
115 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
117 // ----------------------------------------------------------------------------
118 // wxToolBar creation
119 // ----------------------------------------------------------------------------
121 void wxToolBar::Init()
124 m_needsLayout
= FALSE
;
126 // unknown widths for the tools and separators
127 m_widthSeparator
= INVALID_WIDTH
;
132 wxRenderer
*renderer
= GetRenderer();
134 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
135 SetMargins(renderer
->GetToolBarMargin());
138 bool wxToolBar::Create(wxWindow
*parent
,
143 const wxString
& name
)
145 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
146 wxDefaultValidator
, name
) )
151 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
158 wxToolBar::~wxToolBar()
160 // Make sure the toolbar is removed from the parent.
164 void wxToolBar::SetMargins(int x
, int y
)
166 // This required for similar visual effects under
167 // native platforms and wxUniv.
168 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
171 // ----------------------------------------------------------------------------
172 // wxToolBar tool-related methods
173 // ----------------------------------------------------------------------------
175 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
177 // check the "other" direction first: it must be inside the toolbar or we
178 // don't risk finding anything
181 if ( x
< 0 || x
> m_maxWidth
)
184 // we always use x, even for a vertical toolbar, this makes the code
190 if ( y
< 0 || y
> m_maxHeight
)
194 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
196 node
= node
->GetNext() )
198 wxToolBarToolBase
*tool
= node
->GetData();
199 wxRect rectTool
= GetToolRect(tool
);
201 wxCoord startTool
, endTool
;
202 GetRectLimits(rectTool
, &startTool
, &endTool
);
204 if ( x
>= startTool
&& x
<= endTool
)
206 // don't return the separators from here, they don't accept any
208 return tool
->IsSeparator() ? NULL
: tool
;
215 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
217 wxToolBarToolBase
*tool
= FindById(id
);
219 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
221 tool
->SetShortHelp(help
);
224 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
225 wxToolBarToolBase
* WXUNUSED(tool
))
227 // recalculate the toolbar geometry before redrawing it the next time
228 m_needsLayout
= TRUE
;
230 // and ensure that we indeed are going to redraw
236 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
237 wxToolBarToolBase
* WXUNUSED(tool
))
240 m_needsLayout
= TRUE
;
247 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
249 // created disabled-state bitmap on demand
250 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
252 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
254 // TODO: don't hardcode 180
255 unsigned char bg_red
= 180;
256 unsigned char bg_green
= 180;
257 unsigned char bg_blue
= 180;
259 unsigned char mask_red
= image
.GetMaskRed();
260 unsigned char mask_green
= image
.GetMaskGreen();
261 unsigned char mask_blue
= image
.GetMaskBlue();
263 bool has_mask
= image
.HasMask();
266 for (y
= 0; y
< image
.GetHeight(); y
++)
268 for (x
= 0; x
< image
.GetWidth(); x
++)
270 unsigned char red
= image
.GetRed(x
,y
);
271 unsigned char green
= image
.GetGreen(x
,y
);
272 unsigned char blue
= image
.GetBlue(x
,y
);
273 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
275 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
276 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
277 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
278 image
.SetRGB( x
, y
, red
, green
, blue
);
283 for (y
= 0; y
< image
.GetHeight(); y
++)
285 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
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
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
293 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
294 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
295 image
.SetRGB( x
, y
, red
, green
, blue
);
300 tool
->SetDisabledBitmap(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
);
331 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
333 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet (TODO)") );
338 // ----------------------------------------------------------------------------
339 // wxToolBar geometry
340 // ----------------------------------------------------------------------------
342 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
344 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
348 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
350 // ensure that we always have the valid tool position
353 wxConstCast(this, wxToolBar
)->DoLayout();
356 rect
.x
= tool
->m_x
- m_xMargin
;
357 rect
.y
= tool
->m_y
- m_yMargin
;
361 rect
.width
= m_defaultWidth
;
362 rect
.height
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultHeight
;
366 rect
.width
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultWidth
;
367 rect
.height
= m_defaultHeight
;
370 rect
.width
+= 2*m_xMargin
;
371 rect
.height
+= 2*m_yMargin
;
376 bool wxToolBar::Realize()
378 if ( !wxToolBarBase::Realize() )
381 m_needsLayout
= TRUE
;
384 SetBestSize(wxDefaultSize
);
389 void wxToolBar::DoLayout()
391 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
393 m_needsLayout
= FALSE
;
395 wxCoord x
= m_xMargin
,
398 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
399 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
,
400 *pCur
= IsVertical() ? &y
: &x
;
402 // calculate the positions of all elements
403 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
405 node
= node
->GetNext() )
407 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
412 // TODO ugly number fiddling
413 *pCur
+= ( tool
->IsSeparator() ? m_widthSeparator
: (widthTool
+2) ) + margin
;
416 // calculate the total toolbar size
417 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
418 yMin
= m_defaultHeight
+ 2*m_yMargin
;
420 m_maxWidth
= x
< xMin
? xMin
: x
;
421 m_maxHeight
= y
< yMin
? yMin
: y
;
424 wxSize
wxToolBar::DoGetBestClientSize() const
426 return wxSize(m_maxWidth
, m_maxHeight
);
429 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
431 int old_width
, old_height
;
432 GetSize(&old_width
, &old_height
);
434 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
436 // Correct width and height if needed.
437 if ( width
== -1 || height
== -1 )
439 int tmp_width
, tmp_height
;
440 GetSize(&tmp_width
, &tmp_height
);
448 // We must refresh the frame size when the toolbar changes size
449 // otherwise the toolbar can be shown incorrectly
450 if ( old_width
!= width
|| old_height
!= height
)
452 // But before we send the size event check it
453 // we have a frame that is not being deleted.
454 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
455 if ( frame
&& !frame
->IsBeingDeleted() )
457 frame
->SendSizeEvent();
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
466 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
468 RefreshRect(GetToolRect(tool
));
471 void wxToolBar::GetRectLimits(const wxRect
& rect
,
475 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
479 *start
= rect
.GetTop();
480 *end
= rect
.GetBottom();
484 *start
= rect
.GetLeft();
485 *end
= rect
.GetRight();
489 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
491 // prepare the variables used below
492 wxDC
& dc
= renderer
->GetDC();
493 wxRenderer
*rend
= renderer
->GetRenderer();
494 // dc.SetFont(GetFont()); -- uncomment when we support labels
496 // draw the border separating us from the menubar (if there is no menubar
497 // we probably shouldn't draw it?)
500 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
503 // get the update rect and its limits depending on the orientation
504 wxRect rectUpdate
= GetUpdateClientRect();
506 GetRectLimits(rectUpdate
, &start
, &end
);
508 // and redraw all the tools intersecting it
509 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
511 node
= node
->GetNext() )
513 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
514 wxRect rectTool
= GetToolRect(tool
);
515 wxCoord startTool
, endTool
;
516 GetRectLimits(rectTool
, &startTool
, &endTool
);
518 if ( endTool
< start
)
520 // we're still to the left of the area to redraw
524 if ( startTool
> end
)
526 // we're beyond the area to redraw, nothing left to do
530 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
532 // Draw seperators only in flat mode
536 // deal with the flags
539 if ( tool
->IsEnabled() )
541 // The toolbars without wxTB_FLAT don't react to the mouse hovering
542 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
543 flags
|= wxCONTROL_CURRENT
;
545 else // disabled tool
547 flags
|= wxCONTROL_DISABLED
;
550 //if ( tool == m_toolCaptured )
551 // flags |= wxCONTROL_FOCUSED;
553 if ( tool
->IsPressed() )
554 flags
= wxCONTROL_PRESSED
;
558 if ( !tool
->IsSeparator() )
560 // label = tool->GetLabel();
561 bitmap
= tool
->GetBitmap();
563 //else: leave both the label and the bitmap invalid to draw a separator
565 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
);
569 // ----------------------------------------------------------------------------
571 // ----------------------------------------------------------------------------
573 bool wxToolBar::PerformAction(const wxControlAction
& action
,
575 const wxString
& strArg
)
577 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
579 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
581 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
583 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
585 else if ( action
== wxACTION_TOOLBAR_PRESS
)
587 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
593 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
595 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
597 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
603 else if ( action
== wxACTION_TOOLBAR_CLICK
)
606 if ( tool
->CanBeToggled() )
612 isToggled
= tool
->IsToggled();
614 else // simple non-checkable tool
618 OnLeftClick( tool
->GetId(), isToggled
);
620 else if ( action
== wxACTION_TOOLBAR_ENTER
)
622 wxCHECK_MSG( tool
, FALSE
, _T("no tool to enter?") );
624 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
626 tool
->SetUnderMouse( TRUE
);
628 if ( !tool
->IsToggled() )
632 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
634 wxCHECK_MSG( tool
, FALSE
, _T("no tool to leave?") );
636 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
638 tool
->SetUnderMouse( FALSE
);
640 if ( !tool
->IsToggled() )
645 return wxControl::PerformAction(action
, numArg
, strArg
);
650 // ============================================================================
651 // wxStdToolbarInputHandler implementation
652 // ============================================================================
654 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
655 : wxStdInputHandler(handler
)
658 m_toolCapture
= NULL
;
662 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
663 const wxKeyEvent
& event
,
666 // TODO: when we have a current button we should allow the arrow
668 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
671 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
672 const wxMouseEvent
& event
)
674 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
675 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
677 if ( event
.Button(1) )
680 if ( event
.LeftDown() || event
.LeftDClick() )
682 if ( !tool
|| !tool
->IsEnabled() )
686 m_winCapture
->CaptureMouse();
688 m_toolCapture
= tool
;
690 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
694 else if ( event
.LeftUp() )
698 m_winCapture
->ReleaseMouse();
704 if ( tool
== m_toolCapture
)
705 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
707 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
710 m_toolCapture
= NULL
;
714 //else: don't do anything special about the double click
717 return wxStdInputHandler::HandleMouse(consumer
, event
);
720 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
721 const wxMouseEvent
& event
)
723 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
725 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
728 if ( event
.Leaving() )
730 // We cannot possibly be over a tool when
731 // leaving the toolbar
736 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
741 // During capture we only care of the captured tool
742 if (tool
&& (tool
!= m_toolCapture
))
745 if (tool
== m_toolLast
)
749 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
751 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
757 if (tool
== m_toolLast
)
762 // Leave old tool if any
763 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
768 // Enter new tool if any
769 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
781 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
782 const wxFocusEvent
& event
)
786 // We shouldn't be left with a highlighted button
787 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
793 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
796 if (m_toolCapture
&& !activated
)
798 // We shouldn't be left with a highlighted button
799 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
805 #endif // wxUSE_TOOLBAR