]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/newgrid/griddemo.cpp
Added SetSelectionMode
[wxWidgets.git] / samples / newgrid / griddemo.cpp
index b936e76b784a2c2b048cf2f845723bb3b2f3a121..1e3586dd3dc1477959a30917d1203af89cc16ad1 100644 (file)
@@ -188,7 +188,7 @@ GridFrame::GridFrame()
     logger->SetTimestamp( NULL );
 
     // this will create a grid and, by default, an associated grid
     logger->SetTimestamp( NULL );
 
     // this will create a grid and, by default, an associated grid
-    // table for string gs_dataBugsGrid
+    // table for strings
     grid->CreateGrid( 100, 100 );
 
     grid->SetRowSize( 0, 60 );
     grid->CreateGrid( 100, 100 );
 
     grid->SetRowSize( 0, 60 );
@@ -231,6 +231,16 @@ GridFrame::GridFrame()
     grid->SetColSize(4, 120);
     grid->SetColMinimalWidth(4, 120);
 
     grid->SetColSize(4, 120);
     grid->SetColMinimalWidth(4, 120);
 
+    grid->SetColFormatFloat(5);
+    grid->SetCellValue(0, 5, "3.1415");
+    grid->SetCellValue(1, 5, "1415");
+    grid->SetCellValue(2, 5, "12345.67890");
+
+    grid->SetColFormatFloat(6, 6, 2);
+    grid->SetCellValue(0, 6, "3.1415");
+    grid->SetCellValue(1, 6, "1415");
+    grid->SetCellValue(2, 6, "12345.67890");
+
     wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
     topSizer->Add( grid,
                    1,
     wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
     topSizer->Add( grid,
                    1,
@@ -472,23 +482,27 @@ void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
 
 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
 {
 
 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
 {
+#if 0
     if ( grid->IsSelection() )
     {
         int topRow, bottomRow, leftCol, rightCol;
         grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
         grid->DeleteRows( topRow, bottomRow - topRow + 1 );
     }
     if ( grid->IsSelection() )
     {
         int topRow, bottomRow, leftCol, rightCol;
         grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
         grid->DeleteRows( topRow, bottomRow - topRow + 1 );
     }
+#endif
 }
 
 
 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
 {
 }
 
 
 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
 {
+#if 0
     if ( grid->IsSelection() )
     {
         int topRow, bottomRow, leftCol, rightCol;
         grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
         grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
     }
     if ( grid->IsSelection() )
     {
         int topRow, bottomRow, leftCol, rightCol;
         grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
         grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
     }
+#endif
 }
 
 
 }
 
 
@@ -652,11 +666,24 @@ void GridFrame::OnVTable(wxCommandEvent& )
 {
     static long s_sizeGrid = 10000;
 
 {
     static long s_sizeGrid = 10000;
 
+#ifdef __WXMOTIF__
+    // MB: wxGetNumberFromUser doesn't work properly for wxMotif
+    wxString s;
+    s << s_sizeGrid;
+    s = wxGetTextFromUser( "Size of the table to create",
+                           "Size:",
+                           s );
+
+    s.ToLong( &s_sizeGrid );
+
+#else
     s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
                                      "Size: ",
                                      "wxGridDemo question",
                                      s_sizeGrid,
                                      0, 32000, this);
     s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
                                      "Size: ",
                                      "wxGridDemo question",
                                      s_sizeGrid,
                                      0, 32000, this);
+#endif
+
     if ( s_sizeGrid != -1 )
     {
         BigGridFrame* win = new BigGridFrame(s_sizeGrid);
     if ( s_sizeGrid != -1 )
     {
         BigGridFrame* win = new BigGridFrame(s_sizeGrid);
@@ -685,22 +712,78 @@ void MyGridCellRenderer::Draw(wxGrid& grid,
     dc.DrawEllipse(rect);
 }
 
     dc.DrawEllipse(rect);
 }
 
-
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
-// BigGridFrame and BigGridTable:  Sample of a non-standard table
+// MyGridCellAttrProvider
 // ----------------------------------------------------------------------------
 
 // ----------------------------------------------------------------------------
 
+MyGridCellAttrProvider::MyGridCellAttrProvider()
+{
+    m_attrForOddRows = new wxGridCellAttr;
+    m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
+}
+
+MyGridCellAttrProvider::~MyGridCellAttrProvider()
+{
+    m_attrForOddRows->DecRef();
+}
+
+wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col) const
+{
+    wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col);
+
+    if ( row % 2 )
+    {
+        if ( !attr )
+        {
+            attr = m_attrForOddRows;
+            attr->IncRef();
+        }
+        else
+        {
+            if ( !attr->HasBackgroundColour() )
+            {
+                wxGridCellAttr *attrNew = attr->Clone();
+                attr->DecRef();
+                attr = attrNew;
+                attr->SetBackgroundColour(*wxLIGHT_GREY);
+            }
+        }
+    }
+
+    return attr;
+}
+
+// ============================================================================
+// BigGridFrame and BigGridTable:  Sample of a non-standard table
+// ============================================================================
+
 BigGridFrame::BigGridFrame(long sizeGrid)
             : wxFrame(NULL, -1, "Plugin Virtual Table",
                       wxDefaultPosition, wxSize(500, 450))
 {
     m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
     m_table = new BigGridTable(sizeGrid);
 BigGridFrame::BigGridFrame(long sizeGrid)
             : wxFrame(NULL, -1, "Plugin Virtual Table",
                       wxDefaultPosition, wxSize(500, 450))
 {
     m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
     m_table = new BigGridTable(sizeGrid);
+
+    // VZ: I don't understand why this slows down the display that much,
+    //     must profile it...
+    //m_table->SetAttrProvider(new MyGridCellAttrProvider);
+
     m_grid->SetTable(m_table, TRUE);
     m_grid->SetTable(m_table, TRUE);
+
+#if defined __WXMOTIF__
+    // MB: the grid isn't getting a sensible default size under wxMotif
+    int cw, ch;
+    GetClientSize( &cw, &ch );
+    m_grid->SetSize( cw, ch );
+#endif
 }
 
 }
 
-// ----------------------------------------------------------------------------
+// ============================================================================
 // BugsGridFrame: a "realistic" table
 // BugsGridFrame: a "realistic" table
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// bugs table data
 // ----------------------------------------------------------------------------
 
 enum Columns
 // ----------------------------------------------------------------------------
 
 enum Columns
@@ -736,10 +819,10 @@ static const wxChar* severities[] =
 static struct BugsGridData
 {
     int id;
 static struct BugsGridData
 {
     int id;
-    const wxChar *summary;
+    wxChar summary[80];
     Severity severity;
     int prio;
     Severity severity;
     int prio;
-    const wxChar *platform;
+    wxChar platform[12];
     bool opened;
 } gs_dataBugsGrid [] =
 {
     bool opened;
 } gs_dataBugsGrid [] =
 {
@@ -759,6 +842,10 @@ static const wxChar *headers[Col_Max] =
     _T("Opened?"),
 };
 
     _T("Opened?"),
 };
 
+// ----------------------------------------------------------------------------
+// BugsGridTable
+// ----------------------------------------------------------------------------
+
 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
 {
     switch ( col )
 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
 {
     switch ( col )
@@ -771,8 +858,10 @@ wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
             // fall thorugh (TODO should be a list)
 
         case Col_Summary:
             // fall thorugh (TODO should be a list)
 
         case Col_Summary:
+            return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
+
         case Col_Platform:
         case Col_Platform:
-            return wxGRID_VALUE_STRING;
+            return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
 
         case Col_Opened:
             return wxGRID_VALUE_BOOL;
 
         case Col_Opened:
             return wxGRID_VALUE_BOOL;
@@ -854,13 +943,14 @@ void BugsGridTable::SetValue( int row, int col, const wxString& value )
                     gd.severity = Sev_Normal;
                 }
             }
                     gd.severity = Sev_Normal;
                 }
             }
+            break;
 
         case Col_Summary:
 
         case Col_Summary:
-            gd.summary = value;
+            wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
             break;
 
         case Col_Platform:
             break;
 
         case Col_Platform:
-            gd.platform = value;
+            wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
             break;
     }
 }
             break;
     }
 }
@@ -961,12 +1051,17 @@ BugsGridTable::BugsGridTable()
 {
 }
 
 {
 }
 
+// ----------------------------------------------------------------------------
+// BugsGridFrame
+// ----------------------------------------------------------------------------
+
 BugsGridFrame::BugsGridFrame()
              : wxFrame(NULL, -1, "Bugs table",
                        wxDefaultPosition, wxSize(500, 300))
 {
     wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
     wxGridTableBase *table = new BugsGridTable();
 BugsGridFrame::BugsGridFrame()
              : wxFrame(NULL, -1, "Bugs table",
                        wxDefaultPosition, wxSize(500, 300))
 {
     wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
     wxGridTableBase *table = new BugsGridTable();
+    table->SetAttrProvider(new MyGridCellAttrProvider);
     grid->SetTable(table, TRUE);
 
     wxGridCellAttr *attrRO = new wxGridCellAttr,
     grid->SetTable(table, TRUE);
 
     wxGridCellAttr *attrRO = new wxGridCellAttr,
@@ -982,5 +1077,11 @@ BugsGridFrame::BugsGridFrame()
     grid->SetColAttr(Col_Priority, attrRangeEditor);
     grid->SetColAttr(Col_Severity, attrCombo);
 
     grid->SetColAttr(Col_Priority, attrRangeEditor);
     grid->SetColAttr(Col_Severity, attrCombo);
 
-    grid->AutoSizeColumns();
+    grid->SetMargins(0, 0);
+
+    grid->Fit();
+    wxSize size = grid->GetSize();
+    size.x += 10;
+    size.y += 10;
+    SetClientSize(size);
 }
 }