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 #if !USE_SHARED_LIBRARY
51 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
52 EVT_IDLE(wxToolBarBase::OnIdle
)
56 IMPLEMENT_CLASS(wxToolBarBase
, wxControl
)
58 #include "wx/listimpl.cpp"
60 WX_DEFINE_LIST(wxToolBarToolsList
);
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 bool wxToolBarToolBase::Enable(bool enable
)
72 if ( m_enabled
== enable
)
80 bool wxToolBarToolBase::Toggle(bool toggle
)
82 wxASSERT_MSG( m_isToggle
, _T("can't toggle this tool") );
84 if ( m_toggled
== toggle
)
92 bool wxToolBarToolBase::SetToggle(bool toggle
)
94 if ( m_isToggle
== toggle
)
102 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
104 if ( m_shortHelpString
== help
)
107 m_shortHelpString
= help
;
112 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
114 if ( m_longHelpString
== help
)
117 m_longHelpString
= help
;
122 wxToolBarToolBase::~wxToolBarToolBase()
126 // ----------------------------------------------------------------------------
127 // wxToolBarBase adding/deleting items
128 // ----------------------------------------------------------------------------
130 wxToolBarBase::wxToolBarBase()
132 // the list owns the pointers
133 m_tools
.DeleteContents(TRUE
);
135 m_xMargin
= m_yMargin
= 0;
137 m_maxRows
= m_maxCols
= 0;
140 wxToolBarToolBase
*wxToolBarBase::AddTool(int id
,
141 const wxBitmap
& bitmap
,
142 const wxBitmap
& pushedBitmap
,
144 wxCoord
WXUNUSED(xPos
),
145 wxCoord
WXUNUSED(yPos
),
146 wxObject
*clientData
,
147 const wxString
& helpString1
,
148 const wxString
& helpString2
)
150 return InsertTool(GetToolsCount(), id
, bitmap
, pushedBitmap
,
151 toggle
, clientData
, helpString1
, helpString2
);
154 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
156 const wxBitmap
& bitmap
,
157 const wxBitmap
& pushedBitmap
,
159 wxObject
*clientData
,
160 const wxString
& helpString1
,
161 const wxString
& helpString2
)
163 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
164 _T("invalid position in wxToolBar::InsertTool()") );
166 wxToolBarToolBase
*tool
= CreateTool(id
, bitmap
, pushedBitmap
, toggle
,
167 clientData
, helpString1
, helpString2
);
169 if ( !tool
|| !DoInsertTool(pos
, tool
) )
176 m_tools
.Insert(pos
, tool
);
181 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
183 return InsertControl(GetToolsCount(), control
);
186 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
188 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
189 _T("toolbar: can't insert NULL control") );
191 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
192 _T("control must have toolbar as parent") );
194 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
195 _T("invalid position in wxToolBar::InsertControl()") );
197 wxToolBarToolBase
*tool
= CreateTool(control
);
199 if ( !tool
|| !DoInsertTool(pos
, tool
) )
206 m_tools
.Insert(pos
, tool
);
211 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
213 return InsertSeparator(GetToolsCount());
216 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
218 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
219 _T("invalid position in wxToolBar::InsertSeparator()") );
221 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
222 wxNullBitmap
, wxNullBitmap
,
223 FALSE
, (wxObject
*)NULL
,
224 wxEmptyString
, wxEmptyString
);
226 if ( !tool
|| !DoInsertTool(pos
, tool
) )
233 m_tools
.Insert(pos
, tool
);
238 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
241 wxToolBarToolsList::Node
*node
;
242 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
244 if ( node
->GetData()->GetId() == id
)
252 // don't give any error messages - sometimes we might call RemoveTool()
253 // without knowing whether the tool is or not in the toolbar
254 return (wxToolBarToolBase
*)NULL
;
257 wxToolBarToolBase
*tool
= node
->GetData();
258 if ( !DoDeleteTool(pos
, tool
) )
260 return (wxToolBarToolBase
*)NULL
;
263 // the node would delete the data, so set it to NULL to avoid this
266 m_tools
.DeleteNode(node
);
271 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
273 wxCHECK_MSG( pos
< GetToolsCount(), FALSE
,
274 _T("invalid position in wxToolBar::DeleteToolByPos()") );
276 wxToolBarToolsList::Node
*node
= m_tools
.Item(pos
);
278 if ( !DoDeleteTool(pos
, node
->GetData()) )
283 m_tools
.DeleteNode(node
);
288 bool wxToolBarBase::DeleteTool(int id
)
291 wxToolBarToolsList::Node
*node
;
292 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
294 if ( node
->GetData()->GetId() == id
)
300 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
305 m_tools
.DeleteNode(node
);
310 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
312 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
314 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
316 node
= node
->GetNext() )
318 tool
= node
->GetData();
319 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);
473 event
.SetExtraLong((long) toggleDown
);
475 // Send events to this toolbar instead (and thence up the window hierarchy)
476 GetEventHandler()->ProcessEvent(event
);
481 // Call when right button down.
482 void wxToolBarBase::OnRightClick(int id
,
486 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
487 event
.SetEventObject(this);
490 GetEventHandler()->ProcessEvent(event
);
493 // Called when the mouse cursor enters a tool bitmap (no button pressed).
494 // Argument is -1 if mouse is exiting the toolbar.
495 // Note that for this event, the id of the window is used,
496 // and the integer parameter of wxCommandEvent is used to retrieve
498 void wxToolBarBase::OnMouseEnter(int id
)
500 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
501 event
.SetEventObject(this);
504 (void)GetEventHandler()->ProcessEvent(event
);
506 wxToolBarToolBase
*tool
= FindById(id
);
507 if ( !tool
|| !tool
->GetLongHelp() )
510 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
514 frame
->SetStatusText(tool
->GetLongHelp());
517 // ----------------------------------------------------------------------------
519 // ----------------------------------------------------------------------------
521 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
528 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
529 void wxToolBarBase::DoToolbarUpdates()
531 wxWindow
* parent
= this;
532 while (parent
->GetParent())
533 parent
= parent
->GetParent();
536 wxWindow
* focusWin
= wxFindFocusDescendant(parent
);
538 wxWindow
* focusWin
= (wxWindow
*) NULL
;
541 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler() ;
543 for ( wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
545 node
= node
->GetNext() )
547 int id
= node
->GetData()->GetId();
549 wxUpdateUIEvent
event(id
);
550 event
.SetEventObject(this);
552 if ( evtHandler
->ProcessEvent(event
) )
554 if ( event
.GetSetEnabled() )
555 EnableTool(id
, event
.GetEnabled());
556 if ( event
.GetSetChecked() )
557 ToggleTool(id
, event
.GetChecked());
559 if ( event
.GetSetText() )
566 #endif // wxUSE_TOOLBAR