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(), wxT("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(), NULL
,
181 wxT("invalid position in wxToolBar::InsertTool()") );
183 return DoInsertNewTool(pos
, CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
184 clientData
, shortHelp
, longHelp
));
187 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
189 return InsertTool(GetToolsCount(), tool
);
193 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
195 wxCHECK_MSG( pos
<= GetToolsCount(), NULL
,
196 wxT("invalid position in wxToolBar::InsertTool()") );
198 if ( !tool
|| !DoInsertTool(pos
, tool
) )
203 m_tools
.Insert(pos
, tool
);
210 wxToolBarBase::AddControl(wxControl
*control
, const wxString
& label
)
212 return InsertControl(GetToolsCount(), control
, label
);
216 wxToolBarBase::InsertControl(size_t pos
,
218 const wxString
& label
)
220 wxCHECK_MSG( control
, NULL
,
221 wxT("toolbar: can't insert NULL control") );
223 wxCHECK_MSG( control
->GetParent() == this, NULL
,
224 wxT("control must have toolbar as parent") );
226 return DoInsertNewTool(pos
, CreateTool(control
, label
));
229 wxControl
*wxToolBarBase::FindControl( int id
)
231 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
233 node
= node
->GetNext() )
235 const wxToolBarToolBase
* const tool
= node
->GetData();
236 if ( tool
->IsControl() )
238 wxControl
* const control
= tool
->GetControl();
242 wxFAIL_MSG( wxT("NULL control in toolbar?") );
244 else if ( control
->GetId() == id
)
255 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
257 return InsertSeparator(GetToolsCount());
260 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
262 return DoInsertNewTool(pos
, CreateSeparator());
265 wxToolBarToolBase
*wxToolBarBase::AddStretchableSpace()
267 return InsertStretchableSpace(GetToolsCount());
270 wxToolBarToolBase
*wxToolBarBase::InsertStretchableSpace(size_t pos
)
272 wxToolBarToolBase
* const tool
= CreateSeparator();
275 // this is a hack but we know that all the current implementations
276 // don't really use the tool when it's created, they will do it
277 // InsertTool() at earliest and maybe even in Realize() much later
279 // so we can create the tool as a plain separator and mark it as being
280 // a stretchable space later
281 tool
->MakeStretchable();
284 return DoInsertNewTool(pos
, tool
);
287 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
290 wxToolBarToolsList::compatibility_iterator node
;
291 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
293 if ( node
->GetData()->GetId() == id
)
301 // don't give any error messages - sometimes we might call RemoveTool()
302 // without knowing whether the tool is or not in the toolbar
306 wxToolBarToolBase
*tool
= node
->GetData();
307 wxCHECK_MSG( tool
, NULL
, "NULL tool in the tools list?" );
309 if ( !DoDeleteTool(pos
, tool
) )
319 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
321 wxCHECK_MSG( pos
< GetToolsCount(), false,
322 wxT("invalid position in wxToolBar::DeleteToolByPos()") );
324 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item(pos
);
326 if ( !DoDeleteTool(pos
, node
->GetData()) )
331 delete node
->GetData();
337 bool wxToolBarBase::DeleteTool(int id
)
340 wxToolBarToolsList::compatibility_iterator node
;
341 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
343 if ( node
->GetData()->GetId() == id
)
349 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
354 delete node
->GetData();
360 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
362 wxToolBarToolBase
*tool
= NULL
;
364 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
366 node
= node
->GetNext() )
368 tool
= node
->GetData();
369 if ( tool
->GetId() == id
)
381 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase
*tool
)
383 wxCHECK_RET( tool
, wxT("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
385 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
388 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Find(tool
);
389 wxCHECK_RET( node
, wxT("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
391 wxToolBarToolsList::compatibility_iterator nodeNext
= node
->GetNext();
394 wxToolBarToolBase
*toolNext
= nodeNext
->GetData();
396 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
399 if ( toolNext
->Toggle(false) )
401 DoToggleTool(toolNext
, false);
404 nodeNext
= nodeNext
->GetNext();
407 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
410 wxToolBarToolBase
*toolNext
= nodePrev
->GetData();
412 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
415 if ( toolNext
->Toggle(false) )
417 DoToggleTool(toolNext
, false);
420 nodePrev
= nodePrev
->GetPrevious();
424 void wxToolBarBase::ClearTools()
426 while ( GetToolsCount() )
432 void wxToolBarBase::AdjustToolBitmapSize()
434 const wxSize
sizeOrig(m_defaultWidth
, m_defaultHeight
);
436 wxSize
sizeActual(sizeOrig
);
438 for ( wxToolBarToolsList::const_iterator i
= m_tools
.begin();
442 const wxBitmap
& bmp
= (*i
)->GetNormalBitmap();
444 sizeActual
.IncTo(bmp
.GetSize());
447 if ( sizeActual
!= sizeOrig
)
448 SetToolBitmapSize(sizeActual
);
451 bool wxToolBarBase::Realize()
453 // check if we have anything to do
454 if ( m_tools
.empty() )
457 // make sure tool size is larger enough for all all bitmaps to fit in
458 // (this is consistent with what other ports do):
459 AdjustToolBitmapSize();
464 wxToolBarBase::~wxToolBarBase()
466 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
468 // notify the frame that it doesn't have a tool bar any longer to avoid
470 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
471 if ( frame
&& frame
->GetToolBar() == this )
473 frame
->SetToolBar(NULL
);
477 // ----------------------------------------------------------------------------
478 // wxToolBarBase tools state
479 // ----------------------------------------------------------------------------
481 void wxToolBarBase::EnableTool(int id
, bool enable
)
483 wxToolBarToolBase
*tool
= FindById(id
);
486 if ( tool
->Enable(enable
) )
488 DoEnableTool(tool
, enable
);
493 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
495 wxToolBarToolBase
*tool
= FindById(id
);
496 if ( tool
&& tool
->CanBeToggled() )
498 if ( tool
->Toggle(toggle
) )
500 UnToggleRadioGroup(tool
);
501 DoToggleTool(tool
, toggle
);
506 void wxToolBarBase::SetToggle(int id
, bool toggle
)
508 wxToolBarToolBase
*tool
= FindById(id
);
511 if ( tool
->SetToggle(toggle
) )
513 DoSetToggle(tool
, toggle
);
518 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
520 wxToolBarToolBase
*tool
= FindById(id
);
523 (void)tool
->SetShortHelp(help
);
527 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
529 wxToolBarToolBase
*tool
= FindById(id
);
532 (void)tool
->SetLongHelp(help
);
536 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
538 wxToolBarToolBase
*tool
= FindById(id
);
540 return tool
? tool
->GetClientData() : NULL
;
543 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
545 wxToolBarToolBase
*tool
= FindById(id
);
547 wxCHECK_RET( tool
, wxT("no such tool in wxToolBar::SetToolClientData") );
549 tool
->SetClientData(clientData
);
552 int wxToolBarBase::GetToolPos(int id
) const
555 wxToolBarToolsList::compatibility_iterator node
;
557 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
559 if ( node
->GetData()->GetId() == id
)
568 bool wxToolBarBase::GetToolState(int id
) const
570 wxToolBarToolBase
*tool
= FindById(id
);
571 wxCHECK_MSG( tool
, false, wxT("no such tool") );
573 return tool
->IsToggled();
576 bool wxToolBarBase::GetToolEnabled(int id
) const
578 wxToolBarToolBase
*tool
= FindById(id
);
579 wxCHECK_MSG( tool
, false, wxT("no such tool") );
581 return tool
->IsEnabled();
584 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
586 wxToolBarToolBase
*tool
= FindById(id
);
587 wxCHECK_MSG( tool
, wxEmptyString
, wxT("no such tool") );
589 return tool
->GetShortHelp();
592 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
594 wxToolBarToolBase
*tool
= FindById(id
);
595 wxCHECK_MSG( tool
, wxEmptyString
, wxT("no such tool") );
597 return tool
->GetLongHelp();
600 // ----------------------------------------------------------------------------
601 // wxToolBarBase geometry
602 // ----------------------------------------------------------------------------
604 void wxToolBarBase::SetMargins(int x
, int y
)
610 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
615 bool wxToolBarBase::IsVertical() const
617 return HasFlag(wxTB_LEFT
| wxTB_RIGHT
);
621 // ----------------------------------------------------------------------------
623 // ----------------------------------------------------------------------------
625 // Only allow toggle if returns true
626 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
628 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
629 event
.SetEventObject(this);
631 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
632 event
.SetInt((int)toggleDown
);
634 // and SetExtraLong() for backwards compatibility
635 event
.SetExtraLong((long)toggleDown
);
637 // Send events to this toolbar instead (and thence up the window hierarchy)
638 HandleWindowEvent(event
);
643 // Call when right button down.
644 void wxToolBarBase::OnRightClick(int id
,
648 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
649 event
.SetEventObject(this);
652 GetEventHandler()->ProcessEvent(event
);
655 // Called when the mouse cursor enters a tool bitmap (no button pressed).
656 // Argument is wxID_ANY if mouse is exiting the toolbar.
657 // Note that for this event, the id of the window is used,
658 // and the integer parameter of wxCommandEvent is used to retrieve
660 void wxToolBarBase::OnMouseEnter(int id
)
662 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
663 event
.SetEventObject(this);
666 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
670 if ( id
!= wxID_ANY
)
672 const wxToolBarToolBase
* const tool
= FindById(id
);
674 help
= tool
->GetLongHelp();
677 // call DoGiveHelp() even if help string is empty to avoid showing the
678 // help for the previously selected tool when another one is selected
679 frame
->DoGiveHelp(help
, id
!= wxID_ANY
);
682 (void)GetEventHandler()->ProcessEvent(event
);
685 // ----------------------------------------------------------------------------
687 // ----------------------------------------------------------------------------
689 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
690 void wxToolBarBase::UpdateWindowUI(long flags
)
692 wxWindowBase::UpdateWindowUI(flags
);
694 // don't waste time updating state of tools in a hidden toolbar
698 // There is no sense in updating the toolbar UI
699 // if the parent window is about to get destroyed
700 wxWindow
*tlw
= wxGetTopLevelParent( this );
701 if (tlw
&& wxPendingDelete
.Member( tlw
))
704 wxEvtHandler
* evtHandler
= GetEventHandler() ;
706 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
708 node
= node
->GetNext() )
710 wxToolBarToolBase
* const tool
= node
->GetData();
711 if ( tool
->IsSeparator() )
714 int id
= tool
->GetId();
716 wxUpdateUIEvent
event(id
);
717 event
.SetEventObject(this);
719 if ( evtHandler
->ProcessEvent(event
) )
721 if ( event
.GetSetEnabled() )
722 EnableTool(id
, event
.GetEnabled());
723 if ( event
.GetSetChecked() )
724 ToggleTool(id
, event
.GetChecked());
726 if ( event
.GetSetText() )
733 bool wxToolBarBase::SetDropdownMenu(int toolid
, wxMenu
* menu
)
735 wxToolBarToolBase
* const tool
= FindById(toolid
);
736 wxCHECK_MSG( tool
, false, wxT("invalid tool id") );
738 wxCHECK_MSG( tool
->GetKind() == wxITEM_DROPDOWN
, false,
739 wxT("menu can be only associated with drop down tools") );
741 tool
->SetDropdownMenu(menu
);
746 #if WXWIN_COMPATIBILITY_2_8
748 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
751 out
= in
.ConvertToGreyscale();
754 #endif // wxUSE_IMAGE
758 #endif // WXWIN_COMPATIBILITY_2_8
760 #endif // wxUSE_TOOLBAR