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"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // value meaning that m_widthSeparator is not initialized
50 static const wxCoord INVALID_WIDTH
= -1;
52 // ----------------------------------------------------------------------------
53 // wxToolBarTool: our implementation of wxToolBarToolBase
54 // ----------------------------------------------------------------------------
56 class WXDLLEXPORT wxToolBarTool
: public wxToolBarToolBase
59 wxToolBarTool(wxToolBar
*tbar
,
61 const wxString
& label
,
62 const wxBitmap
& bmpNormal
,
63 const wxBitmap
& bmpDisabled
,
66 const wxString
& shortHelp
,
67 const wxString
& longHelp
)
68 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
69 clientData
, shortHelp
, longHelp
)
82 // is this tool pressed, even temporarily? (this is different from being
83 // permanently toggled which is what IsToggled() returns)
84 bool IsPressed() const
85 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
87 // are we temporarily pressed/unpressed?
88 bool IsInverted() const { return m_isInverted
; }
90 // press the tool temporarily by inverting its toggle state
91 void Invert() { m_isInverted
= !m_isInverted
; }
94 void SetUnderMouse( bool under
= TRUE
) { m_underMouse
= under
; }
95 bool IsUnderMouse() { return m_underMouse
; }
98 // the tool position (the size is known by the toolbar itself)
103 // TRUE if the tool is pressed
106 // TRUE if the tool is under the mouse
110 // ============================================================================
111 // wxToolBar implementation
112 // ============================================================================
114 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
116 // ----------------------------------------------------------------------------
117 // wxToolBar creation
118 // ----------------------------------------------------------------------------
120 void wxToolBar::Init()
123 m_needsLayout
= FALSE
;
125 // unknown widths for the tools and separators
126 m_widthSeparator
= INVALID_WIDTH
;
131 wxRenderer
*renderer
= GetRenderer();
133 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
134 SetMargins(renderer
->GetToolBarMargin());
137 bool wxToolBar::Create(wxWindow
*parent
,
142 const wxString
& name
)
144 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
145 wxDefaultValidator
, name
) )
150 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
157 wxToolBar::~wxToolBar()
159 // Make sure the toolbar is removed from the parent.
163 void wxToolBar::SetMargins(int x
, int y
)
165 // This required for similar visual effects under
166 // native platforms and wxUniv.
167 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
170 // ----------------------------------------------------------------------------
171 // wxToolBar tool-related methods
172 // ----------------------------------------------------------------------------
174 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
176 // check the "other" direction first: it must be inside the toolbar or we
177 // don't risk finding anything
180 if ( x
< 0 || x
> m_maxWidth
)
183 // we always use x, even for a vertical toolbar, this makes the code
189 if ( y
< 0 || y
> m_maxHeight
)
193 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
195 node
= node
->GetNext() )
197 wxToolBarToolBase
*tool
= node
->GetData();
198 wxRect rectTool
= GetToolRect(tool
);
200 wxCoord startTool
, endTool
;
201 GetRectLimits(rectTool
, &startTool
, &endTool
);
203 if ( x
>= startTool
&& x
<= endTool
)
205 // don't return the separators from here, they don't accept any
207 return tool
->IsSeparator() ? NULL
: tool
;
214 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
216 wxToolBarToolBase
*tool
= FindById(id
);
218 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
220 tool
->SetShortHelp(help
);
223 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
224 wxToolBarToolBase
* WXUNUSED(tool
))
226 // recalculate the toolbar geometry before redrawing it the next time
227 m_needsLayout
= TRUE
;
229 // and ensure that we indeed are going to redraw
235 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
236 wxToolBarToolBase
* WXUNUSED(tool
))
239 m_needsLayout
= TRUE
;
246 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
248 // created disabled-state bitmap on demand
249 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
251 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
253 // TODO: don't hardcode 180
254 unsigned char bg_red
= 180;
255 unsigned char bg_green
= 180;
256 unsigned char bg_blue
= 180;
258 unsigned char mask_red
= image
.GetMaskRed();
259 unsigned char mask_green
= image
.GetMaskGreen();
260 unsigned char mask_blue
= image
.GetMaskBlue();
262 bool has_mask
= image
.HasMask();
265 for (y
= 0; y
< image
.GetHeight(); y
++)
267 for (x
= 0; x
< image
.GetWidth(); x
++)
269 unsigned char red
= image
.GetRed(x
,y
);
270 unsigned char green
= image
.GetGreen(x
,y
);
271 unsigned char blue
= image
.GetBlue(x
,y
);
272 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
274 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
275 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
276 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
277 image
.SetRGB( x
, y
, red
, green
, blue
);
282 for (y
= 0; y
< image
.GetHeight(); y
++)
284 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
286 unsigned char red
= image
.GetRed(x
,y
);
287 unsigned char green
= image
.GetGreen(x
,y
);
288 unsigned char blue
= image
.GetBlue(x
,y
);
289 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
291 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
292 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
293 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
294 image
.SetRGB( x
, y
, red
, green
, blue
);
299 tool
->SetDisabledBitmap(image
);
305 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
307 // note that if we're called the tool did change state (the base class
308 // checks for it), so it's not necessary to check for this again here
312 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
317 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
318 const wxString
& label
,
319 const wxBitmap
& bmpNormal
,
320 const wxBitmap
& bmpDisabled
,
322 wxObject
*clientData
,
323 const wxString
& shortHelp
,
324 const wxString
& longHelp
)
326 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
327 clientData
, shortHelp
, longHelp
);
330 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
332 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet (TODO)") );
337 // ----------------------------------------------------------------------------
338 // wxToolBar geometry
339 // ----------------------------------------------------------------------------
341 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
343 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
347 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
349 // ensure that we always have the valid tool position
352 wxConstCast(this, wxToolBar
)->DoLayout();
355 rect
.x
= tool
->m_x
- m_xMargin
;
356 rect
.y
= tool
->m_y
- m_yMargin
;
360 rect
.width
= m_defaultWidth
;
361 rect
.height
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultHeight
;
365 rect
.width
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultWidth
;
366 rect
.height
= m_defaultHeight
;
369 rect
.width
+= 2*m_xMargin
;
370 rect
.height
+= 2*m_yMargin
;
375 bool wxToolBar::Realize()
377 if ( !wxToolBarBase::Realize() )
380 m_needsLayout
= TRUE
;
383 SetBestSize(wxDefaultSize
);
388 void wxToolBar::DoLayout()
390 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
392 m_needsLayout
= FALSE
;
394 wxCoord x
= m_xMargin
,
397 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
398 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
,
399 *pCur
= IsVertical() ? &y
: &x
;
401 // calculate the positions of all elements
402 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
404 node
= node
->GetNext() )
406 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
411 // TODO ugly number fiddling
412 *pCur
+= ( tool
->IsSeparator() ? m_widthSeparator
: (widthTool
+2) ) + margin
;
415 // calculate the total toolbar size
416 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
417 yMin
= m_defaultHeight
+ 2*m_yMargin
;
419 m_maxWidth
= x
< xMin
? xMin
: x
;
420 m_maxHeight
= y
< yMin
? yMin
: y
;
423 wxSize
wxToolBar::DoGetBestClientSize() const
425 return wxSize(m_maxWidth
, m_maxHeight
);
428 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
430 int old_width
, old_height
;
431 GetSize(&old_width
, &old_height
);
433 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
435 // Correct width and height if needed.
436 if ( width
== -1 || height
== -1 )
438 int tmp_width
, tmp_height
;
439 GetSize(&tmp_width
, &tmp_height
);
447 // We must refresh the frame size when the toolbar changes size
448 // otherwise the toolbar can be shown incorrectly
449 if ( old_width
!= width
|| old_height
!= height
)
451 // But before we send the size event check it
452 // we have a frame that is not being deleted.
453 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
454 if ( frame
&& !frame
->IsBeingDeleted() )
456 frame
->SendSizeEvent();
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
467 RefreshRect(GetToolRect(tool
));
470 void wxToolBar::GetRectLimits(const wxRect
& rect
,
474 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
478 *start
= rect
.GetTop();
479 *end
= rect
.GetBottom();
483 *start
= rect
.GetLeft();
484 *end
= rect
.GetRight();
488 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
490 // prepare the variables used below
491 wxDC
& dc
= renderer
->GetDC();
492 wxRenderer
*rend
= renderer
->GetRenderer();
493 // dc.SetFont(GetFont()); -- uncomment when we support labels
495 // draw the border separating us from the menubar (if there is no menubar
496 // we probably shouldn't draw it?)
499 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
502 // get the update rect and its limits depending on the orientation
503 wxRect rectUpdate
= GetUpdateClientRect();
505 GetRectLimits(rectUpdate
, &start
, &end
);
507 // and redraw all the tools intersecting it
508 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
510 node
= node
->GetNext() )
512 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
513 wxRect rectTool
= GetToolRect(tool
);
514 wxCoord startTool
, endTool
;
515 GetRectLimits(rectTool
, &startTool
, &endTool
);
517 if ( endTool
< start
)
519 // we're still to the left of the area to redraw
523 if ( startTool
> end
)
525 // we're beyond the area to redraw, nothing left to do
529 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
531 // Draw seperators only in flat mode
535 // deal with the flags
538 if ( tool
->IsEnabled() )
540 // The toolbars without wxTB_FLAT don't react to the mouse hovering
541 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
542 flags
|= wxCONTROL_CURRENT
;
544 else // disabled tool
546 flags
|= wxCONTROL_DISABLED
;
549 //if ( tool == m_toolCaptured )
550 // flags |= wxCONTROL_FOCUSED;
552 if ( tool
->IsPressed() )
553 flags
= wxCONTROL_PRESSED
;
557 if ( !tool
->IsSeparator() )
559 // label = tool->GetLabel();
560 bitmap
= tool
->GetBitmap();
562 //else: leave both the label and the bitmap invalid to draw a separator
564 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
);
568 // ----------------------------------------------------------------------------
570 // ----------------------------------------------------------------------------
572 bool wxToolBar::PerformAction(const wxControlAction
& action
,
574 const wxString
& strArg
)
576 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
578 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
580 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
582 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
584 else if ( action
== wxACTION_TOOLBAR_PRESS
)
586 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
592 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
594 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
596 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
602 else if ( action
== wxACTION_TOOLBAR_CLICK
)
605 if ( tool
->CanBeToggled() )
611 isToggled
= tool
->IsToggled();
613 else // simple non-checkable tool
617 OnLeftClick( tool
->GetId(), isToggled
);
619 else if ( action
== wxACTION_TOOLBAR_ENTER
)
621 wxCHECK_MSG( tool
, FALSE
, _T("no tool to enter?") );
623 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
625 tool
->SetUnderMouse( TRUE
);
627 if ( !tool
->IsToggled() )
631 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
633 wxCHECK_MSG( tool
, FALSE
, _T("no tool to leave?") );
635 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
637 tool
->SetUnderMouse( FALSE
);
639 if ( !tool
->IsToggled() )
644 return wxControl::PerformAction(action
, numArg
, strArg
);
649 // ============================================================================
650 // wxStdToolbarInputHandler implementation
651 // ============================================================================
653 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
654 : wxStdInputHandler(handler
)
657 m_toolCapture
= NULL
;
661 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
662 const wxKeyEvent
& event
,
665 // TODO: when we have a current button we should allow the arrow
667 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
670 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
671 const wxMouseEvent
& event
)
673 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
674 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
676 if ( event
.Button(1) )
679 if ( event
.LeftDown() || event
.LeftDClick() )
681 if ( !tool
|| !tool
->IsEnabled() )
685 m_winCapture
->CaptureMouse();
687 m_toolCapture
= tool
;
689 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
693 else if ( event
.LeftUp() )
697 m_winCapture
->ReleaseMouse();
703 if ( tool
== m_toolCapture
)
704 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
706 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
709 m_toolCapture
= NULL
;
713 //else: don't do anything special about the double click
716 return wxStdInputHandler::HandleMouse(consumer
, event
);
719 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
720 const wxMouseEvent
& event
)
722 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
724 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
727 if ( event
.Leaving() )
729 // We cannot possibly be over a tool when
730 // leaving the toolbar
735 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
740 // During capture we only care of the captured tool
741 if (tool
&& (tool
!= m_toolCapture
))
744 if (tool
== m_toolLast
)
748 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
750 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
756 if (tool
== m_toolLast
)
761 // Leave old tool if any
762 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
767 // Enter new tool if any
768 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
780 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
781 const wxFocusEvent
& event
)
785 // We shouldn't be left with a highlighted button
786 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
792 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
795 if (m_toolCapture
&& !activated
)
797 // We shouldn't be left with a highlighted button
798 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
804 #endif // wxUSE_TOOLBAR