]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/newgrid/griddemo.cpp
Applied patch [ 651640 ] Toolbar sizing fix
[wxWidgets.git] / samples / newgrid / griddemo.cpp
index e78e342be9a6107e299fcff95f876471cfc08449..e1820b8a53983060fe4a7068715456537134b4cb 100644 (file)
 #endif
 
 #ifndef WX_PRECOMP
-#include "wx/wx.h"
+    #include "wx/wx.h"
 #endif
 
 #include "wx/colordlg.h"
+#include "wx/fontdlg.h"
 
 #include "wx/grid.h"
 #include "wx/generic/gridctrl.h"
@@ -74,8 +75,11 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame )
     EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
     EVT_MENU( ID_TOGGLEGRIDLINES, GridFrame::ToggleGridLines )
     EVT_MENU( ID_AUTOSIZECOLS, GridFrame::AutoSizeCols )
+    EVT_MENU( ID_CELLOVERFLOW, GridFrame::CellOverflow )
+    EVT_MENU( ID_RESIZECELL, GridFrame::ResizeCell )
     EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
     EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
+    EVT_MENU( ID_SETLABEL_FONT, GridFrame::SetLabelFont )
     EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
     EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
     EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
@@ -97,6 +101,7 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame )
     EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
     EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
     EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
+    EVT_MENU( ID_SMALL_GRID, GridFrame::OnSmallGrid)
 
     EVT_MENU( ID_DESELECT_CELL, GridFrame::DeselectCell)
     EVT_MENU( ID_DESELECT_COL, GridFrame::DeselectCol)
@@ -135,6 +140,7 @@ GridFrame::GridFrame()
     wxMenu *fileMenu = new wxMenu;
     fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V");
     fileMenu->Append( ID_BUGS_TABLE, "&Bugs table test\tCtrl-B");
+    fileMenu->Append( ID_SMALL_GRID, "&Small Grid test\tCtrl-S");
     fileMenu->AppendSeparator();
     fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
 
@@ -149,6 +155,8 @@ GridFrame::GridFrame()
     viewMenu->Append( ID_SET_HIGHLIGHT_WIDTH, "&Set Cell Highlight Width...", "" );
     viewMenu->Append( ID_SET_RO_HIGHLIGHT_WIDTH, "&Set Cell RO Highlight Width...", "" );
     viewMenu->Append( ID_AUTOSIZECOLS, "&Auto-size cols" );
+    viewMenu->Append( ID_CELLOVERFLOW, "&Overflow cells", "", TRUE );
+    viewMenu->Append( ID_RESIZECELL, "&Resize cell (7,1)", "", TRUE );
 
     wxMenu *rowLabelMenu = new wxMenu;
 
@@ -169,11 +177,12 @@ GridFrame::GridFrame()
     colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
 
     wxMenu *colMenu = new wxMenu;
-    colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
-    colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
-    colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
-    colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour" );
-    colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour" );
+    colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour..." );
+    colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour..." );
+    colMenu->Append( ID_SETLABEL_FONT, "Set label fo&nt..." );
+    colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour..." );
+    colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour..." );
+    colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour..." );
 
     wxMenu *editMenu = new wxMenu;
     editMenu->Append( ID_INSERTROW, "Insert &row" );
@@ -237,7 +246,13 @@ GridFrame::GridFrame()
 
     // this will create a grid and, by default, an associated grid
     // table for strings
-    grid->CreateGrid( 100, 100 );
+    grid->CreateGrid( 0, 0 );
+    grid->AppendRows(100);
+    grid->AppendCols(100);
+
+    int ir = grid->GetNumberRows();
+    grid->DeleteRows(0, ir);
+    grid->AppendRows(ir);
 
     grid->SetRowSize( 0, 60 );
     grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
@@ -245,17 +260,23 @@ GridFrame::GridFrame()
     grid->SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." );
     grid->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer);
     grid->SetCellEditor( 0,  1 , new wxGridCellAutoWrapStringEditor);
-    
+
     grid->SetCellValue( 0, 2, "Blah" );
     grid->SetCellValue( 0, 3, "Read only" );
     grid->SetReadOnly( 0, 3 );
-    
+
     grid->SetCellValue( 0, 4, "Can veto edit this cell" );
 
     grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
 
     grid->SetRowSize( 99, 60 );
     grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
+    grid->SetCellValue( 1, 0, "This default cell will overflow into neighboring cells, but not if you turn overflow off.");
+
+    grid->SetCellTextColour(1, 2, *wxRED);
+    grid->SetCellBackgroundColour(1, 2, *wxGREEN);
+
+    grid->SetCellValue( 1, 4, "I'm in the middle");
 
     grid->SetCellValue(2, 2, "red");
 
@@ -286,7 +307,7 @@ GridFrame::GridFrame()
 
     grid->SetCellTextColour(5, 8, *wxGREEN);
     grid->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
-    grid->SetCellValue(5, 5, "Bg from row attr\nText col from col attr");
+    grid->SetCellValue(5, 5, "Bg from row attr Text col from col attr and this text is so long that it covers over many many empty cells but is broken by one that isn't");
 
     grid->SetColFormatFloat(6);
     grid->SetCellValue(0, 6, "3.1415");
@@ -298,6 +319,21 @@ GridFrame::GridFrame()
     grid->SetCellValue(1, 7, "1415");
     grid->SetCellValue(2, 7, "12345.67890");
 
+    const wxString choices[] =
+    {
+        _T("Please select a choice"),
+        _T("This takes two cells"),
+        _T("Another choice"),
+    };
+    grid->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices), choices));
+    grid->SetCellSize(4, 0, 1, 2);
+    grid->SetCellValue(4, 0, choices[0]);
+    grid->SetCellOverflow(4, 0, FALSE);
+
+    grid->SetCellSize(7, 1, 3, 4);
+    grid->SetCellAlignment(7, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
+    grid->SetCellValue(7, 1, "Big box!");
+
     wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
     topSizer->Add( grid,
                    1,
@@ -333,6 +369,7 @@ void GridFrame::SetDefaults()
     GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE );
     GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, TRUE );
     GetMenuBar()->Check( ID_TOGGLEGRIDLINES, TRUE );
+    GetMenuBar()->Check( ID_CELLOVERFLOW, TRUE );
 }
 
 
@@ -431,6 +468,20 @@ void GridFrame::AutoSizeCols( wxCommandEvent& WXUNUSED(ev) )
     grid->Refresh();
 }
 
+void GridFrame::CellOverflow( wxCommandEvent& ev )
+{
+    grid->SetDefaultCellOverflow(ev.IsChecked());
+    grid->Refresh();
+}
+
+void GridFrame::ResizeCell( wxCommandEvent& ev )
+{
+    if (ev.IsChecked())
+        grid->SetCellSize( 7, 1, 5, 5 );
+    else
+        grid->SetCellSize( 7, 1, 1, 5 );
+    grid->Refresh();
+}
 
 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
 {
@@ -459,6 +510,14 @@ void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
     }
 }
 
+void GridFrame::SetLabelFont( wxCommandEvent& WXUNUSED(ev) )
+{
+    wxFont font = wxGetFontFromUser(this);
+    if ( font.Ok() )
+    {
+        grid->SetLabelFont(font);
+    }
+}
 
 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
 {
@@ -589,8 +648,8 @@ void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
         for ( int n = 0; n < grid->GetNumberRows(); )
             if ( grid->IsInSelection( n , 0 ) )
                 grid->DeleteRows( n, 1 );
-           else
-               n++;
+        else
+            n++;
         grid->EndBatch();
     }
 }
@@ -604,8 +663,8 @@ void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
         for ( int n = 0; n < grid->GetNumberCols(); )
             if ( grid->IsInSelection( 0 , n ) )
                 grid->DeleteCols( n, 1 );
-           else
-               n++;
+        else
+            n++;
         grid->EndBatch();
     }
 }
@@ -646,8 +705,11 @@ void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
     wxColour col = wxGetColourFromUser(this);
     if ( col.Ok() )
     {
+        // Check the new Refresh function by passing it a rectangle
+        // which exactly fits the grid.
+        wxRect r(wxPoint(0, 0), grid->GetSize());
         grid->SetDefaultCellBackgroundColour(col);
-        grid->Refresh();
+        grid->Refresh(TRUE, &r);
     }
 }
 
@@ -813,14 +875,13 @@ void GridFrame::OnEditorShown( wxGridEvent& ev )
 
     if ( (ev.GetCol() == 4) &&
          (ev.GetRow() == 0) &&
-        (wxMessageBox(_T("Are you sure you wish to edit this cell"),
-                      _T("Checking"),wxYES_NO) == wxNO ) ) {
+     (wxMessageBox(_T("Are you sure you wish to edit this cell"),
+                   _T("Checking"),wxYES_NO) == wxNO ) ) {
 
-        ev.Veto();
-        return;
+     ev.Veto();
+     return;
     }
-                     
-       
+
     wxLogMessage( wxT("Cell editor shown.") );
 
     ev.Skip();
@@ -828,14 +889,14 @@ void GridFrame::OnEditorShown( wxGridEvent& ev )
 
 void GridFrame::OnEditorHidden( wxGridEvent& ev )
 {
-   
+
     if ( (ev.GetCol() == 4) &&
          (ev.GetRow() == 0) &&
-        (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
-                      _T("Checking"),wxYES_NO) == wxNO ) ) {
+     (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
+                   _T("Checking"),wxYES_NO) == wxNO ) ) {
 
-        ev.Veto();
-        return;
+        ev.Veto();
+        return;
     }
 
     wxLogMessage( wxT("Cell editor hidden.") );
@@ -864,6 +925,16 @@ void GridFrame::OnBugsTable(wxCommandEvent& )
     frame->Show(TRUE);
 }
 
+void GridFrame::OnSmallGrid(wxCommandEvent& )
+{
+    wxFrame* frame = new wxFrame(NULL, -1, "A Small Grid",
+                                 wxDefaultPosition, wxSize(640, 480));
+    wxPanel* panel = new wxPanel(frame, -1);
+    wxGrid* grid = new wxGrid(panel, -1, wxPoint(10,10), wxSize(400,400),
+                              wxWANTS_CHARS | wxSIMPLE_BORDER);
+    grid->CreateGrid(3,3);
+    frame->Show(TRUE);
+}
 
 void GridFrame::OnVTable(wxCommandEvent& )
 {