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 IMPLEMENT_DYNAMIC_CLASS(wxToolBarToolBase
, wxObject
)
64 bool wxToolBarToolBase::Enable(bool enable
)
66 if ( m_enabled
== enable
)
74 bool wxToolBarToolBase::Toggle(bool toggle
)
76 wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") );
78 if ( m_toggled
== toggle
)
86 bool wxToolBarToolBase::SetToggle(bool toggle
)
88 wxItemKind kind
= toggle
? wxITEM_CHECK
: wxITEM_NORMAL
;
97 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
99 if ( m_shortHelpString
== help
)
102 m_shortHelpString
= help
;
107 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
109 if ( m_longHelpString
== help
)
112 m_longHelpString
= help
;
117 #if WXWIN_COMPATIBILITY_2_2
119 const wxBitmap
& wxToolBarToolBase::GetBitmap1() const
121 return GetNormalBitmap();
124 const wxBitmap
& wxToolBarToolBase::GetBitmap2() const
126 return GetDisabledBitmap();
129 void wxToolBarToolBase::SetBitmap1(const wxBitmap
& bmp
)
131 SetNormalBitmap(bmp
);
134 void wxToolBarToolBase::SetBitmap2(const wxBitmap
& bmp
)
136 SetDisabledBitmap(bmp
);
139 #endif // WXWIN_COMPATIBILITY_2_2
141 // ----------------------------------------------------------------------------
142 // wxToolBarBase adding/deleting items
143 // ----------------------------------------------------------------------------
145 wxToolBarBase::wxToolBarBase()
147 // the list owns the pointers
148 m_xMargin
= m_yMargin
= 0;
150 m_maxRows
= m_maxCols
= 0;
153 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
154 const wxString
& label
,
155 const wxBitmap
& bitmap
,
156 const wxBitmap
& bmpDisabled
,
158 const wxString
& shortHelp
,
159 const wxString
& longHelp
,
160 wxObject
*clientData
,
161 wxCoord
WXUNUSED(xPos
),
162 wxCoord
WXUNUSED(yPos
))
164 InvalidateBestSize();
165 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
166 kind
, shortHelp
, longHelp
, clientData
);
169 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
171 const wxString
& label
,
172 const wxBitmap
& bitmap
,
173 const wxBitmap
& bmpDisabled
,
175 const wxString
& shortHelp
,
176 const wxString
& longHelp
,
177 wxObject
*clientData
)
179 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
180 _T("invalid position in wxToolBar::InsertTool()") );
182 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
183 clientData
, shortHelp
, longHelp
);
185 if ( !InsertTool(pos
, tool
) )
195 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
197 return InsertTool(GetToolsCount(), tool
);
201 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
203 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
204 _T("invalid position in wxToolBar::InsertTool()") );
206 if ( !tool
|| !DoInsertTool(pos
, tool
) )
211 m_tools
.Insert(pos
, tool
);
216 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
218 return InsertControl(GetToolsCount(), control
);
221 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
223 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
224 _T("toolbar: can't insert NULL control") );
226 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
227 _T("control must have toolbar as parent") );
229 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
230 _T("invalid position in wxToolBar::InsertControl()") );
232 wxToolBarToolBase
*tool
= CreateTool(control
);
234 if ( !InsertTool(pos
, tool
) )
244 wxControl
*wxToolBarBase::FindControl( int id
)
246 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
248 node
= node
->GetNext() )
250 const wxToolBarToolBase
* const tool
= node
->GetData();
251 if ( tool
->IsControl() )
253 wxControl
* const control
= tool
->GetControl();
257 wxFAIL_MSG( _T("NULL control in toolbar?") );
259 else if ( control
->GetId() == id
)
270 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
272 return InsertSeparator(GetToolsCount());
275 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
277 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
278 _T("invalid position in wxToolBar::InsertSeparator()") );
280 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
282 wxNullBitmap
, wxNullBitmap
,
283 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
284 wxEmptyString
, wxEmptyString
);
286 if ( !tool
|| !DoInsertTool(pos
, tool
) )
293 m_tools
.Insert(pos
, tool
);
298 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
301 wxToolBarToolsList::compatibility_iterator node
;
302 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
304 if ( node
->GetData()->GetId() == id
)
312 // don't give any error messages - sometimes we might call RemoveTool()
313 // without knowing whether the tool is or not in the toolbar
314 return (wxToolBarToolBase
*)NULL
;
317 wxToolBarToolBase
*tool
= node
->GetData();
318 if ( !DoDeleteTool(pos
, tool
) )
320 return (wxToolBarToolBase
*)NULL
;
328 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
330 wxCHECK_MSG( pos
< GetToolsCount(), false,
331 _T("invalid position in wxToolBar::DeleteToolByPos()") );
333 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item(pos
);
335 if ( !DoDeleteTool(pos
, node
->GetData()) )
340 delete node
->GetData();
346 bool wxToolBarBase::DeleteTool(int id
)
349 wxToolBarToolsList::compatibility_iterator node
;
350 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
352 if ( node
->GetData()->GetId() == id
)
358 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
363 delete node
->GetData();
369 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
371 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
373 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
375 node
= node
->GetNext() )
377 tool
= node
->GetData();
378 if ( tool
->GetId() == id
)
390 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase
*tool
)
392 wxCHECK_RET( tool
, _T("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
394 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
397 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Find(tool
);
398 wxCHECK_RET( node
, _T("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
400 wxToolBarToolsList::compatibility_iterator nodeNext
= node
->GetNext();
403 wxToolBarToolBase
*tool
= nodeNext
->GetData();
405 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
408 if ( tool
->Toggle(false) )
410 DoToggleTool(tool
, false);
413 nodeNext
= nodeNext
->GetNext();
416 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
419 wxToolBarToolBase
*tool
= nodePrev
->GetData();
421 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
424 if ( tool
->Toggle(false) )
426 DoToggleTool(tool
, false);
429 nodePrev
= nodePrev
->GetPrevious();
433 void wxToolBarBase::ClearTools()
435 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
438 bool wxToolBarBase::Realize()
443 wxToolBarBase::~wxToolBarBase()
445 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
447 // notify the frame that it doesn't have a tool bar any longer to avoid
449 wxFrameBase
*frame
= wxDynamicCast(GetParent(), wxFrameBase
);
450 if ( frame
&& frame
->GetToolBar() == this )
452 frame
->SetToolBar(NULL
);
456 // ----------------------------------------------------------------------------
457 // wxToolBarBase tools state
458 // ----------------------------------------------------------------------------
460 void wxToolBarBase::EnableTool(int id
, bool enable
)
462 wxToolBarToolBase
*tool
= FindById(id
);
465 if ( tool
->Enable(enable
) )
467 DoEnableTool(tool
, enable
);
472 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
474 wxToolBarToolBase
*tool
= FindById(id
);
475 if ( tool
&& tool
->CanBeToggled() )
477 if ( tool
->Toggle(toggle
) )
479 UnToggleRadioGroup(tool
);
480 DoToggleTool(tool
, toggle
);
485 void wxToolBarBase::SetToggle(int id
, bool toggle
)
487 wxToolBarToolBase
*tool
= FindById(id
);
490 if ( tool
->SetToggle(toggle
) )
492 DoSetToggle(tool
, toggle
);
497 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
499 wxToolBarToolBase
*tool
= FindById(id
);
502 (void)tool
->SetShortHelp(help
);
506 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
508 wxToolBarToolBase
*tool
= FindById(id
);
511 (void)tool
->SetLongHelp(help
);
515 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
517 wxToolBarToolBase
*tool
= FindById(id
);
519 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
522 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
524 wxToolBarToolBase
*tool
= FindById(id
);
526 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
528 tool
->SetClientData(clientData
);
531 int wxToolBarBase::GetToolPos(int id
) const
534 wxToolBarToolsList::compatibility_iterator node
;
536 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
538 if ( node
->GetData()->GetId() == id
)
547 bool wxToolBarBase::GetToolState(int id
) const
549 wxToolBarToolBase
*tool
= FindById(id
);
550 wxCHECK_MSG( tool
, false, _T("no such tool") );
552 return tool
->IsToggled();
555 bool wxToolBarBase::GetToolEnabled(int id
) const
557 wxToolBarToolBase
*tool
= FindById(id
);
558 wxCHECK_MSG( tool
, false, _T("no such tool") );
560 return tool
->IsEnabled();
563 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
565 wxToolBarToolBase
*tool
= FindById(id
);
566 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
568 return tool
->GetShortHelp();
571 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
573 wxToolBarToolBase
*tool
= FindById(id
);
574 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
576 return tool
->GetLongHelp();
579 // ----------------------------------------------------------------------------
580 // wxToolBarBase geometry
581 // ----------------------------------------------------------------------------
583 void wxToolBarBase::SetMargins(int x
, int y
)
589 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
594 // ----------------------------------------------------------------------------
596 // ----------------------------------------------------------------------------
598 // Only allow toggle if returns true
599 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
601 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
602 event
.SetEventObject(this);
604 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
605 event
.SetInt((int)toggleDown
);
607 // and SetExtraLong() for backwards compatibility
608 event
.SetExtraLong((long)toggleDown
);
610 // Send events to this toolbar instead (and thence up the window hierarchy)
611 GetEventHandler()->ProcessEvent(event
);
616 // Call when right button down.
617 void wxToolBarBase::OnRightClick(int id
,
621 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
622 event
.SetEventObject(this);
625 GetEventHandler()->ProcessEvent(event
);
628 // Called when the mouse cursor enters a tool bitmap (no button pressed).
629 // Argument is wxID_ANY if mouse is exiting the toolbar.
630 // Note that for this event, the id of the window is used,
631 // and the integer parameter of wxCommandEvent is used to retrieve
633 void wxToolBarBase::OnMouseEnter(int id
)
635 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
636 event
.SetEventObject(this);
639 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
643 wxToolBarToolBase
* tool
= id
== wxID_ANY
? (wxToolBarToolBase
*)NULL
: FindById(id
);
645 help
= tool
->GetLongHelp();
646 frame
->DoGiveHelp( help
, id
!= wxID_ANY
);
649 (void)GetEventHandler()->ProcessEvent(event
);
652 // ----------------------------------------------------------------------------
654 // ----------------------------------------------------------------------------
656 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
657 void wxToolBarBase::UpdateWindowUI(long flags
)
659 wxWindowBase::UpdateWindowUI(flags
);
661 // There is no sense in updating the toolbar UI
662 // if the parent window is about to get destroyed
663 wxWindow
*tlw
= wxGetTopLevelParent( this );
664 if (tlw
&& wxPendingDelete
.Member( tlw
))
667 wxEvtHandler
* evtHandler
= GetEventHandler() ;
669 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
671 node
= node
->GetNext() )
673 int id
= node
->GetData()->GetId();
675 wxUpdateUIEvent
event(id
);
676 event
.SetEventObject(this);
678 if ( evtHandler
->ProcessEvent(event
) )
680 if ( event
.GetSetEnabled() )
681 EnableTool(id
, event
.GetEnabled());
682 if ( event
.GetSetChecked() )
683 ToggleTool(id
, event
.GetChecked());
685 if ( event
.GetSetText() )
692 // Helper function, used by wxCreateGreyedImage
694 static void wxGreyOutImage( const wxImage
& src
,
696 const wxColour
& darkCol
,
697 const wxColour
& lightCol
,
698 const wxColour
& bgCol
)
700 // Second attempt, just making things monochrome
701 int width
= src
.GetWidth();
702 int height
= src
.GetHeight();
704 int redCur
, greenCur
, blueCur
;
705 for ( int x
= 0; x
< width
; x
++ )
707 for ( int y
= 1; y
< height
; y
++ )
709 redCur
= src
.GetRed(x
, y
);
710 greenCur
= src
.GetGreen(x
, y
);
711 blueCur
= src
.GetBlue(x
, y
);
713 // Change light things to the background colour
714 if ( redCur
>= (lightCol
.Red() - 50) && greenCur
>= (lightCol
.Green() - 50) && blueCur
>= (lightCol
.Blue() - 50) )
716 dest
.SetRGB(x
,y
, bgCol
.Red(), bgCol
.Green(), bgCol
.Blue());
718 else if ( redCur
== bgCol
.Red() && greenCur
== bgCol
.Green() && blueCur
== bgCol
.Blue() )
720 // Leave the background colour as-is
721 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
723 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
725 // Change dark things to really dark
726 dest
.SetRGB(x
,y
, darkCol
.Red(), darkCol
.Green(), darkCol
.Blue());
733 * Make a greyed-out image suitable for disabled buttons.
734 * This code is adapted from wxNewBitmapButton in FL.
737 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
741 // assuming the pixels along the edges are of the background color
742 wxColour
bgCol(in
.GetRed(0, 0), in
.GetGreen(0, 0), in
.GetBlue(0, 0));
744 wxColour darkCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) ;
745 wxColour lightCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
) ;
747 wxGreyOutImage(in
, out
, darkCol
, lightCol
, bgCol
);
752 #endif // wxUSE_TOOLBAR