]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/grid.cpp
fixed another return FALSE in a function returning a pointer (patch 545046)
[wxWidgets.git] / src / generic / grid.cpp
index 527c9dc906695c30f6ca5d78c90d4f2f6e29da68..13ad0909b87c4f205cc2983472caeb02237f1b02 100644 (file)
@@ -185,7 +185,7 @@ private:
     DECLARE_EVENT_TABLE()
 };
 
-class WXDLLEXPORT wxGridWindow : public wxPanel
+class WXDLLEXPORT wxGridWindow : public wxWindow
 {
 public:
     wxGridWindow()
@@ -403,12 +403,12 @@ static const int GRID_HASH_SIZE = 100;
 // private functions
 // ----------------------------------------------------------------------------
 
-static inline GetScrollX(int x)
+static inline int GetScrollX(int x)
 {
     return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X;
 }
 
-static inline GetScrollY(int y)
+static inline int GetScrollY(int y)
 {
     return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y;
 }
@@ -708,73 +708,7 @@ bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event)
 
 void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
 {
-    // we don't check for !HasModifiers() because IsAcceptedKey() did it
-
-    // insert the key in the control
-    wxChar ch;
-    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:
-            ch = _T('0') + keycode - WXK_NUMPAD0;
-            break;
-
-        case WXK_MULTIPLY:
-        case WXK_NUMPAD_MULTIPLY:
-            ch = _T('*');
-            break;
-
-        case WXK_ADD:
-        case WXK_NUMPAD_ADD:
-            ch = _T('+');
-            break;
-
-        case WXK_SUBTRACT:
-        case WXK_NUMPAD_SUBTRACT:
-            ch = _T('-');
-            break;
-
-        case WXK_DECIMAL:
-        case WXK_NUMPAD_DECIMAL:
-            ch = _T('.');
-            break;
-
-        case WXK_DIVIDE:
-        case WXK_NUMPAD_DIVIDE:
-            ch = _T('/');
-            break;
-
-        default:
-            if ( keycode < 256 && keycode >= 0 && isprint(keycode) )
-            {
-                // FIXME this is not going to work for non letters...
-                if ( !event.ShiftDown() )
-                {
-                    keycode = tolower(keycode);
-                }
-
-                ch = (wxChar)keycode;
-            }
-            else
-            {
-                ch = _T('\0');
-            }
-    }
-
-    if ( ch )
-    {
-        Text()->AppendText(ch);
-    }
-    else
+    if ( !Text()->EmulateKeyPress(event) )
     {
         event.Skip();
     }
@@ -1898,9 +1832,24 @@ void wxGridCellBoolRenderer::Draw(wxGrid& grid,
 // wxGridCellAttr
 // ----------------------------------------------------------------------------
 
+void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
+{
+    m_nRef = 1;
+
+    m_isReadOnly = Unset;
+
+    m_renderer = NULL;
+    m_editor = NULL;
+
+    m_attrkind = wxGridCellAttr::Cell;
+
+    SetDefAttr(attrDefault);
+}
+
 wxGridCellAttr *wxGridCellAttr::Clone() const
 {
-    wxGridCellAttr *attr = new wxGridCellAttr;
+    wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
+
     if ( HasTextColour() )
         attr->SetTextColour(GetTextColour());
     if ( HasBackgroundColour() )
@@ -1926,8 +1875,6 @@ wxGridCellAttr *wxGridCellAttr::Clone() const
 
     attr->SetKind( m_attrkind );
 
-    attr->SetDefAttr(m_defGridAttr);
-
     return attr;
 }
 
@@ -1971,7 +1918,7 @@ const wxColour& wxGridCellAttr::GetTextColour() const
     {
         return m_colText;
     }
-    else if (m_defGridAttr != this)
+    else if (m_defGridAttr && m_defGridAttr != this)
     {
         return m_defGridAttr->GetTextColour();
     }
@@ -1987,7 +1934,7 @@ const wxColour& wxGridCellAttr::GetBackgroundColour() const
 {
     if (HasBackgroundColour())
         return m_colBack;
-    else if (m_defGridAttr != this)
+    else if (m_defGridAttr && m_defGridAttr != this)
         return m_defGridAttr->GetBackgroundColour();
     else
     {
@@ -2001,7 +1948,7 @@ const wxFont& wxGridCellAttr::GetFont() const
 {
     if (HasFont())
         return m_font;
-    else if (m_defGridAttr != this)
+    else if (m_defGridAttr && m_defGridAttr != this)
         return m_defGridAttr->GetFont();
     else
     {
@@ -2018,7 +1965,7 @@ void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
         if ( hAlign ) *hAlign = m_hAlign;
         if ( vAlign ) *vAlign = m_vAlign;
     }
-    else if (m_defGridAttr != this)
+    else if (m_defGridAttr && m_defGridAttr != this)
         m_defGridAttr->GetAlignment(hAlign, vAlign);
     else
     {
@@ -2059,7 +2006,7 @@ wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col)
 
         if ( !renderer )
         {
-            if ( this != m_defGridAttr )
+            if (m_defGridAttr &&  this != m_defGridAttr )
             {
                 // if we still don't have one then use the grid default
                 // (no need for IncRef() here neither)
@@ -2107,7 +2054,7 @@ wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) cons
 
         if ( !editor )
         {
-            if ( this != m_defGridAttr )
+            if ( m_defGridAttr && this != m_defGridAttr )
             {
                 // if we still don't have one then use the grid default
                 // (no need for IncRef() here neither)
@@ -3473,9 +3420,9 @@ void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
 
 //////////////////////////////////////////////////////////////////////
 
-IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxPanel )
+IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
 
-BEGIN_EVENT_TABLE( wxGridWindow, wxPanel )
+BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
     EVT_PAINT( wxGridWindow::OnPaint )
     EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel)
     EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
@@ -3488,7 +3435,7 @@ wxGridWindow::wxGridWindow( wxGrid *parent,
                             wxGridRowLabelWindow *rowLblWin,
                             wxGridColLabelWindow *colLblWin,
                             wxWindowID id, const wxPoint &pos, const wxSize &size )
-        : wxPanel( parent, id, pos, size, wxWANTS_CHARS, "grid window" )
+        : wxWindow( parent, id, pos, size, wxWANTS_CHARS, "grid window" )
 {
     m_owner = parent;
     m_rowLabelWin = rowLblWin;
@@ -3519,7 +3466,7 @@ void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
 
 void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
 {
-    wxPanel::ScrollWindow( dx, dy, rect );
+    wxWindow::ScrollWindow( dx, dy, rect );
     m_rowLabelWin->ScrollWindow( 0, dy, rect );
     m_colLabelWin->ScrollWindow( dx, 0, rect );
 }
@@ -3616,17 +3563,17 @@ void wxGrid::Create()
 
     m_cellEditCtrlEnabled = FALSE;
 
-    m_defaultCellAttr = new wxGridCellAttr;
-    m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
+    m_defaultCellAttr = new wxGridCellAttr();
 
     // Set default cell attributes
+    m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
     m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
     m_defaultCellAttr->SetFont(GetFont());
     m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
     m_defaultCellAttr->SetTextColour(
-        wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT));
+        wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
     m_defaultCellAttr->SetBackgroundColour(
-        wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
+        wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
     m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
     m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
 
@@ -3798,8 +3745,8 @@ void wxGrid::Init()
 
     m_selectingTopLeft = wxGridNoCellCoords;
     m_selectingBottomRight = wxGridNoCellCoords;
-    m_selectionBackground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
-    m_selectionForeground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
+    m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
+    m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
 
     m_editable = TRUE;  // default for whole grid
 
@@ -3807,7 +3754,7 @@ void wxGrid::Init()
     m_batchCount = 0;
 
     m_extraWidth =
-    m_extraHeight = 50;
+    m_extraHeight = 0;
 }
 
 // ----------------------------------------------------------------------------
@@ -3892,9 +3839,9 @@ void wxGrid::CalcDimensions()
     int cw, ch;
     GetClientSize( &cw, &ch );
 
-    if ( m_colLabelWin->IsShown() )
-        cw -= m_rowLabelWidth;
     if ( m_rowLabelWin->IsShown() )
+        cw -= m_rowLabelWidth;
+    if ( m_colLabelWin->IsShown() )
         ch -= m_colLabelHeight;
 
     // grid total size
@@ -3932,6 +3879,10 @@ void wxGrid::CalcDimensions()
     SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y,
                    GetScrollX(w), GetScrollY(h), x, y,
                    GetBatchCount() != 0);
+
+    // if our OnSize() hadn't been called (it would if we have scrollbars), we
+    // still must reposition the children
+    CalcWindowSizes();
 }
 
 
@@ -5819,7 +5770,14 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
                          || editor->IsAcceptedKey(event) )
                     {
                         EnableCellEditControl();
-                        editor->StartingKey(event);
+
+                        // the editor could be not shown for a variety of
+                        // reasons (i.e. blocked by the app or whatever), so
+                        // check if it really was created
+                        if ( m_cellEditCtrlEnabled )
+                        {
+                            editor->StartingKey(event);
+                        }
                     }
                     else
                     {
@@ -8101,19 +8059,20 @@ wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
 {
     wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
-        wxASSERT_MSG( m_table,
-                      _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
 
-    attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell );
-        if ( !attr )
-        {
-            attr = new wxGridCellAttr;
+    wxCHECK_MSG( m_table, attr,
+                  _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
+
+    attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
+    if ( !attr )
+    {
+        attr = new wxGridCellAttr(m_defaultCellAttr);
+
+        // artificially inc the ref count to match DecRef() in caller
+        attr->IncRef();
+        m_table->SetAttr(attr, row, col);
+    }
 
-            // artificially inc the ref count to match DecRef() in caller
-            attr->IncRef();
-            m_table->SetAttr(attr, row, col);
-        }
-    attr->SetDefAttr(m_defaultCellAttr);
     return attr;
 }