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"
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
)
329 void wxToolBarBase::ClearTools()
334 bool wxToolBarBase::Realize()
339 wxToolBarBase::~wxToolBarBase()
343 // ----------------------------------------------------------------------------
344 // wxToolBarBase tools state
345 // ----------------------------------------------------------------------------
347 void wxToolBarBase::EnableTool(int id
, bool enable
)
349 wxToolBarToolBase
*tool
= FindById(id
);
352 if ( tool
->Enable(enable
) )
354 DoEnableTool(tool
, enable
);
359 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
361 wxToolBarToolBase
*tool
= FindById(id
);
362 if ( tool
&& tool
->CanBeToggled() )
364 if ( tool
->Toggle(toggle
) )
366 DoToggleTool(tool
, toggle
);
371 void wxToolBarBase::SetToggle(int id
, bool toggle
)
373 wxToolBarToolBase
*tool
= FindById(id
);
376 if ( tool
->SetToggle(toggle
) )
378 DoSetToggle(tool
, toggle
);
383 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
385 wxToolBarToolBase
*tool
= FindById(id
);
388 (void)tool
->SetShortHelp(help
);
392 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
394 wxToolBarToolBase
*tool
= FindById(id
);
397 (void)tool
->SetLongHelp(help
);
401 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
403 wxToolBarToolBase
*tool
= FindById(id
);
405 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
408 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
410 wxToolBarToolBase
*tool
= FindById(id
);
412 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
414 tool
->SetClientData(clientData
);
417 bool wxToolBarBase::GetToolState(int id
) const
419 wxToolBarToolBase
*tool
= FindById(id
);
420 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
422 return tool
->IsToggled();
425 bool wxToolBarBase::GetToolEnabled(int id
) const
427 wxToolBarToolBase
*tool
= FindById(id
);
428 wxCHECK_MSG( tool
, FALSE
, _T("no such tool") );
430 return tool
->IsEnabled();
433 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
435 wxToolBarToolBase
*tool
= FindById(id
);
436 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
438 return tool
->GetShortHelp();
441 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
443 wxToolBarToolBase
*tool
= FindById(id
);
444 wxCHECK_MSG( tool
, _T(""), _T("no such tool") );
446 return tool
->GetLongHelp();
449 // ----------------------------------------------------------------------------
450 // wxToolBarBase geometry
451 // ----------------------------------------------------------------------------
453 void wxToolBarBase::SetMargins(int x
, int y
)
459 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 // Only allow toggle if returns TRUE
469 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
471 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
472 event
.SetEventObject(this);
474 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
475 event
.SetInt((int)toggleDown
);
477 // and SetExtraLong() for backwards compatibility
478 event
.SetExtraLong((long)toggleDown
);
480 // Send events to this toolbar instead (and thence up the window hierarchy)
481 GetEventHandler()->ProcessEvent(event
);
486 // Call when right button down.
487 void wxToolBarBase::OnRightClick(int id
,
491 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
492 event
.SetEventObject(this);
495 GetEventHandler()->ProcessEvent(event
);
498 // Called when the mouse cursor enters a tool bitmap (no button pressed).
499 // Argument is -1 if mouse is exiting the toolbar.
500 // Note that for this event, the id of the window is used,
501 // and the integer parameter of wxCommandEvent is used to retrieve
503 void wxToolBarBase::OnMouseEnter(int id
)
505 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
506 event
.SetEventObject(this);
509 (void)GetEventHandler()->ProcessEvent(event
);
511 wxToolBarToolBase
*tool
= FindById(id
);
512 if ( !tool
|| !tool
->GetLongHelp() )
515 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
519 frame
->SetStatusText(tool
->GetLongHelp());
522 // ----------------------------------------------------------------------------
524 // ----------------------------------------------------------------------------
526 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
533 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
534 void wxToolBarBase::DoToolbarUpdates()
536 wxWindow
* parent
= this;
537 while (parent
->GetParent())
538 parent
= parent
->GetParent();
541 wxWindow
* focusWin
= wxFindFocusDescendant(parent
);
543 wxWindow
* focusWin
= (wxWindow
*) NULL
;
546 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler() ;
548 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
550 node
= node
->GetNext() )
552 int id
= node
->GetData()->GetId();
554 wxUpdateUIEvent
event(id
);
555 event
.SetEventObject(this);
557 if ( evtHandler
->ProcessEvent(event
) )
559 if ( event
.GetSetEnabled() )
560 EnableTool(id
, event
.GetEnabled());
561 if ( event
.GetSetChecked() )
562 ToggleTool(id
, event
.GetChecked());
564 if ( event
.GetSetText() )
571 #endif // wxUSE_TOOLBAR