/////////////////////////////////////////////////////////////////////////////
-// Name: tbarbase.cpp
-// Purpose: Toolbar base classes
+// Name: common/tbarbase.cpp
+// Purpose: wxToolBarBase implementation
// Author: Julian Smart
-// Modified by:
+// Modified by: VZ at 11.12.99 (wxScrollableToolBar splitted off)
// Created: 04/01/98
// RCS-ID: $Id$
// Copyright: (c) Julian Smart and Markus Holzem
-// Licence: wxWindows license
+// Licence: wxWindows license
/////////////////////////////////////////////////////////////////////////////
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
#ifdef __GNUG__
-#pragma implementation "tbarbase.h"
+ #pragma implementation "tbarbase.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
-#pragma hdrstop
+ #pragma hdrstop
#endif
#ifndef WX_PRECOMP
-#include "wx/wx.h"
+ #include "wx/wx.h"
#endif
+#include "wx/frame.h"
+
// For ::UpdateWindow
-#ifdef __WINDOWS__
+#ifdef __WXMSW__
#include <windows.h>
#endif
-#if USE_TOOLBAR
+#if wxUSE_TOOLBAR
#include "wx/tbarbase.h"
+// ----------------------------------------------------------------------------
+// wxWindows macros
+// ----------------------------------------------------------------------------
+
#if !USE_SHARED_LIBRARY
-IMPLEMENT_DYNAMIC_CLASS(wxToolBarBase, wxControl)
-IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool, wxObject)
-
-BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
- EVT_SCROLL(wxToolBarBase::OnScroll)
- EVT_SIZE(wxToolBarBase::OnSize)
- EVT_IDLE(wxToolBarBase::OnIdle)
-END_EVENT_TABLE()
+ BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
+ EVT_IDLE(wxToolBarBase::OnIdle)
+ END_EVENT_TABLE()
#endif
-// Keep a list of all toolbars created, so you can tell whether a toolbar
-// is still valid: a tool may have quit the toolbar.
-static wxList gs_ToolBars;
-
-wxToolBarTool::wxToolBarTool(const int theIndex,
- const wxBitmap& theBitmap1, const wxBitmap& theBitmap2, const bool toggle,
- const long xPos, const long yPos, const wxString& helpS1, const wxString& helpS2)
-{
- m_toolStyle = wxTOOL_STYLE_BUTTON;
- m_clientData = NULL;
- m_index = theIndex;
- m_isToggle = toggle;
- m_toggleState = FALSE;
- m_enabled = TRUE;
- m_bitmap1 = theBitmap1;
- m_bitmap2 = theBitmap2;
- m_x = xPos;
- m_y = yPos;
- m_width = m_height = 0;
- m_deleteSecondBitmap = FALSE;
- if (m_bitmap1.Ok())
- {
- m_width = m_bitmap1.GetWidth()+2;
- m_height = m_bitmap1.GetHeight()+2;
- }
- m_shortHelpString = helpS1;
- m_longHelpString = helpS2;
-}
-
-wxToolBarTool::~wxToolBarTool(void)
-{
-/*
- if (m_deleteSecondBitmap && m_bitmap2)
- delete m_bitmap2;
-*/
-}
-
-
-// class wxToolBar
-
-wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER)
-{
- gs_ToolBars.Append(this);
-
- m_tilingDirection = wxVERTICAL;
- m_rowsOrColumns = 0;
- m_maxWidth = 0;
- m_maxHeight = 0;
- m_defaultWidth = 16;
- m_defaultHeight = 15;
- m_xMargin = 0;
- m_yMargin = 0;
- m_toolPacking = 1;
- m_toolSeparation = 5;
- m_currentTool = -1;
-
- m_xScrollPixelsPerLine = 0;
- m_yScrollPixelsPerLine = 0;
- m_xScrollingEnabled = TRUE;
- m_yScrollingEnabled = TRUE;
- m_xScrollPosition = 0;
- m_yScrollPosition = 0;
- m_calcScrolledOffset = TRUE;
- m_xScrollLines = 0;
- m_yScrollLines = 0;
- m_xScrollLinesPerPage = 0;
- m_yScrollLinesPerPage = 0;
-}
-
-wxToolBarBase::~wxToolBarBase ()
-{
- gs_ToolBars.DeleteObject(this);
-
- for ( wxNode *node = m_tools.First(); node; node = node->Next() )
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- delete tool;
- }
-}
+#include "wx/listimpl.cpp"
-// Only allow toggle if returns TRUE
-bool wxToolBarBase::OnLeftClick(int toolIndex, bool toggleDown)
+WX_DEFINE_LIST(wxToolBarToolsList);
+
+// ============================================================================
+// implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxToolBarToolBase
+// ----------------------------------------------------------------------------
+
+bool wxToolBarToolBase::Enable(bool enable)
{
- wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, toolIndex);
- event.SetEventObject(this);
- event.SetExtraLong((long) toggleDown);
+ if ( m_enabled == enable )
+ return FALSE;
- GetEventHandler()->ProcessEvent(event);
+ m_enabled = enable;
return TRUE;
}
-// Call when right button down.
-void wxToolBarBase::OnRightClick(int toolIndex, long x, long y)
+bool wxToolBarToolBase::Toggle(bool toggle)
{
- wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, toolIndex);
- event.SetEventObject(this);
+ wxASSERT_MSG( m_isToggle, _T("can't toggle this tool") );
- GetEventHandler()->ProcessEvent(event);
-}
+ if ( m_toggled == toggle )
+ return FALSE;
-// Called when the mouse cursor enters a tool bitmap (no button pressed).
-// Argument is -1 if mouse is exiting the toolbar.
-void wxToolBarBase::OnMouseEnter ( int toolIndex )
-{
- wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, toolIndex);
- event.SetEventObject(this);
+ m_toggled = toggle;
- GetEventHandler()->ProcessEvent(event);
+ return TRUE;
}
-// If pushedBitmap is NULL, a reversed version of bitmap is
-// created and used as the pushed/toggled image.
-// If toggle is TRUE, the button toggles between the two states.
-wxToolBarTool *wxToolBarBase::AddTool(const int index, const wxBitmap& bitmap, const wxBitmap& pushedBitmap,
- const bool toggle, const long xPos, const long yPos, wxObject *clientData,
- const wxString& helpString1, const wxString& helpString2)
+bool wxToolBarToolBase::SetToggle(bool toggle)
{
- wxToolBarTool *tool = new wxToolBarTool(index, bitmap, pushedBitmap, toggle, xPos, yPos, helpString1, helpString2);
- tool->m_clientData = clientData;
+ if ( m_isToggle == toggle )
+ return FALSE;
- if (xPos > -1)
- tool->m_x = xPos;
- else
- tool->m_x = m_xMargin;
+ m_isToggle = toggle;
- if (yPos > -1)
- tool->m_y = yPos;
- else
- tool->m_y = m_yMargin;
-
- // Calculate reasonable max size in case Layout() not called
- if ((tool->m_x + bitmap.GetWidth() + m_xMargin) > m_maxWidth)
- m_maxWidth = (tool->m_x + bitmap.GetWidth() + m_xMargin);
-
- if ((tool->m_y + bitmap.GetHeight() + m_yMargin) > m_maxHeight)
- m_maxHeight = (tool->m_y + bitmap.GetHeight() + m_yMargin);
-
- m_tools.Append((long)index, tool);
- return tool;
+ return TRUE;
}
-void wxToolBarBase::AddSeparator ()
+bool wxToolBarToolBase::SetShortHelp(const wxString& help)
{
- wxToolBarTool *tool = new wxToolBarTool;
- tool->m_toolStyle = wxTOOL_STYLE_SEPARATOR;
- m_tools.Append(tool);
+ if ( m_shortHelpString == help )
+ return FALSE;
+
+ m_shortHelpString = help;
+
+ return TRUE;
}
-void wxToolBarBase::ClearTools(void)
+bool wxToolBarToolBase::SetLongHelp(const wxString& help)
{
- m_pressedTool = m_currentTool = -1;
- wxNode *node = m_tools.First();
- while (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- wxNode *nextNode = node->Next();
- delete tool;
- delete node;
- node = nextNode;
- }
+ if ( m_longHelpString == help )
+ return FALSE;
+
+ m_longHelpString = help;
+
+ return TRUE;
}
-void wxToolBarBase::EnableTool(const int index, const bool enable)
+wxToolBarToolBase::~wxToolBarToolBase()
{
- wxNode *node = m_tools.Find((long)index);
- if (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- if (tool)
- tool->m_enabled = enable;
- }
}
-void wxToolBarBase::ToggleTool(const int index, const bool toggle)
+// ----------------------------------------------------------------------------
+// wxToolBarBase adding/deleting items
+// ----------------------------------------------------------------------------
+
+wxToolBarBase::wxToolBarBase()
{
+ // the list owns the pointers
+ m_tools.DeleteContents(TRUE);
+
+ m_xMargin = m_yMargin = 0;
+
+ m_maxRows = m_maxCols = 0;
}
-void wxToolBarBase::SetToggle(const int index, const bool value)
+wxToolBarToolBase *wxToolBarBase::AddTool(int id,
+ const wxBitmap& bitmap,
+ const wxBitmap& pushedBitmap,
+ bool toggle,
+ wxCoord WXUNUSED(xPos),
+ wxCoord WXUNUSED(yPos),
+ wxObject *clientData,
+ const wxString& helpString1,
+ const wxString& helpString2)
{
- wxNode *node=m_tools.Find((long)index);
- if (node){
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- tool->m_isToggle = value;
- }
+ return InsertTool(GetToolsCount(), id, bitmap, pushedBitmap,
+ toggle, clientData, helpString1, helpString2);
}
-bool wxToolBarBase::GetToolState(const int index) const
+wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos,
+ int id,
+ const wxBitmap& bitmap,
+ const wxBitmap& pushedBitmap,
+ bool toggle,
+ wxObject *clientData,
+ const wxString& helpString1,
+ const wxString& helpString2)
{
- wxNode *node = m_tools.Find((long)index);
- if (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- if (tool)
+ wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
+ _T("invalid position in wxToolBar::InsertTool()") );
+
+ wxToolBarToolBase *tool = CreateTool(id, bitmap, pushedBitmap, toggle,
+ clientData, helpString1, helpString2);
+
+ if ( !tool || !DoInsertTool(pos, tool) )
{
- return tool->m_toggleState;
+ delete tool;
+
+ return NULL;
}
- else return FALSE;
- }
- else return FALSE;
+
+ m_tools.Insert(pos, tool);
+
+ return tool;
}
-bool wxToolBarBase::GetToolEnabled(const int index) const
+wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
{
- wxNode *node = m_tools.Find((long)index);
- if (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- if (tool)
- {
- return tool->m_enabled;
- }
- else return FALSE;
- }
- else return FALSE;
+ return InsertControl(GetToolsCount(), control);
}
-wxObject *wxToolBarBase::GetToolClientData(const int index) const
+wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
{
- wxNode *node = m_tools.Find((long)index);
- if (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- if (tool)
+ wxCHECK_MSG( control, (wxToolBarToolBase *)NULL,
+ _T("toolbar: can't insert NULL control") );
+
+ wxCHECK_MSG( control->GetParent() == this, (wxToolBarToolBase *)NULL,
+ _T("control must have toolbar as parent") );
+
+ wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
+ _T("invalid position in wxToolBar::InsertControl()") );
+
+ wxToolBarToolBase *tool = CreateTool(control);
+
+ if ( !tool || !DoInsertTool(pos, tool) )
{
- return tool->m_clientData;
+ delete tool;
+
+ return NULL;
}
- else return NULL;
- }
- else return NULL;
-}
-void wxToolBarBase::SetToolShortHelp(const int index, const wxString& helpString)
-{
- wxNode *node=m_tools.Find((long)index);
- if (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- tool->m_shortHelpString = helpString;
- }
-}
+ m_tools.Insert(pos, tool);
-wxString wxToolBarBase::GetToolShortHelp(const int index) const
-{
- wxNode *node=m_tools.Find((long)index);
- if (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- return tool->m_shortHelpString;
- }
- else
- return wxString("");
+ return tool;
}
-void wxToolBarBase::SetToolLongHelp(const int index, const wxString& helpString)
+wxToolBarToolBase *wxToolBarBase::AddSeparator()
{
- wxNode *node=m_tools.Find((long)index);
- if (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- tool->m_longHelpString = helpString;
- }
+ return InsertSeparator(GetToolsCount());
}
-wxString wxToolBarBase::GetToolLongHelp(const int index) const
+wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
{
- wxNode *node=m_tools.Find((long)index);
- if (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- return tool->m_longHelpString;
- }
- else
- return wxString("");
+ wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
+ _T("invalid position in wxToolBar::InsertSeparator()") );
+
+ wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR,
+ wxNullBitmap, wxNullBitmap,
+ FALSE, (wxObject *)NULL,
+ wxEmptyString, wxEmptyString);
+
+ if ( !tool || !DoInsertTool(pos, tool) )
+ {
+ delete tool;
+
+ return NULL;
+ }
+
+ m_tools.Insert(pos, tool);
+
+ return tool;
}
-wxToolBarTool *wxToolBarBase::FindToolForPosition(const long x, const long y) const
+wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
{
- wxNode *node = m_tools.First();
- while (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- if ((x >= tool->m_x) && (y >= tool->m_y) &&
- (x <= (tool->m_x + tool->GetWidth())) &&
- (y <= (tool->m_y + tool->GetHeight())))
- return tool;
+ size_t pos = 0;
+ wxToolBarToolsList::Node *node;
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
+ {
+ if ( node->GetData()->GetId() == id )
+ break;
+
+ pos++;
+ }
+
+ if ( !node )
+ {
+ // don't give any error messages - sometimes we might call RemoveTool()
+ // without knowing whether the tool is or not in the toolbar
+ return (wxToolBarToolBase *)NULL;
+ }
+
+ wxToolBarToolBase *tool = node->GetData();
+ if ( !DoDeleteTool(pos, tool) )
+ {
+ return (wxToolBarToolBase *)NULL;
+ }
+
+ // the node would delete the data, so set it to NULL to avoid this
+ node->SetData(NULL);
- node = node->Next();
- }
- return NULL;
+ m_tools.DeleteNode(node);
+
+ return tool;
}
-wxSize wxToolBarBase::GetMaxSize ( void ) const
+bool wxToolBarBase::DeleteToolByPos(size_t pos)
{
- return wxSize(m_maxWidth, m_maxHeight);
+ wxCHECK_MSG( pos < GetToolsCount(), FALSE,
+ _T("invalid position in wxToolBar::DeleteToolByPos()") );
+
+ wxToolBarToolsList::Node *node = m_tools.Item(pos);
+
+ if ( !DoDeleteTool(pos, node->GetData()) )
+ {
+ return FALSE;
+ }
+
+ m_tools.DeleteNode(node);
+
+ return TRUE;
}
-// Okay, so we've left the tool we're in ... we must check if
-// the tool we're leaving was a 'sprung push button' and if so,
-// spring it back to the up state.
-//
-void wxToolBarBase::SetMargins(const int x, const int y)
+bool wxToolBarBase::DeleteTool(int id)
{
- m_xMargin = x;
- m_yMargin = y;
+ size_t pos = 0;
+ wxToolBarToolsList::Node *node;
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
+ {
+ if ( node->GetData()->GetId() == id )
+ break;
+
+ pos++;
+ }
+
+ if ( !node || !DoDeleteTool(pos, node->GetData()) )
+ {
+ return FALSE;
+ }
+
+ m_tools.DeleteNode(node);
+
+ return TRUE;
}
-void wxToolBarBase::SetToolPacking(const int packing)
+wxToolBarToolBase *wxToolBarBase::FindById(int id) const
{
- m_toolPacking = packing;
+ wxToolBarToolBase *tool = (wxToolBarToolBase *)NULL;
+
+ for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
+ node;
+ node = node->GetNext() )
+ {
+ tool = node->GetData();
+ if ( tool->GetId() == id )
+ {
+ // found
+ break;
+ }
+ }
+
+ return tool;
}
-void wxToolBarBase::SetToolSeparation(const int separation)
+void wxToolBarBase::ClearTools()
{
- m_toolSeparation = separation;
+ m_tools.Clear();
}
-void wxToolBarBase::Command(wxCommandEvent& event)
+bool wxToolBarBase::Realize()
{
+ return TRUE;
}
-void wxToolBarBase::Layout(void)
+wxToolBarBase::~wxToolBarBase()
{
- m_currentRowsOrColumns = 0;
- m_lastX = m_xMargin;
- m_lastY = m_yMargin;
- int maxToolWidth = 0;
- int maxToolHeight = 0;
- m_maxWidth = 0;
- m_maxHeight = 0;
-
- // Find the maximum tool width and height
- wxNode *node = m_tools.First();
- while (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- if (tool->GetWidth() > maxToolWidth)
- maxToolWidth = (int)tool->GetWidth();
- if (tool->GetHeight() > maxToolHeight)
- maxToolHeight = (int)tool->GetHeight();
- node = node->Next();
- }
+}
- int separatorSize = m_toolSeparation;
+// ----------------------------------------------------------------------------
+// wxToolBarBase tools state
+// ----------------------------------------------------------------------------
- node = m_tools.First();
- while (node)
- {
- wxToolBarTool *tool = (wxToolBarTool *)node->Data();
- if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
- {
- if (m_tilingDirection == wxHORIZONTAL)
- {
- if (m_currentRowsOrColumns >= m_rowsOrColumns)
- m_lastY += separatorSize;
- else
- m_lastX += separatorSize;
- }
- else
- {
- if (m_currentRowsOrColumns >= m_rowsOrColumns)
- m_lastX += separatorSize;
- else
- m_lastY += separatorSize;
- }
- }
- else if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON)
+void wxToolBarBase::EnableTool(int id, bool enable)
+{
+ wxToolBarToolBase *tool = FindById(id);
+ if ( tool )
{
- if (m_tilingDirection == wxHORIZONTAL)
- {
- if (m_currentRowsOrColumns >= m_rowsOrColumns)
+ if ( tool->Enable(enable) )
{
- m_currentRowsOrColumns = 0;
- m_lastX = m_xMargin;
- m_lastY += maxToolHeight + m_toolPacking;
+ DoEnableTool(tool, enable);
}
- tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
- tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
-
- m_lastX += maxToolWidth + m_toolPacking;
- }
- else
- {
- if (m_currentRowsOrColumns >= m_rowsOrColumns)
- {
- m_currentRowsOrColumns = 0;
- m_lastX += (maxToolWidth + m_toolPacking);
- m_lastY = m_yMargin;
- }
- tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
- tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
-
- m_lastY += maxToolHeight + m_toolPacking;
- }
- m_currentRowsOrColumns ++;
- }
-
- if (m_lastX > m_maxWidth)
- m_maxWidth = m_lastX;
- if (m_lastY > m_maxHeight)
- m_maxHeight = m_lastY;
-
- node = node->Next();
- }
- if (m_tilingDirection == wxVERTICAL)
- m_maxWidth += maxToolWidth;
- else
- m_maxHeight += maxToolHeight;
-
- m_maxWidth += m_xMargin;
- m_maxHeight += m_yMargin;
-}
-
-
-// SCROLLING IMPLEMENTATION
-
-/*
- * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
- * noUnitsX/noUnitsY: : no. units per scrollbar
- */
-void wxToolBarBase::SetScrollbars (const int pixelsPerUnitX, const int pixelsPerUnitY,
- const int noUnitsX, const int noUnitsY,
- const int xPos, const int yPos)
-{
- m_xScrollPixelsPerLine = pixelsPerUnitX;
- m_yScrollPixelsPerLine = pixelsPerUnitY;
- m_xScrollLines = noUnitsX;
- m_yScrollLines = noUnitsY;
-
- int w, h;
- GetSize(&w, &h);
-
- // Recalculate scroll bar range and position
- if (m_xScrollLines > 0)
- {
- m_xScrollPosition = xPos;
- SetScrollPos (wxHORIZONTAL, m_xScrollPosition, TRUE);
- }
- else
- {
- SetScrollbar(wxHORIZONTAL, 0, 0, 0, FALSE);
- m_xScrollPosition = 0;
}
+}
- if (m_yScrollLines > 0)
- {
- m_yScrollPosition = yPos;
- SetScrollPos (wxVERTICAL, m_yScrollPosition, TRUE);
- }
- else
+void wxToolBarBase::ToggleTool(int id, bool toggle)
+{
+ wxToolBarToolBase *tool = FindById(id);
+ if ( tool && tool->CanBeToggled() )
{
- SetScrollbar(wxVERTICAL, 0, 0, 0, FALSE);
- m_yScrollPosition = 0;
+ if ( tool->Toggle(toggle) )
+ {
+ DoToggleTool(tool, toggle);
+ }
}
- AdjustScrollbars();
- Refresh();
-#ifdef __WINDOWS__
- ::UpdateWindow ((HWND) GetHWND());
-#endif
}
-
-void wxToolBarBase::OnScroll(wxScrollEvent& event)
-{
- int orient = event.GetOrientation();
-
- int nScrollInc = CalcScrollInc(event);
- if (nScrollInc == 0)
- return;
-
- if (orient == wxHORIZONTAL)
- {
- int newPos = m_xScrollPosition + nScrollInc;
- SetScrollPos(wxHORIZONTAL, newPos, TRUE );
- }
- else
- {
- int newPos = m_yScrollPosition + nScrollInc;
- SetScrollPos(wxVERTICAL, newPos, TRUE );
- }
-
- if (orient == wxHORIZONTAL)
- {
- if (m_xScrollingEnabled)
- ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, NULL);
- else
- Refresh();
- }
- else
- {
- if (m_yScrollingEnabled)
- ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, NULL);
- else
- Refresh();
- }
-
- if (orient == wxHORIZONTAL)
- {
- m_xScrollPosition += nScrollInc;
- }
- else
- {
- m_yScrollPosition += nScrollInc;
- }
-
-}
-
-int wxToolBarBase::CalcScrollInc(wxScrollEvent& event)
-{
- int pos = event.GetPosition();
- int orient = event.GetOrientation();
-
- int nScrollInc = 0;
- switch (event.GetEventType())
- {
- case wxEVENT_TYPE_SCROLL_TOP:
- {
- if (orient == wxHORIZONTAL)
- nScrollInc = - m_xScrollPosition;
- else
- nScrollInc = - m_yScrollPosition;
- break;
- }
- case wxEVENT_TYPE_SCROLL_BOTTOM:
- {
- if (orient == wxHORIZONTAL)
- nScrollInc = m_xScrollLines - m_xScrollPosition;
- else
- nScrollInc = m_yScrollLines - m_yScrollPosition;
- break;
- }
- case wxEVENT_TYPE_SCROLL_LINEUP:
- {
- nScrollInc = -1;
- break;
- }
- case wxEVENT_TYPE_SCROLL_LINEDOWN:
- {
- nScrollInc = 1;
- break;
- }
- case wxEVENT_TYPE_SCROLL_PAGEUP:
- {
- if (orient == wxHORIZONTAL)
- nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
- else
- nScrollInc = -GetScrollPageSize(wxVERTICAL);
- break;
- }
- case wxEVENT_TYPE_SCROLL_PAGEDOWN:
+void wxToolBarBase::SetToggle(int id, bool toggle)
+{
+ wxToolBarToolBase *tool = FindById(id);
+ if ( tool )
{
- if (orient == wxHORIZONTAL)
- nScrollInc = GetScrollPageSize(wxHORIZONTAL);
- else
- nScrollInc = GetScrollPageSize(wxVERTICAL);
- break;
+ if ( tool->SetToggle(toggle) )
+ {
+ DoSetToggle(tool, toggle);
+ }
}
- case wxEVENT_TYPE_SCROLL_THUMBTRACK:
+}
+
+void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
+{
+ wxToolBarToolBase *tool = FindById(id);
+ if ( tool )
{
- if (orient == wxHORIZONTAL)
- nScrollInc = pos - m_xScrollPosition;
- else
- nScrollInc = pos - m_yScrollPosition;
- break;
+ (void)tool->SetShortHelp(help);
}
- default:
+}
+
+void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
+{
+ wxToolBarToolBase *tool = FindById(id);
+ if ( tool )
{
- break;
+ (void)tool->SetLongHelp(help);
}
- }
- if (orient == wxHORIZONTAL)
- {
- int w, h;
- GetClientSize(&w, &h);
-
- int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
- int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
- if (noPositions < 0)
- noPositions = 0;
-
- if ( (m_xScrollPosition + nScrollInc) < 0 )
- nScrollInc = -m_xScrollPosition; // As -ve as we can go
- else if ( (m_xScrollPosition + nScrollInc) > noPositions )
- nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
-
- return nScrollInc;
- }
- else
- {
- int w, h;
- GetClientSize(&w, &h);
-
- int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
- int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
- if (noPositions < 0)
- noPositions = 0;
-
- if ( (m_yScrollPosition + nScrollInc) < 0 )
- nScrollInc = -m_yScrollPosition; // As -ve as we can go
- else if ( (m_yScrollPosition + nScrollInc) > noPositions )
- nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
-
- return nScrollInc;
- }
-}
-
-// Adjust the scrollbars - new version.
-void wxToolBarBase::AdjustScrollbars(void)
-{
- int w, h;
- GetClientSize(&w, &h);
-
- // Recalculate scroll bar range and position
- if (m_xScrollLines > 0)
- {
- int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
- int newRange = (int) ( ((nMaxWidth)/(float)m_xScrollPixelsPerLine) + 0.5 );
- if (newRange < 0)
- newRange = 0;
-
- m_xScrollPosition = wxMin(newRange, m_xScrollPosition);
-
- // Calculate page size i.e. number of scroll units you get on the
- // current client window
- int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
- if (noPagePositions < 1)
- noPagePositions = 1;
-
- SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, newRange);
- SetScrollPageSize(wxHORIZONTAL, noPagePositions);
- }
- if (m_yScrollLines > 0)
- {
- int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
- int newRange = (int) ( ((nMaxHeight)/(float)m_yScrollPixelsPerLine) + 0.5 );
- if (newRange < 0)
- newRange = 0;
-
- m_yScrollPosition = wxMin(newRange, m_yScrollPosition);
-
- // Calculate page size i.e. number of scroll units you get on the
- // current client window
- int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
- if (noPagePositions < 1)
- noPagePositions = 1;
-
- SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, newRange);
- SetScrollPageSize(wxVERTICAL, noPagePositions);
- }
-}
-
-// Default OnSize resets scrollbars, if any
-void wxToolBarBase::OnSize(wxSizeEvent& event)
-{
-#if USE_CONSTRAINTS
- if (GetAutoLayout())
- Layout();
-#endif
+}
- AdjustScrollbars();
+wxObject *wxToolBarBase::GetToolClientData(int id) const
+{
+ wxToolBarToolBase *tool = FindById(id);
+
+ return tool ? tool->GetClientData() : (wxObject *)NULL;
}
-// Prepare the DC by translating it according to the current scroll position
-void wxToolBarBase::PrepareDC(wxDC& dc)
+void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
{
- dc.SetDeviceOrigin(- m_xScrollPosition * m_xScrollPixelsPerLine, - m_yScrollPosition * m_yScrollPixelsPerLine);
+ wxToolBarToolBase *tool = FindById(id);
+
+ wxCHECK_RET( tool, _T("no such tool in wxToolBar::SetToolClientData") );
+
+ tool->SetClientData(clientData);
}
-void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
+bool wxToolBarBase::GetToolState(int id) const
{
- *x_unit = m_xScrollPixelsPerLine;
- *y_unit = m_yScrollPixelsPerLine;
+ wxToolBarToolBase *tool = FindById(id);
+ wxCHECK_MSG( tool, FALSE, _T("no such tool") );
+
+ return tool->IsToggled();
}
-int wxToolBarBase::GetScrollPageSize(int orient) const
+bool wxToolBarBase::GetToolEnabled(int id) const
{
- if ( orient == wxHORIZONTAL )
- return m_xScrollLinesPerPage;
- else
- return m_yScrollLinesPerPage;
+ wxToolBarToolBase *tool = FindById(id);
+ wxCHECK_MSG( tool, FALSE, _T("no such tool") );
+
+ return tool->IsEnabled();
}
-void wxToolBarBase::SetScrollPageSize(int orient, int pageSize)
+wxString wxToolBarBase::GetToolShortHelp(int id) const
{
- if ( orient == wxHORIZONTAL )
- m_xScrollLinesPerPage = pageSize;
- else
- m_yScrollLinesPerPage = pageSize;
+ wxToolBarToolBase *tool = FindById(id);
+ wxCHECK_MSG( tool, _T(""), _T("no such tool") );
+
+ return tool->GetShortHelp();
}
-/*
- * Scroll to given position (scroll position, not pixel position)
- */
-void wxToolBarBase::Scroll (const int x_pos, const int y_pos)
+wxString wxToolBarBase::GetToolLongHelp(int id) const
{
- int old_x, old_y;
- ViewStart (&old_x, &old_y);
- if (((x_pos == -1) || (x_pos == old_x)) && ((y_pos == -1) || (y_pos == old_y)))
- return;
+ wxToolBarToolBase *tool = FindById(id);
+ wxCHECK_MSG( tool, _T(""), _T("no such tool") );
- if (x_pos > -1)
- {
- m_xScrollPosition = x_pos;
- SetScrollPos (wxHORIZONTAL, x_pos, TRUE);
- }
- if (y_pos > -1)
- {
- m_yScrollPosition = y_pos;
- SetScrollPos (wxVERTICAL, y_pos, TRUE);
- }
- Refresh();
-#ifdef __WINDOWS__
- UpdateWindow ((HWND) GetHWND());
-#endif
+ return tool->GetLongHelp();
}
-void wxToolBarBase::EnableScrolling (const bool x_scroll, const bool y_scroll)
+// ----------------------------------------------------------------------------
+// wxToolBarBase geometry
+// ----------------------------------------------------------------------------
+
+void wxToolBarBase::SetMargins(int x, int y)
{
- m_xScrollingEnabled = x_scroll;
- m_yScrollingEnabled = y_scroll;
+ m_xMargin = x;
+ m_yMargin = y;
}
-void wxToolBarBase::GetVirtualSize (int *x, int *y) const
+void wxToolBarBase::SetRows(int WXUNUSED(nRows))
{
- *x = m_xScrollPixelsPerLine * m_xScrollLines;
- *y = m_yScrollPixelsPerLine * m_yScrollLines;
+ // nothing
}
-// Where the current view starts from
-void wxToolBarBase::ViewStart (int *x, int *y) const
+// ----------------------------------------------------------------------------
+// event processing
+// ----------------------------------------------------------------------------
+
+// Only allow toggle if returns TRUE
+bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
{
- *x = m_xScrollPosition;
- *y = m_yScrollPosition;
+ wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
+ event.SetEventObject(this);
+ event.SetExtraLong((long) toggleDown);
+
+ // Send events to this toolbar instead (and thence up the window hierarchy)
+ GetEventHandler()->ProcessEvent(event);
+
+ return TRUE;
}
-/*
-void wxToolBarBase::CalcScrolledPosition(const int x, const int y, int *xx, int *yy) const
+// Call when right button down.
+void wxToolBarBase::OnRightClick(int id,
+ long WXUNUSED(x),
+ long WXUNUSED(y))
{
- *xx = (m_calcScrolledOffset ? (x - m_xScrollPosition * m_xScrollPixelsPerLine) : x);
- *yy = (m_calcScrolledOffset ? (y - m_yScrollPosition * m_yScrollPixelsPerLine) : y);
+ wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
+ event.SetEventObject(this);
+ event.SetInt(id);
+
+ GetEventHandler()->ProcessEvent(event);
}
-void wxToolBarBase::CalcUnscrolledPosition(const int x, const int y, float *xx, float *yy) const
+// Called when the mouse cursor enters a tool bitmap (no button pressed).
+// Argument is -1 if mouse is exiting the toolbar.
+// Note that for this event, the id of the window is used,
+// and the integer parameter of wxCommandEvent is used to retrieve
+// the tool id.
+void wxToolBarBase::OnMouseEnter(int id)
{
- *xx = (float)(m_calcScrolledOffset ? (x + m_xScrollPosition * m_xScrollPixelsPerLine) : x);
- *yy = (float)(m_calcScrolledOffset ? (y + m_yScrollPosition * m_yScrollPixelsPerLine) : y);
+ wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
+ event.SetEventObject(this);
+ event.SetInt(id);
+
+ (void)GetEventHandler()->ProcessEvent(event);
+
+ wxToolBarToolBase *tool = FindById(id);
+ if ( !tool || !tool->GetLongHelp() )
+ return;
+
+ wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
+ if ( !frame )
+ return;
+
+ frame->SetStatusText(tool->GetLongHelp());
}
-*/
+
+// ----------------------------------------------------------------------------
+// UI updates
+// ----------------------------------------------------------------------------
void wxToolBarBase::OnIdle(wxIdleEvent& event)
{
- DoToolbarUpdates();
+ DoToolbarUpdates();
+
+ event.Skip();
}
// Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
-void wxToolBarBase::DoToolbarUpdates(void)
+void wxToolBarBase::DoToolbarUpdates()
{
- wxNode* node = GetTools().First();
- while (node)
- {
- wxToolBarTool* tool = (wxToolBarTool* ) node->Data();
+ wxEvtHandler* evtHandler = GetEventHandler();
- wxUpdateUIEvent event(tool->m_index);
- event.SetEventObject(this);
+ for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
+ node;
+ node = node->GetNext() )
+ {
+ int id = node->GetData()->GetId();
- if (GetEventHandler()->ProcessEvent(event))
- {
- if (event.GetSetEnabled())
- EnableTool(tool->m_index, event.GetEnabled());
- if (event.GetSetChecked())
- ToggleTool(tool->m_index, event.GetChecked());
-/*
- if (event.GetSetText())
- // Set tooltip?
-*/
- }
+ wxUpdateUIEvent event(id);
+ event.SetEventObject(this);
- node = node->Next();
- }
-}
-
-#ifdef __WINDOWS__
-// Circumvent wxControl::MSWOnMouseMove which doesn't set the cursor.
-void wxToolBarBase::MSWOnMouseMove(const int x, const int y, const WXUINT flags)
-{
- wxWindow::MSWOnMouseMove(x, y, flags);
+ if ( evtHandler->ProcessEvent(event) )
+ {
+ if ( event.GetSetEnabled() )
+ EnableTool(id, event.GetEnabled());
+ if ( event.GetSetChecked() )
+ ToggleTool(id, event.GetChecked());
+#if 0
+ if ( event.GetSetText() )
+ // Set tooltip?
+#endif // 0
+ }
+ }
}
-#endif
-#endif
+#endif // wxUSE_TOOLBAR