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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
)
85 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
86 : wxToolBarToolBase(tbar
, control
)
101 // is this tool pressed, even temporarily? (this is different from being
102 // permanently toggled which is what IsToggled() returns)
103 bool IsPressed() const
104 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
106 // are we temporarily pressed/unpressed?
107 bool IsInverted() const { return m_isInverted
; }
109 // press the tool temporarily by inverting its toggle state
110 void Invert() { m_isInverted
= !m_isInverted
; }
113 void SetUnderMouse( bool under
= TRUE
) { m_underMouse
= under
; }
114 bool IsUnderMouse() { return m_underMouse
; }
117 // the tool position (for controls)
124 // TRUE if the tool is pressed
127 // TRUE if the tool is under the mouse
131 // ============================================================================
132 // wxToolBar implementation
133 // ============================================================================
135 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
137 // ----------------------------------------------------------------------------
138 // wxToolBar creation
139 // ----------------------------------------------------------------------------
141 wxToolBar::wxToolBar()
146 wxToolBar::wxToolBar(wxWindow
*parent
,
151 const wxString
& name
)
155 Create(parent
, id
, pos
, size
, style
, name
);
158 void wxToolBar::Init()
161 m_needsLayout
= FALSE
;
163 // unknown widths for the tools and separators
164 m_widthSeparator
= INVALID_WIDTH
;
169 wxRenderer
*renderer
= GetRenderer();
171 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
172 SetMargins(renderer
->GetToolBarMargin());
175 bool wxToolBar::Create(wxWindow
*parent
,
180 const wxString
& name
)
182 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
183 wxDefaultValidator
, name
) )
188 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
195 wxToolBar::~wxToolBar()
197 // Make sure the toolbar is removed from the parent.
201 void wxToolBar::SetMargins(int x
, int y
)
203 // This required for similar visual effects under
204 // native platforms and wxUniv.
205 wxToolBarBase::SetMargins( x
+ 3, y
+ 3 );
208 // ----------------------------------------------------------------------------
209 // wxToolBar tool-related methods
210 // ----------------------------------------------------------------------------
212 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
214 // check the "other" direction first: it must be inside the toolbar or we
215 // don't risk finding anything
218 if ( x
< 0 || x
> m_maxWidth
)
221 // we always use x, even for a vertical toolbar, this makes the code
227 if ( y
< 0 || y
> m_maxHeight
)
231 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
233 node
= node
->GetNext() )
235 wxToolBarToolBase
*tool
= node
->GetData();
236 wxRect rectTool
= GetToolRect(tool
);
238 wxCoord startTool
, endTool
;
239 GetRectLimits(rectTool
, &startTool
, &endTool
);
241 if ( x
>= startTool
&& x
<= endTool
)
243 // don't return the separators from here, they don't accept any
245 return tool
->IsSeparator() ? NULL
: tool
;
252 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
254 wxToolBarToolBase
*tool
= FindById(id
);
256 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
258 tool
->SetShortHelp(help
);
261 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
262 wxToolBarToolBase
* WXUNUSED(tool
))
264 // recalculate the toolbar geometry before redrawing it the next time
265 m_needsLayout
= TRUE
;
267 // and ensure that we indeed are going to redraw
273 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
274 wxToolBarToolBase
* WXUNUSED(tool
))
277 m_needsLayout
= TRUE
;
284 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
286 // created disabled-state bitmap on demand
287 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
289 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
291 // TODO: don't hardcode 180
292 unsigned char bg_red
= 180;
293 unsigned char bg_green
= 180;
294 unsigned char bg_blue
= 180;
296 unsigned char mask_red
= image
.GetMaskRed();
297 unsigned char mask_green
= image
.GetMaskGreen();
298 unsigned char mask_blue
= image
.GetMaskBlue();
300 bool has_mask
= image
.HasMask();
303 for (y
= 0; y
< image
.GetHeight(); y
++)
305 for (x
= 0; x
< image
.GetWidth(); x
++)
307 unsigned char red
= image
.GetRed(x
,y
);
308 unsigned char green
= image
.GetGreen(x
,y
);
309 unsigned char blue
= image
.GetBlue(x
,y
);
310 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
312 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
313 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
314 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
315 image
.SetRGB( x
, y
, red
, green
, blue
);
320 for (y
= 0; y
< image
.GetHeight(); y
++)
322 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
324 unsigned char red
= image
.GetRed(x
,y
);
325 unsigned char green
= image
.GetGreen(x
,y
);
326 unsigned char blue
= image
.GetBlue(x
,y
);
327 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
329 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
330 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
331 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
332 image
.SetRGB( x
, y
, red
, green
, blue
);
337 tool
->SetDisabledBitmap(image
);
343 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
345 // note that if we're called the tool did change state (the base class
346 // checks for it), so it's not necessary to check for this again here
350 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
355 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
356 const wxString
& label
,
357 const wxBitmap
& bmpNormal
,
358 const wxBitmap
& bmpDisabled
,
360 wxObject
*clientData
,
361 const wxString
& shortHelp
,
362 const wxString
& longHelp
)
364 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
365 clientData
, shortHelp
, longHelp
);
368 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
370 return new wxToolBarTool(this, control
);
373 // ----------------------------------------------------------------------------
374 // wxToolBar geometry
375 // ----------------------------------------------------------------------------
377 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
379 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
383 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
385 // ensure that we always have the valid tool position
388 wxConstCast(this, wxToolBar
)->DoLayout();
391 rect
.x
= tool
->m_x
- m_xMargin
;
392 rect
.y
= tool
->m_y
- m_yMargin
;
396 if (tool
->IsButton())
398 rect
.width
= m_defaultWidth
;
399 rect
.height
= m_defaultHeight
;
401 else if (tool
->IsSeparator())
403 rect
.width
= m_defaultWidth
;
404 rect
.height
= m_widthSeparator
;
408 rect
.width
= tool
->m_width
;
409 rect
.height
= tool
->m_height
;
414 if (tool
->IsButton())
416 rect
.width
= m_defaultWidth
;
417 rect
.height
= m_defaultHeight
;
419 else if (tool
->IsSeparator())
421 rect
.width
= m_widthSeparator
;
422 rect
.height
= m_defaultHeight
;
426 rect
.width
= tool
->m_width
;
427 rect
.height
= tool
->m_height
;
431 rect
.width
+= 2*m_xMargin
;
432 rect
.height
+= 2*m_yMargin
;
437 bool wxToolBar::Realize()
439 if ( !wxToolBarBase::Realize() )
442 m_needsLayout
= TRUE
;
445 SetBestSize(wxDefaultSize
);
450 void wxToolBar::DoLayout()
452 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
454 m_needsLayout
= FALSE
;
456 wxCoord x
= m_xMargin
,
459 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
460 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
,
461 *pCur
= IsVertical() ? &y
: &x
;
463 // calculate the positions of all elements
464 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
466 node
= node
->GetNext() )
468 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
473 // TODO ugly number fiddling
474 if (tool
->IsButton())
478 else if (tool
->IsSeparator())
480 *pCur
+= m_widthSeparator
;
482 else if (!IsVertical()) // horizontal control
484 wxControl
*control
= tool
->GetControl();
485 wxSize size
= control
->GetSize();
486 tool
->m_y
+= (m_defaultHeight
- size
.y
)/2;
487 tool
->m_width
= size
.x
;
488 tool
->m_height
= size
.y
;
490 *pCur
+= tool
->m_width
;
495 // calculate the total toolbar size
496 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
497 yMin
= m_defaultHeight
+ 2*m_yMargin
;
499 m_maxWidth
= x
< xMin
? xMin
: x
;
500 m_maxHeight
= y
< yMin
? yMin
: y
;
503 wxSize
wxToolBar::DoGetBestClientSize() const
505 return wxSize(m_maxWidth
, m_maxHeight
);
508 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
510 int old_width
, old_height
;
511 GetSize(&old_width
, &old_height
);
513 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
515 // Correct width and height if needed.
516 if ( width
== -1 || height
== -1 )
518 int tmp_width
, tmp_height
;
519 GetSize(&tmp_width
, &tmp_height
);
527 // We must refresh the frame size when the toolbar changes size
528 // otherwise the toolbar can be shown incorrectly
529 if ( old_width
!= width
|| old_height
!= height
)
531 // But before we send the size event check it
532 // we have a frame that is not being deleted.
533 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
534 if ( frame
&& !frame
->IsBeingDeleted() )
536 frame
->SendSizeEvent();
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
545 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
547 RefreshRect(GetToolRect(tool
));
550 void wxToolBar::GetRectLimits(const wxRect
& rect
,
554 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
558 *start
= rect
.GetTop();
559 *end
= rect
.GetBottom();
563 *start
= rect
.GetLeft();
564 *end
= rect
.GetRight();
568 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
570 // prepare the variables used below
571 wxDC
& dc
= renderer
->GetDC();
572 wxRenderer
*rend
= renderer
->GetRenderer();
573 // dc.SetFont(GetFont()); -- uncomment when we support labels
575 // draw the border separating us from the menubar (if there is no menubar
576 // we probably shouldn't draw it?)
579 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
582 // get the update rect and its limits depending on the orientation
583 wxRect rectUpdate
= GetUpdateClientRect();
585 GetRectLimits(rectUpdate
, &start
, &end
);
587 // and redraw all the tools intersecting it
588 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
590 node
= node
->GetNext() )
592 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
593 wxRect rectTool
= GetToolRect(tool
);
594 wxCoord startTool
, endTool
;
595 GetRectLimits(rectTool
, &startTool
, &endTool
);
597 if ( endTool
< start
)
599 // we're still to the left of the area to redraw
603 if ( startTool
> end
)
605 // we're beyond the area to redraw, nothing left to do
609 if (tool
->IsSeparator() && !HasFlag(wxTB_FLAT
))
611 // Draw seperators only in flat mode
615 // deal with the flags
618 if ( tool
->IsEnabled() )
620 // The toolbars without wxTB_FLAT don't react to the mouse hovering
621 if ( !HasFlag(wxTB_FLAT
) || tool
->IsUnderMouse() )
622 flags
|= wxCONTROL_CURRENT
;
624 else // disabled tool
626 flags
|= wxCONTROL_DISABLED
;
629 //if ( tool == m_toolCaptured )
630 // flags |= wxCONTROL_FOCUSED;
632 if ( tool
->IsPressed() )
633 flags
= wxCONTROL_PRESSED
;
637 if ( !tool
->IsSeparator() )
639 // label = tool->GetLabel();
640 bitmap
= tool
->GetBitmap();
642 //else: leave both the label and the bitmap invalid to draw a separator
644 if ( !tool
->IsControl() )
646 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
, tool
->GetStyle());
650 wxControl
*control
= tool
->GetControl();
651 control
->Move(tool
->m_x
, tool
->m_y
);
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 bool wxToolBar::PerformAction(const wxControlAction
& action
,
662 const wxString
& strArg
)
664 wxToolBarTool
*tool
= (wxToolBarTool
*) FindById(numArg
);
666 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
668 PerformAction( wxACTION_BUTTON_RELEASE
, numArg
);
670 PerformAction( wxACTION_BUTTON_CLICK
, numArg
);
672 else if ( action
== wxACTION_TOOLBAR_PRESS
)
674 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool
->GetShortHelp().c_str());
680 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
682 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool
->GetShortHelp().c_str());
684 wxASSERT_MSG( tool
->IsInverted(), _T("release unpressed button?") );
690 else if ( action
== wxACTION_TOOLBAR_CLICK
)
693 if ( tool
->CanBeToggled() )
699 isToggled
= tool
->IsToggled();
701 else // simple non-checkable tool
705 OnLeftClick( tool
->GetId(), isToggled
);
707 else if ( action
== wxACTION_TOOLBAR_ENTER
)
709 wxCHECK_MSG( tool
, FALSE
, _T("no tool to enter?") );
711 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
713 tool
->SetUnderMouse( TRUE
);
715 if ( !tool
->IsToggled() )
719 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
721 wxCHECK_MSG( tool
, FALSE
, _T("no tool to leave?") );
723 if ( HasFlag(wxTB_FLAT
) && tool
->IsEnabled() )
725 tool
->SetUnderMouse( FALSE
);
727 if ( !tool
->IsToggled() )
732 return wxControl::PerformAction(action
, numArg
, strArg
);
737 // ============================================================================
738 // wxStdToolbarInputHandler implementation
739 // ============================================================================
741 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
742 : wxStdInputHandler(handler
)
745 m_toolCapture
= NULL
;
749 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
750 const wxKeyEvent
& event
,
753 // TODO: when we have a current button we should allow the arrow
755 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
758 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
759 const wxMouseEvent
& event
)
761 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
762 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
764 if ( event
.Button(1) )
767 if ( event
.LeftDown() || event
.LeftDClick() )
769 if ( !tool
|| !tool
->IsEnabled() )
773 m_winCapture
->CaptureMouse();
775 m_toolCapture
= tool
;
777 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, tool
->GetId() );
781 else if ( event
.LeftUp() )
785 m_winCapture
->ReleaseMouse();
791 if ( tool
== m_toolCapture
)
792 consumer
->PerformAction( wxACTION_BUTTON_TOGGLE
, m_toolCapture
->GetId() );
794 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
797 m_toolCapture
= NULL
;
801 //else: don't do anything special about the double click
804 return wxStdInputHandler::HandleMouse(consumer
, event
);
807 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
808 const wxMouseEvent
& event
)
810 if ( !wxStdInputHandler::HandleMouseMove(consumer
, event
) )
812 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
815 if ( event
.Leaving() )
817 // We cannot possibly be over a tool when
818 // leaving the toolbar
823 tool
= (wxToolBarTool
*) tbar
->FindToolForPosition( event
.GetX(), event
.GetY() );
828 // During capture we only care of the captured tool
829 if (tool
&& (tool
!= m_toolCapture
))
832 if (tool
== m_toolLast
)
836 consumer
->PerformAction( wxACTION_BUTTON_PRESS
, m_toolCapture
->GetId() );
838 consumer
->PerformAction( wxACTION_BUTTON_RELEASE
, m_toolCapture
->GetId() );
844 if (tool
== m_toolLast
)
849 // Leave old tool if any
850 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolLast
->GetId() );
855 // Enter new tool if any
856 consumer
->PerformAction( wxACTION_TOOLBAR_ENTER
, tool
->GetId() );
868 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
869 const wxFocusEvent
& WXUNUSED(event
))
873 // We shouldn't be left with a highlighted button
874 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
880 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
883 if (m_toolCapture
&& !activated
)
885 // We shouldn't be left with a highlighted button
886 consumer
->PerformAction( wxACTION_TOOLBAR_LEAVE
, m_toolCapture
->GetId() );
892 #endif // wxUSE_TOOLBAR