1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/tbarbase.cpp
3 // Purpose: wxToolBarBase implementation
4 // Author: Julian Smart
5 // Modified by: VZ at 11.12.99 (wxScrollableToolBar split off)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/toolbar.h"
32 #include "wx/control.h"
34 #include "wx/settings.h"
35 #if WXWIN_COMPATIBILITY_2_8
37 #endif // WXWIN_COMPATIBILITY_2_8
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
48 #include "wx/listimpl.cpp"
50 WX_DEFINE_LIST(wxToolBarToolsList
)
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxToolBarToolBase
, wxObject
)
62 wxToolBarToolBase::~wxToolBarToolBase()
64 delete m_dropdownMenu
;
66 GetControl()->Destroy();
70 bool wxToolBarToolBase::Enable(bool enable
)
72 if ( m_enabled
== enable
)
80 bool wxToolBarToolBase::Toggle(bool toggle
)
82 wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") );
84 if ( m_toggled
== toggle
)
92 bool wxToolBarToolBase::SetToggle(bool toggle
)
94 wxItemKind kind
= toggle
? wxITEM_CHECK
: wxITEM_NORMAL
;
103 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
105 if ( m_shortHelpString
== help
)
108 m_shortHelpString
= help
;
113 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
115 if ( m_longHelpString
== help
)
118 m_longHelpString
= help
;
124 void wxToolBarToolBase::SetDropdownMenu(wxMenu
* menu
)
126 delete m_dropdownMenu
;
127 m_dropdownMenu
= menu
;
131 // ----------------------------------------------------------------------------
132 // wxToolBarBase adding/deleting items
133 // ----------------------------------------------------------------------------
135 wxToolBarBase::wxToolBarBase()
137 // the list owns the pointers
138 m_xMargin
= m_yMargin
= 0;
139 m_maxRows
= m_maxCols
= 0;
140 m_toolPacking
= m_toolSeparation
= 0;
142 m_defaultHeight
= 15;
145 void wxToolBarBase::FixupStyle()
147 if ( !HasFlag(wxTB_TOP
| wxTB_LEFT
| wxTB_RIGHT
| wxTB_BOTTOM
) )
149 // this is the default
150 m_windowStyle
|= wxTB_TOP
;
154 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
155 const wxString
& label
,
156 const wxBitmap
& bitmap
,
157 const wxBitmap
& bmpDisabled
,
159 const wxString
& shortHelp
,
160 const wxString
& longHelp
,
161 wxObject
*clientData
,
162 wxCoord
WXUNUSED(xPos
),
163 wxCoord
WXUNUSED(yPos
))
165 InvalidateBestSize();
166 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
167 kind
, shortHelp
, longHelp
, clientData
);
170 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
172 const wxString
& label
,
173 const wxBitmap
& bitmap
,
174 const wxBitmap
& bmpDisabled
,
176 const wxString
& shortHelp
,
177 const wxString
& longHelp
,
178 wxObject
*clientData
)
180 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
181 _T("invalid position in wxToolBar::InsertTool()") );
183 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
184 clientData
, shortHelp
, longHelp
);
186 if ( !InsertTool(pos
, tool
) )
196 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
198 return InsertTool(GetToolsCount(), tool
);
202 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
204 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
205 _T("invalid position in wxToolBar::InsertTool()") );
207 if ( !tool
|| !DoInsertTool(pos
, tool
) )
212 m_tools
.Insert(pos
, tool
);
219 wxToolBarBase::AddControl(wxControl
*control
, const wxString
& label
)
221 return InsertControl(GetToolsCount(), control
, label
);
225 wxToolBarBase::InsertControl(size_t pos
,
227 const wxString
& label
)
229 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
230 _T("toolbar: can't insert NULL control") );
232 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
233 _T("control must have toolbar as parent") );
235 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
236 _T("invalid position in wxToolBar::InsertControl()") );
238 wxToolBarToolBase
*tool
= CreateTool(control
, label
);
240 if ( !InsertTool(pos
, tool
) )
250 wxControl
*wxToolBarBase::FindControl( int id
)
252 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
254 node
= node
->GetNext() )
256 const wxToolBarToolBase
* const tool
= node
->GetData();
257 if ( tool
->IsControl() )
259 wxControl
* const control
= tool
->GetControl();
263 wxFAIL_MSG( _T("NULL control in toolbar?") );
265 else if ( control
->GetId() == id
)
276 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
278 return InsertSeparator(GetToolsCount());
281 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
283 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
284 _T("invalid position in wxToolBar::InsertSeparator()") );
286 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
288 wxNullBitmap
, wxNullBitmap
,
289 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
290 wxEmptyString
, wxEmptyString
);
292 if ( !tool
|| !DoInsertTool(pos
, tool
) )
299 m_tools
.Insert(pos
, tool
);
304 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
307 wxToolBarToolsList::compatibility_iterator node
;
308 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
310 if ( node
->GetData()->GetId() == id
)
318 // don't give any error messages - sometimes we might call RemoveTool()
319 // without knowing whether the tool is or not in the toolbar
323 wxToolBarToolBase
*tool
= node
->GetData();
324 wxCHECK_MSG( tool
, NULL
, "NULL tool in the tools list?" );
326 if ( !DoDeleteTool(pos
, tool
) )
336 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
338 wxCHECK_MSG( pos
< GetToolsCount(), false,
339 _T("invalid position in wxToolBar::DeleteToolByPos()") );
341 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item(pos
);
343 if ( !DoDeleteTool(pos
, node
->GetData()) )
348 delete node
->GetData();
354 bool wxToolBarBase::DeleteTool(int id
)
357 wxToolBarToolsList::compatibility_iterator node
;
358 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
360 if ( node
->GetData()->GetId() == id
)
366 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
371 delete node
->GetData();
377 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
379 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
381 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
383 node
= node
->GetNext() )
385 tool
= node
->GetData();
386 if ( tool
->GetId() == id
)
398 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase
*tool
)
400 wxCHECK_RET( tool
, _T("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
402 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
405 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Find(tool
);
406 wxCHECK_RET( node
, _T("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
408 wxToolBarToolsList::compatibility_iterator nodeNext
= node
->GetNext();
411 wxToolBarToolBase
*toolNext
= nodeNext
->GetData();
413 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
416 if ( toolNext
->Toggle(false) )
418 DoToggleTool(toolNext
, false);
421 nodeNext
= nodeNext
->GetNext();
424 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
427 wxToolBarToolBase
*toolNext
= nodePrev
->GetData();
429 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
432 if ( toolNext
->Toggle(false) )
434 DoToggleTool(toolNext
, false);
437 nodePrev
= nodePrev
->GetPrevious();
441 void wxToolBarBase::ClearTools()
443 while ( GetToolsCount() )
449 bool wxToolBarBase::Realize()
454 wxToolBarBase::~wxToolBarBase()
456 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
458 // notify the frame that it doesn't have a tool bar any longer to avoid
460 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
461 if ( frame
&& frame
->GetToolBar() == this )
463 frame
->SetToolBar(NULL
);
467 // ----------------------------------------------------------------------------
468 // wxToolBarBase tools state
469 // ----------------------------------------------------------------------------
471 void wxToolBarBase::EnableTool(int id
, bool enable
)
473 wxToolBarToolBase
*tool
= FindById(id
);
476 if ( tool
->Enable(enable
) )
478 DoEnableTool(tool
, enable
);
483 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
485 wxToolBarToolBase
*tool
= FindById(id
);
486 if ( tool
&& tool
->CanBeToggled() )
488 if ( tool
->Toggle(toggle
) )
490 UnToggleRadioGroup(tool
);
491 DoToggleTool(tool
, toggle
);
496 void wxToolBarBase::SetToggle(int id
, bool toggle
)
498 wxToolBarToolBase
*tool
= FindById(id
);
501 if ( tool
->SetToggle(toggle
) )
503 DoSetToggle(tool
, toggle
);
508 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
510 wxToolBarToolBase
*tool
= FindById(id
);
513 (void)tool
->SetShortHelp(help
);
517 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
519 wxToolBarToolBase
*tool
= FindById(id
);
522 (void)tool
->SetLongHelp(help
);
526 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
528 wxToolBarToolBase
*tool
= FindById(id
);
530 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
533 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
535 wxToolBarToolBase
*tool
= FindById(id
);
537 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
539 tool
->SetClientData(clientData
);
542 int wxToolBarBase::GetToolPos(int id
) const
545 wxToolBarToolsList::compatibility_iterator node
;
547 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
549 if ( node
->GetData()->GetId() == id
)
558 bool wxToolBarBase::GetToolState(int id
) const
560 wxToolBarToolBase
*tool
= FindById(id
);
561 wxCHECK_MSG( tool
, false, _T("no such tool") );
563 return tool
->IsToggled();
566 bool wxToolBarBase::GetToolEnabled(int id
) const
568 wxToolBarToolBase
*tool
= FindById(id
);
569 wxCHECK_MSG( tool
, false, _T("no such tool") );
571 return tool
->IsEnabled();
574 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
576 wxToolBarToolBase
*tool
= FindById(id
);
577 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
579 return tool
->GetShortHelp();
582 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
584 wxToolBarToolBase
*tool
= FindById(id
);
585 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
587 return tool
->GetLongHelp();
590 // ----------------------------------------------------------------------------
591 // wxToolBarBase geometry
592 // ----------------------------------------------------------------------------
594 void wxToolBarBase::SetMargins(int x
, int y
)
600 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
605 bool wxToolBarBase::IsVertical() const
607 return HasFlag(wxTB_LEFT
| wxTB_RIGHT
);
611 // ----------------------------------------------------------------------------
613 // ----------------------------------------------------------------------------
615 // Only allow toggle if returns true
616 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
618 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
619 event
.SetEventObject(this);
621 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
622 event
.SetInt((int)toggleDown
);
624 // and SetExtraLong() for backwards compatibility
625 event
.SetExtraLong((long)toggleDown
);
627 // Send events to this toolbar instead (and thence up the window hierarchy)
628 GetEventHandler()->ProcessEvent(event
);
633 // Call when right button down.
634 void wxToolBarBase::OnRightClick(int id
,
638 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
639 event
.SetEventObject(this);
642 GetEventHandler()->ProcessEvent(event
);
645 // Called when the mouse cursor enters a tool bitmap (no button pressed).
646 // Argument is wxID_ANY if mouse is exiting the toolbar.
647 // Note that for this event, the id of the window is used,
648 // and the integer parameter of wxCommandEvent is used to retrieve
650 void wxToolBarBase::OnMouseEnter(int id
)
652 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
653 event
.SetEventObject(this);
656 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
660 if ( id
!= wxID_ANY
)
662 const wxToolBarToolBase
* const tool
= FindById(id
);
664 help
= tool
->GetLongHelp();
667 // call DoGiveHelp() even if help string is empty to avoid showing the
668 // help for the previously selected tool when another one is selected
669 frame
->DoGiveHelp(help
, id
!= wxID_ANY
);
672 (void)GetEventHandler()->ProcessEvent(event
);
675 // ----------------------------------------------------------------------------
677 // ----------------------------------------------------------------------------
679 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
680 void wxToolBarBase::UpdateWindowUI(long flags
)
682 wxWindowBase::UpdateWindowUI(flags
);
684 // don't waste time updating state of tools in a hidden toolbar
688 // There is no sense in updating the toolbar UI
689 // if the parent window is about to get destroyed
690 wxWindow
*tlw
= wxGetTopLevelParent( this );
691 if (tlw
&& wxPendingDelete
.Member( tlw
))
694 wxEvtHandler
* evtHandler
= GetEventHandler() ;
696 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
698 node
= node
->GetNext() )
700 int id
= node
->GetData()->GetId();
702 wxUpdateUIEvent
event(id
);
703 event
.SetEventObject(this);
705 if ( evtHandler
->ProcessEvent(event
) )
707 if ( event
.GetSetEnabled() )
708 EnableTool(id
, event
.GetEnabled());
709 if ( event
.GetSetChecked() )
710 ToggleTool(id
, event
.GetChecked());
712 if ( event
.GetSetText() )
719 bool wxToolBarBase::SetDropdownMenu(int toolid
, wxMenu
* menu
)
721 wxToolBarToolBase
* const tool
= FindById(toolid
);
722 wxCHECK_MSG( tool
, false, _T("invalid tool id") );
724 wxCHECK_MSG( tool
->GetKind() == wxITEM_DROPDOWN
, false,
725 _T("menu can be only associated with drop down tools") );
727 tool
->SetDropdownMenu(menu
);
732 #if WXWIN_COMPATIBILITY_2_8
734 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
737 out
= in
.ConvertToGreyscale();
740 #endif // wxUSE_IMAGE
744 #endif // WXWIN_COMPATIBILITY_2_8
746 #endif // wxUSE_TOOLBAR