1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        common/tbarbase.cpp 
   3 // Purpose:     wxToolBarBase implementation 
   4 // Author:      Julian Smart 
   5 // Modified by: VZ at 11.12.99 (wxScrollableToolBar splitted off) 
   8 // Copyright:   (c) Julian Smart and Markus Holzem 
   9 // Licence:     wxWindows license 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  21     #pragma implementation "tbarbase.h" 
  24 // For compilers that support precompilation, includes "wx.h". 
  25 #include "wx/wxprec.h" 
  44 #include "wx/tbarbase.h" 
  46 // ---------------------------------------------------------------------------- 
  48 // ---------------------------------------------------------------------------- 
  50 #if !USE_SHARED_LIBRARY 
  51     BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
) 
  52         EVT_IDLE(wxToolBarBase::OnIdle
) 
  56 #include "wx/listimpl.cpp" 
  58 WX_DEFINE_LIST(wxToolBarToolsList
); 
  60 // ============================================================================ 
  62 // ============================================================================ 
  64 // ---------------------------------------------------------------------------- 
  66 // ---------------------------------------------------------------------------- 
  68 bool wxToolBarToolBase::Enable(bool enable
) 
  70     if ( m_enabled 
== enable 
) 
  78 bool wxToolBarToolBase::Toggle(bool toggle
) 
  80     wxASSERT_MSG( m_isToggle
, _T("can't toggle this tool") ); 
  82     if ( m_toggled 
== toggle 
) 
  90 bool wxToolBarToolBase::SetToggle(bool toggle
) 
  92     if ( m_isToggle 
== toggle 
) 
 100 bool wxToolBarToolBase::SetShortHelp(const wxString
& help
) 
 102     if ( m_shortHelpString 
== help 
) 
 105     m_shortHelpString 
= help
; 
 110 bool wxToolBarToolBase::SetLongHelp(const wxString
& help
) 
 112     if ( m_longHelpString 
== help 
) 
 115     m_longHelpString 
= help
; 
 120 wxToolBarToolBase::~wxToolBarToolBase() 
 124 // ---------------------------------------------------------------------------- 
 125 // wxToolBarBase adding/deleting items 
 126 // ---------------------------------------------------------------------------- 
 128 wxToolBarBase::wxToolBarBase() 
 130     // the list owns the pointers 
 131     m_tools
.DeleteContents(TRUE
); 
 133     m_xMargin 
= m_yMargin 
= 0; 
 135     m_maxRows 
= m_maxCols 
= 0; 
 138 wxToolBarToolBase 
*wxToolBarBase::AddTool(int id
, 
 139                                           const wxBitmap
& bitmap
, 
 140                                           const wxBitmap
& pushedBitmap
, 
 142                                           wxCoord 
WXUNUSED(xPos
), 
 143                                           wxCoord 
WXUNUSED(yPos
), 
 144                                           wxObject 
*clientData
, 
 145                                           const wxString
& helpString1
, 
 146                                           const wxString
& helpString2
) 
 148     return InsertTool(GetToolsCount(), id
, bitmap
, pushedBitmap
, 
 149                       toggle
, clientData
, helpString1
, helpString2
); 
 152 wxToolBarToolBase 
*wxToolBarBase::InsertTool(size_t pos
, 
 154                                              const wxBitmap
& bitmap
, 
 155                                              const wxBitmap
& pushedBitmap
, 
 157                                              wxObject 
*clientData
, 
 158                                              const wxString
& helpString1
, 
 159                                              const wxString
& helpString2
) 
 161     wxCHECK_MSG( pos 
<= GetToolsCount(), (wxToolBarToolBase 
*)NULL
, 
 162                  _T("invalid position in wxToolBar::InsertTool()") ); 
 164     wxToolBarToolBase 
*tool 
= CreateTool(id
, bitmap
, pushedBitmap
, toggle
, 
 165                                          clientData
, helpString1
, helpString2
); 
 167     if ( !tool 
|| !DoInsertTool(pos
, tool
) ) 
 174     m_tools
.Insert(pos
, tool
); 
 179 wxToolBarToolBase 
*wxToolBarBase::AddControl(wxControl 
*control
) 
 181     return InsertControl(GetToolsCount(), control
); 
 184 wxToolBarToolBase 
*wxToolBarBase::InsertControl(size_t pos
, wxControl 
*control
) 
 186     wxCHECK_MSG( control
, (wxToolBarToolBase 
*)NULL
, 
 187                  _T("toolbar: can't insert NULL control") ); 
 189     wxCHECK_MSG( control
->GetParent() == this, (wxToolBarToolBase 
*)NULL
, 
 190                  _T("control must have toolbar as parent") ); 
 192     wxCHECK_MSG( pos 
<= GetToolsCount(), (wxToolBarToolBase 
*)NULL
, 
 193                  _T("invalid position in wxToolBar::InsertControl()") ); 
 195     wxToolBarToolBase 
*tool 
= CreateTool(control
); 
 197     if ( !tool 
|| !DoInsertTool(pos
, tool
) ) 
 204     m_tools
.Insert(pos
, tool
); 
 209 wxToolBarToolBase 
*wxToolBarBase::AddSeparator() 
 211     return InsertSeparator(GetToolsCount()); 
 214 wxToolBarToolBase 
*wxToolBarBase::InsertSeparator(size_t pos
) 
 216     wxCHECK_MSG( pos 
<= GetToolsCount(), (wxToolBarToolBase 
*)NULL
, 
 217                  _T("invalid position in wxToolBar::InsertSeparator()") ); 
 219     wxToolBarToolBase 
*tool 
= CreateTool(wxID_SEPARATOR
, 
 220                                          wxNullBitmap
, wxNullBitmap
, 
 221                                          FALSE
, (wxObject 
*)NULL
, 
 222                                          wxEmptyString
, wxEmptyString
); 
 224     if ( !tool 
|| !DoInsertTool(pos
, tool
) ) 
 231     m_tools
.Insert(pos
, tool
); 
 236 wxToolBarToolBase 
*wxToolBarBase::RemoveTool(int id
) 
 239     wxToolBarToolsList::Node 
*node
; 
 240     for ( node 
= m_tools
.GetFirst(); node
; node 
= node
->GetNext() ) 
 242         if ( node
->GetData()->GetId() == id 
) 
 250         // don't give any error messages - sometimes we might call RemoveTool() 
 251         // without knowing whether the tool is or not in the toolbar 
 252         return (wxToolBarToolBase 
*)NULL
; 
 255     wxToolBarToolBase 
*tool 
= node
->GetData(); 
 256     if ( !DoDeleteTool(pos
, tool
) ) 
 258         return (wxToolBarToolBase 
*)NULL
; 
 261     // the node would delete the data, so set it to NULL to avoid this 
 264     m_tools
.DeleteNode(node
); 
 269 bool wxToolBarBase::DeleteToolByPos(size_t pos
) 
 271     wxCHECK_MSG( pos 
< GetToolsCount(), FALSE
, 
 272                  _T("invalid position in wxToolBar::DeleteToolByPos()") ); 
 274     wxToolBarToolsList::Node 
*node 
= m_tools
.Item(pos
); 
 276     if ( !DoDeleteTool(pos
, node
->GetData()) ) 
 281     m_tools
.DeleteNode(node
); 
 286 bool wxToolBarBase::DeleteTool(int id
) 
 289     wxToolBarToolsList::Node 
*node
; 
 290     for ( node 
= m_tools
.GetFirst(); node
; node 
= node
->GetNext() ) 
 292         if ( node
->GetData()->GetId() == id 
) 
 298     if ( !node 
|| !DoDeleteTool(pos
, node
->GetData()) ) 
 303     m_tools
.DeleteNode(node
); 
 308 wxToolBarToolBase 
*wxToolBarBase::FindById(int id
) const 
 310     wxToolBarToolBase 
*tool 
= (wxToolBarToolBase 
*)NULL
; 
 312     for ( wxToolBarToolsList::Node 
*node 
= m_tools
.GetFirst(); 
 314           node 
= node
->GetNext() ) 
 316         tool 
= node
->GetData(); 
 317         if ( tool
->GetId() == id 
) 
 327 void wxToolBarBase::ClearTools() 
 332 bool wxToolBarBase::Realize() 
 337 wxToolBarBase::~wxToolBarBase() 
 341 // ---------------------------------------------------------------------------- 
 342 // wxToolBarBase tools state 
 343 // ---------------------------------------------------------------------------- 
 345 void wxToolBarBase::EnableTool(int id
, bool enable
) 
 347     wxToolBarToolBase 
*tool 
= FindById(id
); 
 350         if ( tool
->Enable(enable
) ) 
 352             DoEnableTool(tool
, enable
); 
 357 void wxToolBarBase::ToggleTool(int id
, bool toggle
) 
 359     wxToolBarToolBase 
*tool 
= FindById(id
); 
 360     if ( tool 
&& tool
->CanBeToggled() ) 
 362         if ( tool
->Toggle(toggle
) ) 
 364             DoToggleTool(tool
, toggle
); 
 369 void wxToolBarBase::SetToggle(int id
, bool toggle
) 
 371     wxToolBarToolBase 
*tool 
= FindById(id
); 
 374         if ( tool
->SetToggle(toggle
) ) 
 376             DoSetToggle(tool
, toggle
); 
 381 void wxToolBarBase::SetToolShortHelp(int id
, const wxString
& help
) 
 383     wxToolBarToolBase 
*tool 
= FindById(id
); 
 386         (void)tool
->SetShortHelp(help
); 
 390 void wxToolBarBase::SetToolLongHelp(int id
, const wxString
& help
) 
 392     wxToolBarToolBase 
*tool 
= FindById(id
); 
 395         (void)tool
->SetLongHelp(help
); 
 399 wxObject 
*wxToolBarBase::GetToolClientData(int id
) const 
 401     wxToolBarToolBase 
*tool 
= FindById(id
); 
 403     return tool 
? tool
->GetClientData() : (wxObject 
*)NULL
; 
 406 void wxToolBarBase::SetToolClientData(int id
, wxObject 
*clientData
) 
 408     wxToolBarToolBase 
*tool 
= FindById(id
); 
 410     wxCHECK_RET( tool
, _T("no such tool in wxToolBar::SetToolClientData") ); 
 412     tool
->SetClientData(clientData
); 
 415 bool wxToolBarBase::GetToolState(int id
) const 
 417     wxToolBarToolBase 
*tool 
= FindById(id
); 
 418     wxCHECK_MSG( tool
, FALSE
, _T("no such tool") ); 
 420     return tool
->IsToggled(); 
 423 bool wxToolBarBase::GetToolEnabled(int id
) const 
 425     wxToolBarToolBase 
*tool 
= FindById(id
); 
 426     wxCHECK_MSG( tool
, FALSE
, _T("no such tool") ); 
 428     return tool
->IsEnabled(); 
 431 wxString 
wxToolBarBase::GetToolShortHelp(int id
) const 
 433     wxToolBarToolBase 
*tool 
= FindById(id
); 
 434     wxCHECK_MSG( tool
, _T(""), _T("no such tool") ); 
 436     return tool
->GetShortHelp(); 
 439 wxString 
wxToolBarBase::GetToolLongHelp(int id
) const 
 441     wxToolBarToolBase 
*tool 
= FindById(id
); 
 442     wxCHECK_MSG( tool
, _T(""), _T("no such tool") ); 
 444     return tool
->GetLongHelp(); 
 447 // ---------------------------------------------------------------------------- 
 448 // wxToolBarBase geometry 
 449 // ---------------------------------------------------------------------------- 
 451 void wxToolBarBase::SetMargins(int x
, int y
) 
 457 void wxToolBarBase::SetRows(int WXUNUSED(nRows
)) 
 462 // ---------------------------------------------------------------------------- 
 464 // ---------------------------------------------------------------------------- 
 466 // Only allow toggle if returns TRUE 
 467 bool wxToolBarBase::OnLeftClick(int id
, bool toggleDown
) 
 469     wxCommandEvent 
event(wxEVT_COMMAND_TOOL_CLICKED
, id
); 
 470     event
.SetEventObject(this); 
 471     event
.SetExtraLong((long) toggleDown
); 
 473     // Send events to this toolbar instead (and thence up the window hierarchy) 
 474     GetEventHandler()->ProcessEvent(event
); 
 479 // Call when right button down. 
 480 void wxToolBarBase::OnRightClick(int id
, 
 484     wxCommandEvent 
event(wxEVT_COMMAND_TOOL_RCLICKED
, id
); 
 485     event
.SetEventObject(this); 
 488     GetEventHandler()->ProcessEvent(event
); 
 491 // Called when the mouse cursor enters a tool bitmap (no button pressed). 
 492 // Argument is -1 if mouse is exiting the toolbar. 
 493 // Note that for this event, the id of the window is used, 
 494 // and the integer parameter of wxCommandEvent is used to retrieve 
 496 void wxToolBarBase::OnMouseEnter(int id
) 
 498     wxCommandEvent 
event(wxEVT_COMMAND_TOOL_ENTER
, GetId()); 
 499     event
.SetEventObject(this); 
 502     (void)GetEventHandler()->ProcessEvent(event
); 
 504     wxToolBarToolBase 
*tool 
= FindById(id
); 
 505     if ( !tool 
|| !tool
->GetLongHelp() ) 
 508     wxFrame 
*frame 
= wxDynamicCast(GetParent(), wxFrame
); 
 512     frame
->SetStatusText(tool
->GetLongHelp()); 
 515 // ---------------------------------------------------------------------------- 
 517 // ---------------------------------------------------------------------------- 
 519 void wxToolBarBase::OnIdle(wxIdleEvent
& event
) 
 526 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers) 
 527 void wxToolBarBase::DoToolbarUpdates() 
 529     wxEvtHandler
* evtHandler 
= GetEventHandler(); 
 531     for ( wxToolBarToolsList::Node
* node 
= m_tools
.GetFirst(); 
 533           node 
= node
->GetNext() ) 
 535         int id 
= node
->GetData()->GetId(); 
 537         wxUpdateUIEvent 
event(id
); 
 538         event
.SetEventObject(this); 
 540         if ( evtHandler
->ProcessEvent(event
) ) 
 542             if ( event
.GetSetEnabled() ) 
 543                 EnableTool(id
, event
.GetEnabled()); 
 544             if ( event
.GetSetChecked() ) 
 545                 ToggleTool(id
, event
.GetChecked()); 
 547             if ( event
.GetSetText() ) 
 554 #endif // wxUSE_TOOLBAR