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"
44 #include "wx/tbarbase.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_CLASS(wxToolBarBase
, wxControl
)
52 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
53 EVT_IDLE(wxToolBarBase::OnIdle
)
56 #include "wx/listimpl.cpp"
58 WX_DEFINE_LIST(wxToolBarToolsList
);
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 bool wxToolBarToolBase::Enable(bool enable
)
70 if ( m_enabled
== enable
)
78 bool wxToolBarToolBase::Toggle(bool toggle
)
80 wxASSERT_MSG( m_isToggle
, _T("can't toggle this tool") );
82 if ( m_toggled
== toggle
)
90 bool wxToolBarToolBase::SetToggle(bool toggle
)
92 if ( m_isToggle
== toggle
)
100 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
102 if ( m_shortHelpString
== help
)
105 m_shortHelpString
= help
;
110 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
112 if ( m_longHelpString
== help
)
115 m_longHelpString
= help
;
120 wxToolBarToolBase::~wxToolBarToolBase()
124 // ----------------------------------------------------------------------------
125 // wxToolBarBase adding/deleting items
126 // ----------------------------------------------------------------------------
128 wxToolBarBase::wxToolBarBase()
130 // the list owns the pointers
131 m_tools
.DeleteContents(TRUE
);
133 m_xMargin
= m_yMargin
= 0;
135 m_maxRows
= m_maxCols
= 0;
138 wxToolBarToolBase
*wxToolBarBase::AddTool(int id
,
139 const wxBitmap
& bitmap
,
140 const wxBitmap
& pushedBitmap
,
142 wxCoord
WXUNUSED(xPos
),
143 wxCoord
WXUNUSED(yPos
),
144 wxObject
*clientData
,
145 const wxString
& helpString1
,
146 const wxString
& helpString2
)
148 return InsertTool(GetToolsCount(), id
, bitmap
, pushedBitmap
,
149 toggle
, clientData
, helpString1
, helpString2
);
152 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
154 const wxBitmap
& bitmap
,
155 const wxBitmap
& pushedBitmap
,
157 wxObject
*clientData
,
158 const wxString
& helpString1
,
159 const wxString
& helpString2
)
161 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
162 _T("invalid position in wxToolBar::InsertTool()") );
164 wxToolBarToolBase
*tool
= CreateTool(id
, bitmap
, pushedBitmap
, toggle
,
165 clientData
, helpString1
, helpString2
);
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 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
211 return InsertSeparator(GetToolsCount());
214 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
216 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
217 _T("invalid position in wxToolBar::InsertSeparator()") );
219 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
220 wxNullBitmap
, wxNullBitmap
,
221 FALSE
, (wxObject
*)NULL
,
222 wxEmptyString
, wxEmptyString
);
224 if ( !tool
|| !DoInsertTool(pos
, tool
) )
231 m_tools
.Insert(pos
, tool
);
236 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
239 wxToolBarToolsList::Node
*node
;
240 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
242 if ( node
->GetData()->GetId() == id
)
250 // don't give any error messages - sometimes we might call RemoveTool()
251 // without knowing whether the tool is or not in the toolbar
252 return (wxToolBarToolBase
*)NULL
;
255 wxToolBarToolBase
*tool
= node
->GetData();
256 if ( !DoDeleteTool(pos
, tool
) )
258 return (wxToolBarToolBase
*)NULL
;
261 // the node would delete the data, so set it to NULL to avoid this
264 m_tools
.DeleteNode(node
);
269 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
271 wxCHECK_MSG( pos
< GetToolsCount(), FALSE
,
272 _T("invalid position in wxToolBar::DeleteToolByPos()") );
274 wxToolBarToolsList::Node
*node
= m_tools
.Item(pos
);
276 if ( !DoDeleteTool(pos
, node
->GetData()) )
281 m_tools
.DeleteNode(node
);
286 bool wxToolBarBase::DeleteTool(int id
)
289 wxToolBarToolsList::Node
*node
;
290 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
292 if ( node
->GetData()->GetId() == id
)
298 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
303 m_tools
.DeleteNode(node
);
308 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
310 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
312 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
314 node
= node
->GetNext() )
316 tool
= node
->GetData();
317 if ( tool
->GetId() == id
)
327 void wxToolBarBase::ClearTools()
332 bool wxToolBarBase::Realize()
337 wxToolBarBase::~wxToolBarBase()
341 // ----------------------------------------------------------------------------
342 // wxToolBarBase tools state
343 // ----------------------------------------------------------------------------
345 void wxToolBarBase::EnableTool(int id
, bool enable
)
347 wxToolBarToolBase
*tool
= FindById(id
);
350 if ( tool
->Enable(enable
) )
352 DoEnableTool(tool
, enable
);
357 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
359 wxToolBarToolBase
*tool
= FindById(id
);
360 if ( tool
&& tool
->CanBeToggled() )
362 if ( tool
->Toggle(toggle
) )
364 DoToggleTool(tool
, toggle
);
369 void wxToolBarBase::SetToggle(int id
, bool toggle
)
371 wxToolBarToolBase
*tool
= FindById(id
);
374 if ( tool
->SetToggle(toggle
) )
376 DoSetToggle(tool
, toggle
);
381 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
383 wxToolBarToolBase
*tool
= FindById(id
);
386 (void)tool
->SetShortHelp(help
);
390 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
392 wxToolBarToolBase
*tool
= FindById(id
);
395 (void)tool
->SetLongHelp(help
);
399 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
401 wxToolBarToolBase
*tool
= FindById(id
);
403 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
406 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
408 wxToolBarToolBase
*tool
= FindById(id
);
410 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
412 tool
->SetClientData(clientData
);
415 bool wxToolBarBase::GetToolState(int id
) const
417 wxToolBarToolBase
*tool
= FindById(id
);
418 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
420 return tool
->IsToggled();
423 bool wxToolBarBase::GetToolEnabled(int id
) const
425 wxToolBarToolBase
*tool
= FindById(id
);
426 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
428 return tool
->IsEnabled();
431 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
433 wxToolBarToolBase
*tool
= FindById(id
);
434 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
436 return tool
->GetShortHelp();
439 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
441 wxToolBarToolBase
*tool
= FindById(id
);
442 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
444 return tool
->GetLongHelp();
447 // ----------------------------------------------------------------------------
448 // wxToolBarBase geometry
449 // ----------------------------------------------------------------------------
451 void wxToolBarBase::SetMargins(int x
, int y
)
457 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
466 // Only allow toggle if returns TRUE
467 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
469 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
470 event
.SetEventObject(this);
472 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
473 event
.SetInt((int)toggleDown
);
475 // and SetExtraLong() for backwards compatibility
476 event
.SetExtraLong((long)toggleDown
);
478 // Send events to this toolbar instead (and thence up the window hierarchy)
479 GetEventHandler()->ProcessEvent(event
);
484 // Call when right button down.
485 void wxToolBarBase::OnRightClick(int id
,
489 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
490 event
.SetEventObject(this);
493 GetEventHandler()->ProcessEvent(event
);
496 // Called when the mouse cursor enters a tool bitmap (no button pressed).
497 // Argument is -1 if mouse is exiting the toolbar.
498 // Note that for this event, the id of the window is used,
499 // and the integer parameter of wxCommandEvent is used to retrieve
501 void wxToolBarBase::OnMouseEnter(int id
)
503 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
504 event
.SetEventObject(this);
507 (void)GetEventHandler()->ProcessEvent(event
);
509 wxToolBarToolBase
*tool
= FindById(id
);
510 if ( !tool
|| !tool
->GetLongHelp() )
513 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
517 frame
->SetStatusText(tool
->GetLongHelp());
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
524 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
531 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
532 void wxToolBarBase::DoToolbarUpdates()
534 wxWindow
* parent
= this;
535 while (parent
->GetParent())
536 parent
= parent
->GetParent();
539 wxWindow
* focusWin
= wxFindFocusDescendant(parent
);
541 wxWindow
* focusWin
= (wxWindow
*) NULL
;
544 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler() ;
546 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
548 node
= node
->GetNext() )
550 int id
= node
->GetData()->GetId();
552 wxUpdateUIEvent
event(id
);
553 event
.SetEventObject(this);
555 if ( evtHandler
->ProcessEvent(event
) )
557 if ( event
.GetSetEnabled() )
558 EnableTool(id
, event
.GetEnabled());
559 if ( event
.GetSetChecked() )
560 ToggleTool(id
, event
.GetChecked());
562 if ( event
.GetSetText() )
569 #endif // wxUSE_TOOLBAR