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 wxToolBarToolBase::~wxToolBarToolBase()
121 // ----------------------------------------------------------------------------
122 // wxToolBarBase adding/deleting items
123 // ----------------------------------------------------------------------------
125 wxToolBarBase::wxToolBarBase()
127 // the list owns the pointers
128 m_xMargin
= m_yMargin
= 0;
130 m_maxRows
= m_maxCols
= 0;
133 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
134 const wxString
& label
,
135 const wxBitmap
& bitmap
,
136 const wxBitmap
& bmpDisabled
,
138 const wxString
& shortHelp
,
139 const wxString
& longHelp
,
140 wxObject
*clientData
,
141 wxCoord
WXUNUSED(xPos
),
142 wxCoord
WXUNUSED(yPos
))
144 InvalidateBestSize();
145 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
146 kind
, shortHelp
, longHelp
, clientData
);
149 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
151 const wxString
& label
,
152 const wxBitmap
& bitmap
,
153 const wxBitmap
& bmpDisabled
,
155 const wxString
& shortHelp
,
156 const wxString
& longHelp
,
157 wxObject
*clientData
)
159 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
160 _T("invalid position in wxToolBar::InsertTool()") );
162 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
163 clientData
, shortHelp
, longHelp
);
165 if ( !InsertTool(pos
, tool
) )
175 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
177 return InsertTool(GetToolsCount(), tool
);
181 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
183 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
184 _T("invalid position in wxToolBar::InsertTool()") );
186 if ( !tool
|| !DoInsertTool(pos
, tool
) )
191 m_tools
.Insert(pos
, tool
);
196 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
198 return InsertControl(GetToolsCount(), control
);
201 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
203 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
204 _T("toolbar: can't insert NULL control") );
206 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
207 _T("control must have toolbar as parent") );
209 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
210 _T("invalid position in wxToolBar::InsertControl()") );
212 wxToolBarToolBase
*tool
= CreateTool(control
);
214 if ( !InsertTool(pos
, tool
) )
224 wxControl
*wxToolBarBase::FindControl( int id
)
226 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
228 node
= node
->GetNext() )
230 const wxToolBarToolBase
* const tool
= node
->GetData();
231 if ( tool
->IsControl() )
233 wxControl
* const control
= tool
->GetControl();
237 wxFAIL_MSG( _T("NULL control in toolbar?") );
239 else if ( control
->GetId() == id
)
250 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
252 return InsertSeparator(GetToolsCount());
255 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
257 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
258 _T("invalid position in wxToolBar::InsertSeparator()") );
260 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
262 wxNullBitmap
, wxNullBitmap
,
263 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
264 wxEmptyString
, wxEmptyString
);
266 if ( !tool
|| !DoInsertTool(pos
, tool
) )
273 m_tools
.Insert(pos
, tool
);
278 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
281 wxToolBarToolsList::compatibility_iterator node
;
282 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
284 if ( node
->GetData()->GetId() == id
)
292 // don't give any error messages - sometimes we might call RemoveTool()
293 // without knowing whether the tool is or not in the toolbar
294 return (wxToolBarToolBase
*)NULL
;
297 wxToolBarToolBase
*tool
= node
->GetData();
298 if ( !DoDeleteTool(pos
, tool
) )
300 return (wxToolBarToolBase
*)NULL
;
308 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
310 wxCHECK_MSG( pos
< GetToolsCount(), false,
311 _T("invalid position in wxToolBar::DeleteToolByPos()") );
313 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item(pos
);
315 if ( !DoDeleteTool(pos
, node
->GetData()) )
320 delete node
->GetData();
326 bool wxToolBarBase::DeleteTool(int id
)
329 wxToolBarToolsList::compatibility_iterator node
;
330 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
332 if ( node
->GetData()->GetId() == id
)
338 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
343 delete node
->GetData();
349 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
351 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
353 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
355 node
= node
->GetNext() )
357 tool
= node
->GetData();
358 if ( tool
->GetId() == id
)
370 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase
*tool
)
372 wxCHECK_RET( tool
, _T("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
374 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
377 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Find(tool
);
378 wxCHECK_RET( node
, _T("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
380 wxToolBarToolsList::compatibility_iterator nodeNext
= node
->GetNext();
383 wxToolBarToolBase
*tool
= nodeNext
->GetData();
385 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
388 if ( tool
->Toggle(false) )
390 DoToggleTool(tool
, false);
393 nodeNext
= nodeNext
->GetNext();
396 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
399 wxToolBarToolBase
*tool
= nodePrev
->GetData();
401 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
404 if ( tool
->Toggle(false) )
406 DoToggleTool(tool
, false);
409 nodePrev
= nodePrev
->GetPrevious();
413 void wxToolBarBase::ClearTools()
415 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
418 bool wxToolBarBase::Realize()
423 wxToolBarBase::~wxToolBarBase()
425 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
427 // notify the frame that it doesn't have a tool bar any longer to avoid
429 wxFrameBase
*frame
= wxDynamicCast(GetParent(), wxFrameBase
);
430 if ( frame
&& frame
->GetToolBar() == this )
432 frame
->SetToolBar(NULL
);
436 // ----------------------------------------------------------------------------
437 // wxToolBarBase tools state
438 // ----------------------------------------------------------------------------
440 void wxToolBarBase::EnableTool(int id
, bool enable
)
442 wxToolBarToolBase
*tool
= FindById(id
);
445 if ( tool
->Enable(enable
) )
447 DoEnableTool(tool
, enable
);
452 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
454 wxToolBarToolBase
*tool
= FindById(id
);
455 if ( tool
&& tool
->CanBeToggled() )
457 if ( tool
->Toggle(toggle
) )
459 UnToggleRadioGroup(tool
);
460 DoToggleTool(tool
, toggle
);
465 void wxToolBarBase::SetToggle(int id
, bool toggle
)
467 wxToolBarToolBase
*tool
= FindById(id
);
470 if ( tool
->SetToggle(toggle
) )
472 DoSetToggle(tool
, toggle
);
477 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
479 wxToolBarToolBase
*tool
= FindById(id
);
482 (void)tool
->SetShortHelp(help
);
486 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
488 wxToolBarToolBase
*tool
= FindById(id
);
491 (void)tool
->SetLongHelp(help
);
495 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
497 wxToolBarToolBase
*tool
= FindById(id
);
499 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
502 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
504 wxToolBarToolBase
*tool
= FindById(id
);
506 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
508 tool
->SetClientData(clientData
);
511 int wxToolBarBase::GetToolPos(int id
) const
514 wxToolBarToolsList::compatibility_iterator node
;
516 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
518 if ( node
->GetData()->GetId() == id
)
527 bool wxToolBarBase::GetToolState(int id
) const
529 wxToolBarToolBase
*tool
= FindById(id
);
530 wxCHECK_MSG( tool
, false, _T("no such tool") );
532 return tool
->IsToggled();
535 bool wxToolBarBase::GetToolEnabled(int id
) const
537 wxToolBarToolBase
*tool
= FindById(id
);
538 wxCHECK_MSG( tool
, false, _T("no such tool") );
540 return tool
->IsEnabled();
543 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
545 wxToolBarToolBase
*tool
= FindById(id
);
546 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
548 return tool
->GetShortHelp();
551 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
553 wxToolBarToolBase
*tool
= FindById(id
);
554 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
556 return tool
->GetLongHelp();
559 // ----------------------------------------------------------------------------
560 // wxToolBarBase geometry
561 // ----------------------------------------------------------------------------
563 void wxToolBarBase::SetMargins(int x
, int y
)
569 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 // Only allow toggle if returns true
579 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
581 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
582 event
.SetEventObject(this);
584 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
585 event
.SetInt((int)toggleDown
);
587 // and SetExtraLong() for backwards compatibility
588 event
.SetExtraLong((long)toggleDown
);
590 // Send events to this toolbar instead (and thence up the window hierarchy)
591 GetEventHandler()->ProcessEvent(event
);
596 // Call when right button down.
597 void wxToolBarBase::OnRightClick(int id
,
601 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
602 event
.SetEventObject(this);
605 GetEventHandler()->ProcessEvent(event
);
608 // Called when the mouse cursor enters a tool bitmap (no button pressed).
609 // Argument is wxID_ANY if mouse is exiting the toolbar.
610 // Note that for this event, the id of the window is used,
611 // and the integer parameter of wxCommandEvent is used to retrieve
613 void wxToolBarBase::OnMouseEnter(int id
)
615 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
616 event
.SetEventObject(this);
619 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
623 wxToolBarToolBase
* tool
= id
== wxID_ANY
? (wxToolBarToolBase
*)NULL
: FindById(id
);
625 help
= tool
->GetLongHelp();
626 frame
->DoGiveHelp( help
, id
!= wxID_ANY
);
629 (void)GetEventHandler()->ProcessEvent(event
);
632 // ----------------------------------------------------------------------------
634 // ----------------------------------------------------------------------------
636 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
637 void wxToolBarBase::UpdateWindowUI(long flags
)
639 wxWindowBase::UpdateWindowUI(flags
);
641 // There is no sense in updating the toolbar UI
642 // if the parent window is about to get destroyed
643 wxWindow
*tlw
= wxGetTopLevelParent( this );
644 if (tlw
&& wxPendingDelete
.Member( tlw
))
647 wxEvtHandler
* evtHandler
= GetEventHandler() ;
649 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
651 node
= node
->GetNext() )
653 int id
= node
->GetData()->GetId();
655 wxUpdateUIEvent
event(id
);
656 event
.SetEventObject(this);
658 if ( evtHandler
->ProcessEvent(event
) )
660 if ( event
.GetSetEnabled() )
661 EnableTool(id
, event
.GetEnabled());
662 if ( event
.GetSetChecked() )
663 ToggleTool(id
, event
.GetChecked());
665 if ( event
.GetSetText() )
672 // Helper function, used by wxCreateGreyedImage
674 static void wxGreyOutImage( const wxImage
& src
,
676 const wxColour
& darkCol
,
677 const wxColour
& lightCol
,
678 const wxColour
& bgCol
)
680 // Second attempt, just making things monochrome
681 int width
= src
.GetWidth();
682 int height
= src
.GetHeight();
684 int redCur
, greenCur
, blueCur
;
685 for ( int x
= 0; x
< width
; x
++ )
687 for ( int y
= 1; y
< height
; y
++ )
689 redCur
= src
.GetRed(x
, y
);
690 greenCur
= src
.GetGreen(x
, y
);
691 blueCur
= src
.GetBlue(x
, y
);
693 // Change light things to the background colour
694 if ( redCur
>= (lightCol
.Red() - 50) && greenCur
>= (lightCol
.Green() - 50) && blueCur
>= (lightCol
.Blue() - 50) )
696 dest
.SetRGB(x
,y
, bgCol
.Red(), bgCol
.Green(), bgCol
.Blue());
698 else if ( redCur
== bgCol
.Red() && greenCur
== bgCol
.Green() && blueCur
== bgCol
.Blue() )
700 // Leave the background colour as-is
701 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
703 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
705 // Change dark things to really dark
706 dest
.SetRGB(x
,y
, darkCol
.Red(), darkCol
.Green(), darkCol
.Blue());
713 * Make a greyed-out image suitable for disabled buttons.
714 * This code is adapted from wxNewBitmapButton in FL.
717 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
721 // assuming the pixels along the edges are of the background color
722 wxColour
bgCol(in
.GetRed(0, 0), in
.GetGreen(0, 0), in
.GetBlue(0, 0));
724 wxColour darkCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) ;
725 wxColour lightCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
) ;
727 wxGreyOutImage(in
, out
, darkCol
, lightCol
, bgCol
);
732 #endif // wxUSE_TOOLBAR