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( CanBeToggled(), _T("can't toggle this tool") );
79 if ( m_toggled
== toggle
)
87 bool wxToolBarToolBase::SetToggle(bool toggle
)
89 wxItemKind kind
= toggle
? wxITEM_CHECK
: wxITEM_NORMAL
;
98 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
100 if ( m_shortHelpString
== help
)
103 m_shortHelpString
= help
;
108 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
110 if ( m_longHelpString
== help
)
113 m_longHelpString
= help
;
118 wxToolBarToolBase::~wxToolBarToolBase()
122 // ----------------------------------------------------------------------------
123 // wxToolBarBase adding/deleting items
124 // ----------------------------------------------------------------------------
126 wxToolBarBase::wxToolBarBase()
128 // the list owns the pointers
129 m_tools
.DeleteContents(TRUE
);
131 m_xMargin
= m_yMargin
= 0;
133 m_maxRows
= m_maxCols
= 0;
136 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
137 const wxString
& label
,
138 const wxBitmap
& bitmap
,
139 const wxBitmap
& bmpDisabled
,
141 const wxString
& shortHelp
,
142 const wxString
& longHelp
,
143 wxObject
*clientData
,
144 wxCoord
WXUNUSED(xPos
),
145 wxCoord
WXUNUSED(yPos
))
147 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
148 kind
, shortHelp
, longHelp
, clientData
);
151 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
153 const wxString
& label
,
154 const wxBitmap
& bitmap
,
155 const wxBitmap
& bmpDisabled
,
157 const wxString
& shortHelp
,
158 const wxString
& longHelp
,
159 wxObject
*clientData
)
161 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
162 _T("invalid position in wxToolBar::InsertTool()") );
164 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
165 clientData
, shortHelp
, longHelp
);
167 if ( !tool
|| !DoInsertTool(pos
, tool
) )
174 m_tools
.Insert(pos
, tool
);
179 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
181 return InsertControl(GetToolsCount(), control
);
184 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
186 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
187 _T("toolbar: can't insert NULL control") );
189 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
190 _T("control must have toolbar as parent") );
192 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
193 _T("invalid position in wxToolBar::InsertControl()") );
195 wxToolBarToolBase
*tool
= CreateTool(control
);
197 if ( !tool
|| !DoInsertTool(pos
, tool
) )
204 m_tools
.Insert(pos
, tool
);
209 wxControl
*wxToolBarBase::FindControl( int id
)
211 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
213 node
= node
->GetNext() )
215 wxControl
*control
= node
->GetData()->GetControl();
219 if (control
->GetId() == id
)
227 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
229 return InsertSeparator(GetToolsCount());
232 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
234 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
235 _T("invalid position in wxToolBar::InsertSeparator()") );
237 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
239 wxNullBitmap
, wxNullBitmap
,
240 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
241 wxEmptyString
, wxEmptyString
);
243 if ( !tool
|| !DoInsertTool(pos
, tool
) )
250 m_tools
.Insert(pos
, tool
);
255 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
258 wxToolBarToolsList::Node
*node
;
259 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
261 if ( node
->GetData()->GetId() == id
)
269 // don't give any error messages - sometimes we might call RemoveTool()
270 // without knowing whether the tool is or not in the toolbar
271 return (wxToolBarToolBase
*)NULL
;
274 wxToolBarToolBase
*tool
= node
->GetData();
275 if ( !DoDeleteTool(pos
, tool
) )
277 return (wxToolBarToolBase
*)NULL
;
280 // the node would delete the data, so set it to NULL to avoid this
283 m_tools
.DeleteNode(node
);
288 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
290 wxCHECK_MSG( pos
< GetToolsCount(), FALSE
,
291 _T("invalid position in wxToolBar::DeleteToolByPos()") );
293 wxToolBarToolsList::Node
*node
= m_tools
.Item(pos
);
295 if ( !DoDeleteTool(pos
, node
->GetData()) )
300 m_tools
.DeleteNode(node
);
305 bool wxToolBarBase::DeleteTool(int id
)
308 wxToolBarToolsList::Node
*node
;
309 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
311 if ( node
->GetData()->GetId() == id
)
317 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
322 m_tools
.DeleteNode(node
);
327 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
329 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
331 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
333 node
= node
->GetNext() )
335 tool
= node
->GetData();
336 if ( tool
->GetId() == id
)
348 void wxToolBarBase::ClearTools()
353 bool wxToolBarBase::Realize()
358 wxToolBarBase::~wxToolBarBase()
362 // ----------------------------------------------------------------------------
363 // wxToolBarBase tools state
364 // ----------------------------------------------------------------------------
366 void wxToolBarBase::EnableTool(int id
, bool enable
)
368 wxToolBarToolBase
*tool
= FindById(id
);
371 if ( tool
->Enable(enable
) )
373 DoEnableTool(tool
, enable
);
378 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
380 wxToolBarToolBase
*tool
= FindById(id
);
381 if ( tool
&& tool
->CanBeToggled() )
383 if ( tool
->Toggle(toggle
) )
385 DoToggleTool(tool
, toggle
);
390 void wxToolBarBase::SetToggle(int id
, bool toggle
)
392 wxToolBarToolBase
*tool
= FindById(id
);
395 if ( tool
->SetToggle(toggle
) )
397 DoSetToggle(tool
, toggle
);
402 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
404 wxToolBarToolBase
*tool
= FindById(id
);
407 (void)tool
->SetShortHelp(help
);
411 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
413 wxToolBarToolBase
*tool
= FindById(id
);
416 (void)tool
->SetLongHelp(help
);
420 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
422 wxToolBarToolBase
*tool
= FindById(id
);
424 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
427 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
429 wxToolBarToolBase
*tool
= FindById(id
);
431 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
433 tool
->SetClientData(clientData
);
436 bool wxToolBarBase::GetToolState(int id
) const
438 wxToolBarToolBase
*tool
= FindById(id
);
439 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
441 return tool
->IsToggled();
444 bool wxToolBarBase::GetToolEnabled(int id
) const
446 wxToolBarToolBase
*tool
= FindById(id
);
447 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
449 return tool
->IsEnabled();
452 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
454 wxToolBarToolBase
*tool
= FindById(id
);
455 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
457 return tool
->GetShortHelp();
460 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
462 wxToolBarToolBase
*tool
= FindById(id
);
463 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
465 return tool
->GetLongHelp();
468 // ----------------------------------------------------------------------------
469 // wxToolBarBase geometry
470 // ----------------------------------------------------------------------------
472 void wxToolBarBase::SetMargins(int x
, int y
)
478 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
487 // Only allow toggle if returns TRUE
488 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
490 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
491 event
.SetEventObject(this);
493 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
494 event
.SetInt((int)toggleDown
);
496 // and SetExtraLong() for backwards compatibility
497 event
.SetExtraLong((long)toggleDown
);
499 // Send events to this toolbar instead (and thence up the window hierarchy)
500 GetEventHandler()->ProcessEvent(event
);
505 // Call when right button down.
506 void wxToolBarBase::OnRightClick(int id
,
510 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
511 event
.SetEventObject(this);
514 GetEventHandler()->ProcessEvent(event
);
517 // Called when the mouse cursor enters a tool bitmap (no button pressed).
518 // Argument is -1 if mouse is exiting the toolbar.
519 // Note that for this event, the id of the window is used,
520 // and the integer parameter of wxCommandEvent is used to retrieve
522 void wxToolBarBase::OnMouseEnter(int id
)
524 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
525 event
.SetEventObject(this);
528 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
531 wxToolBarToolBase
* tool
= id
== -1 ? (wxToolBarToolBase
*)0 : FindById(id
);
532 wxString help
= tool
? tool
->GetLongHelp() : wxString();
533 frame
->DoGiveHelp( help
, id
!= -1 );
536 (void)GetEventHandler()->ProcessEvent(event
);
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
550 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
551 void wxToolBarBase::DoToolbarUpdates()
553 wxWindow
* parent
= this;
554 while (parent
->GetParent())
555 parent
= parent
->GetParent();
557 // This kind of #ifdef is a good way to annoy people. It breaks
558 // apps, but only on one platform and due to a hack in officially
559 // platform independent code. It took me hours to fix this. RR.
562 // wxWindow* focusWin = wxFindFocusDescendant(parent);
564 wxWindow
* focusWin
= (wxWindow
*) NULL
;
567 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler() ;
569 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
571 node
= node
->GetNext() )
573 int id
= node
->GetData()->GetId();
575 wxUpdateUIEvent
event(id
);
576 event
.SetEventObject(this);
578 if ( evtHandler
->ProcessEvent(event
) )
580 if ( event
.GetSetEnabled() )
581 EnableTool(id
, event
.GetEnabled());
582 if ( event
.GetSetChecked() )
583 ToggleTool(id
, event
.GetChecked());
585 if ( event
.GetSetText() )
592 // Helper function, used by wxCreateGreyedImage
594 static void wxGreyOutImage( const wxImage
& src
,
596 const wxColour
& darkCol
,
597 const wxColour
& lightCol
,
598 const wxColour
& bgCol
)
600 // Second attempt, just making things monochrome
601 int width
= src
.GetWidth();
602 int height
= src
.GetHeight();
604 int redCur
, greenCur
, blueCur
;
605 for ( int x
= 0; x
< width
; x
++ )
607 for ( int y
= 1; y
< height
; y
++ )
609 redCur
= src
.GetRed(x
, y
);
610 greenCur
= src
.GetGreen(x
, y
);
611 blueCur
= src
.GetBlue(x
, y
);
613 // Change light things to the background colour
614 if ( redCur
>= (lightCol
.Red() - 50) && greenCur
>= (lightCol
.Green() - 50) && blueCur
>= (lightCol
.Blue() - 50) )
616 dest
.SetRGB(x
,y
, bgCol
.Red(), bgCol
.Green(), bgCol
.Blue());
618 else if ( redCur
== bgCol
.Red() && greenCur
== bgCol
.Green() && blueCur
== bgCol
.Blue() )
620 // Leave the background colour as-is
621 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
623 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
625 // Change dark things to really dark
626 dest
.SetRGB(x
,y
, darkCol
.Red(), darkCol
.Green(), darkCol
.Blue());
633 * Make a greyed-out image suitable for disabled buttons.
634 * This code is adapted from wxNewBitmapButton in FL.
637 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
641 // assuming the pixels along the edges are of the background color
642 wxColour
bgCol(in
.GetRed(0, 0), in
.GetGreen(0, 0), in
.GetBlue(0, 0));
644 wxColour darkCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
) ;
645 wxColour lightCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
) ;
647 wxGreyOutImage(in
, out
, darkCol
, lightCol
, bgCol
);
652 #endif // wxUSE_TOOLBAR