-// ============================================================================
-// declarations
-// ============================================================================
-
-// ----------------------------------------------------------------------------
-// headers
-// ----------------------------------------------------------------------------
-
-#ifdef __GNUG__
- #pragma implementation "grid.h"
-#endif
-
-// For compilers that support precompilatixon, includes "wx/wx.h".
-#include "wx/wxprec.h"
-
-#include "wx/defs.h"
-
-#ifdef __BORLANDC__
- #pragma hdrstop
-#endif
-
-#if wxUSE_GRID
-
-#ifndef WX_PRECOMP
- #include "wx/utils.h"
- #include "wx/dcclient.h"
- #include "wx/settings.h"
- #include "wx/log.h"
- #include "wx/textctrl.h"
- #include "wx/checkbox.h"
- #include "wx/combobox.h"
- #include "wx/valtext.h"
-#endif
-
-#include "wx/textfile.h"
-#include "wx/spinctrl.h"
-#include "wx/tokenzr.h"
-
-#include "wx/grid.h"
-#include "wx/generic/gridsel.h"
-
-#if defined(__WXMOTIF__)
- #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
-#else
- #define WXUNUSED_MOTIF(identifier) identifier
-#endif
-
-#if defined(__WXGTK__)
- #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
-#else
- #define WXUNUSED_GTK(identifier) identifier
-#endif
-
-// Required for wxIs... functions
-#include <ctype.h>
-
-// ----------------------------------------------------------------------------
-// array classes
-// ----------------------------------------------------------------------------
-
-WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr *, wxArrayAttrs);
-
-struct wxGridCellWithAttr
-{
- wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
- : coords(row, col), attr(attr_)
- {
- }
-
- ~wxGridCellWithAttr()
- {
- attr->DecRef();
- }
-
- wxGridCellCoords coords;
- wxGridCellAttr *attr;
-
-// Cannot do this:
-// DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
-// without rewriting the macros, which require a public copy constructor.
-};
-
-WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray);
-
-#include "wx/arrimpl.cpp"
-
-WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
-WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
-
-// ----------------------------------------------------------------------------
-// events
-// ----------------------------------------------------------------------------
-
-DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK)
-DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK)
-DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK)
-DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK)
-DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK)
-DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK)
-DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK)
-DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK)
-DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE)
-DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE)
-DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT)
-DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE)
-DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL)
-DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN)
-DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN)
-DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED)
-
-// ----------------------------------------------------------------------------
-// private classes
-// ----------------------------------------------------------------------------
-
-class WXDLLEXPORT wxGridRowLabelWindow : public wxWindow
-{
-public:
- wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; }
- wxGridRowLabelWindow( wxGrid *parent, wxWindowID id,
- const wxPoint &pos, const wxSize &size );
-
-private:
- wxGrid *m_owner;
-
- void OnPaint( wxPaintEvent& event );
- void OnMouseEvent( wxMouseEvent& event );
- void OnMouseWheel( wxMouseEvent& event );
- void OnKeyDown( wxKeyEvent& event );
- void OnKeyUp( wxKeyEvent& );
-
- DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow)
- DECLARE_EVENT_TABLE()
- DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow)
-};
-
-
-class WXDLLEXPORT wxGridColLabelWindow : public wxWindow
-{
-public:
- wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; }
- wxGridColLabelWindow( wxGrid *parent, wxWindowID id,
- const wxPoint &pos, const wxSize &size );
-
-private:
- wxGrid *m_owner;
-
- void OnPaint( wxPaintEvent &event );
- void OnMouseEvent( wxMouseEvent& event );
- void OnMouseWheel( wxMouseEvent& event );
- void OnKeyDown( wxKeyEvent& event );
- void OnKeyUp( wxKeyEvent& );
-
- DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow)
- DECLARE_EVENT_TABLE()
- DECLARE_NO_COPY_CLASS(wxGridColLabelWindow)
-};
-
-
-class WXDLLEXPORT wxGridCornerLabelWindow : public wxWindow
-{
-public:
- wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; }
- wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id,
- const wxPoint &pos, const wxSize &size );
-
-private:
- wxGrid *m_owner;
-
- void OnMouseEvent( wxMouseEvent& event );
- void OnMouseWheel( wxMouseEvent& event );
- void OnKeyDown( wxKeyEvent& event );
- void OnKeyUp( wxKeyEvent& );
- void OnPaint( wxPaintEvent& event );
-
- DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow)
- DECLARE_EVENT_TABLE()
- DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow)
-};
-
-class WXDLLEXPORT wxGridWindow : public wxWindow
-{
-public:
- wxGridWindow()
- {
- m_owner = (wxGrid *)NULL;
- m_rowLabelWin = (wxGridRowLabelWindow *)NULL;
- m_colLabelWin = (wxGridColLabelWindow *)NULL;
- }
-
- wxGridWindow( wxGrid *parent,
- wxGridRowLabelWindow *rowLblWin,
- wxGridColLabelWindow *colLblWin,
- wxWindowID id, const wxPoint &pos, const wxSize &size );
- ~wxGridWindow();
-
- void ScrollWindow( int dx, int dy, const wxRect *rect );
-
- wxGrid* GetOwner() { return m_owner; }
-
-private:
- wxGrid *m_owner;
- wxGridRowLabelWindow *m_rowLabelWin;
- wxGridColLabelWindow *m_colLabelWin;
-
- void OnPaint( wxPaintEvent &event );
- void OnMouseWheel( wxMouseEvent& event );
- void OnMouseEvent( wxMouseEvent& event );
- void OnKeyDown( wxKeyEvent& );
- void OnKeyUp( wxKeyEvent& );
- void OnEraseBackground( wxEraseEvent& );
-
-
- DECLARE_DYNAMIC_CLASS(wxGridWindow)
- DECLARE_EVENT_TABLE()
- DECLARE_NO_COPY_CLASS(wxGridWindow)
-};
-
-
-
-class wxGridCellEditorEvtHandler : public wxEvtHandler
-{
-public:
- wxGridCellEditorEvtHandler()
- : m_grid(0), m_editor(0)
- { }
- wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
- : m_grid(grid), m_editor(editor)
- { }
-
- void OnKeyDown(wxKeyEvent& event);
- void OnChar(wxKeyEvent& event);
-
-private:
- wxGrid* m_grid;
- wxGridCellEditor* m_editor;
- DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
- DECLARE_EVENT_TABLE()
- DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler)
-};
-
-
-IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler )
-BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
- EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
- EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
-END_EVENT_TABLE()
-
-
-
-// ----------------------------------------------------------------------------
-// the internal data representation used by wxGridCellAttrProvider
-// ----------------------------------------------------------------------------
-
-// this class stores attributes set for cells
-class WXDLLEXPORT wxGridCellAttrData
-{
-public:
- void SetAttr(wxGridCellAttr *attr, int row, int col);
- wxGridCellAttr *GetAttr(int row, int col) const;
- void UpdateAttrRows( size_t pos, int numRows );
- void UpdateAttrCols( size_t pos, int numCols );
-
-private:
- // searches for the attr for given cell, returns wxNOT_FOUND if not found
- int FindIndex(int row, int col) const;
-
- wxGridCellWithAttrArray m_attrs;
-};
-
-// this class stores attributes set for rows or columns
-class WXDLLEXPORT wxGridRowOrColAttrData
-{
-public:
- // empty ctor to suppress warnings
- wxGridRowOrColAttrData() { }
- ~wxGridRowOrColAttrData();
-
- void SetAttr(wxGridCellAttr *attr, int rowOrCol);
- wxGridCellAttr *GetAttr(int rowOrCol) const;
- void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols );
-
-private:
- wxArrayInt m_rowsOrCols;
- wxArrayAttrs m_attrs;
-};
-
-// NB: this is just a wrapper around 3 objects: one which stores cell
-// attributes, and 2 others for row/col ones
-class WXDLLEXPORT wxGridCellAttrProviderData
-{
-public:
- wxGridCellAttrData m_cellAttrs;
- wxGridRowOrColAttrData m_rowAttrs,
- m_colAttrs;
-};
-
-
-// ----------------------------------------------------------------------------
-// data structures used for the data type registry
-// ----------------------------------------------------------------------------
-
-struct wxGridDataTypeInfo
-{
- wxGridDataTypeInfo(const wxString& typeName,
- wxGridCellRenderer* renderer,
- wxGridCellEditor* editor)
- : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
- { }
-
- ~wxGridDataTypeInfo()
- {
- wxSafeDecRef(m_renderer);
- wxSafeDecRef(m_editor);
- }
-
- wxString m_typeName;
- wxGridCellRenderer* m_renderer;
- wxGridCellEditor* m_editor;
-
- DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo)
-};
-
-
-WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray);
-
-
-class WXDLLEXPORT wxGridTypeRegistry
-{
-public:
- wxGridTypeRegistry() {}
- ~wxGridTypeRegistry();
-
- void RegisterDataType(const wxString& typeName,
- wxGridCellRenderer* renderer,
- wxGridCellEditor* editor);
-
- // find one of already registered data types
- int FindRegisteredDataType(const wxString& typeName);
-
- // try to FindRegisteredDataType(), if this fails and typeName is one of
- // standard typenames, register it and return its index
- int FindDataType(const wxString& typeName);
-
- // try to FindDataType(), if it fails see if it is not one of already
- // registered data types with some params in which case clone the
- // registered data type and set params for it
- int FindOrCloneDataType(const wxString& typeName);
-
- wxGridCellRenderer* GetRenderer(int index);
- wxGridCellEditor* GetEditor(int index);
-
-private:
- wxGridDataTypeInfoArray m_typeinfo;
-};
-
-// ----------------------------------------------------------------------------
-// conditional compilation
-// ----------------------------------------------------------------------------
-
-#ifndef WXGRID_DRAW_LINES
-#define WXGRID_DRAW_LINES 1
-#endif
-
-// ----------------------------------------------------------------------------
-// globals
-// ----------------------------------------------------------------------------
-
-//#define DEBUG_ATTR_CACHE
-#ifdef DEBUG_ATTR_CACHE
- static size_t gs_nAttrCacheHits = 0;
- static size_t gs_nAttrCacheMisses = 0;
-#endif // DEBUG_ATTR_CACHE
-
-// ----------------------------------------------------------------------------
-// constants
-// ----------------------------------------------------------------------------
-
-wxGridCellCoords wxGridNoCellCoords( -1, -1 );
-wxRect wxGridNoCellRect( -1, -1, -1, -1 );
-
-// scroll line size
-// TODO: this doesn't work at all, grid cells have different sizes and approx
-// calculations don't work as because of the size mismatch scrollbars
-// sometimes fail to be shown when they should be or vice versa
-//
-// The scroll bars may be a little flakey once in a while, but that is
-// surely much less horrible than having scroll lines of only 1!!!
-// -- Robin
-//
-// Well, it's still seriously broken so it might be better but needs
-// fixing anyhow
-// -- Vadim
-static const size_t GRID_SCROLL_LINE_X = 15; // 1;
-static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X;
-
-// the size of hash tables used a bit everywhere (the max number of elements
-// in these hash tables is the number of rows/columns)
-static const int GRID_HASH_SIZE = 100;
-
-// ----------------------------------------------------------------------------
-// private functions
-// ----------------------------------------------------------------------------
-
-static inline int GetScrollX(int x)
-{
- return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X;
-}
-
-static inline int GetScrollY(int y)
-{
- return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y;
-}
-
-// ============================================================================
-// implementation
-// ============================================================================
-
-// ----------------------------------------------------------------------------
-// wxGridCellEditor
-// ----------------------------------------------------------------------------
-
-wxGridCellEditor::wxGridCellEditor()
-{
- m_control = NULL;
- m_attr = NULL;
-}
-
-
-wxGridCellEditor::~wxGridCellEditor()
-{
- Destroy();
-}
-
-void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
- wxWindowID WXUNUSED(id),
- wxEvtHandler* evtHandler)
-{
- if ( evtHandler )
- m_control->PushEventHandler(evtHandler);
-}
-
-void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
- wxGridCellAttr *attr)
-{
- // erase the background because we might not fill the cell
- wxClientDC dc(m_control->GetParent());
- wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow);
- if (gridWindow)
- gridWindow->GetOwner()->PrepareDC(dc);
-
- dc.SetPen(*wxTRANSPARENT_PEN);
- dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
- dc.DrawRectangle(rectCell);
-
- // redraw the control we just painted over
- m_control->Refresh();
-}
-
-void wxGridCellEditor::Destroy()
-{
- if (m_control)
- {
- m_control->PopEventHandler(TRUE /* delete it*/);
-
- m_control->Destroy();
- m_control = NULL;
- }
-}
-
-void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
- m_control->Show(show);
-
- if ( show )
- {
- // set the colours/fonts if we have any
- if ( attr )
- {
- m_colFgOld = m_control->GetForegroundColour();
- m_control->SetForegroundColour(attr->GetTextColour());
-
- m_colBgOld = m_control->GetBackgroundColour();
- m_control->SetBackgroundColour(attr->GetBackgroundColour());
-
- m_fontOld = m_control->GetFont();
- m_control->SetFont(attr->GetFont());
-
- // can't do anything more in the base class version, the other
- // attributes may only be used by the derived classes
- }
- }
- else
- {
- // restore the standard colours fonts
- if ( m_colFgOld.Ok() )
- {
- m_control->SetForegroundColour(m_colFgOld);
- m_colFgOld = wxNullColour;
- }
-
- if ( m_colBgOld.Ok() )
- {
- m_control->SetBackgroundColour(m_colBgOld);
- m_colBgOld = wxNullColour;
- }
-
- if ( m_fontOld.Ok() )
- {
- m_control->SetFont(m_fontOld);
- m_fontOld = wxNullFont;
- }
- }
-}
-
-void wxGridCellEditor::SetSize(const wxRect& rect)
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
- m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
-}
-
-void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
-{
- event.Skip();
-}
-
-bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event)
-{
- // accept the simple key presses, not anything with Ctrl/Alt/Meta
- return !(event.ControlDown() || event.AltDown());
-}
-
-void wxGridCellEditor::StartingKey(wxKeyEvent& event)
-{
- event.Skip();
-}
-
-void wxGridCellEditor::StartingClick()
-{
-}
-
-#if wxUSE_TEXTCTRL
-
-// ----------------------------------------------------------------------------
-// wxGridCellTextEditor
-// ----------------------------------------------------------------------------
-
-wxGridCellTextEditor::wxGridCellTextEditor()
-{
- m_maxChars = 0;
-}
-
-void wxGridCellTextEditor::Create(wxWindow* parent,
- wxWindowID id,
- wxEvtHandler* evtHandler)
-{
- m_control = new wxTextCtrl(parent, id, wxEmptyString,
- wxDefaultPosition, wxDefaultSize
-#if defined(__WXMSW__)
- , wxTE_PROCESS_TAB | wxTE_MULTILINE |
- wxTE_NO_VSCROLL | wxTE_AUTO_SCROLL
-#endif
- );
-
- // set max length allowed in the textctrl, if the parameter was set
- if (m_maxChars != 0)
- {
- ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars);
- }
-
- wxGridCellEditor::Create(parent, id, evtHandler);
-}
-
-void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
- wxGridCellAttr * WXUNUSED(attr))
-{
- // as we fill the entire client area, don't do anything here to minimize
- // flicker
-}
-
-void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
-{
- wxRect rect(rectOrig);
-
- // Make the edit control large enough to allow for internal
- // margins
- //
- // TODO: remove this if the text ctrl sizing is improved esp. for
- // unix
- //
-#if defined(__WXGTK__)
- if (rect.x != 0)
- {
- rect.x += 1;
- rect.y += 1;
- rect.width -= 1;
- rect.height -= 1;
- }
-#else // !GTK
- int extra_x = ( rect.x > 2 )? 2 : 1;
-
-// MB: treat MSW separately here otherwise the caret doesn't show
-// when the editor is in the first row.
-#if defined(__WXMSW__)
- int extra_y = 2;
-#else
- int extra_y = ( rect.y > 2 )? 2 : 1;
-#endif // MSW
-
-#if defined(__WXMOTIF__)
- extra_x *= 2;
- extra_y *= 2;
-#endif
- rect.SetLeft( wxMax(0, rect.x - extra_x) );
- rect.SetTop( wxMax(0, rect.y - extra_y) );
- rect.SetRight( rect.GetRight() + 2*extra_x );
- rect.SetBottom( rect.GetBottom() + 2*extra_y );
-#endif // GTK/!GTK
-
- wxGridCellEditor::SetSize(rect);
-}
-
-void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
-
- m_startValue = grid->GetTable()->GetValue(row, col);
-
- DoBeginEdit(m_startValue);
-}
-
-void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
-{
- Text()->SetValue(startValue);
- Text()->SetInsertionPointEnd();
- Text()->SetSelection(-1,-1);
- Text()->SetFocus();
-}
-
-bool wxGridCellTextEditor::EndEdit(int row, int col,
- wxGrid* grid)
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
-
- bool changed = FALSE;
- wxString value = Text()->GetValue();
- if (value != m_startValue)
- changed = TRUE;
-
- if (changed)
- grid->GetTable()->SetValue(row, col, value);
-
- m_startValue = wxEmptyString;
- Text()->SetValue(m_startValue);
-
- return changed;
-}
-
-
-void wxGridCellTextEditor::Reset()
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
-
- DoReset(m_startValue);
-}
-
-void wxGridCellTextEditor::DoReset(const wxString& startValue)
-{
- Text()->SetValue(startValue);
- Text()->SetInsertionPointEnd();
-}
-
-bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event)
-{
- if ( wxGridCellEditor::IsAcceptedKey(event) )
- {
- int keycode = event.GetKeyCode();
- switch ( keycode )
- {
- case WXK_NUMPAD0:
- case WXK_NUMPAD1:
- case WXK_NUMPAD2:
- case WXK_NUMPAD3:
- case WXK_NUMPAD4:
- case WXK_NUMPAD5:
- case WXK_NUMPAD6:
- case WXK_NUMPAD7:
- case WXK_NUMPAD8:
- case WXK_NUMPAD9:
- case WXK_MULTIPLY:
- case WXK_NUMPAD_MULTIPLY:
- case WXK_ADD:
- case WXK_NUMPAD_ADD:
- case WXK_SUBTRACT:
- case WXK_NUMPAD_SUBTRACT:
- case WXK_DECIMAL:
- case WXK_NUMPAD_DECIMAL:
- case WXK_DIVIDE:
- case WXK_NUMPAD_DIVIDE:
- return TRUE;
-
- default:
- // accept 8 bit chars too if isprint() agrees
- if ( (keycode < 255) && (isprint(keycode)) )
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
-void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
-{
- if ( !Text()->EmulateKeyPress(event) )
- {
- event.Skip();
- }
-}
-
-void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
- WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
-{
-#if defined(__WXMOTIF__) || defined(__WXGTK__)
- // wxMotif needs a little extra help...
- size_t pos = (size_t)( Text()->GetInsertionPoint() );
- wxString s( Text()->GetValue() );
- s = s.Left(pos) + wxT("\n") + s.Mid(pos);
- Text()->SetValue(s);
- Text()->SetInsertionPoint( pos );
-#else
- // the other ports can handle a Return key press
- //
- event.Skip();
-#endif
-}
-
-void wxGridCellTextEditor::SetParameters(const wxString& params)
-{
- if ( !params )
- {
- // reset to default
- m_maxChars = 0;
- }
- else
- {
- long tmp;
- if ( !params.ToLong(&tmp) )
- {
- wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str());
- }
- else
- {
- m_maxChars = (size_t)tmp;
- }
- }
-}
-
-// return the value in the text control
-wxString wxGridCellTextEditor::GetValue() const
-{
- return Text()->GetValue();
-}
-
-// ----------------------------------------------------------------------------
-// wxGridCellNumberEditor
-// ----------------------------------------------------------------------------
-
-wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
-{
- m_min = min;
- m_max = max;
-}
-
-void wxGridCellNumberEditor::Create(wxWindow* parent,
- wxWindowID id,
- wxEvtHandler* evtHandler)
-{
- if ( HasRange() )
- {
- // create a spin ctrl
- m_control = new wxSpinCtrl(parent, -1, wxEmptyString,
- wxDefaultPosition, wxDefaultSize,
- wxSP_ARROW_KEYS,
- m_min, m_max);
-
- wxGridCellEditor::Create(parent, id, evtHandler);
- }
- else
- {
- // just a text control
- wxGridCellTextEditor::Create(parent, id, evtHandler);
-
-#if wxUSE_VALIDATORS
- Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
-#endif // wxUSE_VALIDATORS
- }
-}
-
-void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
-{
- // first get the value
- wxGridTableBase *table = grid->GetTable();
- if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
- {
- m_valueOld = table->GetValueAsLong(row, col);
- }
- else
- {
- m_valueOld = 0;
- wxString sValue = table->GetValue(row, col);
- if (! sValue.ToLong(&m_valueOld) && ! sValue.IsEmpty())
- {
- wxFAIL_MSG( _T("this cell doesn't have numeric value") );
- return;
- }
- }
-
- if ( HasRange() )
- {
- Spin()->SetValue((int)m_valueOld);
- Spin()->SetFocus();
- }
- else
- {
- DoBeginEdit(GetString());
- }
-}
-
-bool wxGridCellNumberEditor::EndEdit(int row, int col,
- wxGrid* grid)
-{
- bool changed;
- long value = 0;
- wxString text;
-
- if ( HasRange() )
- {
- value = Spin()->GetValue();
- changed = value != m_valueOld;
- if (changed)
- text = wxString::Format(wxT("%ld"), value);
- }
- else
- {
- text = Text()->GetValue();
- changed = (text.IsEmpty() || text.ToLong(&value)) && (value != m_valueOld);
- }
-
- if ( changed )
- {
- if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
- grid->GetTable()->SetValueAsLong(row, col, value);
- else
- grid->GetTable()->SetValue(row, col, text);
- }
-
- return changed;
-}
-
-void wxGridCellNumberEditor::Reset()
-{
- if ( HasRange() )
- {
- Spin()->SetValue((int)m_valueOld);
- }
- else
- {
- DoReset(GetString());
- }
-}
-
-bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event)
-{
- if ( wxGridCellEditor::IsAcceptedKey(event) )
- {
- int keycode = event.GetKeyCode();
- switch ( keycode )
- {
- case WXK_NUMPAD0:
- case WXK_NUMPAD1:
- case WXK_NUMPAD2:
- case WXK_NUMPAD3:
- case WXK_NUMPAD4:
- case WXK_NUMPAD5:
- case WXK_NUMPAD6:
- case WXK_NUMPAD7:
- case WXK_NUMPAD8:
- case WXK_NUMPAD9:
- case WXK_ADD:
- case WXK_NUMPAD_ADD:
- case WXK_SUBTRACT:
- case WXK_NUMPAD_SUBTRACT:
- case WXK_UP:
- case WXK_DOWN:
- return TRUE;
-
- default:
- if ( (keycode < 128) && isdigit(keycode) )
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
-void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
-{
- if ( !HasRange() )
- {
- int keycode = event.GetKeyCode();
- if ( isdigit(keycode) || keycode == '+' || keycode == '-'
- || keycode == WXK_NUMPAD0
- || keycode == WXK_NUMPAD1
- || keycode == WXK_NUMPAD2
- || keycode == WXK_NUMPAD3
- || keycode == WXK_NUMPAD4
- || keycode == WXK_NUMPAD5
- || keycode == WXK_NUMPAD6
- || keycode == WXK_NUMPAD7
- || keycode == WXK_NUMPAD8
- || keycode == WXK_NUMPAD9
- || keycode == WXK_ADD
- || keycode == WXK_NUMPAD_ADD
- || keycode == WXK_SUBTRACT
- || keycode == WXK_NUMPAD_SUBTRACT)
- {
- wxGridCellTextEditor::StartingKey(event);
-
- // skip Skip() below
- return;
- }
- }
-
- event.Skip();
-}
-
-void wxGridCellNumberEditor::SetParameters(const wxString& params)
-{
- if ( !params )
- {
- // reset to default
- m_min =
- m_max = -1;
- }
- else
- {
- long tmp;
- if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
- {
- m_min = (int)tmp;
-
- if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
- {
- m_max = (int)tmp;
-
- // skip the error message below
- return;
- }
- }
-
- wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str());
- }
-}
-
-// return the value in the spin control if it is there (the text control otherwise)
-wxString wxGridCellNumberEditor::GetValue() const
-{
- wxString s;
-
- if( HasRange() )
- {
- long value = Spin()->GetValue();
- s.Printf(wxT("%ld"), value);
- }
- else
- {
- s = Text()->GetValue();
- }
- return s;
-}
-
-// ----------------------------------------------------------------------------
-// wxGridCellFloatEditor
-// ----------------------------------------------------------------------------
-
-wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision)
-{
- m_width = width;
- m_precision = precision;
-}
-
-void wxGridCellFloatEditor::Create(wxWindow* parent,
- wxWindowID id,
- wxEvtHandler* evtHandler)
-{
- wxGridCellTextEditor::Create(parent, id, evtHandler);
-
-#if wxUSE_VALIDATORS
- Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
-#endif // wxUSE_VALIDATORS
-}
-
-void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
-{
- // first get the value
- wxGridTableBase *table = grid->GetTable();
- if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
- {
- m_valueOld = table->GetValueAsDouble(row, col);
- }
- else
- {
- m_valueOld = 0.0;
- wxString sValue = table->GetValue(row, col);
- if (! sValue.ToDouble(&m_valueOld) && ! sValue.IsEmpty())
- {
- wxFAIL_MSG( _T("this cell doesn't have float value") );
- return;
- }
- }
-
- DoBeginEdit(GetString());
-}
-
-bool wxGridCellFloatEditor::EndEdit(int row, int col,
- wxGrid* grid)
-{
- double value = 0.0;
- wxString text(Text()->GetValue());
-
- if ( (text.IsEmpty() || text.ToDouble(&value)) && (value != m_valueOld) )
- {
- if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
- grid->GetTable()->SetValueAsDouble(row, col, value);
- else
- grid->GetTable()->SetValue(row, col, text);
-
- return TRUE;
- }
- return FALSE;
-}
-
-void wxGridCellFloatEditor::Reset()
-{
- DoReset(GetString());
-}
-
-void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
-{
- int keycode = event.GetKeyCode();
- if ( isdigit(keycode) || keycode == '+' || keycode == '-' || keycode == '.'
- || keycode == WXK_NUMPAD0
- || keycode == WXK_NUMPAD1
- || keycode == WXK_NUMPAD2
- || keycode == WXK_NUMPAD3
- || keycode == WXK_NUMPAD4
- || keycode == WXK_NUMPAD5
- || keycode == WXK_NUMPAD6
- || keycode == WXK_NUMPAD7
- || keycode == WXK_NUMPAD8
- || keycode == WXK_NUMPAD9
- || keycode == WXK_ADD
- || keycode == WXK_NUMPAD_ADD
- || keycode == WXK_SUBTRACT
- || keycode == WXK_NUMPAD_SUBTRACT)
- {
- wxGridCellTextEditor::StartingKey(event);
-
- // skip Skip() below
- return;
- }
-
- event.Skip();
-}
-
-void wxGridCellFloatEditor::SetParameters(const wxString& params)
-{
- if ( !params )
- {
- // reset to default
- m_width =
- m_precision = -1;
- }
- else
- {
- long tmp;
- if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
- {
- m_width = (int)tmp;
-
- if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
- {
- m_precision = (int)tmp;
-
- // skip the error message below
- return;
- }
- }
-
- wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str());
- }
-}
-
-wxString wxGridCellFloatEditor::GetString() const
-{
- wxString fmt;
- if ( m_width == -1 )
- {
- // default width/precision
- fmt = _T("%f");
- }
- else if ( m_precision == -1 )
- {
- // default precision
- fmt.Printf(_T("%%%d.f"), m_width);
- }
- else
- {
- fmt.Printf(_T("%%%d.%df"), m_width, m_precision);
- }
-
- return wxString::Format(fmt, m_valueOld);
-}
-
-bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
-{
- if ( wxGridCellEditor::IsAcceptedKey(event) )
- {
- int keycode = event.GetKeyCode();
- switch ( keycode )
- {
- case WXK_NUMPAD0:
- case WXK_NUMPAD1:
- case WXK_NUMPAD2:
- case WXK_NUMPAD3:
- case WXK_NUMPAD4:
- case WXK_NUMPAD5:
- case WXK_NUMPAD6:
- case WXK_NUMPAD7:
- case WXK_NUMPAD8:
- case WXK_NUMPAD9:
- case WXK_ADD:
- case WXK_NUMPAD_ADD:
- case WXK_SUBTRACT:
- case WXK_NUMPAD_SUBTRACT:
- case WXK_DECIMAL:
- case WXK_NUMPAD_DECIMAL:
- return TRUE;
-
- default:
- // additionally accept 'e' as in '1e+6'
- if ( (keycode < 128) &&
- (isdigit(keycode) || tolower(keycode) == 'e') )
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
-#endif // wxUSE_TEXTCTRL
-
-#if wxUSE_CHECKBOX
-
-// ----------------------------------------------------------------------------
-// wxGridCellBoolEditor
-// ----------------------------------------------------------------------------
-
-void wxGridCellBoolEditor::Create(wxWindow* parent,
- wxWindowID id,
- wxEvtHandler* evtHandler)
-{
- m_control = new wxCheckBox(parent, id, wxEmptyString,
- wxDefaultPosition, wxDefaultSize,
- wxNO_BORDER);
-
- wxGridCellEditor::Create(parent, id, evtHandler);
-}
-
-void wxGridCellBoolEditor::SetSize(const wxRect& r)
-{
- bool resize = FALSE;
- wxSize size = m_control->GetSize();
- wxCoord minSize = wxMin(r.width, r.height);
-
- // check if the checkbox is not too big/small for this cell
- wxSize sizeBest = m_control->GetBestSize();
- if ( !(size == sizeBest) )
- {
- // reset to default size if it had been made smaller
- size = sizeBest;
-
- resize = TRUE;
- }
-
- if ( size.x >= minSize || size.y >= minSize )
- {
- // leave 1 pixel margin
- size.x = size.y = minSize - 2;
-
- resize = TRUE;
- }
-
- if ( resize )
- {
- m_control->SetSize(size);
- }
-
- // position it in the centre of the rectangle (TODO: support alignment?)
-
-#if defined(__WXGTK__) || defined (__WXMOTIF__)
- // the checkbox without label still has some space to the right in wxGTK,
- // so shift it to the right
- size.x -= 8;
-#elif defined(__WXMSW__)
- // here too, but in other way
- size.x += 1;
- size.y -= 2;
-#endif
-
- int hAlign = wxALIGN_CENTRE;
- int vAlign = wxALIGN_CENTRE;
- if (GetCellAttr())
- GetCellAttr()->GetAlignment(& hAlign, & vAlign);
-
- int x = 0, y = 0;
- if (hAlign == wxALIGN_LEFT)
- {
- x = r.x + 2;
-#ifdef __WXMSW__
- x += 2;
-#endif
- y = r.y + r.height/2 - size.y/2;
- }
- else if (hAlign == wxALIGN_RIGHT)
- {
- x = r.x + r.width - size.x - 2;
- y = r.y + r.height/2 - size.y/2;
- }
- else if (hAlign == wxALIGN_CENTRE)
- {
- x = r.x + r.width/2 - size.x/2;
- y = r.y + r.height/2 - size.y/2;
- }
-
- m_control->Move(x, y);
-}
-
-void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
-{
- m_control->Show(show);
-
- if ( show )
- {
- wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
- CBox()->SetBackgroundColour(colBg);
- }
-}
-
-void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
-
- if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
- m_startValue = grid->GetTable()->GetValueAsBool(row, col);
- else
- {
- wxString cellval( grid->GetTable()->GetValue(row, col) );
- m_startValue = !( !cellval || (cellval == wxT("0")) );
- }
- CBox()->SetValue(m_startValue);
- CBox()->SetFocus();
-}
-
-bool wxGridCellBoolEditor::EndEdit(int row, int col,
- wxGrid* grid)
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
-
- bool changed = FALSE;
- bool value = CBox()->GetValue();
- if ( value != m_startValue )
- changed = TRUE;
-
- if ( changed )
- {
- if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
- grid->GetTable()->SetValueAsBool(row, col, value);
- else
- grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
- }
-
- return changed;
-}
-
-void wxGridCellBoolEditor::Reset()
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
-
- CBox()->SetValue(m_startValue);
-}
-
-void wxGridCellBoolEditor::StartingClick()
-{
- CBox()->SetValue(!CBox()->GetValue());
-}
-
-bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event)
-{
- if ( wxGridCellEditor::IsAcceptedKey(event) )
- {
- int keycode = event.GetKeyCode();
- switch ( keycode )
- {
- case WXK_MULTIPLY:
- case WXK_NUMPAD_MULTIPLY:
- case WXK_ADD:
- case WXK_NUMPAD_ADD:
- case WXK_SUBTRACT:
- case WXK_NUMPAD_SUBTRACT:
- case WXK_SPACE:
- case '+':
- case '-':
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
-// return the value as "1" for true and the empty string for false
-wxString wxGridCellBoolEditor::GetValue() const
-{
- bool bSet = CBox()->GetValue();
- return bSet ? _T("1") : wxEmptyString;
-}
-
-#endif // wxUSE_CHECKBOX
-
-#if wxUSE_COMBOBOX
-
-// ----------------------------------------------------------------------------
-// wxGridCellChoiceEditor
-// ----------------------------------------------------------------------------
-
-wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
- const wxString choices[],
- bool allowOthers)
- : m_allowOthers(allowOthers)
-{
- if ( count )
- {
- m_choices.Alloc(count);
- for ( size_t n = 0; n < count; n++ )
- {
- m_choices.Add(choices[n]);
- }
- }
-}
-
-wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
-{
- wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
- editor->m_allowOthers = m_allowOthers;
- editor->m_choices = m_choices;
-
- return editor;
-}
-
-void wxGridCellChoiceEditor::Create(wxWindow* parent,
- wxWindowID id,
- wxEvtHandler* evtHandler)
-{
- size_t count = m_choices.GetCount();
- wxString *choices = new wxString[count];
- for ( size_t n = 0; n < count; n++ )
- {
- choices[n] = m_choices[n];
- }
-
- m_control = new wxComboBox(parent, id, wxEmptyString,
- wxDefaultPosition, wxDefaultSize,
- count, choices,
- m_allowOthers ? 0 : wxCB_READONLY);
-
- delete [] choices;
-
- wxGridCellEditor::Create(parent, id, evtHandler);
-}
-
-void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
- wxGridCellAttr * attr)
-{
- // as we fill the entire client area, don't do anything here to minimize
- // flicker
-
- // TODO: It doesn't actually fill the client area since the height of a
- // combo always defaults to the standard... Until someone has time to
- // figure out the right rectangle to paint, just do it the normal way...
- wxGridCellEditor::PaintBackground(rectCell, attr);
-}
-
-void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
-{
- wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
-
- m_startValue = grid->GetTable()->GetValue(row, col);
-
- if (m_allowOthers)
- Combo()->SetValue(m_startValue);
- else
- {
- // find the right position, or default to the first if not found
- int pos = Combo()->FindString(m_startValue);
- if (pos == -1)
- pos = 0;
- Combo()->SetSelection(pos);
- }
- Combo()->SetInsertionPointEnd();
- Combo()->SetFocus();
-}
-
-bool wxGridCellChoiceEditor::EndEdit(int row, int col,
- wxGrid* grid)
-{
- wxString value = Combo()->GetValue();
- bool changed = value != m_startValue;
-
- if ( changed )
- grid->GetTable()->SetValue(row, col, value);
-
- m_startValue = wxEmptyString;
- if (m_allowOthers)
- Combo()->SetValue(m_startValue);
- else
- Combo()->SetSelection(0);
-
- return changed;
-}
-
-void wxGridCellChoiceEditor::Reset()
-{
- Combo()->SetValue(m_startValue);
- Combo()->SetInsertionPointEnd();
-}
-
-void wxGridCellChoiceEditor::SetParameters(const wxString& params)
-{
- if ( !params )
- {
- // what can we do?
- return;
- }
-
- m_choices.Empty();
-
- wxStringTokenizer tk(params, _T(','));
- while ( tk.HasMoreTokens() )
- {
- m_choices.Add(tk.GetNextToken());
- }
-}
-
-// return the value in the text control
-wxString wxGridCellChoiceEditor::GetValue() const
-{
- return Combo()->GetValue();
-}
-
-#endif // wxUSE_COMBOBOX
-
-// ----------------------------------------------------------------------------
-// wxGridCellEditorEvtHandler
-// ----------------------------------------------------------------------------
-
-void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
-{
- switch ( event.GetKeyCode() )
- {
- case WXK_ESCAPE:
- m_editor->Reset();
- m_grid->DisableCellEditControl();
- break;
-
- case WXK_TAB:
- m_grid->GetEventHandler()->ProcessEvent( event );
- break;
-
- case WXK_RETURN:
- case WXK_NUMPAD_ENTER:
- if (!m_grid->GetEventHandler()->ProcessEvent(event))
- m_editor->HandleReturn(event);
- break;
-
-
- default:
- event.Skip();
- }
-}
-
-void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
-{
- switch ( event.GetKeyCode() )
- {
- case WXK_ESCAPE:
- case WXK_TAB:
- case WXK_RETURN:
- case WXK_NUMPAD_ENTER:
- break;
-
- default:
- event.Skip();
- }
-}
-
-// ----------------------------------------------------------------------------
-// wxGridCellWorker is an (almost) empty common base class for
-// wxGridCellRenderer and wxGridCellEditor managing ref counting
-// ----------------------------------------------------------------------------
-
-void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
-{
- // nothing to do
-}
-
-wxGridCellWorker::~wxGridCellWorker()
-{
-}
-
-// ============================================================================
-// renderer classes
-// ============================================================================
-
-// ----------------------------------------------------------------------------
-// wxGridCellRenderer
-// ----------------------------------------------------------------------------
-
-void wxGridCellRenderer::Draw(wxGrid& grid,
- wxGridCellAttr& attr,
- wxDC& dc,
- const wxRect& rect,
- int WXUNUSED(row), int WXUNUSED(col),
- bool isSelected)
-{
- dc.SetBackgroundMode( wxSOLID );
-
- // grey out fields if the grid is disabled
- if( grid.IsEnabled() )
- {
- if ( isSelected )
- {
- dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
- }
- else
- {
- dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
- }
- }
- else
- {
- dc.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE), wxSOLID));
- }
-
- dc.SetPen( *wxTRANSPARENT_PEN );
- dc.DrawRectangle(rect);
-}