// Name: src/generic/grid.cpp
// Purpose: wxGrid and related classes
// Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
-// Modified by: Robin Dunn, Vadim Zeitlin
+// Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
// Created: 1/08/1999
// RCS-ID: $Id$
// Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
#include "wx/valtext.h"
#include "wx/intl.h"
#include "wx/math.h"
+ #include "wx/listbox.h"
#endif
#include "wx/textfile.h"
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_COL_MOVE)
DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT)
DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE)
DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL)
public:
wxGridWindow()
{
- m_owner = (wxGrid *)NULL;
- m_rowLabelWin = (wxGridRowLabelWindow *)NULL;
- m_colLabelWin = (wxGridColLabelWindow *)NULL;
+ m_owner = NULL;
+ m_rowLabelWin = NULL;
+ m_colLabelWin = NULL;
}
wxGridWindow( wxGrid *parent,
int key = 0;
bool keyOk = true;
+#ifdef __WXGTK20__
+ // If it's a F-Key or other special key then it shouldn't start the
+ // editor.
+ if (event.GetKeyCode() >= WXK_START)
+ return false;
+#endif
#if wxUSE_UNICODE
// if the unicode key code is not really a unicode character (it may
// be a function key or etc., the platforms appear to always give us a
m_control = new wxTextCtrl(parent, id, wxEmptyString,
wxDefaultPosition, wxDefaultSize
#if defined(__WXMSW__)
- , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL
+ , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL | wxNO_BORDER
#endif
);
rect.width -= 1;
rect.height -= 1;
}
-#else // !GTK
- int extra_x = ( rect.x > 2 ) ? 2 : 1;
+#elif defined(__WXMSW__)
+ if ( rect.x == 0 )
+ rect.x += 2;
+ else
+ rect.x += 3;
-// 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;
+ if ( rect.y == 0 )
+ rect.y += 2;
+ else
+ rect.y += 3;
+
+ rect.width -= 2;
+ rect.height -= 2;
#else
+ int extra_x = ( rect.x > 2 ) ? 2 : 1;
int extra_y = ( rect.y > 2 ) ? 2 : 1;
-#endif
-#if defined(__WXMOTIF__)
- extra_x *= 2;
- extra_y *= 2;
-#endif
+ #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
+#endif
wxGridCellEditor::SetSize(rect);
}
else
{
long tmp;
- if ( !params.ToLong(&tmp) )
+ if ( params.ToLong(&tmp) )
{
- wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str() );
+ m_maxChars = (size_t)tmp;
}
else
{
- m_maxChars = (size_t)tmp;
+ wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str() );
}
}
}
#if wxUSE_VALIDATORS
Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
-#endif // wxUSE_VALIDATORS
+#endif
}
}
#if wxUSE_VALIDATORS
Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
-#endif // wxUSE_VALIDATORS
+#endif
}
void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
{
wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
+ wxT("The wxGridCellEditor must be created first!"));
if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
{
wxGrid* grid)
{
wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
+ wxT("The wxGridCellEditor must be created first!"));
bool changed = false;
bool value = CBox()->GetValue();
void wxGridCellBoolEditor::Reset()
{
wxASSERT_MSG(m_control,
- wxT("The wxGridCellEditor must be Created first!"));
+ wxT("The wxGridCellEditor must be created first!"));
CBox()->SetValue(m_startValue);
}
// 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...
+ // 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!"));
+ wxT("The wxGridCellEditor must be created first!"));
wxGridCellEditorEvtHandler* evtHandler = NULL;
if (m_control)
event.Skip();
break;
}
+
case WXK_END:
{
if ( wholeCellVisible )
default:
event.Skip();
+ break;
}
}
// different coloured text when the grid is disabled
if ( grid.IsEnabled() )
{
- if ( isSelected )
- {
- dc.SetTextBackground( grid.GetSelectionBackground() );
- dc.SetTextForeground( grid.GetSelectionForeground() );
- }
- else
- {
- dc.SetTextBackground( attr.GetBackgroundColour() );
- dc.SetTextForeground( attr.GetTextColour() );
- }
+ if ( isSelected )
+ {
+ dc.SetTextBackground( grid.GetSelectionBackground() );
+ dc.SetTextForeground( grid.GetSelectionForeground() );
+ }
+ else
+ {
+ dc.SetTextBackground( attr.GetBackgroundColour() );
+ dc.SetTextForeground( attr.GetTextColour() );
+ }
}
else
{
- dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
- dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
+ dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
+ dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
}
dc.SetFont( attr.GetFont() );
}
if (is_empty)
+ {
rect.width += grid.GetColSize(i);
+ }
else
{
i--;
wxRect clip = rect;
clip.x += rectCell.width;
// draw each overflow cell individually
- int col_end = col+cell_cols + overflowCols;
+ int col_end = col + cell_cols + overflowCols;
if (col_end >= grid.GetNumberCols())
col_end = grid.GetNumberCols() - 1;
- for (int i = col+cell_cols; i <= col_end; i++)
+ for (int i = col + cell_cols; i <= col_end; i++)
{
clip.width = grid.GetColSize(i) - 1;
dc.DestroyClippingRegion();
// draw a border around checkmark
int vAlign, hAlign;
- attr.GetAlignment(& hAlign, &vAlign);
+ attr.GetAlignment(&hAlign, &vAlign);
wxRect rectBorder;
if (hAlign == wxALIGN_CENTRE)
bool value;
if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
+ {
value = grid.GetTable()->GetValueAsBool(row, col);
+ }
else
{
wxString cellval( grid.GetTable()->GetValue(row, col) );
*vAlign = m_vAlign;
}
else if (m_defGridAttr && m_defGridAttr != this)
+ {
m_defGridAttr->GetAlignment(hAlign, vAlign);
+ }
else
{
wxFAIL_MSG(wxT("Missing default cell attribute"));
// used, otherwise the default editor or renderer is fetched from the grid and
// used. It should be the default for the data type of the cell. If it is
// NULL (because the table has a type that the grid does not have in its
-// registry,) then the grid's default editor or renderer is used.
+// registry), then the grid's default editor or renderer is used.
wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
{
- wxGridCellRenderer *renderer;
+ wxGridCellRenderer *renderer = NULL;
if ( m_renderer && this != m_defGridAttr )
{
// GetDefaultRendererForCell() will do IncRef() for us
renderer = grid->GetDefaultRendererForCell(row, col);
}
- else
- {
- renderer = NULL;
- }
- if ( !renderer )
+ if ( renderer == NULL )
{
- if (m_defGridAttr && this != m_defGridAttr )
+ if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
{
// if we still don't have one then use the grid default
// (no need for IncRef() here neither)
// same as above, except for s/renderer/editor/g
wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
{
- wxGridCellEditor *editor;
+ wxGridCellEditor *editor = NULL;
if ( m_editor && this != m_defGridAttr )
{
// GetDefaultEditorForCell() will do IncRef() for us
editor = grid->GetDefaultEditorForCell(row, col);
}
- else
- {
- editor = NULL;
- }
- if ( !editor )
+ if ( editor == NULL )
{
- if ( m_defGridAttr && this != m_defGridAttr )
+ if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
{
// if we still don't have one then use the grid default
// (no need for IncRef() here neither)
return AppendCols( numCols );
}
+ if ( !m_colLabels.IsEmpty() )
+ {
+ m_colLabels.Insert( wxEmptyString, pos, numCols );
+
+ size_t i;
+ for ( i = pos; i < pos + numCols; i++ )
+ m_colLabels[i] = wxGridTableBase::GetColLabelValue( i );
+ }
+
for ( row = 0; row < curNumRows; row++ )
{
for ( col = pos; col < pos + numCols; col++ )
return false;
}
- if ( numCols > curNumCols - pos )
+ int colID;
+ if ( GetView() )
+ colID = GetView()->GetColAt( pos );
+ else
+ colID = pos;
+
+ if ( numCols > curNumCols - colID )
{
- numCols = curNumCols - pos;
+ numCols = curNumCols - colID;
+ }
+
+ if ( !m_colLabels.IsEmpty() )
+ {
+ m_colLabels.RemoveAt( colID, numCols );
}
for ( row = 0; row < curNumRows; row++ )
}
else
{
- m_data[row].RemoveAt( pos, numCols );
+ m_data[row].RemoveAt( colID, numCols );
}
}
wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
wxWindowID id,
const wxPoint &pos, const wxSize &size )
- : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE )
+ : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE )
{
m_owner = parent;
}
wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 );
#else // !__WXGTK__
- dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW), 1, wxSOLID) );
+ dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
dc.DrawLine( client_width - 1, client_height - 1, client_width - 1, 0 );
dc.DrawLine( client_width - 1, client_height - 1, 0, client_height - 1 );
dc.DrawLine( 0, 0, client_width, 0 );
const wxArrayInt& BorderArray, int nMax,
bool clipToMinMax);
-#define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
- m_minAcceptableColWidth, \
- m_colRights, m_numCols, true)
+#define internalXToCol(x) XToCol(x, true)
#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
m_minAcceptableRowHeight, \
m_rowBottoms, m_numRows, true)
long style, const wxString& name)
{
if (!wxScrolledWindow::Create(parent, id, pos, size,
- style | wxWANTS_CHARS , name))
+ style | wxWANTS_CHARS, name))
return false;
m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE);
// be removed as well as the #else cases below.
#define _USE_VISATTR 0
-#if _USE_VISATTR
-#include "wx/listbox.h"
-#endif
-
void wxGrid::Create()
{
// set to true by CreateGrid
}
else
{
- m_labelBackgroundColour = wxColour( wxT("WHITE") );
+ m_labelBackgroundColour = *wxWHITE;
}
- m_labelTextColour = wxColour( wxT("BLACK") );
+ m_labelTextColour = *wxBLACK;
// init attr cache
m_attrCache.row = -1;
m_cellHighlightPenWidth = 2;
m_cellHighlightROPenWidth = 1;
+ m_canDragColMove = false;
+
m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
m_winCapture = (wxWindow *)NULL;
m_canDragRowSize = true;
for ( int i = 0; i < m_numCols; i++ )
{
- colRight += m_defaultColWidth;
+ colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
m_colRights.Add( colRight );
}
}
int wxGrid::GetColLeft(int col) const
{
- return m_colRights.IsEmpty() ? col * m_defaultColWidth
+ return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
: m_colRights[col] - m_colWidths[col];
}
int wxGrid::GetColRight(int col) const
{
- return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
+ return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
: m_colRights[col];
}
ch -= m_colLabelHeight;
// grid total size
- int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
+ int w = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) + m_extraWidth + 1 : 0;
int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
// take into account editor if shown
if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
- if ( m_colLabelWin && m_colLabelWin->IsShown() )
- m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight);
+ if ( m_colLabelWin && m_colLabelWin->IsShown() )
+ m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight );
if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
- m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight);
+ m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight );
if ( m_gridWin && m_gridWin->IsShown() )
- m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight);
+ m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight );
}
// this is called when the grid table sends a message
int numCols = msg.GetCommandInt2();
m_numCols += numCols;
+ if ( !m_colAt.IsEmpty() )
+ {
+ //Shift the column IDs
+ int i;
+ for ( i = 0; i < m_numCols - numCols; i++ )
+ {
+ if ( m_colAt[i] >= (int)pos )
+ m_colAt[i] += numCols;
+ }
+
+ m_colAt.Insert( pos, pos, numCols );
+
+ //Set the new columns' positions
+ for ( i = pos + 1; i < (int)pos + numCols; i++ )
+ {
+ m_colAt[i] = i;
+ }
+ }
+
if ( !m_colWidths.IsEmpty() )
{
m_colWidths.Insert( m_defaultColWidth, pos, numCols );
int right = 0;
if ( pos > 0 )
- right = m_colRights[pos - 1];
+ right = m_colRights[GetColAt( pos - 1 )];
- for ( i = pos; i < m_numCols; i++ )
+ int colPos;
+ for ( colPos = pos; colPos < m_numCols; colPos++ )
{
+ i = GetColAt( colPos );
+
right += m_colWidths[i];
m_colRights[i] = right;
}
CalcDimensions();
m_colLabelWin->Refresh();
}
-
}
result = true;
break;
int numCols = msg.GetCommandInt();
int oldNumCols = m_numCols;
m_numCols += numCols;
+
+ if ( !m_colAt.IsEmpty() )
+ {
+ m_colAt.Add( 0, numCols );
+
+ //Set the new columns' positions
+ int i;
+ for ( i = oldNumCols; i < m_numCols; i++ )
+ {
+ m_colAt[i] = i;
+ }
+ }
+
if ( !m_colWidths.IsEmpty() )
{
m_colWidths.Add( m_defaultColWidth, numCols );
int right = 0;
if ( oldNumCols > 0 )
- right = m_colRights[oldNumCols - 1];
+ right = m_colRights[GetColAt( oldNumCols - 1 )];
- for ( i = oldNumCols; i < m_numCols; i++ )
+ int colPos;
+ for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
{
+ i = GetColAt( colPos );
+
right += m_colWidths[i];
m_colRights[i] = right;
}
int numCols = msg.GetCommandInt2();
m_numCols -= numCols;
+ if ( !m_colAt.IsEmpty() )
+ {
+ int colID = GetColAt( pos );
+
+ m_colAt.RemoveAt( pos, numCols );
+
+ //Shift the column IDs
+ int colPos;
+ for ( colPos = 0; colPos < m_numCols; colPos++ )
+ {
+ if ( m_colAt[colPos] > colID )
+ m_colAt[colPos] -= numCols;
+ }
+ }
+
if ( !m_colWidths.IsEmpty() )
{
m_colWidths.RemoveAt( pos, numCols );
m_colRights.RemoveAt( pos, numCols );
int w = 0;
- for ( i = 0; i < m_numCols; i++ )
+ int colPos;
+ for ( colPos = 0; colPos < m_numCols; colPos++ )
{
+ i = GetColAt( colPos );
+
w += m_colWidths[i];
m_colRights[i] = w;
}
#if defined(__WXMOTIF__)
int cw, ch;
m_gridWin->GetClientSize( &cw, &ch );
- if ( r.GetTop() > ch ) r.SetTop( 0 );
+ if ( r.GetTop() > ch )
+ r.SetTop( 0 );
r.SetBottom( wxMin( r.GetBottom(), ch ) );
#endif
#if defined(__WXMOTIF__)
int cw, ch;
m_gridWin->GetClientSize( &cw, &ch );
- if ( r.GetLeft() > cw ) r.SetLeft( 0 );
+ if ( r.GetLeft() > cw )
+ r.SetLeft( 0 );
r.SetRight( wxMin( r.GetRight(), cw ) );
#endif
// find the cells within these bounds
//
int col;
- for ( col = internalXToCol(left); col < m_numCols; col++ )
+ int colPos;
+ for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
{
+ col = GetColAt( colPos );
+
if ( GetColRight(col) < left )
continue;
if ( GetRowTop(row) > bottom )
break;
- for ( col = internalXToCol(left); col < m_numCols; col++ )
+ int colPos;
+ for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
{
+ col = GetColAt( colPos );
+
if ( GetColRight(col) <= left )
continue;
{
m_isDragging = true;
m_colLabelWin->CaptureMouse();
+
+ if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL )
+ m_dragRowOrCol = XToCol( x );
}
if ( event.LeftIsDown() )
}
break;
+ case WXGRID_CURSOR_MOVE_COL:
+ {
+ if ( x < 0 )
+ m_moveToCol = GetColAt( 0 );
+ else
+ m_moveToCol = XToCol( x );
+
+ int markerX;
+
+ if ( m_moveToCol < 0 )
+ markerX = GetColRight( GetColAt( m_numCols - 1 ) );
+ else
+ markerX = GetColLeft( m_moveToCol );
+
+ if ( markerX != m_dragLastPos )
+ {
+ wxClientDC dc( m_colLabelWin );
+
+ int cw, ch;
+ m_colLabelWin->GetClientSize( &cw, &ch );
+
+ markerX++;
+
+ //Clean up the last indicator
+ if ( m_dragLastPos >= 0 )
+ {
+ wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 );
+ dc.SetPen(pen);
+ dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
+ dc.SetPen(wxNullPen);
+
+ if ( XToCol( m_dragLastPos ) != -1 )
+ DrawColLabel( dc, XToCol( m_dragLastPos ) );
+ }
+
+ //Moving to the same place? Don't draw a marker
+ if ( (m_moveToCol == m_dragRowOrCol)
+ || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1)
+ || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 )))
+ {
+ m_dragLastPos = -1;
+ return;
+ }
+
+ //Draw the marker
+ wxPen pen( *wxBLUE, 2 );
+ dc.SetPen(pen);
+
+ dc.DrawLine( markerX, 0, markerX, ch );
+
+ dc.SetPen(wxNullPen);
+
+ m_dragLastPos = markerX - 1;
+ }
+ }
+ break;
+
// default label to suppress warnings about "enumeration value
// 'xxx' not handled in switch
default:
if ( col >= 0 &&
!SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
{
- if ( !event.ShiftDown() && !event.CmdDown() )
- ClearSelection();
- if ( m_selection )
+ if ( m_canDragColMove )
{
- if ( event.ShiftDown() )
- {
- m_selection->SelectBlock( 0,
- m_currentCellCoords.GetCol(),
- GetNumberRows() - 1, col,
- event.ControlDown(),
- event.ShiftDown(),
- event.AltDown(),
- event.MetaDown() );
- }
- else
+ //Show button as pressed
+ wxClientDC dc( m_colLabelWin );
+ int colLeft = GetColLeft( col );
+ int colRight = GetColRight( col ) - 1;
+ dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) );
+ dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
+ dc.DrawLine( colLeft, 1, colRight, 1 );
+
+ ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin);
+ }
+ else
+ {
+ if ( !event.ShiftDown() && !event.CmdDown() )
+ ClearSelection();
+ if ( m_selection )
{
- m_selection->SelectCol( col,
- event.ControlDown(),
- event.ShiftDown(),
- event.AltDown(),
- event.MetaDown() );
+ if ( event.ShiftDown() )
+ {
+ m_selection->SelectBlock( 0,
+ m_currentCellCoords.GetCol(),
+ GetNumberRows() - 1, col,
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ }
+ else
+ {
+ m_selection->SelectCol( col,
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ }
}
- }
- ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
+ }
}
}
else
//
else if ( event.LeftUp() )
{
- if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
+ switch ( m_cursorMode )
{
- DoEndDragResizeCol();
+ case WXGRID_CURSOR_RESIZE_COL:
+ DoEndDragResizeCol();
- // Note: we are ending the event *after* doing
- // default processing in this case
- //
- SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
+ // Note: we are ending the event *after* doing
+ // default processing in this case
+ //
+ SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
+ break;
+
+ case WXGRID_CURSOR_MOVE_COL:
+ DoEndDragMoveCol();
+
+ SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event );
+ break;
+
+ case WXGRID_CURSOR_SELECT_COL:
+ case WXGRID_CURSOR_SELECT_CELL:
+ case WXGRID_CURSOR_RESIZE_ROW:
+ case WXGRID_CURSOR_SELECT_ROW:
+ // nothing to do (?)
+ break;
}
ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
_T("RESIZE_ROW"),
_T("RESIZE_COL"),
_T("SELECT_ROW"),
- _T("SELECT_COL")
+ _T("SELECT_COL"),
+ _T("MOVE_COL"),
};
wxLogTrace(_T("grid"),
win->SetCursor( m_colResizeCursor );
break;
+ case WXGRID_CURSOR_MOVE_COL:
+ win->SetCursor( wxCursor(wxCURSOR_HAND) );
+ break;
+
default:
win->SetCursor( *wxSTANDARD_CURSOR );
break;
}
}
+void wxGrid::DoEndDragMoveCol()
+{
+ //The user clicked on the column but didn't actually drag
+ if ( m_dragLastPos < 0 )
+ {
+ m_colLabelWin->Refresh(); //Do this to "unpress" the column
+ return;
+ }
+
+ int newPos;
+ if ( m_moveToCol == -1 )
+ newPos = m_numCols - 1;
+ else
+ {
+ newPos = GetColPos( m_moveToCol );
+ if ( newPos > GetColPos( m_dragRowOrCol ) )
+ newPos--;
+ }
+
+ SetColPos( m_dragRowOrCol, newPos );
+}
+
+void wxGrid::SetColPos( int colID, int newPos )
+{
+ if ( m_colAt.IsEmpty() )
+ {
+ m_colAt.Alloc( m_numCols );
+
+ int i;
+ for ( i = 0; i < m_numCols; i++ )
+ {
+ m_colAt.Add( i );
+ }
+ }
+
+ int oldPos = GetColPos( colID );
+
+ //Reshuffle the m_colAt array
+ if ( newPos > oldPos )
+ {
+ int i;
+ for ( i = oldPos; i < newPos; i++ )
+ {
+ m_colAt[i] = m_colAt[i+1];
+ }
+ }
+ else
+ {
+ int i;
+ for ( i = oldPos; i > newPos; i-- )
+ {
+ m_colAt[i] = m_colAt[i-1];
+ }
+ }
+
+ m_colAt[newPos] = colID;
+
+ //Recalculate the column rights
+ if ( !m_colWidths.IsEmpty() )
+ {
+ int colRight = 0;
+ int colPos;
+ for ( colPos = 0; colPos < m_numCols; colPos++ )
+ {
+ int colID = GetColAt( colPos );
+
+ colRight += m_colWidths[colID];
+ m_colRights[colID] = colRight;
+ }
+ }
+
+ m_colLabelWin->Refresh();
+ m_gridWin->Refresh();
+}
+
+
+
+void wxGrid::EnableDragColMove( bool enable )
+{
+ if ( m_canDragColMove == enable )
+ return;
+
+ m_canDragColMove = enable;
+
+ if ( !m_canDragColMove )
+ {
+ m_colAt.Clear();
+
+ //Recalculate the column rights
+ if ( !m_colWidths.IsEmpty() )
+ {
+ int colRight = 0;
+ int colPos;
+ for ( colPos = 0; colPos < m_numCols; colPos++ )
+ {
+ colRight += m_colWidths[colPos];
+ m_colRights[colPos] = colRight;
+ }
+ }
+
+ m_colLabelWin->Refresh();
+ m_gridWin->Refresh();
+ }
+}
+
+
//
// ------ interaction with data model
//
if ( m_table )
{
- for ( row = 0; row < m_numRows; row++ )
+ for ( row = 0; row < m_numRows; row++ )
{
- for ( col = 0; col < m_numCols; col++ )
+ for ( col = 0; col < m_numCols; col++ )
{
m_table->SetValue( row, col, GetCellValue(row, col) );
}
{
wxGridCellCoords cell( row + cell_rows, col + cell_cols );
bool marked = false;
- for ( int j = 0; j < numCells; j++ )
+ for ( int j = 0; j < numCells; j++ )
{
if ( cell == cells[j] )
{
{
for ( int l = 0; l < cell_rows; l++ )
{
- // find a cell in this row to left alreay marked for repaint
+ // find a cell in this row to leave already marked for repaint
int left = col;
for (int k = 0; k < int(redrawCells.GetCount()); k++)
if ((redrawCells[k].GetCol() < left) &&
numCells = redrawCells.GetCount();
- for ( i = numCells - 1; i >= 0; i-- )
+ for ( i = numCells - 1; i >= 0; i-- )
{
DrawCell( dc, redrawCells[i] );
}
int right, bottom;
CalcUnscrolledPosition( cw, ch, &right, &bottom );
- int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
+ int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
if ( right > rightCol || bottom > bottomRow )
if (penWidth > 0)
{
- // The center of th drawn line is where the position/width/height of
- // the rectangle is actually at, (on wxMSW at least,) so we will
- // reduce the size of the rectangle to compensate for the thickness of
- // the line. If this is too strange on non wxMSW platforms then
+ // The center of the drawn line is where the position/width/height of
+ // the rectangle is actually at (on wxMSW at least), so the
+ // size of the rectangle is reduced to compensate for the thickness of
+ // the line. If this is too strange on non-wxMSW platforms then
// please #ifdef this appropriately.
rect.x += penWidth / 2;
rect.y += penWidth / 2;
// avoid drawing grid lines past the last row and col
//
- right = wxMin( right, GetColRight(m_numCols - 1) );
+ right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) );
bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
// no gridlines inside multicells, clip them out
- int leftCol = internalXToCol(left);
+ int leftCol = GetColPos( internalXToCol(left) );
int topRow = internalYToRow(top);
- int rightCol = internalXToCol(right);
+ int rightCol = GetColPos( internalXToCol(right) );
int bottomRow = internalYToRow(bottom);
#ifndef __WXMAC__
for (j=topRow; j<bottomRow; j++)
{
- for (i=leftCol; i<rightCol; i++)
+ int colPos;
+ for (colPos=leftCol; colPos<rightCol; colPos++)
{
+ i = GetColAt( colPos );
+
GetCellSize( j, i, &cell_rows, &cell_cols );
if ((cell_rows > 1) || (cell_cols > 1))
{
}
}
#else
- wxRegion clippedcells( left , top, right - left, bottom - top);
+ wxRegion clippedcells( left, top, right - left, bottom - top );
int i, j, cell_rows, cell_cols;
wxRect rect;
// vertical grid lines
//
- for ( i = internalXToCol(left); i < m_numCols; i++ )
+ int colPos;
+ for ( colPos = leftCol; colPos < m_numCols; colPos++ )
{
+ i = GetColAt( colPos );
+
int colRight = GetColRight(i) - 1;
if ( colRight > right )
{
dc.DestroyClippingRegion();
}
-void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
+void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
{
if ( !m_numRows )
return;
int rowTop = GetRowTop(row),
rowBottom = GetRowBottom(row) - 1;
- dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW), 1, wxSOLID) );
+ dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
dc.DrawLine( 0, rowTop, 0, rowBottom );
dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
size_t i;
size_t numLabels = cols.GetCount();
- for ( i = 0; i < numLabels; i++ )
+ for ( i = 0; i < numLabels; i++ )
{
DrawColLabel( dc, cols[i] );
}
#else
int colRight = GetColRight(col) - 1;
- dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW), 1, wxSOLID) );
+ dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 );
dc.DrawLine( colLeft, 0, colRight, 0 );
dc.DrawLine( colLeft, m_colLabelHeight - 1,
textOrientation );
}
-void wxGrid::DrawTextRectangle( wxDC& dc,
+// VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
+// add textOrientation support
+void wxGrid::DrawTextRectangle(wxDC& dc,
const wxArrayString& lines,
const wxRect& rect,
int horizAlign,
int vertAlign,
- int textOrientation )
+ int textOrientation)
{
- long textWidth = 0, textHeight = 0;
- long lineWidth = 0, lineHeight = 0;
- int nLines;
+ if ( lines.empty() )
+ return;
- dc.SetClippingRegion( rect );
+ wxDCClipper clip(dc, rect);
- nLines = lines.GetCount();
- if ( nLines > 0 )
- {
- int l;
- float x = 0.0, y = 0.0;
+ long textWidth,
+ textHeight;
- if ( textOrientation == wxHORIZONTAL )
- GetTextBoxSize( dc, lines, &textWidth, &textHeight );
- else
- GetTextBoxSize( dc, lines, &textHeight, &textWidth );
+ if ( textOrientation == wxHORIZONTAL )
+ GetTextBoxSize( dc, lines, &textWidth, &textHeight );
+ else
+ GetTextBoxSize( dc, lines, &textHeight, &textWidth );
- switch ( vertAlign )
- {
+ int x = 0,
+ y = 0;
+ switch ( vertAlign )
+ {
case wxALIGN_BOTTOM:
if ( textOrientation == wxHORIZONTAL )
y = rect.y + (rect.height - textHeight - 1);
else
x = rect.x + 1;
break;
- }
+ }
- // Align each line of a multi-line label
- for ( l = 0; l < nLines; l++ )
+ // Align each line of a multi-line label
+ size_t nLines = lines.GetCount();
+ for ( size_t l = 0; l < nLines; l++ )
+ {
+ const wxString& line = lines[l];
+
+ if ( line.empty() )
{
- dc.GetTextExtent(lines[l], &lineWidth, &lineHeight);
+ *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
+ continue;
+ }
- switch ( horizAlign )
- {
+ long lineWidth,
+ lineHeight;
+ dc.GetTextExtent(line, &lineWidth, &lineHeight);
+
+ switch ( horizAlign )
+ {
case wxALIGN_RIGHT:
if ( textOrientation == wxHORIZONTAL )
x = rect.x + (rect.width - lineWidth - 1);
else
y = rect.y + rect.height - 1;
break;
- }
+ }
- if ( textOrientation == wxHORIZONTAL )
- {
- dc.DrawText( lines[l], (int)x, (int)y );
- y += lineHeight;
- }
- else
- {
- dc.DrawRotatedText( lines[l], (int)x, (int)y, 90.0 );
- x += lineHeight;
- }
+ if ( textOrientation == wxHORIZONTAL )
+ {
+ dc.DrawText( line, x, y );
+ y += lineHeight;
+ }
+ else
+ {
+ dc.DrawRotatedText( line, x, y, 90.0 );
+ x += lineHeight;
}
}
-
- dc.DestroyClippingRegion();
}
// Split multi-line text up into an array of strings.
long lineW = 0, lineH = 0;
size_t i;
- for ( i = 0; i < lines.GetCount(); i++ )
+ for ( i = 0; i < lines.GetCount(); i++ )
{
dc.GetTextExtent( lines[i], &lineW, &lineH );
w = wxMax( w, lineW );
if (rect.x < 0)
nXMove = rect.x;
- // performed in PaintBackground()
-#if 0
// erase the highlight and the cell contents because the editor
// might not cover the entire cell
wxClientDC dc( m_gridWin );
PrepareDC( dc );
- dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
+ dc.SetBrush(wxBrush(GetCellAttr(row, col)->GetBackgroundColour(), wxSOLID));
dc.SetPen(*wxTRANSPARENT_PEN);
dc.DrawRectangle(rect);
-#endif
// cell is shifted by one pixel
// However, don't allow x or y to become negative
editor->SetCellAttr( attr );
editor->SetSize( rect );
- editor->GetControl()->Move(
- editor->GetControl()->GetPosition().x + nXMove,
- editor->GetControl()->GetPosition().y );
+ if (nXMove != 0)
+ editor->GetControl()->Move(
+ editor->GetControl()->GetPosition().x + nXMove,
+ editor->GetControl()->GetPosition().y );
editor->Show( true, attr );
- int colXPos = 0;
- for (int i = 0; i < m_currentCellCoords.GetCol(); i++)
- {
- colXPos += GetColSize( i );
- }
-
- int xUnit = 1, yUnit = 1;
- GetScrollPixelsPerUnit( &xUnit, &yUnit );
- if (m_currentCellCoords.GetCol() != 0)
- Scroll( colXPos / xUnit - 1, GetScrollPos( wxVERTICAL ) );
- else
- Scroll( colXPos / xUnit, GetScrollPos( wxVERTICAL ) );
-
// recalc dimensions in case we need to
// expand the scrolled window to account for editor
CalcDimensions();
m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
}
-int wxGrid::XToCol( int x )
+int wxGrid::XToCol( int x, bool clipToMinMax )
{
- return CoordToRowOrCol(x, m_defaultColWidth,
- m_minAcceptableColWidth, m_colRights, m_numCols, false);
+ if (x < 0)
+ return clipToMinMax && (m_numCols > 0) ? GetColAt( 0 ) : -1;
+
+ if (!m_defaultColWidth)
+ m_defaultColWidth = 1;
+
+ int maxPos = x / m_defaultColWidth;
+ int minPos = 0;
+
+ if (m_colRights.IsEmpty())
+ {
+ if(maxPos < m_numCols)
+ return GetColAt( maxPos );
+ return clipToMinMax ? GetColAt( m_numCols - 1 ) : -1;
+ }
+
+ if ( maxPos >= m_numCols)
+ maxPos = m_numCols - 1;
+ else
+ {
+ if ( x >= m_colRights[GetColAt( maxPos )])
+ {
+ minPos = maxPos;
+ if (m_minAcceptableColWidth)
+ maxPos = x / m_minAcceptableColWidth;
+ else
+ maxPos = m_numCols - 1;
+ }
+ if ( maxPos >= m_numCols)
+ maxPos = m_numCols - 1;
+ }
+
+ //X is beyond the last column
+ if ( x >= m_colRights[GetColAt( maxPos )])
+ return clipToMinMax ? GetColAt( maxPos ) : -1;
+
+ //X is before the first column
+ if ( x < m_colRights[GetColAt( 0 )] )
+ return GetColAt( 0 );
+
+ //Perform a binary search
+ while ( maxPos - minPos > 0 )
+ {
+ wxCHECK_MSG(m_colRights[GetColAt( minPos )] <= x && x < m_colRights[GetColAt( maxPos )],
+ 0, _T("wxGrid: internal error in XToCol"));
+
+ if (x >= m_colRights[GetColAt( maxPos - 1 )])
+ return GetColAt( maxPos );
+ else
+ maxPos--;
+ int median = minPos + (maxPos - minPos + 1) / 2;
+ if (x < m_colRights[GetColAt( median )])
+ maxPos = median;
+ else
+ minPos = median;
+ }
+ return GetColAt( maxPos );
}
// return the row number that that the y coord is near the edge of, or
{
int h = r.GetHeight();
ypos = r.GetTop();
- for ( i = row - 1; i >= 0; i-- )
+ for ( i = row - 1; i >= 0; i-- )
{
int rowHeight = GetRowHeight(i);
if ( h + rowHeight > ch )
}
// we divide it later by GRID_SCROLL_LINE, make sure that we don't
- // have rounding errors (this is important, because if we do, we
- // might not scroll at all and some cells won't be redrawn)
+ // have rounding errors (this is important, because if we do,
+ // we might not scroll at all and some cells won't be redrawn)
//
- // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
- // scroll unit...
+ // Sometimes GRID_SCROLL_LINE / 2 is not enough,
+ // so just add a full scroll unit...
ypos += m_scrollLineY;
}
HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
}
}
- else if ( m_currentCellCoords.GetCol() > 0 )
+ else if ( GetColPos( m_currentCellCoords.GetCol() ) > 0 )
{
int row = m_currentCellCoords.GetRow();
- int col = m_currentCellCoords.GetCol() - 1;
+ int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) - 1 );
ClearSelection();
+
MakeCellVisible( row, col );
SetCurrentCell( row, col );
}
HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
}
}
- else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
+ else if ( GetColPos( m_currentCellCoords.GetCol() ) < m_numCols - 1 )
{
int row = m_currentCellCoords.GetRow();
- int col = m_currentCellCoords.GetCol() + 1;
+ int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) + 1 );
ClearSelection();
+
MakeCellVisible( row, col );
SetCurrentCell( row, col );
}
if ( newRow == row )
{
- //row > 0 , so newrow can never be less than 0 here.
+ // row > 0, so newRow can never be less than 0 here.
newRow = row - 1;
}
int newRow = internalYToRow( y + ch );
if ( newRow == row )
{
- // row < m_numRows , so newrow can't overflow here.
+ // row < m_numRows, so newRow can't overflow here.
newRow = row + 1;
}
{
if ( !LookupAttr(row, col, &attr) )
{
- attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any)
+ attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
: (wxGridCellAttr *)NULL;
CacheAttr(row, col, attr);
}
m_rowHeights[row] = h;
int i;
- for ( i = row; i < m_numRows; i++ )
+ for ( i = row; i < m_numRows; i++ )
{
m_rowBottoms[i] += diff;
}
// should we check that it's bigger than GetColMinimalWidth(col) here?
// (VZ)
// No, because it is reasonable to assume the library user know's
- // what he is doing. However whe should test against the weaker
+ // what he is doing. However we should test against the weaker
// constraint of minimalAcceptableWidth, as this breaks rendering
//
// This test then fixes sf.net bug #645734
InitColWidths();
}
- // if < 0 calc new width from label
+ // if < 0 then calculate new width from label
if ( width < 0 )
{
- long w, h;
- wxArrayString lines;
- wxClientDC dc(m_colLabelWin);
- dc.SetFont(GetLabelFont());
- StringToLines(GetColLabelValue(col), lines);
- GetTextBoxSize(dc, lines, &w, &h);
- width = w + 6;
+ long w, h;
+ wxArrayString lines;
+ wxClientDC dc(m_colLabelWin);
+ dc.SetFont(GetLabelFont());
+ StringToLines(GetColLabelValue(col), lines);
+ GetTextBoxSize(dc, lines, &w, &h);
+ width = w + 6;
}
int w = wxMax( 0, width );
m_colWidths[col] = w;
int i;
- for ( i = col; i < m_numCols; i++ )
+ int colPos;
+ for ( colPos = GetColPos( col ); colPos < m_numCols; colPos++ )
{
+ i = GetColAt( colPos );
m_colRights[i] += diff;
}
wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
extent = column ? size.x : size.y;
if ( extent > extentMax )
- {
extentMax = extent;
- }
renderer->DecRef();
}
extent = column ? w : h;
if ( extent > extentMax )
- {
extentMax = extent;
- }
if ( !extentMax )
{
for ( int col = 0; col < m_numCols; col++ )
{
if ( !calcOnly )
- {
AutoSizeColumn(col, setAsMin);
- }
width += GetColWidth(col);
}
long w, h;
// Hide the edit control, so it
- // won't interfer with drag-shrinking.
+ // won't interfere with drag-shrinking.
if ( IsCellEditControlShown() )
{
HideCellEditControl();
height = 80;
// Round up to a multiple the scroll rate
- // NOTE: this still doesn't get rid of the scrollbars;
+ // NOTE: this still doesn't get rid of the scrollbars;
// is there any magic incantation for that?
int xpu, ypu;
GetScrollPixelsPerUnit(&xpu, &ypu);