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
)
73 // the tool position (the size is known by the toolbar itself)
78 // ============================================================================
79 // wxToolBar implementation
80 // ============================================================================
82 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
);
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 void wxToolBar::Init()
91 m_needsLayout
= FALSE
;
93 // unknown widths for the tools and separators
94 m_widthSeparator
= INVALID_WIDTH
;
100 m_toolCurrent
= NULL
;
102 wxRenderer
*renderer
= GetRenderer();
104 SetToolBitmapSize(renderer
->GetToolBarButtonSize(&m_widthSeparator
));
105 SetMargins(renderer
->GetToolBarMargin());
108 bool wxToolBar::Create(wxWindow
*parent
,
113 const wxString
& name
)
115 if ( !wxToolBarBase::Create(parent
, id
, pos
, size
, style
,
116 wxDefaultValidator
, name
) )
121 CreateInputHandler(wxINP_HANDLER_TOOLBAR
);
128 wxToolBar::~wxToolBar()
132 // ----------------------------------------------------------------------------
133 // wxToolBar tool-related methods
134 // ----------------------------------------------------------------------------
136 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
138 // check the "other" direction first: it must be inside the toolbar or we
139 // don't risk finding anything
142 if ( x
< 0 || x
> m_maxWidth
)
145 // we always use x, even for a vertical toolbar, this makes the code
151 if ( y
< 0 || y
> m_maxHeight
)
155 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
157 node
= node
->GetNext() )
159 wxToolBarToolBase
*tool
= node
->GetData();
160 wxRect rectTool
= GetToolRect(tool
);
162 wxCoord startTool
, endTool
;
163 GetRectLimits(rectTool
, &startTool
, &endTool
);
165 if ( x
>= startTool
&& x
<= endTool
)
167 // don't return the separators from here, they don't accept any
169 return tool
->IsSeparator() ? NULL
: tool
;
176 void wxToolBar::SetToolShortHelp(int id
, const wxString
& help
)
178 wxToolBarToolBase
*tool
= FindById(id
);
180 wxCHECK_RET( tool
, _T("SetToolShortHelp: no such tool") );
182 tool
->SetShortHelp(help
);
185 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
186 wxToolBarToolBase
* WXUNUSED(tool
))
188 // recalculate the toolbar geometry before redrawing it the next time
189 m_needsLayout
= TRUE
;
191 // and ensure that we indeed are going to redraw
197 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
),
198 wxToolBarToolBase
* WXUNUSED(tool
))
201 m_needsLayout
= TRUE
;
208 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
210 // created disabled-state bitmap on demand
211 if ( !enable
&& !tool
->GetDisabledBitmap().Ok() )
213 wxImage
image( tool
->GetNormalBitmap() );
215 // TODO: don't hardcode 180
216 unsigned char bg_red
= 180;
217 unsigned char bg_green
= 180;
218 unsigned char bg_blue
= 180;
220 unsigned char mask_red
= image
.GetMaskRed();
221 unsigned char mask_green
= image
.GetMaskGreen();
222 unsigned char mask_blue
= image
.GetMaskBlue();
224 bool has_mask
= image
.HasMask();
227 for (y
= 0; y
< image
.GetHeight(); y
++)
229 for (x
= 0; x
< image
.GetWidth(); x
++)
231 unsigned char red
= image
.GetRed(x
,y
);
232 unsigned char green
= image
.GetGreen(x
,y
);
233 unsigned char blue
= image
.GetBlue(x
,y
);
234 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
236 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
237 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
238 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
239 image
.SetRGB( x
, y
, red
, green
, blue
);
244 for (y
= 0; y
< image
.GetHeight(); y
++)
246 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
248 unsigned char red
= image
.GetRed(x
,y
);
249 unsigned char green
= image
.GetGreen(x
,y
);
250 unsigned char blue
= image
.GetBlue(x
,y
);
251 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
253 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
254 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
255 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
256 image
.SetRGB( x
, y
, red
, green
, blue
);
261 tool
->SetDisabledBitmap( image
.ConvertToBitmap() );
267 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
269 // note that if we're called the tool did change state (the base class
270 // checks for it), so it's not necessary to check for this again here
274 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool WXUNUSED(toggle
))
279 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
280 const wxBitmap
& bitmap1
,
281 const wxBitmap
& bitmap2
,
283 wxObject
*clientData
,
284 const wxString
& shortHelpString
,
285 const wxString
& longHelpString
)
287 return new wxToolBarTool( this, id
, bitmap1
, bitmap2
, toggle
,
288 clientData
, shortHelpString
, longHelpString
);
291 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
293 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet (TODO)") );
298 // ----------------------------------------------------------------------------
299 // wxToolBar geometry
300 // ----------------------------------------------------------------------------
302 wxRect
wxToolBar::GetToolRect(wxToolBarToolBase
*toolBase
) const
304 const wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
308 wxCHECK_MSG( tool
, rect
, _T("GetToolRect: NULL tool") );
310 // ensure that we always have the valid tool position
313 wxConstCast(this, wxToolBar
)->DoLayout();
316 rect
.x
= tool
->m_x
- m_xMargin
;
317 rect
.y
= tool
->m_y
- m_yMargin
;
321 rect
.width
= m_defaultWidth
;
322 rect
.height
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultHeight
;
326 rect
.width
= tool
->IsSeparator() ? m_widthSeparator
: m_defaultWidth
;
327 rect
.height
= m_defaultHeight
;
330 rect
.width
+= 2*m_xMargin
;
331 rect
.height
+= 2*m_yMargin
;
336 bool wxToolBar::Realize()
338 if ( !wxToolBarBase::Realize() )
341 m_needsLayout
= TRUE
;
344 SetBestSize(wxDefaultSize
);
349 void wxToolBar::DoLayout()
351 wxASSERT_MSG( m_needsLayout
, _T("why are we called?") );
353 m_needsLayout
= FALSE
;
355 wxCoord x
= m_xMargin
,
358 const wxCoord widthTool
= IsVertical() ? m_defaultHeight
: m_defaultWidth
;
359 wxCoord margin
= IsVertical() ? m_xMargin
: m_yMargin
,
360 *pCur
= IsVertical() ? &y
: &x
;
362 // calculate the positions of all elements
363 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
365 node
= node
->GetNext() )
367 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
372 *pCur
+= (tool
->IsSeparator() ? m_widthSeparator
: widthTool
) + margin
;
375 // calculate the total toolbar size
376 wxCoord xMin
= m_defaultWidth
+ 2*m_xMargin
,
377 yMin
= m_defaultHeight
+ 2*m_yMargin
;
379 m_maxWidth
= x
< xMin
? xMin
: x
;
380 m_maxHeight
= y
< yMin
? yMin
: y
;
383 wxSize
wxToolBar::DoGetBestClientSize() const
385 return wxSize(m_maxWidth
, m_maxHeight
);
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 void wxToolBar::RefreshTool(wxToolBarToolBase
*tool
)
394 RefreshRect(GetToolRect(tool
));
397 void wxToolBar::GetRectLimits(const wxRect
& rect
,
401 wxCHECK_RET( start
&& end
, _T("NULL pointer in GetRectLimits") );
405 *start
= rect
.GetTop();
406 *end
= rect
.GetBottom();
410 *start
= rect
.GetLeft();
411 *end
= rect
.GetRight();
415 void wxToolBar::DoDraw(wxControlRenderer
*renderer
)
417 // prepare the variables used below
418 wxDC
& dc
= renderer
->GetDC();
419 wxRenderer
*rend
= renderer
->GetRenderer();
420 // dc.SetFont(GetFont()); -- uncomment when we support labels
422 // draw the border separating us from the menubar (if there is no menubar
423 // we probably shouldn't draw it?)
426 rend
->DrawHorizontalLine(dc
, 0, 0, GetClientSize().x
);
429 // get the update rect and its limits depending on the orientation
430 wxRect rectUpdate
= GetUpdateClientRect();
432 GetRectLimits(rectUpdate
, &start
, &end
);
434 // and redraw all the tools intersecting it
435 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
437 node
= node
->GetNext() )
439 wxToolBarToolBase
*tool
= node
->GetData();
440 wxRect rectTool
= GetToolRect(tool
);
441 wxCoord startTool
, endTool
;
442 GetRectLimits(rectTool
, &startTool
, &endTool
);
444 if ( endTool
< start
)
446 // we're still to the left of the area to redraw
450 if ( startTool
> end
)
452 // we're beyond the area to redraw, nothing left to do
456 // deal with the flags
459 if ( tool
->IsEnabled() )
461 // the toolbars without wxTB_FLAT don't react to the mouse hovering
462 if ( HasFlag(wxTB_FLAT
) && (tool
== m_toolCurrent
) )
463 flags
|= wxCONTROL_CURRENT
;
465 else // disabled tool
467 flags
|= wxCONTROL_DISABLED
;
470 if ( tool
->IsToggled() )
471 flags
|= wxCONTROL_PRESSED
;
475 if ( !tool
->IsSeparator() )
477 label
= tool
->GetLabel();
478 bitmap
= tool
->GetBitmap();
480 //else: leave both the label and the bitmap invalid to draw a separator
482 rend
->DrawToolBarButton(dc
, label
, bitmap
, rectTool
, flags
);
486 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
490 void wxToolBar::Press()
492 wxCHECK_RET( m_toolCurrent
, _T("no tool to press?") );
494 m_toolPressed
= m_toolCurrent
;
495 if ( !m_toolPressed
->IsToggled() )
497 m_toolPressed
->Toggle(TRUE
);
499 RefreshTool(m_toolPressed
);
503 void wxToolBar::Release()
505 wxCHECK_RET( m_toolPressed
, _T("no tool to release?") );
507 if ( m_toolPressed
->IsToggled() )
509 m_toolPressed
->Toggle(FALSE
);
511 RefreshTool(m_toolPressed
);
513 m_toolPressed
= NULL
;
517 void wxToolBar::Toggle()
519 wxCHECK_RET( m_toolPressed
, _T("no tool to toggle?") );
521 // the togglable tools should keep their state when the mouse is released
522 if ( !m_toolPressed
->CanBeToggled() )
524 m_toolPressed
->Toggle();
527 RefreshTool(m_toolPressed
);
529 m_toolCurrent
= m_toolPressed
;
530 m_toolPressed
= NULL
;
535 void wxToolBar::Click()
537 wxCHECK_RET( m_toolCurrent
, _T("no tool to click?") );
539 OnLeftClick(m_toolCurrent
->GetId(), m_toolCurrent
->IsToggled());
542 bool wxToolBar::PerformAction(const wxControlAction
& action
,
544 const wxString
& strArg
)
546 if ( action
== wxACTION_TOOLBAR_TOGGLE
)
548 else if ( action
== wxACTION_TOOLBAR_PRESS
)
550 else if ( action
== wxACTION_TOOLBAR_RELEASE
)
552 else if ( action
== wxACTION_TOOLBAR_CLICK
)
554 else if ( action
== wxACTION_TOOLBAR_ENTER
)
556 wxToolBarToolBase
*toolCurrentOld
= m_toolCurrent
;
557 m_toolCurrent
= FindById((int)numArg
);
559 if ( m_toolCurrent
!= toolCurrentOld
)
561 // the appearance of the current tool only changes for the flat
563 if ( HasFlag(wxTB_FLAT
) )
565 // and only if the tool was/is enabled
566 if ( toolCurrentOld
&& toolCurrentOld
->IsEnabled() )
567 RefreshTool(toolCurrentOld
);
571 if ( m_toolCurrent
->IsEnabled() )
572 RefreshTool(m_toolCurrent
);
576 wxFAIL_MSG( _T("no current tool in wxACTION_TOOLBAR_ENTER?") );
581 else if ( action
== wxACTION_TOOLBAR_LEAVE
)
585 wxToolBarToolBase
*toolCurrentOld
= m_toolCurrent
;
586 m_toolCurrent
= NULL
;
588 RefreshTool(toolCurrentOld
);
592 return wxControl::PerformAction(action
, numArg
, strArg
);
597 // ============================================================================
598 // wxStdToolbarInputHandler implementation
599 // ============================================================================
601 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler
*handler
)
602 : wxStdButtonInputHandler(handler
)
606 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer
*consumer
,
607 const wxKeyEvent
& event
,
610 // TODO: when we have a current button we should allow the arrow
612 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
615 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
616 const wxMouseEvent
& event
)
618 if ( !wxStdButtonInputHandler::HandleMouseMove(consumer
, event
) )
620 wxToolBarToolBase
*tool
;
622 if ( event
.Leaving() )
628 wxToolBar
*tbar
= wxStaticCast(consumer
->GetInputWindow(), wxToolBar
);
629 tool
= tbar
->FindToolForPosition(event
.GetX(), event
.GetY());
633 consumer
->PerformAction(wxACTION_TOOLBAR_ENTER
, tool
->GetId());
635 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);
643 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer
*consumer
,
644 const wxFocusEvent
& event
)
646 // we shouldn't be left with a highlighted button
647 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);
652 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer
*consumer
,
657 consumer
->PerformAction(wxACTION_TOOLBAR_LEAVE
);