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 IMPLEMENT_CLASS(wxToolBarBase
, wxControl
)
49 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
52 #include "wx/listimpl.cpp"
54 WX_DEFINE_LIST(wxToolBarToolsList
);
56 // ============================================================================
58 // ============================================================================
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
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_tools
.DeleteContents(TRUE
);
130 m_xMargin
= m_yMargin
= 0;
132 m_maxRows
= m_maxCols
= 0;
135 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
136 const wxString
& label
,
137 const wxBitmap
& bitmap
,
138 const wxBitmap
& bmpDisabled
,
140 const wxString
& shortHelp
,
141 const wxString
& longHelp
,
142 wxObject
*clientData
,
143 wxCoord
WXUNUSED(xPos
),
144 wxCoord
WXUNUSED(yPos
))
146 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
147 kind
, shortHelp
, longHelp
, clientData
);
150 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
152 const wxString
& label
,
153 const wxBitmap
& bitmap
,
154 const wxBitmap
& bmpDisabled
,
156 const wxString
& shortHelp
,
157 const wxString
& longHelp
,
158 wxObject
*clientData
)
160 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
161 _T("invalid position in wxToolBar::InsertTool()") );
163 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
164 clientData
, shortHelp
, longHelp
);
166 if ( !InsertTool(pos
, tool
) )
176 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
178 return InsertTool(GetToolsCount(), tool
);
182 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
184 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
185 _T("invalid position in wxToolBar::InsertTool()") );
187 if ( !tool
|| !DoInsertTool(pos
, tool
) )
192 m_tools
.Insert(pos
, tool
);
197 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
199 return InsertControl(GetToolsCount(), control
);
202 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
204 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
205 _T("toolbar: can't insert NULL control") );
207 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
208 _T("control must have toolbar as parent") );
210 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
211 _T("invalid position in wxToolBar::InsertControl()") );
213 wxToolBarToolBase
*tool
= CreateTool(control
);
215 if ( !InsertTool(pos
, tool
) )
225 wxControl
*wxToolBarBase::FindControl( int id
)
227 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
229 node
= node
->GetNext() )
231 const wxToolBarToolBase
* const tool
= node
->GetData();
232 if ( tool
->IsControl() )
234 wxControl
* const control
= tool
->GetControl();
238 wxFAIL_MSG( _T("NULL control in toolbar?") );
240 else if ( control
->GetId() == id
)
251 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
253 return InsertSeparator(GetToolsCount());
256 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
258 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
259 _T("invalid position in wxToolBar::InsertSeparator()") );
261 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
263 wxNullBitmap
, wxNullBitmap
,
264 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
265 wxEmptyString
, wxEmptyString
);
267 if ( !tool
|| !DoInsertTool(pos
, tool
) )
274 m_tools
.Insert(pos
, tool
);
279 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
282 wxToolBarToolsList::Node
*node
;
283 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
285 if ( node
->GetData()->GetId() == id
)
293 // don't give any error messages - sometimes we might call RemoveTool()
294 // without knowing whether the tool is or not in the toolbar
295 return (wxToolBarToolBase
*)NULL
;
298 wxToolBarToolBase
*tool
= node
->GetData();
299 if ( !DoDeleteTool(pos
, tool
) )
301 return (wxToolBarToolBase
*)NULL
;
304 // the node would delete the data, so set it to NULL to avoid this
307 m_tools
.DeleteNode(node
);
312 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
314 wxCHECK_MSG( pos
< GetToolsCount(), FALSE
,
315 _T("invalid position in wxToolBar::DeleteToolByPos()") );
317 wxToolBarToolsList::Node
*node
= m_tools
.Item(pos
);
319 if ( !DoDeleteTool(pos
, node
->GetData()) )
324 m_tools
.DeleteNode(node
);
329 bool wxToolBarBase::DeleteTool(int id
)
332 wxToolBarToolsList::Node
*node
;
333 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
335 if ( node
->GetData()->GetId() == id
)
341 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
346 m_tools
.DeleteNode(node
);
351 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
353 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
355 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
357 node
= node
->GetNext() )
359 tool
= node
->GetData();
360 if ( tool
->GetId() == id
)
372 void wxToolBarBase::ClearTools()
377 bool wxToolBarBase::Realize()
382 wxToolBarBase::~wxToolBarBase()
386 // ----------------------------------------------------------------------------
387 // wxToolBarBase tools state
388 // ----------------------------------------------------------------------------
390 void wxToolBarBase::EnableTool(int id
, bool enable
)
392 wxToolBarToolBase
*tool
= FindById(id
);
395 if ( tool
->Enable(enable
) )
397 DoEnableTool(tool
, enable
);
402 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
404 wxToolBarToolBase
*tool
= FindById(id
);
405 if ( tool
&& tool
->CanBeToggled() )
407 if ( tool
->Toggle(toggle
) )
409 DoToggleTool(tool
, toggle
);
414 void wxToolBarBase::SetToggle(int id
, bool toggle
)
416 wxToolBarToolBase
*tool
= FindById(id
);
419 if ( tool
->SetToggle(toggle
) )
421 DoSetToggle(tool
, toggle
);
426 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
428 wxToolBarToolBase
*tool
= FindById(id
);
431 (void)tool
->SetShortHelp(help
);
435 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
437 wxToolBarToolBase
*tool
= FindById(id
);
440 (void)tool
->SetLongHelp(help
);
444 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
446 wxToolBarToolBase
*tool
= FindById(id
);
448 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
451 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
453 wxToolBarToolBase
*tool
= FindById(id
);
455 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
457 tool
->SetClientData(clientData
);
460 int wxToolBarBase::GetToolPos(int id
) const
463 wxToolBarToolsList::Node
*node
;
465 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
467 if ( node
->GetData()->GetId() == id
)
476 bool wxToolBarBase::GetToolState(int id
) const
478 wxToolBarToolBase
*tool
= FindById(id
);
479 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
481 return tool
->IsToggled();
484 bool wxToolBarBase::GetToolEnabled(int id
) const
486 wxToolBarToolBase
*tool
= FindById(id
);
487 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
489 return tool
->IsEnabled();
492 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
494 wxToolBarToolBase
*tool
= FindById(id
);
495 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
497 return tool
->GetShortHelp();
500 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
502 wxToolBarToolBase
*tool
= FindById(id
);
503 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
505 return tool
->GetLongHelp();
508 // ----------------------------------------------------------------------------
509 // wxToolBarBase geometry
510 // ----------------------------------------------------------------------------
512 void wxToolBarBase::SetMargins(int x
, int y
)
518 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
523 // ----------------------------------------------------------------------------
525 // ----------------------------------------------------------------------------
527 // Only allow toggle if returns TRUE
528 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
530 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
531 event
.SetEventObject(this);
533 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
534 event
.SetInt((int)toggleDown
);
536 // and SetExtraLong() for backwards compatibility
537 event
.SetExtraLong((long)toggleDown
);
539 // Send events to this toolbar instead (and thence up the window hierarchy)
540 GetEventHandler()->ProcessEvent(event
);
545 // Call when right button down.
546 void wxToolBarBase::OnRightClick(int id
,
550 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
551 event
.SetEventObject(this);
554 GetEventHandler()->ProcessEvent(event
);
557 // Called when the mouse cursor enters a tool bitmap (no button pressed).
558 // Argument is -1 if mouse is exiting the toolbar.
559 // Note that for this event, the id of the window is used,
560 // and the integer parameter of wxCommandEvent is used to retrieve
562 void wxToolBarBase::OnMouseEnter(int id
)
564 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
565 event
.SetEventObject(this);
568 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
571 wxToolBarToolBase
* tool
= id
== -1 ? (wxToolBarToolBase
*)0 : FindById(id
);
572 wxString help
= tool
? tool
->GetLongHelp() : wxString();
573 frame
->DoGiveHelp( help
, id
!= -1 );
576 (void)GetEventHandler()->ProcessEvent(event
);
579 // ----------------------------------------------------------------------------
581 // ----------------------------------------------------------------------------
583 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
584 void wxToolBarBase::UpdateWindowUI(long flags
)
586 wxWindowBase::UpdateWindowUI(flags
);
588 wxEvtHandler
* evtHandler
= GetEventHandler() ;
590 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
592 node
= node
->GetNext() )
594 int id
= node
->GetData()->GetId();
596 wxUpdateUIEvent
event(id
);
597 event
.SetEventObject(this);
599 if ( evtHandler
->ProcessEvent(event
) )
601 if ( event
.GetSetEnabled() )
602 EnableTool(id
, event
.GetEnabled());
603 if ( event
.GetSetChecked() )
604 ToggleTool(id
, event
.GetChecked());
606 if ( event
.GetSetText() )
613 // Helper function, used by wxCreateGreyedImage
615 static void wxGreyOutImage( const wxImage
& src
,
617 const wxColour
& darkCol
,
618 const wxColour
& lightCol
,
619 const wxColour
& bgCol
)
621 // Second attempt, just making things monochrome
622 int width
= src
.GetWidth();
623 int height
= src
.GetHeight();
625 int redCur
, greenCur
, blueCur
;
626 for ( int x
= 0; x
< width
; x
++ )
628 for ( int y
= 1; y
< height
; y
++ )
630 redCur
= src
.GetRed(x
, y
);
631 greenCur
= src
.GetGreen(x
, y
);
632 blueCur
= src
.GetBlue(x
, y
);
634 // Change light things to the background colour
635 if ( redCur
>= (lightCol
.Red() - 50) && greenCur
>= (lightCol
.Green() - 50) && blueCur
>= (lightCol
.Blue() - 50) )
637 dest
.SetRGB(x
,y
, bgCol
.Red(), bgCol
.Green(), bgCol
.Blue());
639 else if ( redCur
== bgCol
.Red() && greenCur
== bgCol
.Green() && blueCur
== bgCol
.Blue() )
641 // Leave the background colour as-is
642 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
644 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
646 // Change dark things to really dark
647 dest
.SetRGB(x
,y
, darkCol
.Red(), darkCol
.Green(), darkCol
.Blue());
654 * Make a greyed-out image suitable for disabled buttons.
655 * This code is adapted from wxNewBitmapButton in FL.
658 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
662 // assuming the pixels along the edges are of the background color
663 wxColour
bgCol(in
.GetRed(0, 0), in
.GetGreen(0, 0), in
.GetBlue(0, 0));
665 wxColour darkCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) ;
666 wxColour lightCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
) ;
668 wxGreyOutImage(in
, out
, darkCol
, lightCol
, bgCol
);
673 #endif // wxUSE_TOOLBAR