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"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
45 #include "wx/listimpl.cpp"
47 WX_DEFINE_LIST(wxToolBarToolsList
)
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxToolBarToolBase
, wxObject
)
59 bool wxToolBarToolBase::Enable(bool enable
)
61 if ( m_enabled
== enable
)
69 bool wxToolBarToolBase::Toggle(bool toggle
)
71 wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") );
73 if ( m_toggled
== toggle
)
81 bool wxToolBarToolBase::SetToggle(bool toggle
)
83 wxItemKind kind
= toggle
? wxITEM_CHECK
: wxITEM_NORMAL
;
92 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
)
94 if ( m_shortHelpString
== help
)
97 m_shortHelpString
= help
;
102 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
)
104 if ( m_longHelpString
== help
)
107 m_longHelpString
= help
;
112 // ----------------------------------------------------------------------------
113 // wxToolBarBase adding/deleting items
114 // ----------------------------------------------------------------------------
116 wxToolBarBase::wxToolBarBase()
118 // the list owns the pointers
119 m_xMargin
= m_yMargin
= 0;
120 m_maxRows
= m_maxCols
= 0;
121 m_toolPacking
= m_toolSeparation
= 0;
123 m_defaultHeight
= 15;
126 wxToolBarToolBase
*wxToolBarBase::DoAddTool(int id
,
127 const wxString
& label
,
128 const wxBitmap
& bitmap
,
129 const wxBitmap
& bmpDisabled
,
131 const wxString
& shortHelp
,
132 const wxString
& longHelp
,
133 wxObject
*clientData
,
134 wxCoord
WXUNUSED(xPos
),
135 wxCoord
WXUNUSED(yPos
))
137 InvalidateBestSize();
138 return InsertTool(GetToolsCount(), id
, label
, bitmap
, bmpDisabled
,
139 kind
, shortHelp
, longHelp
, clientData
);
142 wxToolBarToolBase
*wxToolBarBase::InsertTool(size_t pos
,
144 const wxString
& label
,
145 const wxBitmap
& bitmap
,
146 const wxBitmap
& bmpDisabled
,
148 const wxString
& shortHelp
,
149 const wxString
& longHelp
,
150 wxObject
*clientData
)
152 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
153 _T("invalid position in wxToolBar::InsertTool()") );
155 wxToolBarToolBase
*tool
= CreateTool(id
, label
, bitmap
, bmpDisabled
, kind
,
156 clientData
, shortHelp
, longHelp
);
158 if ( !InsertTool(pos
, tool
) )
168 wxToolBarToolBase
*wxToolBarBase::AddTool(wxToolBarToolBase
*tool
)
170 return InsertTool(GetToolsCount(), tool
);
174 wxToolBarBase::InsertTool(size_t pos
, wxToolBarToolBase
*tool
)
176 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
177 _T("invalid position in wxToolBar::InsertTool()") );
179 if ( !tool
|| !DoInsertTool(pos
, tool
) )
184 m_tools
.Insert(pos
, tool
);
189 wxToolBarToolBase
*wxToolBarBase::AddControl(wxControl
*control
)
191 return InsertControl(GetToolsCount(), control
);
194 wxToolBarToolBase
*wxToolBarBase::InsertControl(size_t pos
, wxControl
*control
)
196 wxCHECK_MSG( control
, (wxToolBarToolBase
*)NULL
,
197 _T("toolbar: can't insert NULL control") );
199 wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase
*)NULL
,
200 _T("control must have toolbar as parent") );
202 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
203 _T("invalid position in wxToolBar::InsertControl()") );
205 wxToolBarToolBase
*tool
= CreateTool(control
);
207 if ( !InsertTool(pos
, tool
) )
217 wxControl
*wxToolBarBase::FindControl( int id
)
219 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
221 node
= node
->GetNext() )
223 const wxToolBarToolBase
* const tool
= node
->GetData();
224 if ( tool
->IsControl() )
226 wxControl
* const control
= tool
->GetControl();
230 wxFAIL_MSG( _T("NULL control in toolbar?") );
232 else if ( control
->GetId() == id
)
243 wxToolBarToolBase
*wxToolBarBase::AddSeparator()
245 return InsertSeparator(GetToolsCount());
248 wxToolBarToolBase
*wxToolBarBase::InsertSeparator(size_t pos
)
250 wxCHECK_MSG( pos
<= GetToolsCount(), (wxToolBarToolBase
*)NULL
,
251 _T("invalid position in wxToolBar::InsertSeparator()") );
253 wxToolBarToolBase
*tool
= CreateTool(wxID_SEPARATOR
,
255 wxNullBitmap
, wxNullBitmap
,
256 wxITEM_SEPARATOR
, (wxObject
*)NULL
,
257 wxEmptyString
, wxEmptyString
);
259 if ( !tool
|| !DoInsertTool(pos
, tool
) )
266 m_tools
.Insert(pos
, tool
);
271 wxToolBarToolBase
*wxToolBarBase::RemoveTool(int id
)
274 wxToolBarToolsList::compatibility_iterator node
;
275 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
277 if ( node
->GetData()->GetId() == id
)
285 // don't give any error messages - sometimes we might call RemoveTool()
286 // without knowing whether the tool is or not in the toolbar
287 return (wxToolBarToolBase
*)NULL
;
290 wxToolBarToolBase
*tool
= node
->GetData();
291 if ( !DoDeleteTool(pos
, tool
) )
293 return (wxToolBarToolBase
*)NULL
;
301 bool wxToolBarBase::DeleteToolByPos(size_t pos
)
303 wxCHECK_MSG( pos
< GetToolsCount(), false,
304 _T("invalid position in wxToolBar::DeleteToolByPos()") );
306 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item(pos
);
308 if ( !DoDeleteTool(pos
, node
->GetData()) )
313 delete node
->GetData();
319 bool wxToolBarBase::DeleteTool(int id
)
322 wxToolBarToolsList::compatibility_iterator node
;
323 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
325 if ( node
->GetData()->GetId() == id
)
331 if ( !node
|| !DoDeleteTool(pos
, node
->GetData()) )
336 delete node
->GetData();
342 wxToolBarToolBase
*wxToolBarBase::FindById(int id
) const
344 wxToolBarToolBase
*tool
= (wxToolBarToolBase
*)NULL
;
346 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
348 node
= node
->GetNext() )
350 tool
= node
->GetData();
351 if ( tool
->GetId() == id
)
363 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase
*tool
)
365 wxCHECK_RET( tool
, _T("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
367 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
370 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Find(tool
);
371 wxCHECK_RET( node
, _T("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
373 wxToolBarToolsList::compatibility_iterator nodeNext
= node
->GetNext();
376 wxToolBarToolBase
*toolNext
= nodeNext
->GetData();
378 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
381 if ( toolNext
->Toggle(false) )
383 DoToggleTool(toolNext
, false);
386 nodeNext
= nodeNext
->GetNext();
389 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
392 wxToolBarToolBase
*toolNext
= nodePrev
->GetData();
394 if ( !toolNext
->IsButton() || toolNext
->GetKind() != wxITEM_RADIO
)
397 if ( toolNext
->Toggle(false) )
399 DoToggleTool(toolNext
, false);
402 nodePrev
= nodePrev
->GetPrevious();
406 void wxToolBarBase::ClearTools()
408 while ( GetToolsCount() )
414 bool wxToolBarBase::Realize()
419 wxToolBarBase::~wxToolBarBase()
421 WX_CLEAR_LIST(wxToolBarToolsList
, m_tools
);
423 // notify the frame that it doesn't have a tool bar any longer to avoid
425 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
426 if ( frame
&& frame
->GetToolBar() == this )
428 frame
->SetToolBar(NULL
);
432 // ----------------------------------------------------------------------------
433 // wxToolBarBase tools state
434 // ----------------------------------------------------------------------------
436 void wxToolBarBase::EnableTool(int id
, bool enable
)
438 wxToolBarToolBase
*tool
= FindById(id
);
441 if ( tool
->Enable(enable
) )
443 DoEnableTool(tool
, enable
);
448 void wxToolBarBase::ToggleTool(int id
, bool toggle
)
450 wxToolBarToolBase
*tool
= FindById(id
);
451 if ( tool
&& tool
->CanBeToggled() )
453 if ( tool
->Toggle(toggle
) )
455 UnToggleRadioGroup(tool
);
456 DoToggleTool(tool
, toggle
);
461 void wxToolBarBase::SetToggle(int id
, bool toggle
)
463 wxToolBarToolBase
*tool
= FindById(id
);
466 if ( tool
->SetToggle(toggle
) )
468 DoSetToggle(tool
, toggle
);
473 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
)
475 wxToolBarToolBase
*tool
= FindById(id
);
478 (void)tool
->SetShortHelp(help
);
482 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
)
484 wxToolBarToolBase
*tool
= FindById(id
);
487 (void)tool
->SetLongHelp(help
);
491 wxObject
*wxToolBarBase::GetToolClientData(int id
) const
493 wxToolBarToolBase
*tool
= FindById(id
);
495 return tool
? tool
->GetClientData() : (wxObject
*)NULL
;
498 void wxToolBarBase::SetToolClientData(int id
, wxObject
*clientData
)
500 wxToolBarToolBase
*tool
= FindById(id
);
502 wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") );
504 tool
->SetClientData(clientData
);
507 int wxToolBarBase::GetToolPos(int id
) const
510 wxToolBarToolsList::compatibility_iterator node
;
512 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
514 if ( node
->GetData()->GetId() == id
)
523 bool wxToolBarBase::GetToolState(int id
) const
525 wxToolBarToolBase
*tool
= FindById(id
);
526 wxCHECK_MSG( tool
, false, _T("no such tool") );
528 return tool
->IsToggled();
531 bool wxToolBarBase::GetToolEnabled(int id
) const
533 wxToolBarToolBase
*tool
= FindById(id
);
534 wxCHECK_MSG( tool
, false, _T("no such tool") );
536 return tool
->IsEnabled();
539 wxString
wxToolBarBase::GetToolShortHelp(int id
) const
541 wxToolBarToolBase
*tool
= FindById(id
);
542 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
544 return tool
->GetShortHelp();
547 wxString
wxToolBarBase::GetToolLongHelp(int id
) const
549 wxToolBarToolBase
*tool
= FindById(id
);
550 wxCHECK_MSG( tool
, wxEmptyString
, _T("no such tool") );
552 return tool
->GetLongHelp();
555 // ----------------------------------------------------------------------------
556 // wxToolBarBase geometry
557 // ----------------------------------------------------------------------------
559 void wxToolBarBase::SetMargins(int x
, int y
)
565 void wxToolBarBase::SetRows(int WXUNUSED(nRows
))
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 // Only allow toggle if returns true
575 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
)
577 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, id
);
578 event
.SetEventObject(this);
580 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
581 event
.SetInt((int)toggleDown
);
583 // and SetExtraLong() for backwards compatibility
584 event
.SetExtraLong((long)toggleDown
);
586 // Send events to this toolbar instead (and thence up the window hierarchy)
587 GetEventHandler()->ProcessEvent(event
);
592 // Call when right button down.
593 void wxToolBarBase::OnRightClick(int id
,
597 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
);
598 event
.SetEventObject(this);
601 GetEventHandler()->ProcessEvent(event
);
604 // Called when the mouse cursor enters a tool bitmap (no button pressed).
605 // Argument is wxID_ANY if mouse is exiting the toolbar.
606 // Note that for this event, the id of the window is used,
607 // and the integer parameter of wxCommandEvent is used to retrieve
609 void wxToolBarBase::OnMouseEnter(int id
)
611 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
612 event
.SetEventObject(this);
615 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
619 wxToolBarToolBase
* tool
= id
== wxID_ANY
? (wxToolBarToolBase
*)NULL
: FindById(id
);
621 help
= tool
->GetLongHelp();
622 frame
->DoGiveHelp( help
, id
!= wxID_ANY
);
625 (void)GetEventHandler()->ProcessEvent(event
);
628 // ----------------------------------------------------------------------------
630 // ----------------------------------------------------------------------------
632 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
633 void wxToolBarBase::UpdateWindowUI(long flags
)
635 wxWindowBase::UpdateWindowUI(flags
);
637 // There is no sense in updating the toolbar UI
638 // if the parent window is about to get destroyed
639 wxWindow
*tlw
= wxGetTopLevelParent( this );
640 if (tlw
&& wxPendingDelete
.Member( tlw
))
643 wxEvtHandler
* evtHandler
= GetEventHandler() ;
645 for ( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
647 node
= node
->GetNext() )
649 int id
= node
->GetData()->GetId();
651 wxUpdateUIEvent
event(id
);
652 event
.SetEventObject(this);
654 if ( evtHandler
->ProcessEvent(event
) )
656 if ( event
.GetSetEnabled() )
657 EnableTool(id
, event
.GetEnabled());
658 if ( event
.GetSetChecked() )
659 ToggleTool(id
, event
.GetChecked());
661 if ( event
.GetSetText() )
671 * Make a greyed-out image suitable for disabled buttons.
672 * This code is adapted from wxNewBitmapButton in FL.
675 bool wxCreateGreyedImage(const wxImage
& src
, wxImage
& dst
)
679 unsigned char rBg
, gBg
, bBg
;
682 src
.GetOrFindMaskColour(&rBg
, &gBg
, &bBg
);
683 dst
.SetMaskColour(rBg
, gBg
, bBg
);
685 else // assuming the pixels along the edges are of the background color
687 rBg
= src
.GetRed(0, 0);
688 gBg
= src
.GetGreen(0, 0);
689 bBg
= src
.GetBlue(0, 0);
692 const wxColour
colBg(rBg
, gBg
, bBg
);
694 const wxColour colDark
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
695 const wxColour colLight
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
697 // Second attempt, just making things monochrome
698 const int width
= src
.GetWidth();
699 const int height
= src
.GetHeight();
701 for ( int x
= 0; x
< width
; x
++ )
703 for ( int y
= 0; y
< height
; y
++ )
705 const int r
= src
.GetRed(x
, y
);
706 const int g
= src
.GetGreen(x
, y
);
707 const int b
= src
.GetBlue(x
, y
);
709 if ( r
== rBg
&& g
== gBg
&& b
== bBg
)
711 // Leave the background colour as-is
715 // Change light things to the background colour
717 if ( r
>= (colLight
.Red() - 50) &&
718 g
>= (colLight
.Green() - 50) &&
719 b
>= (colLight
.Blue() - 50) )
723 else // Change dark things to really dark
728 dst
.SetRGB(x
, y
, col
.Red(), col
.Green(), col
.Blue());
735 #endif // wxUSE_IMAGE
737 #endif // wxUSE_TOOLBAR