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
;
68 bool wxToolBarToolBase::Enable(bool enable
)
70 if ( m_enabled
== enable
)
78 bool wxToolBarToolBase::Toggle(bool toggle
)
80 wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") );
82 if ( m_toggled
== toggle
)
90 bool wxToolBarToolBase::SetToggle(bool toggle
)
92 wxItemKind kind
= toggle
? wxITEM_CHECK
: wxITEM_NORMAL
;
101 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
103 if ( m_shortHelpString
== help
)
106 m_shortHelpString
= help
;
111 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
113 if ( m_longHelpString
== help
)
116 m_longHelpString
= help
;
122 void wxToolBarToolBase::SetDropdownMenu(wxMenu
* menu
)
124 delete m_dropdownMenu
;
125 m_dropdownMenu
= menu
;
128 // ----------------------------------------------------------------------------
129 // wxToolBarBase adding/deleting items
130 // ----------------------------------------------------------------------------
132 wxToolBarBase::wxToolBarBase()
134 // the list owns the pointers
135 m_xMargin
= m_yMargin
= 0;
136 m_maxRows
= m_maxCols
= 0;
137 m_toolPacking
= m_toolSeparation
= 0;
139 m_defaultHeight
= 15;
142 void wxToolBarBase::FixupStyle()
144 if ( !HasFlag(wxTB_TOP
| wxTB_LEFT
| wxTB_RIGHT
| wxTB_BOTTOM
) )
146 // this is the default
147 m_windowStyle
|= wxTB_TOP
;
151 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
152 const wxString
& label
,
153 const wxBitmap
& bitmap
,
154 const wxBitmap
& bmpDisabled
,
156 const wxString
& shortHelp
,
157 const wxString
& longHelp
,
158 wxObject
*clientData
,
159 wxCoord
WXUNUSED(xPos
),
160 wxCoord
WXUNUSED(yPos
))
162 InvalidateBestSize();
163 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
164 kind
, shortHelp
, longHelp
, clientData
);
167 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
169 const wxString
& label
,
170 const wxBitmap
& bitmap
,
171 const wxBitmap
& bmpDisabled
,
173 const wxString
& shortHelp
,
174 const wxString
& longHelp
,
175 wxObject
*clientData
)
177 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
178 _T("invalid position in wxToolBar::InsertTool()") );
180 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
181 clientData
, shortHelp
, longHelp
);
183 if ( !InsertTool(pos
, tool
) )
193 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
195 return InsertTool(GetToolsCount(), tool
);
199 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
201 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
202 _T("invalid position in wxToolBar::InsertTool()") );
204 if ( !tool
|| !DoInsertTool(pos
, tool
) )
209 m_tools
.Insert(pos
, tool
);
215 wxToolBarBase::AddControl(wxControl
*control
, const wxString
& label
)
217 return InsertControl(GetToolsCount(), control
, label
);
221 wxToolBarBase::InsertControl(size_t pos
,
223 const wxString
& label
)
225 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
226 _T("toolbar: can't insert NULL control") );
228 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
229 _T("control must have toolbar as parent") );
231 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
232 _T("invalid position in wxToolBar::InsertControl()") );
234 wxToolBarToolBase
*tool
= CreateTool(control
, label
);
236 if ( !InsertTool(pos
, tool
) )
246 wxControl
*wxToolBarBase::FindControl( int id
)
248 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
250 node
= node
->GetNext() )
252 const wxToolBarToolBase
* const tool
= node
->GetData();
253 if ( tool
->IsControl() )
255 wxControl
* const control
= tool
->GetControl();
259 wxFAIL_MSG( _T("NULL control in toolbar?") );
261 else if ( control
->GetId() == id
)
272 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
274 return InsertSeparator(GetToolsCount());
277 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
279 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
280 _T("invalid position in wxToolBar::InsertSeparator()") );
282 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
284 wxNullBitmap
, wxNullBitmap
,
285 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
286 wxEmptyString
, wxEmptyString
);
288 if ( !tool
|| !DoInsertTool(pos
, tool
) )
295 m_tools
.Insert(pos
, tool
);
300 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
303 wxToolBarToolsList::compatibility_iterator node
;
304 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
306 if ( node
->GetData()->GetId() == id
)
314 // don't give any error messages - sometimes we might call RemoveTool()
315 // without knowing whether the tool is or not in the toolbar
316 return (wxToolBarToolBase
*)NULL
;
319 wxToolBarToolBase
*tool
= node
->GetData();
320 if ( !DoDeleteTool(pos
, tool
) )
322 return (wxToolBarToolBase
*)NULL
;
330 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
332 wxCHECK_MSG( pos
< GetToolsCount(), false,
333 _T("invalid position in wxToolBar::DeleteToolByPos()") );
335 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item(pos
);
337 if ( !DoDeleteTool(pos
, node
->GetData()) )
342 delete node
->GetData();
348 bool wxToolBarBase::DeleteTool(int id
)
351 wxToolBarToolsList::compatibility_iterator node
;
352 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
354 if ( node
->GetData()->GetId() == id
)
360 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
365 delete node
->GetData();
371 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
373 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
375 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
377 node
= node
->GetNext() )
379 tool
= node
->GetData();
380 if ( tool
->GetId() == id
)
392 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase
*tool
)
394 wxCHECK_RET( tool
, _T("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
396 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
399 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Find(tool
);
400 wxCHECK_RET( node
, _T("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
402 wxToolBarToolsList::compatibility_iterator nodeNext
= node
->GetNext();
405 wxToolBarToolBase
*toolNext
= nodeNext
->GetData();
407 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
410 if ( toolNext
->Toggle(false) )
412 DoToggleTool(toolNext
, false);
415 nodeNext
= nodeNext
->GetNext();
418 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
421 wxToolBarToolBase
*toolNext
= nodePrev
->GetData();
423 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
426 if ( toolNext
->Toggle(false) )
428 DoToggleTool(toolNext
, false);
431 nodePrev
= nodePrev
->GetPrevious();
435 void wxToolBarBase::ClearTools()
437 while ( GetToolsCount() )
443 bool wxToolBarBase::Realize()
448 wxToolBarBase::~wxToolBarBase()
450 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
452 // notify the frame that it doesn't have a tool bar any longer to avoid
454 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
455 if ( frame
&& frame
->GetToolBar() == this )
457 frame
->SetToolBar(NULL
);
461 // ----------------------------------------------------------------------------
462 // wxToolBarBase tools state
463 // ----------------------------------------------------------------------------
465 void wxToolBarBase::EnableTool(int id
, bool enable
)
467 wxToolBarToolBase
*tool
= FindById(id
);
470 if ( tool
->Enable(enable
) )
472 DoEnableTool(tool
, enable
);
477 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
479 wxToolBarToolBase
*tool
= FindById(id
);
480 if ( tool
&& tool
->CanBeToggled() )
482 if ( tool
->Toggle(toggle
) )
484 UnToggleRadioGroup(tool
);
485 DoToggleTool(tool
, toggle
);
490 void wxToolBarBase::SetToggle(int id
, bool toggle
)
492 wxToolBarToolBase
*tool
= FindById(id
);
495 if ( tool
->SetToggle(toggle
) )
497 DoSetToggle(tool
, toggle
);
502 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
504 wxToolBarToolBase
*tool
= FindById(id
);
507 (void)tool
->SetShortHelp(help
);
511 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
513 wxToolBarToolBase
*tool
= FindById(id
);
516 (void)tool
->SetLongHelp(help
);
520 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
522 wxToolBarToolBase
*tool
= FindById(id
);
524 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
527 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
529 wxToolBarToolBase
*tool
= FindById(id
);
531 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
533 tool
->SetClientData(clientData
);
536 int wxToolBarBase::GetToolPos(int id
) const
539 wxToolBarToolsList::compatibility_iterator node
;
541 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
543 if ( node
->GetData()->GetId() == id
)
552 bool wxToolBarBase::GetToolState(int id
) const
554 wxToolBarToolBase
*tool
= FindById(id
);
555 wxCHECK_MSG( tool
, false, _T("no such tool") );
557 return tool
->IsToggled();
560 bool wxToolBarBase::GetToolEnabled(int id
) const
562 wxToolBarToolBase
*tool
= FindById(id
);
563 wxCHECK_MSG( tool
, false, _T("no such tool") );
565 return tool
->IsEnabled();
568 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
570 wxToolBarToolBase
*tool
= FindById(id
);
571 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
573 return tool
->GetShortHelp();
576 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
578 wxToolBarToolBase
*tool
= FindById(id
);
579 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
581 return tool
->GetLongHelp();
584 // ----------------------------------------------------------------------------
585 // wxToolBarBase geometry
586 // ----------------------------------------------------------------------------
588 void wxToolBarBase::SetMargins(int x
, int y
)
594 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
599 // ----------------------------------------------------------------------------
601 // ----------------------------------------------------------------------------
603 // Only allow toggle if returns true
604 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
606 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
607 event
.SetEventObject(this);
609 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
610 event
.SetInt((int)toggleDown
);
612 // and SetExtraLong() for backwards compatibility
613 event
.SetExtraLong((long)toggleDown
);
615 // Send events to this toolbar instead (and thence up the window hierarchy)
616 GetEventHandler()->ProcessEvent(event
);
621 // Call when right button down.
622 void wxToolBarBase::OnRightClick(int id
,
626 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
627 event
.SetEventObject(this);
630 GetEventHandler()->ProcessEvent(event
);
633 // Called when the mouse cursor enters a tool bitmap (no button pressed).
634 // Argument is wxID_ANY if mouse is exiting the toolbar.
635 // Note that for this event, the id of the window is used,
636 // and the integer parameter of wxCommandEvent is used to retrieve
638 void wxToolBarBase::OnMouseEnter(int id
)
640 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
641 event
.SetEventObject(this);
644 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
648 if ( id
!= wxID_ANY
)
650 const wxToolBarToolBase
* const tool
= FindById(id
);
652 help
= tool
->GetLongHelp();
655 // call DoGiveHelp() even if help string is empty to avoid showing the
656 // help for the previously selected tool when another one is selected
657 frame
->DoGiveHelp(help
, id
!= wxID_ANY
);
660 (void)GetEventHandler()->ProcessEvent(event
);
663 // ----------------------------------------------------------------------------
665 // ----------------------------------------------------------------------------
667 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
668 void wxToolBarBase::UpdateWindowUI(long flags
)
670 wxWindowBase::UpdateWindowUI(flags
);
672 // don't waste time updating state of tools in a hidden toolbar
676 // There is no sense in updating the toolbar UI
677 // if the parent window is about to get destroyed
678 wxWindow
*tlw
= wxGetTopLevelParent( this );
679 if (tlw
&& wxPendingDelete
.Member( tlw
))
682 wxEvtHandler
* evtHandler
= GetEventHandler() ;
684 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
686 node
= node
->GetNext() )
688 int id
= node
->GetData()->GetId();
690 wxUpdateUIEvent
event(id
);
691 event
.SetEventObject(this);
693 if ( evtHandler
->ProcessEvent(event
) )
695 if ( event
.GetSetEnabled() )
696 EnableTool(id
, event
.GetEnabled());
697 if ( event
.GetSetChecked() )
698 ToggleTool(id
, event
.GetChecked());
700 if ( event
.GetSetText() )
707 bool wxToolBarBase::SetDropdownMenu(int toolid
, wxMenu
* menu
)
709 wxToolBarToolBase
* const tool
= FindById(toolid
);
710 wxCHECK_MSG( tool
, false, _T("invalid tool id") );
712 wxCHECK_MSG( tool
->GetKind() == wxITEM_DROPDOWN
, false,
713 _T("menu can be only associated with drop down tools") );
715 tool
->SetDropdownMenu(menu
);
720 #if WXWIN_COMPATIBILITY_2_8
722 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
725 out
= in
.ConvertToGreyscale();
728 #endif // wxUSE_IMAGE
732 #endif // WXWIN_COMPATIBILITY_2_8
734 #endif // wxUSE_TOOLBAR