1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/tbarbase.cpp
3 // Purpose: wxToolBarBase implementation
4 // Author: Julian Smart
5 // Modified by: VZ at 11.12.99 (wxScrollableToolBar splitted off)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "tbarbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/control.h"
39 #include "wx/settings.h"
41 #include "wx/toolbar.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
50 #include "wx/listimpl.cpp"
52 WX_DEFINE_LIST(wxToolBarToolsList
);
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 bool wxToolBarToolBase::Enable(bool enable
)
64 if ( m_enabled
== enable
)
72 bool wxToolBarToolBase::Toggle(bool toggle
)
74 wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") );
76 if ( m_toggled
== toggle
)
84 bool wxToolBarToolBase::SetToggle(bool toggle
)
86 wxItemKind kind
= toggle
? wxITEM_CHECK
: wxITEM_NORMAL
;
95 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
97 if ( m_shortHelpString
== help
)
100 m_shortHelpString
= help
;
105 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
107 if ( m_longHelpString
== help
)
110 m_longHelpString
= help
;
115 wxToolBarToolBase::~wxToolBarToolBase()
119 // ----------------------------------------------------------------------------
120 // wxToolBarBase adding/deleting items
121 // ----------------------------------------------------------------------------
123 wxToolBarBase::wxToolBarBase()
125 // the list owns the pointers
126 m_xMargin
= m_yMargin
= 0;
128 m_maxRows
= m_maxCols
= 0;
131 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
132 const wxString
& label
,
133 const wxBitmap
& bitmap
,
134 const wxBitmap
& bmpDisabled
,
136 const wxString
& shortHelp
,
137 const wxString
& longHelp
,
138 wxObject
*clientData
,
139 wxCoord
WXUNUSED(xPos
),
140 wxCoord
WXUNUSED(yPos
))
142 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
143 kind
, shortHelp
, longHelp
, clientData
);
146 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
148 const wxString
& label
,
149 const wxBitmap
& bitmap
,
150 const wxBitmap
& bmpDisabled
,
152 const wxString
& shortHelp
,
153 const wxString
& longHelp
,
154 wxObject
*clientData
)
156 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
157 _T("invalid position in wxToolBar::InsertTool()") );
159 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
160 clientData
, shortHelp
, longHelp
);
162 if ( !InsertTool(pos
, tool
) )
172 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
174 return InsertTool(GetToolsCount(), tool
);
178 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
180 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
181 _T("invalid position in wxToolBar::InsertTool()") );
183 if ( !tool
|| !DoInsertTool(pos
, tool
) )
188 m_tools
.Insert(pos
, tool
);
193 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
195 return InsertControl(GetToolsCount(), control
);
198 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
200 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
201 _T("toolbar: can't insert NULL control") );
203 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
204 _T("control must have toolbar as parent") );
206 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
207 _T("invalid position in wxToolBar::InsertControl()") );
209 wxToolBarToolBase
*tool
= CreateTool(control
);
211 if ( !InsertTool(pos
, tool
) )
221 wxControl
*wxToolBarBase::FindControl( int id
)
223 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
225 node
= node
->GetNext() )
227 const wxToolBarToolBase
* const tool
= node
->GetData();
228 if ( tool
->IsControl() )
230 wxControl
* const control
= tool
->GetControl();
234 wxFAIL_MSG( _T("NULL control in toolbar?") );
236 else if ( control
->GetId() == id
)
247 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
249 return InsertSeparator(GetToolsCount());
252 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
254 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
255 _T("invalid position in wxToolBar::InsertSeparator()") );
257 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
259 wxNullBitmap
, wxNullBitmap
,
260 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
261 wxEmptyString
, wxEmptyString
);
263 if ( !tool
|| !DoInsertTool(pos
, tool
) )
270 m_tools
.Insert(pos
, tool
);
275 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
278 wxToolBarToolsList::compatibility_iterator node
;
279 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
281 if ( node
->GetData()->GetId() == id
)
289 // don't give any error messages - sometimes we might call RemoveTool()
290 // without knowing whether the tool is or not in the toolbar
291 return (wxToolBarToolBase
*)NULL
;
294 wxToolBarToolBase
*tool
= node
->GetData();
295 if ( !DoDeleteTool(pos
, tool
) )
297 return (wxToolBarToolBase
*)NULL
;
305 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
307 wxCHECK_MSG( pos
< GetToolsCount(), FALSE
,
308 _T("invalid position in wxToolBar::DeleteToolByPos()") );
310 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item(pos
);
312 if ( !DoDeleteTool(pos
, node
->GetData()) )
317 delete node
->GetData();
323 bool wxToolBarBase::DeleteTool(int id
)
326 wxToolBarToolsList::compatibility_iterator node
;
327 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
329 if ( node
->GetData()->GetId() == id
)
335 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
340 delete node
->GetData();
346 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
348 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
350 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
352 node
= node
->GetNext() )
354 tool
= node
->GetData();
355 if ( tool
->GetId() == id
)
367 void wxToolBarBase::ClearTools()
369 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
372 bool wxToolBarBase::Realize()
377 wxToolBarBase::~wxToolBarBase()
379 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
382 // ----------------------------------------------------------------------------
383 // wxToolBarBase tools state
384 // ----------------------------------------------------------------------------
386 void wxToolBarBase::EnableTool(int id
, bool enable
)
388 wxToolBarToolBase
*tool
= FindById(id
);
391 if ( tool
->Enable(enable
) )
393 DoEnableTool(tool
, enable
);
398 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
400 wxToolBarToolBase
*tool
= FindById(id
);
401 if ( tool
&& tool
->CanBeToggled() )
403 if ( tool
->Toggle(toggle
) )
405 DoToggleTool(tool
, toggle
);
410 void wxToolBarBase::SetToggle(int id
, bool toggle
)
412 wxToolBarToolBase
*tool
= FindById(id
);
415 if ( tool
->SetToggle(toggle
) )
417 DoSetToggle(tool
, toggle
);
422 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
424 wxToolBarToolBase
*tool
= FindById(id
);
427 (void)tool
->SetShortHelp(help
);
431 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
433 wxToolBarToolBase
*tool
= FindById(id
);
436 (void)tool
->SetLongHelp(help
);
440 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
442 wxToolBarToolBase
*tool
= FindById(id
);
444 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
447 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
449 wxToolBarToolBase
*tool
= FindById(id
);
451 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
453 tool
->SetClientData(clientData
);
456 int wxToolBarBase::GetToolPos(int id
) const
459 wxToolBarToolsList::compatibility_iterator node
;
461 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
463 if ( node
->GetData()->GetId() == id
)
472 bool wxToolBarBase::GetToolState(int id
) const
474 wxToolBarToolBase
*tool
= FindById(id
);
475 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
477 return tool
->IsToggled();
480 bool wxToolBarBase::GetToolEnabled(int id
) const
482 wxToolBarToolBase
*tool
= FindById(id
);
483 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
485 return tool
->IsEnabled();
488 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
490 wxToolBarToolBase
*tool
= FindById(id
);
491 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
493 return tool
->GetShortHelp();
496 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
498 wxToolBarToolBase
*tool
= FindById(id
);
499 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
501 return tool
->GetLongHelp();
504 // ----------------------------------------------------------------------------
505 // wxToolBarBase geometry
506 // ----------------------------------------------------------------------------
508 void wxToolBarBase::SetMargins(int x
, int y
)
514 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 // Only allow toggle if returns TRUE
524 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
526 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
527 event
.SetEventObject(this);
529 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
530 event
.SetInt((int)toggleDown
);
532 // and SetExtraLong() for backwards compatibility
533 event
.SetExtraLong((long)toggleDown
);
535 // Send events to this toolbar instead (and thence up the window hierarchy)
536 GetEventHandler()->ProcessEvent(event
);
541 // Call when right button down.
542 void wxToolBarBase::OnRightClick(int id
,
546 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
547 event
.SetEventObject(this);
550 GetEventHandler()->ProcessEvent(event
);
553 // Called when the mouse cursor enters a tool bitmap (no button pressed).
554 // Argument is -1 if mouse is exiting the toolbar.
555 // Note that for this event, the id of the window is used,
556 // and the integer parameter of wxCommandEvent is used to retrieve
558 void wxToolBarBase::OnMouseEnter(int id
)
560 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
561 event
.SetEventObject(this);
564 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
567 wxToolBarToolBase
* tool
= id
== -1 ? (wxToolBarToolBase
*)0 : FindById(id
);
568 wxString help
= tool
? tool
->GetLongHelp() : wxString();
569 frame
->DoGiveHelp( help
, id
!= -1 );
572 (void)GetEventHandler()->ProcessEvent(event
);
575 // ----------------------------------------------------------------------------
577 // ----------------------------------------------------------------------------
579 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
580 void wxToolBarBase::UpdateWindowUI(long flags
)
582 wxWindowBase::UpdateWindowUI(flags
);
584 wxEvtHandler
* evtHandler
= GetEventHandler() ;
586 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
588 node
= node
->GetNext() )
590 int id
= node
->GetData()->GetId();
592 wxUpdateUIEvent
event(id
);
593 event
.SetEventObject(this);
595 if ( evtHandler
->ProcessEvent(event
) )
597 if ( event
.GetSetEnabled() )
598 EnableTool(id
, event
.GetEnabled());
599 if ( event
.GetSetChecked() )
600 ToggleTool(id
, event
.GetChecked());
602 if ( event
.GetSetText() )
609 // Helper function, used by wxCreateGreyedImage
611 static void wxGreyOutImage( const wxImage
& src
,
613 const wxColour
& darkCol
,
614 const wxColour
& lightCol
,
615 const wxColour
& bgCol
)
617 // Second attempt, just making things monochrome
618 int width
= src
.GetWidth();
619 int height
= src
.GetHeight();
621 int redCur
, greenCur
, blueCur
;
622 for ( int x
= 0; x
< width
; x
++ )
624 for ( int y
= 1; y
< height
; y
++ )
626 redCur
= src
.GetRed(x
, y
);
627 greenCur
= src
.GetGreen(x
, y
);
628 blueCur
= src
.GetBlue(x
, y
);
630 // Change light things to the background colour
631 if ( redCur
>= (lightCol
.Red() - 50) && greenCur
>= (lightCol
.Green() - 50) && blueCur
>= (lightCol
.Blue() - 50) )
633 dest
.SetRGB(x
,y
, bgCol
.Red(), bgCol
.Green(), bgCol
.Blue());
635 else if ( redCur
== bgCol
.Red() && greenCur
== bgCol
.Green() && blueCur
== bgCol
.Blue() )
637 // Leave the background colour as-is
638 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
640 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
642 // Change dark things to really dark
643 dest
.SetRGB(x
,y
, darkCol
.Red(), darkCol
.Green(), darkCol
.Blue());
650 * Make a greyed-out image suitable for disabled buttons.
651 * This code is adapted from wxNewBitmapButton in FL.
654 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
658 // assuming the pixels along the edges are of the background color
659 wxColour
bgCol(in
.GetRed(0, 0), in
.GetGreen(0, 0), in
.GetBlue(0, 0));
661 wxColour darkCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) ;
662 wxColour lightCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
) ;
664 wxGreyOutImage(in
, out
, darkCol
, lightCol
, bgCol
);
669 #endif // wxUSE_TOOLBAR