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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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::UnToggleRadioGroup(wxToolBarToolBase
*tool
)
369 wxCHECK_RET( tool
, _T("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
371 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
374 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Find(tool
);
375 wxCHECK_RET( node
, _T("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
377 wxToolBarToolsList::compatibility_iterator nodeNext
= node
->GetNext();
380 wxToolBarToolBase
*tool
= nodeNext
->GetData();
382 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
387 nodeNext
= nodeNext
->GetNext();
390 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
393 wxToolBarToolBase
*tool
= nodePrev
->GetData();
395 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
400 nodePrev
= nodePrev
->GetPrevious();
404 void wxToolBarBase::ClearTools()
406 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
409 bool wxToolBarBase::Realize()
414 wxToolBarBase::~wxToolBarBase()
416 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
419 // ----------------------------------------------------------------------------
420 // wxToolBarBase tools state
421 // ----------------------------------------------------------------------------
423 void wxToolBarBase::EnableTool(int id
, bool enable
)
425 wxToolBarToolBase
*tool
= FindById(id
);
428 if ( tool
->Enable(enable
) )
430 DoEnableTool(tool
, enable
);
435 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
437 wxToolBarToolBase
*tool
= FindById(id
);
438 if ( tool
&& tool
->CanBeToggled() )
440 if ( tool
->Toggle(toggle
) )
442 UnToggleRadioGroup(tool
);
443 DoToggleTool(tool
, toggle
);
448 void wxToolBarBase::SetToggle(int id
, bool toggle
)
450 wxToolBarToolBase
*tool
= FindById(id
);
453 if ( tool
->SetToggle(toggle
) )
455 DoSetToggle(tool
, toggle
);
460 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
462 wxToolBarToolBase
*tool
= FindById(id
);
465 (void)tool
->SetShortHelp(help
);
469 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
471 wxToolBarToolBase
*tool
= FindById(id
);
474 (void)tool
->SetLongHelp(help
);
478 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
480 wxToolBarToolBase
*tool
= FindById(id
);
482 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
485 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
487 wxToolBarToolBase
*tool
= FindById(id
);
489 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
491 tool
->SetClientData(clientData
);
494 int wxToolBarBase::GetToolPos(int id
) const
497 wxToolBarToolsList::compatibility_iterator node
;
499 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
501 if ( node
->GetData()->GetId() == id
)
510 bool wxToolBarBase::GetToolState(int id
) const
512 wxToolBarToolBase
*tool
= FindById(id
);
513 wxCHECK_MSG( tool
, false, _T("no such tool") );
515 return tool
->IsToggled();
518 bool wxToolBarBase::GetToolEnabled(int id
) const
520 wxToolBarToolBase
*tool
= FindById(id
);
521 wxCHECK_MSG( tool
, false, _T("no such tool") );
523 return tool
->IsEnabled();
526 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
528 wxToolBarToolBase
*tool
= FindById(id
);
529 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
531 return tool
->GetShortHelp();
534 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
536 wxToolBarToolBase
*tool
= FindById(id
);
537 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
539 return tool
->GetLongHelp();
542 // ----------------------------------------------------------------------------
543 // wxToolBarBase geometry
544 // ----------------------------------------------------------------------------
546 void wxToolBarBase::SetMargins(int x
, int y
)
552 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
557 // ----------------------------------------------------------------------------
559 // ----------------------------------------------------------------------------
561 // Only allow toggle if returns true
562 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
564 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
565 event
.SetEventObject(this);
567 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
568 event
.SetInt((int)toggleDown
);
570 // and SetExtraLong() for backwards compatibility
571 event
.SetExtraLong((long)toggleDown
);
573 // Send events to this toolbar instead (and thence up the window hierarchy)
574 GetEventHandler()->ProcessEvent(event
);
579 // Call when right button down.
580 void wxToolBarBase::OnRightClick(int id
,
584 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
585 event
.SetEventObject(this);
588 GetEventHandler()->ProcessEvent(event
);
591 // Called when the mouse cursor enters a tool bitmap (no button pressed).
592 // Argument is -1 if mouse is exiting the toolbar.
593 // Note that for this event, the id of the window is used,
594 // and the integer parameter of wxCommandEvent is used to retrieve
596 void wxToolBarBase::OnMouseEnter(int id
)
598 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
599 event
.SetEventObject(this);
602 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
605 wxToolBarToolBase
* tool
= id
== -1 ? (wxToolBarToolBase
*)0 : FindById(id
);
606 wxString help
= tool
? tool
->GetLongHelp() : wxString();
607 frame
->DoGiveHelp( help
, id
!= -1 );
610 (void)GetEventHandler()->ProcessEvent(event
);
613 // ----------------------------------------------------------------------------
615 // ----------------------------------------------------------------------------
617 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
618 void wxToolBarBase::UpdateWindowUI(long flags
)
620 wxWindowBase::UpdateWindowUI(flags
);
622 wxEvtHandler
* evtHandler
= GetEventHandler() ;
624 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
626 node
= node
->GetNext() )
628 int id
= node
->GetData()->GetId();
630 wxUpdateUIEvent
event(id
);
631 event
.SetEventObject(this);
633 if ( evtHandler
->ProcessEvent(event
) )
635 if ( event
.GetSetEnabled() )
636 EnableTool(id
, event
.GetEnabled());
637 if ( event
.GetSetChecked() )
638 ToggleTool(id
, event
.GetChecked());
640 if ( event
.GetSetText() )
647 // Helper function, used by wxCreateGreyedImage
649 static void wxGreyOutImage( const wxImage
& src
,
651 const wxColour
& darkCol
,
652 const wxColour
& lightCol
,
653 const wxColour
& bgCol
)
655 // Second attempt, just making things monochrome
656 int width
= src
.GetWidth();
657 int height
= src
.GetHeight();
659 int redCur
, greenCur
, blueCur
;
660 for ( int x
= 0; x
< width
; x
++ )
662 for ( int y
= 1; y
< height
; y
++ )
664 redCur
= src
.GetRed(x
, y
);
665 greenCur
= src
.GetGreen(x
, y
);
666 blueCur
= src
.GetBlue(x
, y
);
668 // Change light things to the background colour
669 if ( redCur
>= (lightCol
.Red() - 50) && greenCur
>= (lightCol
.Green() - 50) && blueCur
>= (lightCol
.Blue() - 50) )
671 dest
.SetRGB(x
,y
, bgCol
.Red(), bgCol
.Green(), bgCol
.Blue());
673 else if ( redCur
== bgCol
.Red() && greenCur
== bgCol
.Green() && blueCur
== bgCol
.Blue() )
675 // Leave the background colour as-is
676 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
678 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
680 // Change dark things to really dark
681 dest
.SetRGB(x
,y
, darkCol
.Red(), darkCol
.Green(), darkCol
.Blue());
688 * Make a greyed-out image suitable for disabled buttons.
689 * This code is adapted from wxNewBitmapButton in FL.
692 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
696 // assuming the pixels along the edges are of the background color
697 wxColour
bgCol(in
.GetRed(0, 0), in
.GetGreen(0, 0), in
.GetBlue(0, 0));
699 wxColour darkCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) ;
700 wxColour lightCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
) ;
702 wxGreyOutImage(in
, out
, darkCol
, lightCol
, bgCol
);
707 #endif // wxUSE_TOOLBAR