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
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
47 #include "wx/listimpl.cpp"
49 WX_DEFINE_LIST(wxToolBarToolsList
)
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxToolBarToolBase
, wxObject
)
61 bool wxToolBarToolBase::Enable(bool enable
)
63 if ( m_enabled
== enable
)
71 bool wxToolBarToolBase::Toggle(bool toggle
)
73 wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") );
75 if ( m_toggled
== toggle
)
83 bool wxToolBarToolBase::SetToggle(bool toggle
)
85 wxItemKind kind
= toggle
? wxITEM_CHECK
: wxITEM_NORMAL
;
94 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
96 if ( m_shortHelpString
== help
)
99 m_shortHelpString
= help
;
104 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
106 if ( m_longHelpString
== help
)
109 m_longHelpString
= help
;
114 // ----------------------------------------------------------------------------
115 // wxToolBarBase adding/deleting items
116 // ----------------------------------------------------------------------------
118 wxToolBarBase::wxToolBarBase()
120 // the list owns the pointers
121 m_xMargin
= m_yMargin
= 0;
122 m_maxRows
= m_maxCols
= 0;
123 m_toolPacking
= m_toolSeparation
= 0;
125 m_defaultHeight
= 15;
128 void wxToolBarBase::FixupStyle()
130 if ( !HasFlag(wxTB_TOP
| wxTB_LEFT
| wxTB_RIGHT
| wxTB_BOTTOM
) )
132 // this is the default
133 m_windowStyle
|= wxTB_TOP
;
137 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
138 const wxString
& label
,
139 const wxBitmap
& bitmap
,
140 const wxBitmap
& bmpDisabled
,
142 const wxString
& shortHelp
,
143 const wxString
& longHelp
,
144 wxObject
*clientData
,
145 wxCoord
WXUNUSED(xPos
),
146 wxCoord
WXUNUSED(yPos
))
148 InvalidateBestSize();
149 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
150 kind
, shortHelp
, longHelp
, clientData
);
153 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
155 const wxString
& label
,
156 const wxBitmap
& bitmap
,
157 const wxBitmap
& bmpDisabled
,
159 const wxString
& shortHelp
,
160 const wxString
& longHelp
,
161 wxObject
*clientData
)
163 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
164 _T("invalid position in wxToolBar::InsertTool()") );
166 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
167 clientData
, shortHelp
, longHelp
);
169 if ( !InsertTool(pos
, tool
) )
179 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
181 return InsertTool(GetToolsCount(), tool
);
185 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
187 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
188 _T("invalid position in wxToolBar::InsertTool()") );
190 if ( !tool
|| !DoInsertTool(pos
, tool
) )
195 m_tools
.Insert(pos
, tool
);
200 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
202 return InsertControl(GetToolsCount(), control
);
205 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
207 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
208 _T("toolbar: can't insert NULL control") );
210 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
211 _T("control must have toolbar as parent") );
213 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
214 _T("invalid position in wxToolBar::InsertControl()") );
216 wxToolBarToolBase
*tool
= CreateTool(control
);
218 if ( !InsertTool(pos
, tool
) )
228 wxControl
*wxToolBarBase::FindControl( int id
)
230 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
232 node
= node
->GetNext() )
234 const wxToolBarToolBase
* const tool
= node
->GetData();
235 if ( tool
->IsControl() )
237 wxControl
* const control
= tool
->GetControl();
241 wxFAIL_MSG( _T("NULL control in toolbar?") );
243 else if ( control
->GetId() == id
)
254 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
256 return InsertSeparator(GetToolsCount());
259 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
261 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
262 _T("invalid position in wxToolBar::InsertSeparator()") );
264 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
266 wxNullBitmap
, wxNullBitmap
,
267 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
268 wxEmptyString
, wxEmptyString
);
270 if ( !tool
|| !DoInsertTool(pos
, tool
) )
277 m_tools
.Insert(pos
, tool
);
282 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
285 wxToolBarToolsList::compatibility_iterator node
;
286 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
288 if ( node
->GetData()->GetId() == id
)
296 // don't give any error messages - sometimes we might call RemoveTool()
297 // without knowing whether the tool is or not in the toolbar
298 return (wxToolBarToolBase
*)NULL
;
301 wxToolBarToolBase
*tool
= node
->GetData();
302 if ( !DoDeleteTool(pos
, tool
) )
304 return (wxToolBarToolBase
*)NULL
;
312 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
314 wxCHECK_MSG( pos
< GetToolsCount(), false,
315 _T("invalid position in wxToolBar::DeleteToolByPos()") );
317 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item(pos
);
319 if ( !DoDeleteTool(pos
, node
->GetData()) )
324 delete node
->GetData();
330 bool wxToolBarBase::DeleteTool(int id
)
333 wxToolBarToolsList::compatibility_iterator node
;
334 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
336 if ( node
->GetData()->GetId() == id
)
342 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
347 delete node
->GetData();
353 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
355 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
357 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
359 node
= node
->GetNext() )
361 tool
= node
->GetData();
362 if ( tool
->GetId() == id
)
374 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase
*tool
)
376 wxCHECK_RET( tool
, _T("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
378 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
381 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Find(tool
);
382 wxCHECK_RET( node
, _T("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
384 wxToolBarToolsList::compatibility_iterator nodeNext
= node
->GetNext();
387 wxToolBarToolBase
*toolNext
= nodeNext
->GetData();
389 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
392 if ( toolNext
->Toggle(false) )
394 DoToggleTool(toolNext
, false);
397 nodeNext
= nodeNext
->GetNext();
400 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
403 wxToolBarToolBase
*toolNext
= nodePrev
->GetData();
405 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
408 if ( toolNext
->Toggle(false) )
410 DoToggleTool(toolNext
, false);
413 nodePrev
= nodePrev
->GetPrevious();
417 void wxToolBarBase::ClearTools()
419 while ( GetToolsCount() )
425 bool wxToolBarBase::Realize()
430 wxToolBarBase::~wxToolBarBase()
432 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
434 // notify the frame that it doesn't have a tool bar any longer to avoid
436 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
437 if ( frame
&& frame
->GetToolBar() == this )
439 frame
->SetToolBar(NULL
);
443 // ----------------------------------------------------------------------------
444 // wxToolBarBase tools state
445 // ----------------------------------------------------------------------------
447 void wxToolBarBase::EnableTool(int id
, bool enable
)
449 wxToolBarToolBase
*tool
= FindById(id
);
452 if ( tool
->Enable(enable
) )
454 DoEnableTool(tool
, enable
);
459 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
461 wxToolBarToolBase
*tool
= FindById(id
);
462 if ( tool
&& tool
->CanBeToggled() )
464 if ( tool
->Toggle(toggle
) )
466 UnToggleRadioGroup(tool
);
467 DoToggleTool(tool
, toggle
);
472 void wxToolBarBase::SetToggle(int id
, bool toggle
)
474 wxToolBarToolBase
*tool
= FindById(id
);
477 if ( tool
->SetToggle(toggle
) )
479 DoSetToggle(tool
, toggle
);
484 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
486 wxToolBarToolBase
*tool
= FindById(id
);
489 (void)tool
->SetShortHelp(help
);
493 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
495 wxToolBarToolBase
*tool
= FindById(id
);
498 (void)tool
->SetLongHelp(help
);
502 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
504 wxToolBarToolBase
*tool
= FindById(id
);
506 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
509 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
511 wxToolBarToolBase
*tool
= FindById(id
);
513 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
515 tool
->SetClientData(clientData
);
518 int wxToolBarBase::GetToolPos(int id
) const
521 wxToolBarToolsList::compatibility_iterator node
;
523 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
525 if ( node
->GetData()->GetId() == id
)
534 bool wxToolBarBase::GetToolState(int id
) const
536 wxToolBarToolBase
*tool
= FindById(id
);
537 wxCHECK_MSG( tool
, false, _T("no such tool") );
539 return tool
->IsToggled();
542 bool wxToolBarBase::GetToolEnabled(int id
) const
544 wxToolBarToolBase
*tool
= FindById(id
);
545 wxCHECK_MSG( tool
, false, _T("no such tool") );
547 return tool
->IsEnabled();
550 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
552 wxToolBarToolBase
*tool
= FindById(id
);
553 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
555 return tool
->GetShortHelp();
558 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
560 wxToolBarToolBase
*tool
= FindById(id
);
561 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
563 return tool
->GetLongHelp();
566 // ----------------------------------------------------------------------------
567 // wxToolBarBase geometry
568 // ----------------------------------------------------------------------------
570 void wxToolBarBase::SetMargins(int x
, int y
)
576 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 // Only allow toggle if returns true
586 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
588 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
589 event
.SetEventObject(this);
591 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
592 event
.SetInt((int)toggleDown
);
594 // and SetExtraLong() for backwards compatibility
595 event
.SetExtraLong((long)toggleDown
);
597 // Send events to this toolbar instead (and thence up the window hierarchy)
598 GetEventHandler()->ProcessEvent(event
);
603 // Call when right button down.
604 void wxToolBarBase::OnRightClick(int id
,
608 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
609 event
.SetEventObject(this);
612 GetEventHandler()->ProcessEvent(event
);
615 // Called when the mouse cursor enters a tool bitmap (no button pressed).
616 // Argument is wxID_ANY if mouse is exiting the toolbar.
617 // Note that for this event, the id of the window is used,
618 // and the integer parameter of wxCommandEvent is used to retrieve
620 void wxToolBarBase::OnMouseEnter(int id
)
622 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
623 event
.SetEventObject(this);
626 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
630 wxToolBarToolBase
* tool
= id
== wxID_ANY
? (wxToolBarToolBase
*)NULL
: FindById(id
);
632 help
= tool
->GetLongHelp();
633 frame
->DoGiveHelp( help
, id
!= wxID_ANY
);
636 (void)GetEventHandler()->ProcessEvent(event
);
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
644 void wxToolBarBase::UpdateWindowUI(long flags
)
646 wxWindowBase::UpdateWindowUI(flags
);
648 // There is no sense in updating the toolbar UI
649 // if the parent window is about to get destroyed
650 wxWindow
*tlw
= wxGetTopLevelParent( this );
651 if (tlw
&& wxPendingDelete
.Member( tlw
))
654 wxEvtHandler
* evtHandler
= GetEventHandler() ;
656 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
658 node
= node
->GetNext() )
660 int id
= node
->GetData()->GetId();
662 wxUpdateUIEvent
event(id
);
663 event
.SetEventObject(this);
665 if ( evtHandler
->ProcessEvent(event
) )
667 if ( event
.GetSetEnabled() )
668 EnableTool(id
, event
.GetEnabled());
669 if ( event
.GetSetChecked() )
670 ToggleTool(id
, event
.GetChecked());
672 if ( event
.GetSetText() )
679 #if WXWIN_COMPATIBILITY_2_8
681 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
)
684 out
= in
.ConvertToGreyscale();
687 #endif // wxUSE_IMAGE
692 #endif // WXWIN_COMPATIBILITY_2_8
694 #endif // wxUSE_TOOLBAR