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
+ 3, y
+ 3 );
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 // TODO ugly number fiddling
409 *pCur
+= ( tool
->IsSeparator() ? m_widthSeparator
: (widthTool
+2) ) + margin
;
412 // calculate the total toolbar size
413 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
414 yMin
= m_defaultHeight
+ 2*m_yMargin
;
416 m_maxWidth
= x
< xMin
? xMin
: x
;
417 m_maxHeight
= y
< yMin
? yMin
: y
;
420 wxSize
wxToolBar::DoGetBestClientSize() const
422 return wxSize(m_maxWidth
, m_maxHeight
);
425 // ----------------------------------------------------------------------------
427 // ----------------------------------------------------------------------------
429 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
431 RefreshRect(GetToolRect(tool
));
434 void wxToolBar::GetRectLimits(const wxRect
& rect
,
438 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
442 *start
= rect
.GetTop();
443 *end
= rect
.GetBottom();
447 *start
= rect
.GetLeft();
448 *end
= rect
.GetRight();
452 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
454 // prepare the variables used below
455 wxDC
& dc
= renderer
->GetDC();
456 wxRenderer
*rend
= renderer
->GetRenderer();
457 // dc.SetFont(GetFont()); -- uncomment when we support labels
459 // draw the border separating us from the menubar (if there is no menubar
460 // we probably shouldn't draw it?)
463 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
466 // get the update rect and its limits depending on the orientation
467 wxRect rectUpdate
= GetUpdateClientRect();
469 GetRectLimits(rectUpdate
, &start
, &end
);
471 // and redraw all the tools intersecting it
472 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
474 node
= node
->GetNext() )
476 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
477 wxRect rectTool
= GetToolRect(tool
);
478 wxCoord startTool
, endTool
;
479 GetRectLimits(rectTool
, &startTool
, &endTool
);
481 if ( endTool
< start
)
483 // we're still to the left of the area to redraw
487 if ( startTool
> end
)
489 // we're beyond the area to redraw, nothing left to do
493 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
495 // Draw seperators only in flat mode
499 // deal with the flags
502 if ( tool
->IsEnabled() )
504 // The toolbars without wxTB_FLAT don't react to the mouse hovering
505 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
506 flags
|= wxCONTROL_CURRENT
;
508 else // disabled tool
510 flags
|= wxCONTROL_DISABLED
;
513 //if ( tool == m_toolCaptured )
514 // flags |= wxCONTROL_FOCUSED;
516 if ( tool
->IsPressed() )
517 flags
= wxCONTROL_PRESSED
;
521 if ( !tool
->IsSeparator() )
523 // label = tool->GetLabel();
524 bitmap
= tool
->GetBitmap();
526 //else: leave both the label and the bitmap invalid to draw a separator
528 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
);
532 // ----------------------------------------------------------------------------
534 // ----------------------------------------------------------------------------
536 bool wxToolBar::PerformAction(const wxControlAction
& action
,
538 const wxString
& strArg
)
540 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
542 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
544 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
546 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
548 else if ( action
== wxACTION_TOOLBAR_PRESS
)
550 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
556 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
558 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
560 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
566 else if ( action
== wxACTION_TOOLBAR_CLICK
)
569 if ( tool
->CanBeToggled() )
575 isToggled
= tool
->IsToggled();
577 else // simple non-checkable tool
581 OnLeftClick( tool
->GetId(), isToggled
);
583 else if ( action
== wxACTION_TOOLBAR_ENTER
)
585 wxCHECK_MSG( tool
, FALSE
, _T("no tool to enter?") );
587 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
589 tool
->SetUnderMouse( TRUE
);
591 if ( !tool
->IsToggled() )
595 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
597 wxCHECK_MSG( tool
, FALSE
, _T("no tool to leave?") );
599 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
601 tool
->SetUnderMouse( FALSE
);
603 if ( !tool
->IsToggled() )
608 return wxControl::PerformAction(action
, numArg
, strArg
);
613 // ============================================================================
614 // wxStdToolbarInputHandler implementation
615 // ============================================================================
617 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
618 : wxStdInputHandler(handler
)
621 m_toolCapture
= NULL
;
625 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
626 const wxKeyEvent
& event
,
629 // TODO: when we have a current button we should allow the arrow
631 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
634 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
635 const wxMouseEvent
& event
)
637 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
638 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
640 if ( event
.Button(1) )
643 if ( event
.LeftDown() || event
.LeftDClick() )
645 if ( !tool
|| !tool
->IsEnabled() )
649 m_winCapture
->CaptureMouse();
651 m_toolCapture
= tool
;
653 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
657 else if ( event
.LeftUp() )
661 m_winCapture
->ReleaseMouse();
665 if ( tool
== m_toolCapture
)
666 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
668 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
670 m_toolCapture
= NULL
;
674 //else: don't do anything special about the double click
677 return wxStdInputHandler::HandleMouse(consumer
, event
);
680 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
681 const wxMouseEvent
& event
)
683 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
685 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
688 if ( event
.Leaving() )
690 // We cannot possibly be over a tool when
691 // leaving the toolbar
696 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
701 // During capture we only care of the captured tool
702 if (tool
&& (tool
!= m_toolCapture
))
705 if (tool
== m_toolLast
)
709 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
711 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
717 if (tool
== m_toolLast
)
722 // Leave old tool if any
723 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
728 // Enter new tool if any
729 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
741 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
742 const wxFocusEvent
& event
)
746 // We shouldn't be left with a highlighted button
747 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
753 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
756 if (m_toolCapture
&& !activated
)
758 // We shouldn't be left with a highlighted button
759 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
765 #endif // wxUSE_TOOLBAR