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"
41 #include "wx/toolbar.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // value meaning that m_widthSeparator is not initialized
49 static const wxCoord INVALID_WIDTH
= -1;
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
)
81 // is this tool pressed, even temporarily? (this is different from being
82 // permanently toggled which is what IsToggled() returns)
83 bool IsPressed() const
84 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
86 // are we temporarily pressed/unpressed?
87 bool IsInverted() const { return m_isInverted
; }
89 // press the tool temporarily by inverting its toggle state
90 void Invert() { m_isInverted
= !m_isInverted
; }
93 void SetUnderMouse( bool under
= TRUE
) { m_underMouse
= under
; }
94 bool IsUnderMouse() { return m_underMouse
; }
97 // the tool position (the size is known by the toolbar itself)
102 // TRUE if the tool is pressed
105 // TRUE if the tool is under the mouse
109 // ============================================================================
110 // wxToolBar implementation
111 // ============================================================================
113 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
115 // ----------------------------------------------------------------------------
116 // wxToolBar creation
117 // ----------------------------------------------------------------------------
119 void wxToolBar::Init()
122 m_needsLayout
= FALSE
;
124 // unknown widths for the tools and separators
125 m_widthSeparator
= INVALID_WIDTH
;
130 wxRenderer
*renderer
= GetRenderer();
132 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
133 SetMargins(renderer
->GetToolBarMargin());
136 bool wxToolBar::Create(wxWindow
*parent
,
141 const wxString
& name
)
143 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
144 wxDefaultValidator
, name
) )
149 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
156 wxToolBar::~wxToolBar()
160 void wxToolBar::SetMargins(int x
, int y
)
162 // This required for similar visual effects under
163 // native platforms and wxUniv.
164 wxToolBarBase::SetMargins( x
+ 2, y
+ 2 );
167 // ----------------------------------------------------------------------------
168 // wxToolBar tool-related methods
169 // ----------------------------------------------------------------------------
171 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
173 // check the "other" direction first: it must be inside the toolbar or we
174 // don't risk finding anything
177 if ( x
< 0 || x
> m_maxWidth
)
180 // we always use x, even for a vertical toolbar, this makes the code
186 if ( y
< 0 || y
> m_maxHeight
)
190 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
192 node
= node
->GetNext() )
194 wxToolBarToolBase
*tool
= node
->GetData();
195 wxRect rectTool
= GetToolRect(tool
);
197 wxCoord startTool
, endTool
;
198 GetRectLimits(rectTool
, &startTool
, &endTool
);
200 if ( x
>= startTool
&& x
<= endTool
)
202 // don't return the separators from here, they don't accept any
204 return tool
->IsSeparator() ? NULL
: tool
;
211 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
213 wxToolBarToolBase
*tool
= FindById(id
);
215 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
217 tool
->SetShortHelp(help
);
220 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
221 wxToolBarToolBase
* WXUNUSED(tool
))
223 // recalculate the toolbar geometry before redrawing it the next time
224 m_needsLayout
= TRUE
;
226 // and ensure that we indeed are going to redraw
232 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
233 wxToolBarToolBase
* WXUNUSED(tool
))
236 m_needsLayout
= TRUE
;
243 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
245 // created disabled-state bitmap on demand
246 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
248 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
250 // TODO: don't hardcode 180
251 unsigned char bg_red
= 180;
252 unsigned char bg_green
= 180;
253 unsigned char bg_blue
= 180;
255 unsigned char mask_red
= image
.GetMaskRed();
256 unsigned char mask_green
= image
.GetMaskGreen();
257 unsigned char mask_blue
= image
.GetMaskBlue();
259 bool has_mask
= image
.HasMask();
262 for (y
= 0; y
< image
.GetHeight(); y
++)
264 for (x
= 0; x
< image
.GetWidth(); x
++)
266 unsigned char red
= image
.GetRed(x
,y
);
267 unsigned char green
= image
.GetGreen(x
,y
);
268 unsigned char blue
= image
.GetBlue(x
,y
);
269 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
271 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
272 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
273 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
274 image
.SetRGB( x
, y
, red
, green
, blue
);
279 for (y
= 0; y
< image
.GetHeight(); y
++)
281 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
283 unsigned char red
= image
.GetRed(x
,y
);
284 unsigned char green
= image
.GetGreen(x
,y
);
285 unsigned char blue
= image
.GetBlue(x
,y
);
286 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
288 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
289 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
290 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
291 image
.SetRGB( x
, y
, red
, green
, blue
);
296 tool
->SetDisabledBitmap(image
);
302 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
304 // note that if we're called the tool did change state (the base class
305 // checks for it), so it's not necessary to check for this again here
309 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
314 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
315 const wxString
& label
,
316 const wxBitmap
& bmpNormal
,
317 const wxBitmap
& bmpDisabled
,
319 wxObject
*clientData
,
320 const wxString
& shortHelp
,
321 const wxString
& longHelp
)
323 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
324 clientData
, shortHelp
, longHelp
);
327 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
329 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet (TODO)") );
334 // ----------------------------------------------------------------------------
335 // wxToolBar geometry
336 // ----------------------------------------------------------------------------
338 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
340 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
344 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
346 // ensure that we always have the valid tool position
349 wxConstCast(this, wxToolBar
)->DoLayout();
352 rect
.x
= tool
->m_x
- m_xMargin
;
353 rect
.y
= tool
->m_y
- m_yMargin
;
357 rect
.width
= m_defaultWidth
;
358 rect
.height
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultHeight
;
362 rect
.width
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultWidth
;
363 rect
.height
= m_defaultHeight
;
366 rect
.width
+= 2*m_xMargin
;
367 rect
.height
+= 2*m_yMargin
;
372 bool wxToolBar::Realize()
374 if ( !wxToolBarBase::Realize() )
377 m_needsLayout
= TRUE
;
380 SetBestSize(wxDefaultSize
);
385 void wxToolBar::DoLayout()
387 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
389 m_needsLayout
= FALSE
;
391 wxCoord x
= m_xMargin
,
394 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
395 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
,
396 *pCur
= IsVertical() ? &y
: &x
;
398 // calculate the positions of all elements
399 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
401 node
= node
->GetNext() )
403 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
408 *pCur
+= (tool
->IsSeparator() ? m_widthSeparator
: widthTool
) + margin
;
411 // calculate the total toolbar size
412 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
413 yMin
= m_defaultHeight
+ 2*m_yMargin
;
415 m_maxWidth
= x
< xMin
? xMin
: x
;
416 m_maxHeight
= y
< yMin
? yMin
: y
;
419 wxSize
wxToolBar::DoGetBestClientSize() const
421 return wxSize(m_maxWidth
, m_maxHeight
);
424 // ----------------------------------------------------------------------------
426 // ----------------------------------------------------------------------------
428 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
430 RefreshRect(GetToolRect(tool
));
433 void wxToolBar::GetRectLimits(const wxRect
& rect
,
437 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
441 *start
= rect
.GetTop();
442 *end
= rect
.GetBottom();
446 *start
= rect
.GetLeft();
447 *end
= rect
.GetRight();
451 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
453 // prepare the variables used below
454 wxDC
& dc
= renderer
->GetDC();
455 wxRenderer
*rend
= renderer
->GetRenderer();
456 // dc.SetFont(GetFont()); -- uncomment when we support labels
458 // draw the border separating us from the menubar (if there is no menubar
459 // we probably shouldn't draw it?)
462 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
465 // get the update rect and its limits depending on the orientation
466 wxRect rectUpdate
= GetUpdateClientRect();
468 GetRectLimits(rectUpdate
, &start
, &end
);
470 // and redraw all the tools intersecting it
471 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
473 node
= node
->GetNext() )
475 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
476 wxRect rectTool
= GetToolRect(tool
);
477 wxCoord startTool
, endTool
;
478 GetRectLimits(rectTool
, &startTool
, &endTool
);
480 if ( endTool
< start
)
482 // we're still to the left of the area to redraw
486 if ( startTool
> end
)
488 // we're beyond the area to redraw, nothing left to do
492 // deal with the flags
495 if ( tool
->IsEnabled() )
497 // The toolbars without wxTB_FLAT don't react to the mouse hovering
498 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
499 flags
|= wxCONTROL_CURRENT
;
501 else // disabled tool
503 flags
|= wxCONTROL_DISABLED
;
506 //if ( tool == m_toolCaptured )
507 // flags |= wxCONTROL_FOCUSED;
509 if ( tool
->IsPressed() )
510 flags
= wxCONTROL_PRESSED
;
514 if ( !tool
->IsSeparator() )
516 // label = tool->GetLabel();
517 bitmap
= tool
->GetBitmap();
519 //else: leave both the label and the bitmap invalid to draw a separator
521 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
);
525 // ----------------------------------------------------------------------------
527 // ----------------------------------------------------------------------------
529 bool wxToolBar::PerformAction(const wxControlAction
& action
,
531 const wxString
& strArg
)
533 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
535 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
537 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
539 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
541 else if ( action
== wxACTION_TOOLBAR_PRESS
)
543 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
549 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
551 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
553 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
559 else if ( action
== wxACTION_TOOLBAR_CLICK
)
562 if ( tool
->CanBeToggled() )
568 isToggled
= tool
->IsToggled();
570 else // simple non-checkable tool
574 OnLeftClick( tool
->GetId(), isToggled
);
576 else if ( action
== wxACTION_TOOLBAR_ENTER
)
578 wxCHECK_MSG( tool
, FALSE
, _T("no tool to enter?") );
580 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
582 tool
->SetUnderMouse( TRUE
);
584 if ( !tool
->IsToggled() )
588 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
590 wxCHECK_MSG( tool
, FALSE
, _T("no tool to leave?") );
592 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
594 tool
->SetUnderMouse( FALSE
);
596 if ( !tool
->IsToggled() )
601 return wxControl::PerformAction(action
, numArg
, strArg
);
606 // ============================================================================
607 // wxStdToolbarInputHandler implementation
608 // ============================================================================
610 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
611 : wxStdInputHandler(handler
)
614 m_toolCapture
= NULL
;
618 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
619 const wxKeyEvent
& event
,
622 // TODO: when we have a current button we should allow the arrow
624 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
627 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
628 const wxMouseEvent
& event
)
630 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
631 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
633 if ( event
.Button(1) )
635 if ( !tool
|| !tool
->IsEnabled() )
638 if ( event
.LeftDown() || event
.LeftDClick() )
641 m_winCapture
->CaptureMouse();
643 m_toolCapture
= tool
;
645 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
649 else if ( event
.LeftUp() )
653 m_winCapture
->ReleaseMouse();
657 if ( tool
== m_toolCapture
)
659 // this will generate a click event
660 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, tool
->GetId() );
662 m_toolCapture
= NULL
;
666 //else: the mouse was released outside the tool or in
669 m_toolCapture
= NULL
;
672 //else: don't do anything special about the double click
675 return wxStdInputHandler::HandleMouse(consumer
, event
);
678 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
679 const wxMouseEvent
& event
)
681 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
683 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
686 if ( event
.Leaving() )
688 // We cannot possibly be over a tool when
689 // leaving the toolbar
694 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
697 if ((tool
) && (tool
== m_toolLast
))
699 // Still over the same tool as last time
705 // Leave old tool if any
706 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
709 if (m_toolCapture
&& (m_toolCapture
!= tool
))
716 // Enter new tool if any
717 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, m_toolLast
->GetId() );
726 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
727 const wxFocusEvent
& event
)
731 // We shouldn't be left with a highlighted button
732 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
738 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
741 if (m_toolCapture
&& !activated
)
743 // We shouldn't be left with a highlighted button
744 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
750 #endif // wxUSE_TOOLBAR