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 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
35 #include "wx/univ/renderer.h"
38 #include "wx/toolbar.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // value meaning that m_widthSeparator is not initialized
47 static const wxCoord INVALID_WIDTH
= wxDefaultCoord
;
49 // ----------------------------------------------------------------------------
50 // wxToolBarTool: our implementation of wxToolBarToolBase
51 // ----------------------------------------------------------------------------
53 class WXDLLEXPORT wxToolBarTool
: public wxToolBarToolBase
56 wxToolBarTool(wxToolBar
*tbar
,
58 const wxString
& label
,
59 const wxBitmap
& bmpNormal
,
60 const wxBitmap
& bmpDisabled
,
63 const wxString
& shortHelp
,
64 const wxString
& longHelp
)
65 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
66 clientData
, shortHelp
, longHelp
)
81 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
82 : wxToolBarToolBase(tbar
, control
)
97 // is this tool pressed, even temporarily? (this is different from being
98 // permanently toggled which is what IsToggled() returns)
99 bool IsPressed() const
100 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
102 // are we temporarily pressed/unpressed?
103 bool IsInverted() const { return m_isInverted
; }
105 // press the tool temporarily by inverting its toggle state
106 void Invert() { m_isInverted
= !m_isInverted
; }
109 void SetUnderMouse( bool under
= true ) { m_underMouse
= under
; }
110 bool IsUnderMouse() { return m_underMouse
; }
113 // the tool position (for controls)
120 // true if the tool is pressed
123 // true if the tool is under the mouse
127 // ============================================================================
128 // wxToolBar implementation
129 // ============================================================================
131 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
133 // ----------------------------------------------------------------------------
134 // wxToolBar creation
135 // ----------------------------------------------------------------------------
137 void wxToolBar::Init()
140 m_needsLayout
= false;
142 // unknown widths for the tools and separators
143 m_widthSeparator
= INVALID_WIDTH
;
148 wxRenderer
*renderer
= GetRenderer();
150 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
151 SetMargins(renderer
->GetToolBarMargin());
154 bool wxToolBar::Create(wxWindow
*parent
,
159 const wxString
& name
)
161 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
162 wxDefaultValidator
, name
) )
167 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
174 wxToolBar::~wxToolBar()
176 // Make sure the toolbar is removed from the parent.
180 void wxToolBar::SetMargins(int x
, int y
)
182 // This required for similar visual effects under
183 // native platforms and wxUniv.
184 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
187 // ----------------------------------------------------------------------------
188 // wxToolBar tool-related methods
189 // ----------------------------------------------------------------------------
191 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
193 // check the "other" direction first: it must be inside the toolbar or we
194 // don't risk finding anything
197 if ( x
< 0 || x
> m_maxWidth
)
200 // we always use x, even for a vertical toolbar, this makes the code
206 if ( y
< 0 || y
> m_maxHeight
)
210 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
212 node
= node
->GetNext() )
214 wxToolBarToolBase
*tool
= node
->GetData();
215 wxRect rectTool
= GetToolRect(tool
);
217 wxCoord startTool
, endTool
;
218 GetRectLimits(rectTool
, &startTool
, &endTool
);
220 if ( x
>= startTool
&& x
<= endTool
)
222 // don't return the separators from here, they don't accept any
224 return tool
->IsSeparator() ? NULL
: tool
;
231 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
233 wxToolBarToolBase
*tool
= FindById(id
);
235 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
237 tool
->SetShortHelp(help
);
240 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
241 wxToolBarToolBase
* WXUNUSED(tool
))
243 // recalculate the toolbar geometry before redrawing it the next time
244 m_needsLayout
= true;
246 // and ensure that we indeed are going to redraw
252 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
253 wxToolBarToolBase
* WXUNUSED(tool
))
256 m_needsLayout
= true;
263 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
265 // created disabled-state bitmap on demand
266 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
268 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
270 // TODO: don't hardcode 180
271 unsigned char bg_red
= 180;
272 unsigned char bg_green
= 180;
273 unsigned char bg_blue
= 180;
275 unsigned char mask_red
= image
.GetMaskRed();
276 unsigned char mask_green
= image
.GetMaskGreen();
277 unsigned char mask_blue
= image
.GetMaskBlue();
279 bool has_mask
= image
.HasMask();
282 for (y
= 0; y
< image
.GetHeight(); y
++)
284 for (x
= 0; x
< image
.GetWidth(); x
++)
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
= (unsigned char)((((wxInt32
) red
- bg_red
) >> 1) + bg_red
);
292 green
= (unsigned char)((((wxInt32
) green
- bg_green
) >> 1) + bg_green
);
293 blue
= (unsigned char)((((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
);
294 image
.SetRGB( x
, y
, red
, green
, blue
);
299 for (y
= 0; y
< image
.GetHeight(); y
++)
301 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
303 unsigned char red
= image
.GetRed(x
,y
);
304 unsigned char green
= image
.GetGreen(x
,y
);
305 unsigned char blue
= image
.GetBlue(x
,y
);
306 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
308 red
= (unsigned char)((((wxInt32
) red
- bg_red
) >> 1) + bg_red
);
309 green
= (unsigned char)((((wxInt32
) green
- bg_green
) >> 1) + bg_green
);
310 blue
= (unsigned char)((((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
);
311 image
.SetRGB( x
, y
, red
, green
, blue
);
316 tool
->SetDisabledBitmap(image
);
322 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
324 // note that if we're called the tool did change state (the base class
325 // checks for it), so it's not necessary to check for this again here
329 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
334 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
335 const wxString
& label
,
336 const wxBitmap
& bmpNormal
,
337 const wxBitmap
& bmpDisabled
,
339 wxObject
*clientData
,
340 const wxString
& shortHelp
,
341 const wxString
& longHelp
)
343 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
344 clientData
, shortHelp
, longHelp
);
347 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
349 return new wxToolBarTool(this, control
);
352 // ----------------------------------------------------------------------------
353 // wxToolBar geometry
354 // ----------------------------------------------------------------------------
356 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
358 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
362 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
364 // ensure that we always have the valid tool position
367 wxConstCast(this, wxToolBar
)->DoLayout();
370 rect
.x
= tool
->m_x
- m_xMargin
;
371 rect
.y
= tool
->m_y
- m_yMargin
;
375 if (tool
->IsButton())
377 rect
.width
= m_defaultWidth
;
378 rect
.height
= m_defaultHeight
;
380 else if (tool
->IsSeparator())
382 rect
.width
= m_defaultWidth
;
383 rect
.height
= m_widthSeparator
;
387 rect
.width
= tool
->m_width
;
388 rect
.height
= tool
->m_height
;
393 if (tool
->IsButton())
395 rect
.width
= m_defaultWidth
;
396 rect
.height
= m_defaultHeight
;
398 else if (tool
->IsSeparator())
400 rect
.width
= m_widthSeparator
;
401 rect
.height
= m_defaultHeight
;
405 rect
.width
= tool
->m_width
;
406 rect
.height
= tool
->m_height
;
410 rect
.width
+= 2*m_xMargin
;
411 rect
.height
+= 2*m_yMargin
;
416 bool wxToolBar::Realize()
418 if ( !wxToolBarBase::Realize() )
421 m_needsLayout
= true;
424 SetBestSize(wxDefaultSize
);
429 void wxToolBar::DoLayout()
431 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
433 m_needsLayout
= false;
435 wxCoord x
= m_xMargin
,
438 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
439 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
;
440 wxCoord
*pCur
= IsVertical() ? &y
: &x
;
442 // calculate the positions of all elements
443 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
445 node
= node
->GetNext() )
447 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
452 // TODO ugly number fiddling
453 if (tool
->IsButton())
457 else if (tool
->IsSeparator())
459 *pCur
+= m_widthSeparator
;
461 else if (!IsVertical()) // horizontal control
463 wxControl
*control
= tool
->GetControl();
464 wxSize size
= control
->GetSize();
465 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
466 tool
->m_width
= size
.x
;
467 tool
->m_height
= size
.y
;
469 *pCur
+= tool
->m_width
;
474 // calculate the total toolbar size
475 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
476 yMin
= m_defaultHeight
+ 2*m_yMargin
;
478 m_maxWidth
= x
< xMin
? xMin
: x
;
479 m_maxHeight
= y
< yMin
? yMin
: y
;
482 wxSize
wxToolBar::DoGetBestClientSize() const
484 return wxSize(m_maxWidth
, m_maxHeight
);
487 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
489 int old_width
, old_height
;
490 GetSize(&old_width
, &old_height
);
492 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
494 // Correct width and height if needed.
495 if ( width
== wxDefaultCoord
|| height
== wxDefaultCoord
)
497 int tmp_width
, tmp_height
;
498 GetSize(&tmp_width
, &tmp_height
);
500 if ( width
== wxDefaultCoord
)
502 if ( height
== wxDefaultCoord
)
506 // We must refresh the frame size when the toolbar changes size
507 // otherwise the toolbar can be shown incorrectly
508 if ( old_width
!= width
|| old_height
!= height
)
510 // But before we send the size event check it
511 // we have a frame that is not being deleted.
512 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
513 if ( frame
&& !frame
->IsBeingDeleted() )
515 frame
->SendSizeEvent();
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
524 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
526 RefreshRect(GetToolRect(tool
));
529 void wxToolBar::GetRectLimits(const wxRect
& rect
,
533 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
537 *start
= rect
.GetTop();
538 *end
= rect
.GetBottom();
542 *start
= rect
.GetLeft();
543 *end
= rect
.GetRight();
547 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
549 // prepare the variables used below
550 wxDC
& dc
= renderer
->GetDC();
551 wxRenderer
*rend
= renderer
->GetRenderer();
552 // dc.SetFont(GetFont()); -- uncomment when we support labels
554 // draw the border separating us from the menubar (if there is no menubar
555 // we probably shouldn't draw it?)
558 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
561 // get the update rect and its limits depending on the orientation
562 wxRect rectUpdate
= GetUpdateClientRect();
564 GetRectLimits(rectUpdate
, &start
, &end
);
566 // and redraw all the tools intersecting it
567 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
569 node
= node
->GetNext() )
571 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
572 wxRect rectTool
= GetToolRect(tool
);
573 wxCoord startTool
, endTool
;
574 GetRectLimits(rectTool
, &startTool
, &endTool
);
576 if ( endTool
< start
)
578 // we're still to the left of the area to redraw
582 if ( startTool
> end
)
584 // we're beyond the area to redraw, nothing left to do
588 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
590 // Draw separators only in flat mode
594 // deal with the flags
597 if ( tool
->IsEnabled() )
599 // The toolbars without wxTB_FLAT don't react to the mouse hovering
600 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
601 flags
|= wxCONTROL_CURRENT
;
603 else // disabled tool
605 flags
|= wxCONTROL_DISABLED
;
608 //if ( tool == m_toolCaptured )
609 // flags |= wxCONTROL_FOCUSED;
611 if ( tool
->IsPressed() )
612 flags
= wxCONTROL_PRESSED
;
616 if ( !tool
->IsSeparator() )
618 // label = tool->GetLabel();
619 bitmap
= tool
->GetBitmap();
621 //else: leave both the label and the bitmap invalid to draw a separator
623 if ( !tool
->IsControl() )
625 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, tool
->GetStyle());
629 wxControl
*control
= tool
->GetControl();
630 control
->Move(tool
->m_x
, tool
->m_y
);
635 // ----------------------------------------------------------------------------
637 // ----------------------------------------------------------------------------
639 bool wxToolBar::PerformAction(const wxControlAction
& action
,
641 const wxString
& strArg
)
643 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
647 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
649 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
651 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
653 else if ( action
== wxACTION_TOOLBAR_PRESS
)
655 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
661 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
663 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
665 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
671 else if ( action
== wxACTION_TOOLBAR_CLICK
)
674 if ( tool
->CanBeToggled() )
680 isToggled
= tool
->IsToggled();
682 else // simple non-checkable tool
686 OnLeftClick( tool
->GetId(), isToggled
);
688 else if ( action
== wxACTION_TOOLBAR_ENTER
)
690 wxCHECK_MSG( tool
, false, _T("no tool to enter?") );
692 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
694 tool
->SetUnderMouse( true );
696 if ( !tool
->IsToggled() )
700 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
702 wxCHECK_MSG( tool
, false, _T("no tool to leave?") );
704 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
706 tool
->SetUnderMouse( false );
708 if ( !tool
->IsToggled() )
713 return wxControl::PerformAction(action
, numArg
, strArg
);
718 // ============================================================================
719 // wxStdToolbarInputHandler implementation
720 // ============================================================================
722 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
723 : wxStdInputHandler(handler
)
726 m_toolCapture
= NULL
;
730 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
731 const wxKeyEvent
& event
,
734 // TODO: when we have a current button we should allow the arrow
736 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
739 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
740 const wxMouseEvent
& event
)
742 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
743 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
745 if ( event
.Button(1) )
748 if ( event
.LeftDown() || event
.LeftDClick() )
750 if ( !tool
|| !tool
->IsEnabled() )
754 m_winCapture
->CaptureMouse();
756 m_toolCapture
= tool
;
758 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
762 else if ( event
.LeftUp() )
766 m_winCapture
->ReleaseMouse();
772 if ( tool
== m_toolCapture
)
773 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
775 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
778 m_toolCapture
= NULL
;
782 //else: don't do anything special about the double click
785 return wxStdInputHandler::HandleMouse(consumer
, event
);
788 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
789 const wxMouseEvent
& event
)
791 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
793 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
796 if ( event
.Leaving() )
798 // We cannot possibly be over a tool when
799 // leaving the toolbar
804 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
809 // During capture we only care of the captured tool
810 if (tool
&& (tool
!= m_toolCapture
))
813 if (tool
== m_toolLast
)
817 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
819 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
825 if (tool
== m_toolLast
)
830 // Leave old tool if any
831 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
836 // Enter new tool if any
837 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
849 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
850 const wxFocusEvent
& WXUNUSED(event
))
854 // We shouldn't be left with a highlighted button
855 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
861 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
864 if (m_toolCapture
&& !activated
)
866 // We shouldn't be left with a highlighted button
867 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
873 #endif // wxUSE_TOOLBAR