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 and Markus Holzem
9 // Licence: wxWindows license
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/tbarbase.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 IMPLEMENT_CLASS(wxToolBarBase
, wxControl
)
49 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
50 EVT_IDLE(wxToolBarBase::OnIdle
)
53 #include "wx/listimpl.cpp"
55 WX_DEFINE_LIST(wxToolBarToolsList
);
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 bool wxToolBarToolBase::Enable(bool enable
)
67 if ( m_enabled
== enable
)
75 bool wxToolBarToolBase::Toggle(bool toggle
)
77 // wxUniv toolbar toggles even non-checkable tools temporarily - should we
78 // change the code there or just allow doing it?
79 //wxASSERT_MSG( m_isToggle, _T("can't toggle this tool") );
81 if ( m_toggled
== toggle
)
89 bool wxToolBarToolBase::SetToggle(bool toggle
)
91 if ( m_isToggle
== toggle
)
99 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
101 if ( m_shortHelpString
== help
)
104 m_shortHelpString
= help
;
109 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
111 if ( m_longHelpString
== help
)
114 m_longHelpString
= help
;
119 wxToolBarToolBase::~wxToolBarToolBase()
123 // ----------------------------------------------------------------------------
124 // wxToolBarBase adding/deleting items
125 // ----------------------------------------------------------------------------
127 wxToolBarBase::wxToolBarBase()
129 // the list owns the pointers
130 m_tools
.DeleteContents(TRUE
);
132 m_xMargin
= m_yMargin
= 0;
134 m_maxRows
= m_maxCols
= 0;
137 wxToolBarToolBase
*wxToolBarBase::AddTool(int id
,
138 const wxBitmap
& bitmap
,
139 const wxBitmap
& pushedBitmap
,
141 wxCoord
WXUNUSED(xPos
),
142 wxCoord
WXUNUSED(yPos
),
143 wxObject
*clientData
,
144 const wxString
& helpString1
,
145 const wxString
& helpString2
)
147 return InsertTool(GetToolsCount(), id
, bitmap
, pushedBitmap
,
148 toggle
, clientData
, helpString1
, helpString2
);
151 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
153 const wxBitmap
& bitmap
,
154 const wxBitmap
& pushedBitmap
,
156 wxObject
*clientData
,
157 const wxString
& helpString1
,
158 const wxString
& helpString2
)
160 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
161 _T("invalid position in wxToolBar::InsertTool()") );
163 wxToolBarToolBase
*tool
= CreateTool(id
, bitmap
, pushedBitmap
, toggle
,
164 clientData
, helpString1
, helpString2
);
166 if ( !tool
|| !DoInsertTool(pos
, tool
) )
173 m_tools
.Insert(pos
, tool
);
178 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
180 return InsertControl(GetToolsCount(), control
);
183 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
185 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
186 _T("toolbar: can't insert NULL control") );
188 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
189 _T("control must have toolbar as parent") );
191 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
192 _T("invalid position in wxToolBar::InsertControl()") );
194 wxToolBarToolBase
*tool
= CreateTool(control
);
196 if ( !tool
|| !DoInsertTool(pos
, tool
) )
203 m_tools
.Insert(pos
, tool
);
208 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
210 return InsertSeparator(GetToolsCount());
213 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
215 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
216 _T("invalid position in wxToolBar::InsertSeparator()") );
218 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
219 wxNullBitmap
, wxNullBitmap
,
220 FALSE
, (wxObject
*)NULL
,
221 wxEmptyString
, wxEmptyString
);
223 if ( !tool
|| !DoInsertTool(pos
, tool
) )
230 m_tools
.Insert(pos
, tool
);
235 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
238 wxToolBarToolsList::Node
*node
;
239 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
241 if ( node
->GetData()->GetId() == id
)
249 // don't give any error messages - sometimes we might call RemoveTool()
250 // without knowing whether the tool is or not in the toolbar
251 return (wxToolBarToolBase
*)NULL
;
254 wxToolBarToolBase
*tool
= node
->GetData();
255 if ( !DoDeleteTool(pos
, tool
) )
257 return (wxToolBarToolBase
*)NULL
;
260 // the node would delete the data, so set it to NULL to avoid this
263 m_tools
.DeleteNode(node
);
268 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
270 wxCHECK_MSG( pos
< GetToolsCount(), FALSE
,
271 _T("invalid position in wxToolBar::DeleteToolByPos()") );
273 wxToolBarToolsList::Node
*node
= m_tools
.Item(pos
);
275 if ( !DoDeleteTool(pos
, node
->GetData()) )
280 m_tools
.DeleteNode(node
);
285 bool wxToolBarBase::DeleteTool(int id
)
288 wxToolBarToolsList::Node
*node
;
289 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
291 if ( node
->GetData()->GetId() == id
)
297 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
302 m_tools
.DeleteNode(node
);
307 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
309 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
311 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
313 node
= node
->GetNext() )
315 tool
= node
->GetData();
316 if ( tool
->GetId() == id
)
328 void wxToolBarBase::ClearTools()
333 bool wxToolBarBase::Realize()
338 wxToolBarBase::~wxToolBarBase()
342 // ----------------------------------------------------------------------------
343 // wxToolBarBase tools state
344 // ----------------------------------------------------------------------------
346 void wxToolBarBase::EnableTool(int id
, bool enable
)
348 wxToolBarToolBase
*tool
= FindById(id
);
351 if ( tool
->Enable(enable
) )
353 DoEnableTool(tool
, enable
);
358 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
360 wxToolBarToolBase
*tool
= FindById(id
);
361 if ( tool
&& tool
->CanBeToggled() )
363 if ( tool
->Toggle(toggle
) )
365 DoToggleTool(tool
, toggle
);
370 void wxToolBarBase::SetToggle(int id
, bool toggle
)
372 wxToolBarToolBase
*tool
= FindById(id
);
375 if ( tool
->SetToggle(toggle
) )
377 DoSetToggle(tool
, toggle
);
382 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
384 wxToolBarToolBase
*tool
= FindById(id
);
387 (void)tool
->SetShortHelp(help
);
391 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
393 wxToolBarToolBase
*tool
= FindById(id
);
396 (void)tool
->SetLongHelp(help
);
400 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
402 wxToolBarToolBase
*tool
= FindById(id
);
404 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
407 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
409 wxToolBarToolBase
*tool
= FindById(id
);
411 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
413 tool
->SetClientData(clientData
);
416 bool wxToolBarBase::GetToolState(int id
) const
418 wxToolBarToolBase
*tool
= FindById(id
);
419 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
421 return tool
->IsToggled();
424 bool wxToolBarBase::GetToolEnabled(int id
) const
426 wxToolBarToolBase
*tool
= FindById(id
);
427 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
429 return tool
->IsEnabled();
432 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
434 wxToolBarToolBase
*tool
= FindById(id
);
435 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
437 return tool
->GetShortHelp();
440 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
442 wxToolBarToolBase
*tool
= FindById(id
);
443 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
445 return tool
->GetLongHelp();
448 // ----------------------------------------------------------------------------
449 // wxToolBarBase geometry
450 // ----------------------------------------------------------------------------
452 void wxToolBarBase::SetMargins(int x
, int y
)
458 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 // Only allow toggle if returns TRUE
468 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
470 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
471 event
.SetEventObject(this);
473 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
474 event
.SetInt((int)toggleDown
);
476 // and SetExtraLong() for backwards compatibility
477 event
.SetExtraLong((long)toggleDown
);
479 // Send events to this toolbar instead (and thence up the window hierarchy)
480 GetEventHandler()->ProcessEvent(event
);
485 // Call when right button down.
486 void wxToolBarBase::OnRightClick(int id
,
490 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
491 event
.SetEventObject(this);
494 GetEventHandler()->ProcessEvent(event
);
497 // Called when the mouse cursor enters a tool bitmap (no button pressed).
498 // Argument is -1 if mouse is exiting the toolbar.
499 // Note that for this event, the id of the window is used,
500 // and the integer parameter of wxCommandEvent is used to retrieve
502 void wxToolBarBase::OnMouseEnter(int id
)
504 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
505 event
.SetEventObject(this);
508 (void)GetEventHandler()->ProcessEvent(event
);
510 wxToolBarToolBase
*tool
= FindById(id
);
511 if ( !tool
|| !tool
->GetLongHelp() )
514 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
518 frame
->SetStatusText(tool
->GetLongHelp());
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
525 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
532 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
533 void wxToolBarBase::DoToolbarUpdates()
535 wxWindow
* parent
= this;
536 while (parent
->GetParent())
537 parent
= parent
->GetParent();
540 wxWindow
* focusWin
= wxFindFocusDescendant(parent
);
542 wxWindow
* focusWin
= (wxWindow
*) NULL
;
545 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler() ;
547 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
549 node
= node
->GetNext() )
551 int id
= node
->GetData()->GetId();
553 wxUpdateUIEvent
event(id
);
554 event
.SetEventObject(this);
556 if ( evtHandler
->ProcessEvent(event
) )
558 if ( event
.GetSetEnabled() )
559 EnableTool(id
, event
.GetEnabled());
560 if ( event
.GetSetChecked() )
561 ToggleTool(id
, event
.GetChecked());
563 if ( event
.GetSetText() )
570 // Helper function, used by wxCreateGreyedImage
572 static void wxGreyOutImage( const wxImage
& src
,
574 const wxColour
& darkCol
,
575 const wxColour
& lightCol
,
576 const wxColour
& bgCol
)
578 // Second attempt, just making things monochrome
579 int width
= src
.GetWidth();
580 int height
= src
.GetHeight();
582 int redCur
, greenCur
, blueCur
;
583 for ( int x
= 0; x
< width
; x
++ )
585 for ( int y
= 1; y
< height
; y
++ )
587 redCur
= src
.GetRed(x
, y
);
588 greenCur
= src
.GetGreen(x
, y
);
589 blueCur
= src
.GetBlue(x
, y
);
591 // Change light things to the background colour
592 if ( redCur
>= (lightCol
.Red() - 50) && greenCur
>= (lightCol
.Green() - 50) && blueCur
>= (lightCol
.Blue() - 50) )
594 dest
.SetRGB(x
,y
, bgCol
.Red(), bgCol
.Green(), bgCol
.Blue());
596 else if ( redCur
== bgCol
.Red() && greenCur
== bgCol
.Green() && blueCur
== bgCol
.Blue() )
598 // Leave the background colour as-is
599 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
601 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
603 // Change dark things to really dark
604 dest
.SetRGB(x
,y
, darkCol
.Red(), darkCol
.Green(), darkCol
.Blue());
611 * Make a greyed-out image suitable for disabled buttons.
612 * This code is adapted from wxNewBitmapButton in FL.
615 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
619 // assuming the pixels along the edges are of the background color
620 wxColour
bgCol(in
.GetRed(0, 0), in
.GetGreen(0, 0), in
.GetBlue(0, 0));
622 wxColour darkCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) ;
623 wxColour lightCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
) ;
625 wxGreyOutImage(in
, out
, darkCol
, lightCol
, bgCol
);
630 #endif // wxUSE_TOOLBAR