]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/grid.cpp
fixed warnings about truncating 64 bit integers
[wxWidgets.git] / src / generic / grid.cpp
index e6ca7a0857a75fdd0f4f7b110516478ec26c0bf9..4a491aae1d6874681611430114a1c80ed5988824 100644 (file)
 // headers
 // ----------------------------------------------------------------------------
 
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
-    #pragma implementation "grid.h"
-#endif
-
 // For compilers that support precompilatixon, includes "wx/wx.h".
 #include "wx/wxprec.h"
 
@@ -42,6 +38,7 @@
     #include "wx/combobox.h"
     #include "wx/valtext.h"
     #include "wx/intl.h"
+    #include "wx/math.h"
 #endif
 
 #include "wx/textfile.h"
@@ -240,27 +237,37 @@ private:
 class wxGridCellEditorEvtHandler : public wxEvtHandler
 {
 public:
-    wxGridCellEditorEvtHandler()
-        : m_grid(0), m_editor(0)
-        { }
     wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
-        : m_grid(grid), m_editor(editor)
-        { }
+        : m_grid(grid),
+          m_editor(editor),
+          m_inSetFocus(false)
+    {
+    }
 
+    void OnKillFocus(wxFocusEvent& event);
     void OnKeyDown(wxKeyEvent& event);
     void OnChar(wxKeyEvent& event);
 
+    void SetInSetFocus(bool inSetFocus) { m_inSetFocus = inSetFocus; }
+
 private:
     wxGrid*             m_grid;
     wxGridCellEditor*   m_editor;
-    DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
+
+    // Work around the fact that a focus kill event can be sent to
+    // a combobox within a set focus event.
+    bool                m_inSetFocus;
+
     DECLARE_EVENT_TABLE()
+    DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
     DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler)
 };
 
 
-IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler )
+IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler)
+
 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
+    EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus )
     EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
     EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
 END_EVENT_TABLE()
@@ -568,7 +575,7 @@ bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event)
     // through in that case.
     if ((ctrl || alt) && !(ctrl && alt))
         return false;
-    
+
 #if wxUSE_UNICODE
     int key = event.GetUnicodeKey();
     bool keyOk = true;
@@ -585,7 +592,7 @@ bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event)
     return keyOk;
 #else // !wxUSE_UNICODE
     int key = event.GetKeyCode();
-    if (key <= 255) 
+    if (key <= 255)
         return true;
     return false;
 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
@@ -749,7 +756,7 @@ void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
     wxTextCtrl* tc = Text();
     wxChar ch;
     long pos;
-    
+
 #if wxUSE_UNICODE
     ch = event.GetUnicodeKey();
     if (ch <= 127)
@@ -956,9 +963,9 @@ bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event)
 
 void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
 {
+    int keycode = event.GetKeyCode();
     if ( !HasRange() )
     {
-        int keycode = event.GetKeyCode();
         if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-')
         {
             wxGridCellTextEditor::StartingKey(event);
@@ -967,7 +974,18 @@ void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
             return;
         }
     }
-
+#if wxUSE_SPINCTRL
+    else
+    {
+        if ( wxIsdigit(keycode) )
+        {
+            wxSpinCtrl* spin = (wxSpinCtrl*)m_control;
+            spin->SetValue(keycode - '0');
+            spin->SetSelection(1,1);
+            return;
+        }
+    }
+#endif
     event.Skip();
 }
 
@@ -1068,7 +1086,8 @@ bool wxGridCellFloatEditor::EndEdit(int row, int col,
     double value = 0.0;
     wxString text(Text()->GetValue());
 
-    if ( (text.empty() || text.ToDouble(&value)) && (value != m_valueOld) )
+    if ( (text.empty() || text.ToDouble(&value)) &&
+            !wxIsSameDouble(value, m_valueOld) )
     {
         if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
             grid->GetTable()->SetValueAsDouble(row, col, value);
@@ -1092,7 +1111,7 @@ void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
     tmpbuf[0] = (char) keycode;
     tmpbuf[1] = '\0';
     wxString strbuf(tmpbuf, *wxConvCurrent);
-#if wxUSE_INTL        
+#if wxUSE_INTL
     bool is_decimal_point = ( strbuf ==
        wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) );
 #else
@@ -1170,14 +1189,14 @@ bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
         tmpbuf[0] = (char) keycode;
         tmpbuf[1] = '\0';
         wxString strbuf(tmpbuf, *wxConvCurrent);
-#if wxUSE_INTL        
+#if wxUSE_INTL
         bool is_decimal_point =
             ( strbuf == wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
                                           wxLOCALE_CAT_NUMBER) );
 #else
         bool is_decimal_point = ( strbuf == _T(".") );
 #endif
-        if ( (keycode < 128) && 
+        if ( (keycode < 128) &&
              (wxIsdigit(keycode) || tolower(keycode) == 'e' ||
               is_decimal_point || keycode == '+' || keycode == '-') )
             return true;
@@ -1361,11 +1380,11 @@ void wxGridCellBoolEditor::StartingKey(wxKeyEvent& event)
         case WXK_SPACE:
             CBox()->SetValue(!CBox()->GetValue());
             break;
-            
+
         case '+':
             CBox()->SetValue(true);
             break;
-                
+
         case '-':
             CBox()->SetValue(false);
             break;
@@ -1446,6 +1465,14 @@ void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
     wxASSERT_MSG(m_control,
                  wxT("The wxGridCellEditor must be Created first!"));
 
+    wxGridCellEditorEvtHandler* evtHandler = NULL;
+    if (m_control)
+        evtHandler = wxDynamicCast(m_control->GetEventHandler(), wxGridCellEditorEvtHandler);
+
+    // Don't immediately end if we get a kill focus event within BeginEdit
+    if (evtHandler)
+        evtHandler->SetInSetFocus(true);
+
     m_startValue = grid->GetTable()->GetValue(row, col);
 
     if (m_allowOthers)
@@ -1454,12 +1481,15 @@ void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
     {
         // find the right position, or default to the first if not found
         int pos = Combo()->FindString(m_startValue);
-        if (pos == -1)
+        if (pos == wxNOT_FOUND)
             pos = 0;
         Combo()->SetSelection(pos);
     }
     Combo()->SetInsertionPointEnd();
     Combo()->SetFocus();
+
+    if (evtHandler)
+        evtHandler->SetInSetFocus(false);
 }
 
 bool wxGridCellChoiceEditor::EndEdit(int row, int col,
@@ -1509,6 +1539,18 @@ wxString wxGridCellChoiceEditor::GetValue() const
 // wxGridCellEditorEvtHandler
 // ----------------------------------------------------------------------------
 
+void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent& event)
+{
+    // Don't disable the cell if we're just starting to edit it
+    if (m_inSetFocus)
+        return;
+
+    // accept changes
+    m_grid->DisableCellEditControl();
+
+    event.Skip();
+}
+
 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
 {
     switch ( event.GetKeyCode() )
@@ -1604,8 +1646,8 @@ void wxGridCellRenderer::Draw(wxGrid& grid,
 // wxGridCellStringRenderer
 // ----------------------------------------------------------------------------
 
-void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
-                                                     wxGridCellAttr& attr,
+void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid,
+                                                     const wxGridCellAttr& attr,
                                                      wxDC& dc,
                                                      bool isSelected)
 {
@@ -1636,7 +1678,7 @@ void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
     dc.SetFont( attr.GetFont() );
 }
 
-wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
+wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr,
                                                wxDC& dc,
                                                const wxString& text)
 {
@@ -1757,7 +1799,7 @@ void wxGridCellStringRenderer::Draw(wxGrid& grid,
 // wxGridCellNumberRenderer
 // ----------------------------------------------------------------------------
 
-wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
+wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col)
 {
     wxGridTableBase *table = grid.GetTable();
     wxString text;
@@ -1823,7 +1865,7 @@ wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
     return renderer;
 }
 
-wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
+wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col)
 {
     wxGridTableBase *table = grid.GetTable();
 
@@ -1928,21 +1970,19 @@ void wxGridCellFloatRenderer::SetParameters(const wxString& params)
             {
                 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
             }
-
         }
-                tmp = params.AfterFirst(_T(','));
-                if ( !tmp.empty() )
-                {
-                    long precision;
+        tmp = params.AfterFirst(_T(','));
+        if ( !tmp.empty() )
+        {
+            long precision;
             if ( tmp.ToLong(&precision) )
-                    {
+            {
                 SetPrecision((int)precision);
-                    }
-                    else
-                    {
+            }
+            else
+            {
                 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
-        }
-
+            }
         }
     }
 }
@@ -2131,8 +2171,8 @@ void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
         mergefrom->GetAlignment( &hAlign, &vAlign);
         SetAlignment(hAlign, vAlign);
     }
-
-    mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
+    if ( !HasSize() && mergefrom->HasSize() )
+        mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
 
     // Directly access member functions as GetRender/Editor don't just return
     // m_renderer/m_editor
@@ -2903,6 +2943,7 @@ wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAtt
         return (wxGridCellAttr *)NULL;
 }
 
+
 void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
 {
     if ( m_attrProvider )
@@ -3649,7 +3690,8 @@ void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
     int client_width = 0;
     GetClientSize( &client_width, &client_height );
 
-#if __WXGTK__
+    // VZ: any reason for this ifdef? (FIXME)
+#ifdef __WXGTK__
     wxRect rect;
     rect.SetX( 1 );
     rect.SetY( 1 );
@@ -3657,7 +3699,7 @@ void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
     rect.SetHeight( client_height - 2 );
 
     wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 );
-#else
+#else // !__WXGTK__
     dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),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 );
@@ -3667,7 +3709,7 @@ void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
     dc.SetPen( *wxWHITE_PEN );
     dc.DrawLine( 1, 1, client_width-1, 1 );
     dc.DrawLine( 1, 1, 1, client_height-1 );
-#endif
+#endif // __WXGTK__/!__WXGTK__
 }
 
 
@@ -3758,6 +3800,9 @@ void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
 
 void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
 {
+    if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this)
+        SetFocus();
+
     m_owner->ProcessGridCellMouseEvent( event );
 }
 
@@ -4879,6 +4924,7 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
                 break;
 
                 case WXGRID_CURSOR_SELECT_ROW:
+                {
                     if ( (row = YToRow( y )) >= 0 )
                     {
                         if ( m_selection )
@@ -4890,6 +4936,8 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
                                                     event.MetaDown() );
                         }
                     }
+                }
+                break;
 
                 // default label to suppress warnings about "enumeration value
                 // 'xxx' not handled in switch
@@ -5101,6 +5149,7 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
                 break;
 
                 case WXGRID_CURSOR_SELECT_COL:
+                {
                     if ( (col = XToCol( x )) >= 0 )
                     {
                         if ( m_selection )
@@ -5112,6 +5161,8 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
                                                     event.MetaDown() );
                         }
                     }
+                }
+                break;
 
                 // default label to suppress warnings about "enumeration value
                 // 'xxx' not handled in switch
@@ -7146,8 +7197,10 @@ void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
     int topRow    = internalYToRow(top);
     int rightCol  = internalXToCol(right);
     int bottomRow = internalYToRow(bottom);
-    wxRegion clippedcells(0, 0, cw, ch);
 
+#ifndef __WXMAC__
+    // CS: I don't know why suddenly unscrolled coordinates are used for clipping
+    wxRegion clippedcells(0, 0, cw, ch);
 
     int i, j, cell_rows, cell_cols;
     wxRect rect;
@@ -7171,6 +7224,30 @@ void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
             }
         }
     }
+#else
+    wxRegion clippedcells( left , top, right - left, bottom - top);
+
+    int i, j, cell_rows, cell_cols;
+    wxRect rect;
+
+    for (j=topRow; j<bottomRow; j++)
+    {
+        for (i=leftCol; i<rightCol; i++)
+        {
+            GetCellSize( j, i, &cell_rows, &cell_cols );
+            if ((cell_rows > 1) || (cell_cols > 1))
+            {
+                rect = CellToRect(j,i);
+                clippedcells.Subtract(rect);
+            }
+            else if ((cell_rows < 0) || (cell_cols < 0))
+            {
+                rect = CellToRect(j+cell_rows, i+cell_cols);
+                clippedcells.Subtract(rect);
+            }
+        }
+    }
+#endif
     dc.SetClippingRegion( clippedcells );
 
     dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
@@ -7229,11 +7306,11 @@ void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
 
 void wxGrid::DrawRowLabel( wxDC& dc, int row )
 {
-    if ( GetRowHeight(row) <= 0 )
+    if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
         return;
 
     wxRect rect;
-#ifdef __WXGTK__
+#ifdef __WXGTK20__
     rect.SetX( 1 );
     rect.SetY( GetRowTop(row) + 1 );
     rect.SetWidth( m_rowLabelWidth - 2 );
@@ -7291,13 +7368,13 @@ void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
 
 void wxGrid::DrawColLabel( wxDC& dc, int col )
 {
-    if ( GetColWidth(col) <= 0 )
+    if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
         return;
 
     int colLeft = GetColLeft(col);
 
     wxRect rect;
-#ifdef __WXGTK__
+#ifdef __WXGTK20__
     rect.SetX( colLeft + 1 );
     rect.SetY( 1 );
     rect.SetWidth( GetColWidth(col) - 2 );
@@ -7488,7 +7565,7 @@ void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
 }
 
 
-void wxGrid::GetTextBoxSize( wxDC& dc,
+void wxGrid::GetTextBoxSize( const wxDC& dc,
                              const wxArrayString& lines,
                              long *width, long *height )
 {
@@ -7783,7 +7860,9 @@ void wxGrid::HideCellEditControl()
         editor->Show( false );
         editor->DecRef();
         attr->DecRef();
+
         m_gridWin->SetFocus();
+
         // refresh whole row to the right
         wxRect rect( CellToRect(row, col) );
         CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
@@ -9665,7 +9744,7 @@ void wxGrid::SetRowMinimalAcceptableHeight( int height )
     if ( height<0 )
         return;
     m_minAcceptableRowHeight = height;
-};
+}
 
 int  wxGrid::GetColMinimalAcceptableWidth() const
 {