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
)
78 // is this tool pressed, even temporarily? (this is different from being
79 // permanently toggled which is what IsToggled() returns)
80 bool IsPressed() const
81 { return CanBeToggled() ? IsToggled() != m_isInverted
: m_isInverted
; }
83 // are we temporarily pressed/unpressed?
84 bool IsInverted() const { return m_isInverted
; }
86 // press the tool temporarily by inverting its toggle state
87 void Invert() { m_isInverted
= !m_isInverted
; }
90 // the tool position (the size is known by the toolbar itself)
95 // TRUE if the tool is pressed
99 // ============================================================================
100 // wxToolBar implementation
101 // ============================================================================
103 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
105 // ----------------------------------------------------------------------------
106 // wxToolBar creation
107 // ----------------------------------------------------------------------------
109 void wxToolBar::Init()
112 m_needsLayout
= FALSE
;
114 // unknown widths for the tools and separators
115 m_widthSeparator
= INVALID_WIDTH
;
120 m_toolPressed
= NULL
;
121 m_toolCurrent
= NULL
;
123 wxRenderer
*renderer
= GetRenderer();
125 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
126 SetMargins(renderer
->GetToolBarMargin());
129 bool wxToolBar::Create(wxWindow
*parent
,
134 const wxString
& name
)
136 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
137 wxDefaultValidator
, name
) )
142 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
149 wxToolBar::~wxToolBar()
153 // ----------------------------------------------------------------------------
154 // wxToolBar tool-related methods
155 // ----------------------------------------------------------------------------
157 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
159 // check the "other" direction first: it must be inside the toolbar or we
160 // don't risk finding anything
163 if ( x
< 0 || x
> m_maxWidth
)
166 // we always use x, even for a vertical toolbar, this makes the code
172 if ( y
< 0 || y
> m_maxHeight
)
176 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
178 node
= node
->GetNext() )
180 wxToolBarToolBase
*tool
= node
->GetData();
181 wxRect rectTool
= GetToolRect(tool
);
183 wxCoord startTool
, endTool
;
184 GetRectLimits(rectTool
, &startTool
, &endTool
);
186 if ( x
>= startTool
&& x
<= endTool
)
188 // don't return the separators from here, they don't accept any
190 return tool
->IsSeparator() ? NULL
: tool
;
197 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
199 wxToolBarToolBase
*tool
= FindById(id
);
201 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
203 tool
->SetShortHelp(help
);
206 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
207 wxToolBarToolBase
* WXUNUSED(tool
))
209 // recalculate the toolbar geometry before redrawing it the next time
210 m_needsLayout
= TRUE
;
212 // and ensure that we indeed are going to redraw
218 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
219 wxToolBarToolBase
* WXUNUSED(tool
))
222 m_needsLayout
= TRUE
;
229 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
231 // created disabled-state bitmap on demand
232 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
234 wxImage
image( tool
->GetNormalBitmap().ConvertToImage() );
236 // TODO: don't hardcode 180
237 unsigned char bg_red
= 180;
238 unsigned char bg_green
= 180;
239 unsigned char bg_blue
= 180;
241 unsigned char mask_red
= image
.GetMaskRed();
242 unsigned char mask_green
= image
.GetMaskGreen();
243 unsigned char mask_blue
= image
.GetMaskBlue();
245 bool has_mask
= image
.HasMask();
248 for (y
= 0; y
< image
.GetHeight(); y
++)
250 for (x
= 0; x
< image
.GetWidth(); x
++)
252 unsigned char red
= image
.GetRed(x
,y
);
253 unsigned char green
= image
.GetGreen(x
,y
);
254 unsigned char blue
= image
.GetBlue(x
,y
);
255 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
257 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
258 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
259 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
260 image
.SetRGB( x
, y
, red
, green
, blue
);
265 for (y
= 0; y
< image
.GetHeight(); y
++)
267 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
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 tool
->SetDisabledBitmap(image
);
288 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
290 // note that if we're called the tool did change state (the base class
291 // checks for it), so it's not necessary to check for this again here
295 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
300 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
301 const wxString
& label
,
302 const wxBitmap
& bmpNormal
,
303 const wxBitmap
& bmpDisabled
,
305 wxObject
*clientData
,
306 const wxString
& shortHelp
,
307 const wxString
& longHelp
)
309 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
310 clientData
, shortHelp
, longHelp
);
313 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
315 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet (TODO)") );
320 // ----------------------------------------------------------------------------
321 // wxToolBar geometry
322 // ----------------------------------------------------------------------------
324 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
326 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
330 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
332 // ensure that we always have the valid tool position
335 wxConstCast(this, wxToolBar
)->DoLayout();
338 rect
.x
= tool
->m_x
- m_xMargin
;
339 rect
.y
= tool
->m_y
- m_yMargin
;
343 rect
.width
= m_defaultWidth
;
344 rect
.height
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultHeight
;
348 rect
.width
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultWidth
;
349 rect
.height
= m_defaultHeight
;
352 rect
.width
+= 2*m_xMargin
;
353 rect
.height
+= 2*m_yMargin
;
358 bool wxToolBar::Realize()
360 if ( !wxToolBarBase::Realize() )
363 m_needsLayout
= TRUE
;
366 SetBestSize(wxDefaultSize
);
371 void wxToolBar::DoLayout()
373 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
375 m_needsLayout
= FALSE
;
377 wxCoord x
= m_xMargin
,
380 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
381 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
,
382 *pCur
= IsVertical() ? &y
: &x
;
384 // calculate the positions of all elements
385 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
387 node
= node
->GetNext() )
389 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
394 *pCur
+= (tool
->IsSeparator() ? m_widthSeparator
: widthTool
) + margin
;
397 // calculate the total toolbar size
398 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
399 yMin
= m_defaultHeight
+ 2*m_yMargin
;
401 m_maxWidth
= x
< xMin
? xMin
: x
;
402 m_maxHeight
= y
< yMin
? yMin
: y
;
405 wxSize
wxToolBar::DoGetBestClientSize() const
407 return wxSize(m_maxWidth
, m_maxHeight
);
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
416 RefreshRect(GetToolRect(tool
));
419 void wxToolBar::GetRectLimits(const wxRect
& rect
,
423 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
427 *start
= rect
.GetTop();
428 *end
= rect
.GetBottom();
432 *start
= rect
.GetLeft();
433 *end
= rect
.GetRight();
437 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
439 // prepare the variables used below
440 wxDC
& dc
= renderer
->GetDC();
441 wxRenderer
*rend
= renderer
->GetRenderer();
442 // dc.SetFont(GetFont()); -- uncomment when we support labels
444 // draw the border separating us from the menubar (if there is no menubar
445 // we probably shouldn't draw it?)
448 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
451 // get the update rect and its limits depending on the orientation
452 wxRect rectUpdate
= GetUpdateClientRect();
454 GetRectLimits(rectUpdate
, &start
, &end
);
456 // and redraw all the tools intersecting it
457 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
459 node
= node
->GetNext() )
461 wxToolBarToolBase
*tool
= node
->GetData();
462 wxRect rectTool
= GetToolRect(tool
);
463 wxCoord startTool
, endTool
;
464 GetRectLimits(rectTool
, &startTool
, &endTool
);
466 if ( endTool
< start
)
468 // we're still to the left of the area to redraw
472 if ( startTool
> end
)
474 // we're beyond the area to redraw, nothing left to do
478 // deal with the flags
481 if ( tool
->IsEnabled() )
483 // the toolbars without wxTB_FLAT don't react to the mouse hovering
484 if ( HasFlag(wxTB_FLAT
) && (tool
== m_toolCurrent
) )
485 flags
|= wxCONTROL_CURRENT
;
487 else // disabled tool
489 flags
|= wxCONTROL_DISABLED
;
492 if ( tool
== m_toolPressed
)
493 flags
|= wxCONTROL_FOCUSED
;
495 if ( ((wxToolBarTool
*)tool
)->IsPressed() )
496 flags
|= wxCONTROL_PRESSED
;
500 if ( !tool
->IsSeparator() )
502 label
= tool
->GetLabel();
503 bitmap
= tool
->GetBitmap();
505 //else: leave both the label and the bitmap invalid to draw a separator
507 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
);
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 void wxToolBar::Press()
517 wxCHECK_RET( m_toolCurrent
, _T("no tool to press?") );
519 wxLogTrace(_T("toolbar"),
520 _T("Button '%s' pressed."),
521 m_toolCurrent
->GetShortHelp().c_str());
523 // this is the tool whose state is going to change
524 m_toolPressed
= (wxToolBarTool
*)m_toolCurrent
;
526 // we must toggle it regardless of whether it is a checkable tool or not,
527 // so use Invert() and not Toggle() here
528 m_toolPressed
->Invert();
530 RefreshTool(m_toolPressed
);
533 void wxToolBar::Release()
535 wxCHECK_RET( m_toolPressed
, _T("no tool to release?") );
537 wxLogTrace(_T("toolbar"),
538 _T("Button '%s' released."),
539 m_toolCurrent
->GetShortHelp().c_str());
541 wxASSERT_MSG( m_toolPressed
->IsInverted(), _T("release unpressed button?") );
543 m_toolPressed
->Invert();
545 RefreshTool(m_toolPressed
);
548 void wxToolBar::Toggle()
550 m_toolCurrent
= m_toolPressed
;
557 void wxToolBar::Click()
559 wxCHECK_RET( m_toolCurrent
, _T("no tool to click?") );
562 if ( m_toolCurrent
->CanBeToggled() )
564 m_toolCurrent
->Toggle();
566 RefreshTool(m_toolCurrent
);
568 isToggled
= m_toolCurrent
->IsToggled();
570 else // simple non-checkable tool
575 OnLeftClick(m_toolCurrent
->GetId(), isToggled
);
578 bool wxToolBar::PerformAction(const wxControlAction
& action
,
580 const wxString
& strArg
)
582 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
584 else if ( action
== wxACTION_TOOLBAR_PRESS
)
586 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
588 else if ( action
== wxACTION_TOOLBAR_CLICK
)
590 else if ( action
== wxACTION_TOOLBAR_ENTER
)
592 wxToolBarToolBase
*toolCurrentOld
= m_toolCurrent
;
593 m_toolCurrent
= FindById((int)numArg
);
595 if ( m_toolCurrent
!= toolCurrentOld
)
597 // the appearance of the current tool only changes for the flat
599 if ( HasFlag(wxTB_FLAT
) )
601 // and only if the tool was/is enabled
602 if ( toolCurrentOld
&& toolCurrentOld
->IsEnabled() )
603 RefreshTool(toolCurrentOld
);
607 if ( m_toolCurrent
->IsEnabled() )
608 RefreshTool(m_toolCurrent
);
612 wxFAIL_MSG( _T("no current tool in wxACTION_TOOLBAR_ENTER?") );
617 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
621 wxToolBarToolBase
*toolCurrentOld
= m_toolCurrent
;
622 m_toolCurrent
= NULL
;
624 RefreshTool(toolCurrentOld
);
628 return wxControl::PerformAction(action
, numArg
, strArg
);
633 // ============================================================================
634 // wxStdToolbarInputHandler implementation
635 // ============================================================================
637 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
638 : wxStdButtonInputHandler(handler
)
642 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
643 const wxKeyEvent
& event
,
646 // TODO: when we have a current button we should allow the arrow
648 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
651 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
652 const wxMouseEvent
& event
)
654 // don't let the base class press the disabled buttons but simply ignore
655 // all events on them
656 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
657 wxToolBarToolBase
*tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
659 if ( !tool
|| !tool
->IsEnabled() )
662 return wxStdButtonInputHandler::HandleMouse(consumer
, event
);
665 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
666 const wxMouseEvent
& event
)
668 if ( !wxStdButtonInputHandler::HandleMouseMove(consumer
, event
) )
670 wxToolBarToolBase
*tool
;
672 if ( event
.Leaving() )
678 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
679 tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
683 consumer
->PerformAction(wxACTION_TOOLBAR_ENTER
, tool
->GetId());
685 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);
693 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
694 const wxFocusEvent
& event
)
696 // we shouldn't be left with a highlighted button
697 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);
702 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
707 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);
712 #endif // wxUSE_TOOLBAR