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"
36 #include "wx/univ/renderer.h"
39 #include "wx/toolbar.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // value meaning that m_widthSeparator is not initialized
47 static const wxCoord INVALID_WIDTH
= -1;
49 // ----------------------------------------------------------------------------
50 // wxToolBarTool: our implementation of wxToolBarToolBase
51 // ----------------------------------------------------------------------------
53 class WXDLLEXPORT wxToolBarTool
: public wxToolBarToolBase
56 wxToolBarTool( wxToolBarBase
*tbar
= (wxToolBarBase
*)NULL
,
57 int id
= wxID_SEPARATOR
,
58 const wxBitmap
& bitmap1
= wxNullBitmap
,
59 const wxBitmap
& bitmap2
= wxNullBitmap
,
61 wxObject
*clientData
= (wxObject
*) NULL
,
62 const wxString
& shortHelpString
= wxEmptyString
,
63 const wxString
& longHelpString
= wxEmptyString
)
64 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
, clientData
,
65 shortHelpString
, longHelpString
)
75 // is this tool pressed, even temporarily? (this is different from being
76 // permanently toggled which is what IsToggled() returns)
77 bool IsPressed() const
78 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
80 // are we temporarily pressed/unpressed?
81 bool IsInverted() const { return m_isInverted
; }
83 // press the tool temporarily by inverting its toggle state
84 void Invert() { m_isInverted
= !m_isInverted
; }
87 // the tool position (the size is known by the toolbar itself)
92 // TRUE if the tool is pressed
96 // ============================================================================
97 // wxToolBar implementation
98 // ============================================================================
100 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
102 // ----------------------------------------------------------------------------
103 // wxToolBar creation
104 // ----------------------------------------------------------------------------
106 void wxToolBar::Init()
109 m_needsLayout
= FALSE
;
111 // unknown widths for the tools and separators
112 m_widthSeparator
= INVALID_WIDTH
;
117 m_toolPressed
= NULL
;
118 m_toolCurrent
= NULL
;
120 wxRenderer
*renderer
= GetRenderer();
122 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
123 SetMargins(renderer
->GetToolBarMargin());
126 bool wxToolBar::Create(wxWindow
*parent
,
131 const wxString
& name
)
133 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
134 wxDefaultValidator
, name
) )
139 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
146 wxToolBar::~wxToolBar()
150 // ----------------------------------------------------------------------------
151 // wxToolBar tool-related methods
152 // ----------------------------------------------------------------------------
154 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
156 // check the "other" direction first: it must be inside the toolbar or we
157 // don't risk finding anything
160 if ( x
< 0 || x
> m_maxWidth
)
163 // we always use x, even for a vertical toolbar, this makes the code
169 if ( y
< 0 || y
> m_maxHeight
)
173 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
175 node
= node
->GetNext() )
177 wxToolBarToolBase
*tool
= node
->GetData();
178 wxRect rectTool
= GetToolRect(tool
);
180 wxCoord startTool
, endTool
;
181 GetRectLimits(rectTool
, &startTool
, &endTool
);
183 if ( x
>= startTool
&& x
<= endTool
)
185 // don't return the separators from here, they don't accept any
187 return tool
->IsSeparator() ? NULL
: tool
;
194 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
196 wxToolBarToolBase
*tool
= FindById(id
);
198 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
200 tool
->SetShortHelp(help
);
203 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
204 wxToolBarToolBase
* WXUNUSED(tool
))
206 // recalculate the toolbar geometry before redrawing it the next time
207 m_needsLayout
= TRUE
;
209 // and ensure that we indeed are going to redraw
215 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
216 wxToolBarToolBase
* WXUNUSED(tool
))
219 m_needsLayout
= TRUE
;
226 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
228 // created disabled-state bitmap on demand
229 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
231 wxImage
image( tool
->GetNormalBitmap() );
233 // TODO: don't hardcode 180
234 unsigned char bg_red
= 180;
235 unsigned char bg_green
= 180;
236 unsigned char bg_blue
= 180;
238 unsigned char mask_red
= image
.GetMaskRed();
239 unsigned char mask_green
= image
.GetMaskGreen();
240 unsigned char mask_blue
= image
.GetMaskBlue();
242 bool has_mask
= image
.HasMask();
245 for (y
= 0; y
< image
.GetHeight(); y
++)
247 for (x
= 0; x
< image
.GetWidth(); x
++)
249 unsigned char red
= image
.GetRed(x
,y
);
250 unsigned char green
= image
.GetGreen(x
,y
);
251 unsigned char blue
= image
.GetBlue(x
,y
);
252 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
254 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
255 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
256 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
257 image
.SetRGB( x
, y
, red
, green
, blue
);
262 for (y
= 0; y
< image
.GetHeight(); y
++)
264 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
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 tool
->SetDisabledBitmap( image
.ConvertToBitmap() );
285 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
287 // note that if we're called the tool did change state (the base class
288 // checks for it), so it's not necessary to check for this again here
292 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
297 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
298 const wxBitmap
& bitmap1
,
299 const wxBitmap
& bitmap2
,
301 wxObject
*clientData
,
302 const wxString
& shortHelpString
,
303 const wxString
& longHelpString
)
305 return new wxToolBarTool( this, id
, bitmap1
, bitmap2
, toggle
,
306 clientData
, shortHelpString
, longHelpString
);
309 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
311 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet (TODO)") );
316 // ----------------------------------------------------------------------------
317 // wxToolBar geometry
318 // ----------------------------------------------------------------------------
320 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
322 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
326 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
328 // ensure that we always have the valid tool position
331 wxConstCast(this, wxToolBar
)->DoLayout();
334 rect
.x
= tool
->m_x
- m_xMargin
;
335 rect
.y
= tool
->m_y
- m_yMargin
;
339 rect
.width
= m_defaultWidth
;
340 rect
.height
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultHeight
;
344 rect
.width
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultWidth
;
345 rect
.height
= m_defaultHeight
;
348 rect
.width
+= 2*m_xMargin
;
349 rect
.height
+= 2*m_yMargin
;
354 bool wxToolBar::Realize()
356 if ( !wxToolBarBase::Realize() )
359 m_needsLayout
= TRUE
;
362 SetBestSize(wxDefaultSize
);
367 void wxToolBar::DoLayout()
369 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
371 m_needsLayout
= FALSE
;
373 wxCoord x
= m_xMargin
,
376 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
377 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
,
378 *pCur
= IsVertical() ? &y
: &x
;
380 // calculate the positions of all elements
381 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
383 node
= node
->GetNext() )
385 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
390 *pCur
+= (tool
->IsSeparator() ? m_widthSeparator
: widthTool
) + margin
;
393 // calculate the total toolbar size
394 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
395 yMin
= m_defaultHeight
+ 2*m_yMargin
;
397 m_maxWidth
= x
< xMin
? xMin
: x
;
398 m_maxHeight
= y
< yMin
? yMin
: y
;
401 wxSize
wxToolBar::DoGetBestClientSize() const
403 return wxSize(m_maxWidth
, m_maxHeight
);
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
412 RefreshRect(GetToolRect(tool
));
415 void wxToolBar::GetRectLimits(const wxRect
& rect
,
419 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
423 *start
= rect
.GetTop();
424 *end
= rect
.GetBottom();
428 *start
= rect
.GetLeft();
429 *end
= rect
.GetRight();
433 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
435 // prepare the variables used below
436 wxDC
& dc
= renderer
->GetDC();
437 wxRenderer
*rend
= renderer
->GetRenderer();
438 // dc.SetFont(GetFont()); -- uncomment when we support labels
440 // draw the border separating us from the menubar (if there is no menubar
441 // we probably shouldn't draw it?)
444 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
447 // get the update rect and its limits depending on the orientation
448 wxRect rectUpdate
= GetUpdateClientRect();
450 GetRectLimits(rectUpdate
, &start
, &end
);
452 // and redraw all the tools intersecting it
453 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
455 node
= node
->GetNext() )
457 wxToolBarToolBase
*tool
= node
->GetData();
458 wxRect rectTool
= GetToolRect(tool
);
459 wxCoord startTool
, endTool
;
460 GetRectLimits(rectTool
, &startTool
, &endTool
);
462 if ( endTool
< start
)
464 // we're still to the left of the area to redraw
468 if ( startTool
> end
)
470 // we're beyond the area to redraw, nothing left to do
474 // deal with the flags
477 if ( tool
->IsEnabled() )
479 // the toolbars without wxTB_FLAT don't react to the mouse hovering
480 if ( HasFlag(wxTB_FLAT
) && (tool
== m_toolCurrent
) )
481 flags
|= wxCONTROL_CURRENT
;
483 else // disabled tool
485 flags
|= wxCONTROL_DISABLED
;
488 if ( tool
== m_toolPressed
)
489 flags
|= wxCONTROL_FOCUSED
;
491 if ( ((wxToolBarTool
*)tool
)->IsPressed() )
492 flags
|= wxCONTROL_PRESSED
;
496 if ( !tool
->IsSeparator() )
498 label
= tool
->GetLabel();
499 bitmap
= tool
->GetBitmap();
501 //else: leave both the label and the bitmap invalid to draw a separator
503 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
);
507 // ----------------------------------------------------------------------------
509 // ----------------------------------------------------------------------------
511 void wxToolBar::Press()
513 wxCHECK_RET( m_toolCurrent
, _T("no tool to press?") );
515 wxLogTrace(_T("toolbar"),
516 _T("Button '%s' pressed."),
517 m_toolCurrent
->GetShortHelp().c_str());
519 // this is the tool whose state is going to change
520 m_toolPressed
= (wxToolBarTool
*)m_toolCurrent
;
522 // we must toggle it regardless of whether it is a checkable tool or not,
523 // so use Invert() and not Toggle() here
524 m_toolPressed
->Invert();
526 RefreshTool(m_toolPressed
);
529 void wxToolBar::Release()
531 wxCHECK_RET( m_toolPressed
, _T("no tool to release?") );
533 wxLogTrace(_T("toolbar"),
534 _T("Button '%s' released."),
535 m_toolCurrent
->GetShortHelp().c_str());
537 wxASSERT_MSG( m_toolPressed
->IsInverted(), _T("release unpressed button?") );
539 m_toolPressed
->Invert();
541 RefreshTool(m_toolPressed
);
544 void wxToolBar::Toggle()
546 m_toolCurrent
= m_toolPressed
;
553 void wxToolBar::Click()
555 wxCHECK_RET( m_toolCurrent
, _T("no tool to click?") );
558 if ( m_toolCurrent
->CanBeToggled() )
560 m_toolCurrent
->Toggle();
562 RefreshTool(m_toolCurrent
);
564 isToggled
= m_toolCurrent
->IsToggled();
566 else // simple non-checkable tool
571 OnLeftClick(m_toolCurrent
->GetId(), isToggled
);
574 bool wxToolBar::PerformAction(const wxControlAction
& action
,
576 const wxString
& strArg
)
578 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
580 else if ( action
== wxACTION_TOOLBAR_PRESS
)
582 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
584 else if ( action
== wxACTION_TOOLBAR_CLICK
)
586 else if ( action
== wxACTION_TOOLBAR_ENTER
)
588 wxToolBarToolBase
*toolCurrentOld
= m_toolCurrent
;
589 m_toolCurrent
= FindById((int)numArg
);
591 if ( m_toolCurrent
!= toolCurrentOld
)
593 // the appearance of the current tool only changes for the flat
595 if ( HasFlag(wxTB_FLAT
) )
597 // and only if the tool was/is enabled
598 if ( toolCurrentOld
&& toolCurrentOld
->IsEnabled() )
599 RefreshTool(toolCurrentOld
);
603 if ( m_toolCurrent
->IsEnabled() )
604 RefreshTool(m_toolCurrent
);
608 wxFAIL_MSG( _T("no current tool in wxACTION_TOOLBAR_ENTER?") );
613 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
617 wxToolBarToolBase
*toolCurrentOld
= m_toolCurrent
;
618 m_toolCurrent
= NULL
;
620 RefreshTool(toolCurrentOld
);
624 return wxControl::PerformAction(action
, numArg
, strArg
);
629 // ============================================================================
630 // wxStdToolbarInputHandler implementation
631 // ============================================================================
633 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
634 : wxStdButtonInputHandler(handler
)
638 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
639 const wxKeyEvent
& event
,
642 // TODO: when we have a current button we should allow the arrow
644 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
647 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
648 const wxMouseEvent
& event
)
650 // don't let the base class press the disabled buttons but simply ignore
651 // all events on them
652 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
653 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
655 if ( tool
&& !tool
->IsEnabled() )
658 return wxStdButtonInputHandler::HandleMouse(consumer
, event
);
661 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
662 const wxMouseEvent
& event
)
664 if ( !wxStdButtonInputHandler::HandleMouseMove(consumer
, event
) )
666 wxToolBarToolBase
*tool
;
668 if ( event
.Leaving() )
674 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
675 tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
679 consumer
->PerformAction(wxACTION_TOOLBAR_ENTER
, tool
->GetId());
681 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);
689 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
690 const wxFocusEvent
& event
)
692 // we shouldn't be left with a highlighted button
693 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);
698 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
703 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);