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 wxASSERT_MSG( m_isToggle
, _T("can't toggle this tool") );
79 if ( m_toggled
== toggle
)
87 bool wxToolBarToolBase::SetToggle(bool toggle
)
89 if ( m_isToggle
== toggle
)
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::AddTool(int id
,
136 const wxBitmap
& bitmap
,
137 const wxBitmap
& pushedBitmap
,
139 wxCoord
WXUNUSED(xPos
),
140 wxCoord
WXUNUSED(yPos
),
141 wxObject
*clientData
,
142 const wxString
& helpString1
,
143 const wxString
& helpString2
)
145 return InsertTool(GetToolsCount(), id
, bitmap
, pushedBitmap
,
146 toggle
, clientData
, helpString1
, helpString2
);
149 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
151 const wxBitmap
& bitmap
,
152 const wxBitmap
& pushedBitmap
,
154 wxObject
*clientData
,
155 const wxString
& helpString1
,
156 const wxString
& helpString2
)
158 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
159 _T("invalid position in wxToolBar::InsertTool()") );
161 wxToolBarToolBase
*tool
= CreateTool(id
, bitmap
, pushedBitmap
, toggle
,
162 clientData
, helpString1
, helpString2
);
164 if ( !tool
|| !DoInsertTool(pos
, tool
) )
171 m_tools
.Insert(pos
, tool
);
176 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
178 return InsertControl(GetToolsCount(), control
);
181 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
183 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
184 _T("toolbar: can't insert NULL control") );
186 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
187 _T("control must have toolbar as parent") );
189 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
190 _T("invalid position in wxToolBar::InsertControl()") );
192 wxToolBarToolBase
*tool
= CreateTool(control
);
194 if ( !tool
|| !DoInsertTool(pos
, tool
) )
201 m_tools
.Insert(pos
, tool
);
206 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
208 return InsertSeparator(GetToolsCount());
211 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
213 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
214 _T("invalid position in wxToolBar::InsertSeparator()") );
216 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
217 wxNullBitmap
, wxNullBitmap
,
218 FALSE
, (wxObject
*)NULL
,
219 wxEmptyString
, wxEmptyString
);
221 if ( !tool
|| !DoInsertTool(pos
, tool
) )
228 m_tools
.Insert(pos
, tool
);
233 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
236 wxToolBarToolsList::Node
*node
;
237 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
239 if ( node
->GetData()->GetId() == id
)
247 // don't give any error messages - sometimes we might call RemoveTool()
248 // without knowing whether the tool is or not in the toolbar
249 return (wxToolBarToolBase
*)NULL
;
252 wxToolBarToolBase
*tool
= node
->GetData();
253 if ( !DoDeleteTool(pos
, tool
) )
255 return (wxToolBarToolBase
*)NULL
;
258 // the node would delete the data, so set it to NULL to avoid this
261 m_tools
.DeleteNode(node
);
266 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
268 wxCHECK_MSG( pos
< GetToolsCount(), FALSE
,
269 _T("invalid position in wxToolBar::DeleteToolByPos()") );
271 wxToolBarToolsList::Node
*node
= m_tools
.Item(pos
);
273 if ( !DoDeleteTool(pos
, node
->GetData()) )
278 m_tools
.DeleteNode(node
);
283 bool wxToolBarBase::DeleteTool(int id
)
286 wxToolBarToolsList::Node
*node
;
287 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
289 if ( node
->GetData()->GetId() == id
)
295 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
300 m_tools
.DeleteNode(node
);
305 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
307 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
309 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
311 node
= node
->GetNext() )
313 tool
= node
->GetData();
314 if ( tool
->GetId() == id
)
326 void wxToolBarBase::ClearTools()
331 bool wxToolBarBase::Realize()
336 wxToolBarBase::~wxToolBarBase()
340 // ----------------------------------------------------------------------------
341 // wxToolBarBase tools state
342 // ----------------------------------------------------------------------------
344 void wxToolBarBase::EnableTool(int id
, bool enable
)
346 wxToolBarToolBase
*tool
= FindById(id
);
349 if ( tool
->Enable(enable
) )
351 DoEnableTool(tool
, enable
);
356 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
358 wxToolBarToolBase
*tool
= FindById(id
);
359 if ( tool
&& tool
->CanBeToggled() )
361 if ( tool
->Toggle(toggle
) )
363 DoToggleTool(tool
, toggle
);
368 void wxToolBarBase::SetToggle(int id
, bool toggle
)
370 wxToolBarToolBase
*tool
= FindById(id
);
373 if ( tool
->SetToggle(toggle
) )
375 DoSetToggle(tool
, toggle
);
380 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
382 wxToolBarToolBase
*tool
= FindById(id
);
385 (void)tool
->SetShortHelp(help
);
389 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
391 wxToolBarToolBase
*tool
= FindById(id
);
394 (void)tool
->SetLongHelp(help
);
398 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
400 wxToolBarToolBase
*tool
= FindById(id
);
402 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
405 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
407 wxToolBarToolBase
*tool
= FindById(id
);
409 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
411 tool
->SetClientData(clientData
);
414 bool wxToolBarBase::GetToolState(int id
) const
416 wxToolBarToolBase
*tool
= FindById(id
);
417 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
419 return tool
->IsToggled();
422 bool wxToolBarBase::GetToolEnabled(int id
) const
424 wxToolBarToolBase
*tool
= FindById(id
);
425 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
427 return tool
->IsEnabled();
430 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
432 wxToolBarToolBase
*tool
= FindById(id
);
433 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
435 return tool
->GetShortHelp();
438 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
440 wxToolBarToolBase
*tool
= FindById(id
);
441 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
443 return tool
->GetLongHelp();
446 // ----------------------------------------------------------------------------
447 // wxToolBarBase geometry
448 // ----------------------------------------------------------------------------
450 void wxToolBarBase::SetMargins(int x
, int y
)
456 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 // Only allow toggle if returns TRUE
466 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
468 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
469 event
.SetEventObject(this);
471 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
472 event
.SetInt((int)toggleDown
);
474 // and SetExtraLong() for backwards compatibility
475 event
.SetExtraLong((long)toggleDown
);
477 // Send events to this toolbar instead (and thence up the window hierarchy)
478 GetEventHandler()->ProcessEvent(event
);
483 // Call when right button down.
484 void wxToolBarBase::OnRightClick(int id
,
488 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
489 event
.SetEventObject(this);
492 GetEventHandler()->ProcessEvent(event
);
495 // Called when the mouse cursor enters a tool bitmap (no button pressed).
496 // Argument is -1 if mouse is exiting the toolbar.
497 // Note that for this event, the id of the window is used,
498 // and the integer parameter of wxCommandEvent is used to retrieve
500 void wxToolBarBase::OnMouseEnter(int id
)
502 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
503 event
.SetEventObject(this);
506 (void)GetEventHandler()->ProcessEvent(event
);
508 wxToolBarToolBase
*tool
= FindById(id
);
509 if ( !tool
|| !tool
->GetLongHelp() )
512 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
516 frame
->SetStatusText(tool
->GetLongHelp());
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
530 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
531 void wxToolBarBase::DoToolbarUpdates()
533 wxWindow
* parent
= this;
534 while (parent
->GetParent())
535 parent
= parent
->GetParent();
538 wxWindow
* focusWin
= wxFindFocusDescendant(parent
);
540 wxWindow
* focusWin
= (wxWindow
*) NULL
;
543 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler() ;
545 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
547 node
= node
->GetNext() )
549 int id
= node
->GetData()->GetId();
551 wxUpdateUIEvent
event(id
);
552 event
.SetEventObject(this);
554 if ( evtHandler
->ProcessEvent(event
) )
556 if ( event
.GetSetEnabled() )
557 EnableTool(id
, event
.GetEnabled());
558 if ( event
.GetSetChecked() )
559 ToggleTool(id
, event
.GetChecked());
561 if ( event
.GetSetText() )
568 // Helper function, used by wxCreateGreyedImage
570 static void wxGreyOutImage( const wxImage
& src
,
572 const wxColour
& darkCol
,
573 const wxColour
& lightCol
,
574 const wxColour
& bgCol
)
576 // Second attempt, just making things monochrome
577 int width
= src
.GetWidth();
578 int height
= src
.GetHeight();
580 int redCur
, greenCur
, blueCur
;
581 for ( int x
= 0; x
< width
; x
++ )
583 for ( int y
= 1; y
< height
; y
++ )
585 redCur
= src
.GetRed(x
, y
);
586 greenCur
= src
.GetGreen(x
, y
);
587 blueCur
= src
.GetBlue(x
, y
);
589 // Change light things to the background colour
590 if ( redCur
>= (lightCol
.Red() - 50) && greenCur
>= (lightCol
.Green() - 50) && blueCur
>= (lightCol
.Blue() - 50) )
592 dest
.SetRGB(x
,y
, bgCol
.Red(), bgCol
.Green(), bgCol
.Blue());
594 else if ( redCur
== bgCol
.Red() && greenCur
== bgCol
.Green() && blueCur
== bgCol
.Blue() )
596 // Leave the background colour as-is
597 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
599 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
601 // Change dark things to really dark
602 dest
.SetRGB(x
,y
, darkCol
.Red(), darkCol
.Green(), darkCol
.Blue());
609 * Make a greyed-out image suitable for disabled buttons.
610 * This code is adapted from wxNewBitmapButton in FL.
613 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
617 // assuming the pixels along the edges are of the background color
618 wxColour
bgCol(in
.GetRed(0, 0), in
.GetGreen(0, 0), in
.GetBlue(0, 0));
620 wxColour darkCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) ;
621 wxColour lightCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
) ;
623 wxGreyOutImage(in
, out
, darkCol
, lightCol
, bgCol
);
628 #endif // wxUSE_TOOLBAR