]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/grid/griddemo.cpp
document that GetSubItemRect() is new since 2.7.0
[wxWidgets.git] / samples / grid / griddemo.cpp
index 2857b714fc0d526fd3c40c1ac75d3a59f9ba908c..b7d6c0aaa79bc90a00726df516d557630c58f586 100644 (file)
@@ -4,7 +4,7 @@
 // Author:      Michael Bedward
 // Modified by:
 // RCS-ID:      $Id$
-// Copyright:   (c) Michael Bedward, Julian Smart
+// Copyright:   (c) Michael Bedward, Julian Smart, Vadim Zeitlin
 // Licence:     wxWindows license
 /////////////////////////////////////////////////////////////////////////////
 
 // headers
 // ----------------------------------------------------------------------------
 
-#if defined(__GNUG__) && !defined(__APPLE__)
-    #pragma implementation
-    #pragma interface
-#endif
-
 // For compilers that support precompilation, includes "wx/wx.h".
 #include "wx/wxprec.h"
 
@@ -74,6 +69,7 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame )
     EVT_MENU( ID_TOGGLEROWSIZING, GridFrame::ToggleRowSizing )
     EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing )
     EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
+    EVT_MENU( ID_TOGGLEGRIDDRAGCELL, GridFrame::ToggleGridDragCell )
     EVT_MENU( ID_TOGGLEGRIDLINES, GridFrame::ToggleGridLines )
     EVT_MENU( ID_AUTOSIZECOLS, GridFrame::AutoSizeCols )
     EVT_MENU( ID_CELLOVERFLOW, GridFrame::CellOverflow )
@@ -98,7 +94,7 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame )
     EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
     EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
 
-    EVT_MENU( ID_ABOUT, GridFrame::About )
+    EVT_MENU( wxID_ABOUT, GridFrame::About )
     EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
     EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
     EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
@@ -113,6 +109,7 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame )
     EVT_MENU( ID_SELECT_ROW, GridFrame::SelectRow)
     EVT_MENU( ID_SELECT_ALL, GridFrame::SelectAll)
     EVT_MENU( ID_SELECT_UNSELECT, GridFrame::OnAddToSelectToggle)
+    EVT_MENU( ID_SHOW_SELECTION, GridFrame::OnShowSelection)
 
     EVT_MENU( ID_SET_HIGHLIGHT_WIDTH, GridFrame::OnSetHighlightWidth)
     EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH, GridFrame::OnSetROHighlightWidth)
@@ -124,6 +121,7 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame )
     EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
     EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
     EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
+    EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag )
 
     EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
     EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
@@ -149,6 +147,7 @@ GridFrame::GridFrame()
     viewMenu->Append( ID_TOGGLEROWSIZING, _T("Ro&w drag-resize"), wxEmptyString, wxITEM_CHECK );
     viewMenu->Append( ID_TOGGLECOLSIZING, _T("C&ol drag-resize"), wxEmptyString, wxITEM_CHECK );
     viewMenu->Append( ID_TOGGLEGRIDSIZING, _T("&Grid drag-resize"), wxEmptyString, wxITEM_CHECK );
+    viewMenu->Append( ID_TOGGLEGRIDDRAGCELL, _T("&Grid drag-cell"), wxEmptyString, wxITEM_CHECK );
     viewMenu->Append( ID_TOGGLEGRIDLINES, _T("&Grid Lines"), wxEmptyString, wxITEM_CHECK );
     viewMenu->Append( ID_SET_HIGHLIGHT_WIDTH, _T("&Set Cell Highlight Width...") );
     viewMenu->Append( ID_SET_RO_HIGHLIGHT_WIDTH, _T("&Set Cell RO Highlight Width...") );
@@ -193,10 +192,14 @@ GridFrame::GridFrame()
     selectMenu->Append( ID_SELECT_UNSELECT, _T("Add new cells to the selection"),
                         _T("When off, old selection is deselected before ")
                         _T("selecting the new cells"), wxITEM_CHECK );
+    selectMenu->Append( ID_SHOW_SELECTION,
+                        _T("&Show current selection\tCtrl-Alt-S"));
+    selectMenu->AppendSeparator();
     selectMenu->Append( ID_SELECT_ALL, _T("Select all"));
     selectMenu->Append( ID_SELECT_ROW, _T("Select row 2"));
     selectMenu->Append( ID_SELECT_COL, _T("Select col 2"));
     selectMenu->Append( ID_SELECT_CELL, _T("Select cell (3, 1)"));
+    selectMenu->AppendSeparator();
     selectMenu->Append( ID_DESELECT_ALL, _T("Deselect all"));
     selectMenu->Append( ID_DESELECT_ROW, _T("Deselect row 2"));
     selectMenu->Append( ID_DESELECT_COL, _T("Deselect col 2"));
@@ -212,7 +215,7 @@ GridFrame::GridFrame()
 
 
     wxMenu *helpMenu = new wxMenu;
-    helpMenu->Append( ID_ABOUT, _T("&About wxGrid demo") );
+    helpMenu->Append( wxID_ABOUT, _T("&About wxGrid demo") );
 
     wxMenuBar *menuBar = new wxMenuBar;
     menuBar->Append( fileMenu, _T("&File") );
@@ -243,8 +246,8 @@ GridFrame::GridFrame()
                              wxTE_MULTILINE );
 
     logger = new wxLogTextCtrl( logWin );
-    m_logOld = logger->SetActiveTarget( logger );
-    logger->SetTimestamp( NULL );
+    m_logOld = wxLog::SetActiveTarget( logger );
+    wxLog::SetTimestamp( NULL );
 #endif // wxUSE_LOG
 
     // this will create a grid and, by default, an associated grid
@@ -348,6 +351,7 @@ GridFrame::GridFrame()
                    wxEXPAND );
 #endif // wxUSE_LOG
 
+    SetAutoLayout(true);
     SetSizer( topSizer );
 
     topSizer->Fit( this );
@@ -373,6 +377,7 @@ void GridFrame::SetDefaults()
     GetMenuBar()->Check( ID_TOGGLEROWSIZING, true );
     GetMenuBar()->Check( ID_TOGGLECOLSIZING, true );
     GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, true );
+    GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL, false );
     GetMenuBar()->Check( ID_TOGGLEGRIDLINES, true );
     GetMenuBar()->Check( ID_CELLOVERFLOW, true );
 }
@@ -430,6 +435,11 @@ void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
         GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
 }
 
+void GridFrame::ToggleGridDragCell( wxCommandEvent& WXUNUSED(ev) )
+{
+    grid->EnableDragCell(
+        GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL ) );
+}
 
 void GridFrame::ToggleGridLines( wxCommandEvent& WXUNUSED(ev) )
 {
@@ -651,10 +661,12 @@ void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
     {
         grid->BeginBatch();
         for ( int n = 0; n < grid->GetNumberRows(); )
+        {
             if ( grid->IsInSelection( n , 0 ) )
                 grid->DeleteRows( n, 1 );
-        else
-            n++;
+            else
+                n++;
+        }
         grid->EndBatch();
     }
 }
@@ -666,10 +678,12 @@ void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
     {
         grid->BeginBatch();
         for ( int n = 0; n < grid->GetNumberCols(); )
+        {
             if ( grid->IsInSelection( 0 , n ) )
                 grid->DeleteCols( n, 1 );
-        else
-            n++;
+            else
+                n++;
+        }
         grid->EndBatch();
     }
 }
@@ -712,7 +726,8 @@ void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
     {
         // Check the new Refresh function by passing it a rectangle
         // which exactly fits the grid.
-        wxRect r(wxPoint(0, 0), grid->GetSize());
+        wxPoint pt(0, 0);
+        wxRect r(pt, grid->GetSize());
         grid->SetDefaultCellBackgroundColour(col);
         grid->Refresh(true, &r);
     }
@@ -823,6 +838,82 @@ void GridFrame::OnColSize( wxGridSizeEvent& ev )
 }
 
 
+void GridFrame::OnShowSelection(wxCommandEvent& WXUNUSED(event))
+{
+    // max number of elements to dump -- otherwise it can take too much time
+    static const size_t countMax = 100;
+
+    bool rows = false;
+
+    switch ( grid->GetSelectionMode() )
+    {
+        case wxGrid::wxGridSelectCells:
+            {
+                const wxGridCellCoordsArray cells(grid->GetSelectedCells());
+                size_t count = cells.size();
+                wxLogMessage(_T("%lu cells selected:"), (unsigned long)count);
+                if ( count > countMax )
+                {
+                    wxLogMessage(_T("[too many selected cells, ")
+                                 _T("showing only the first %lu]"),
+                                 (unsigned long)countMax);
+                    count = countMax;
+                }
+
+                for ( size_t n = 0; n < count; n++ )
+                {
+                    const wxGridCellCoords& c = cells[n];
+                    wxLogMessage(_T("  selected cell %lu: (%d, %d)"),
+                                 (unsigned long)n, c.GetCol(), c.GetRow());
+                }
+            }
+            break;
+
+        case wxGrid::wxGridSelectRows:
+            rows = true;
+            // fall through
+
+        case wxGrid::wxGridSelectColumns:
+            {
+                const wxChar *plural, *single;
+                if ( rows )
+                {
+                    plural = _T("rows");
+                    single = _T("row");
+                }
+                else // columns
+                {
+                    plural = _T("columns");
+                    single = _T("column");
+                }
+
+                const wxArrayInt sels(rows ? grid->GetSelectedRows()
+                                           : grid->GetSelectedCols());
+                size_t count = sels.size();
+                wxLogMessage(_T("%lu %s selected:"),
+                             (unsigned long)count, plural);
+                if ( count > countMax )
+                {
+                    wxLogMessage(_T("[too many selected %s, ")
+                                 _T("showing only the first %lu]"),
+                                 plural, (unsigned long)countMax);
+                    count = countMax;
+                }
+
+                for ( size_t n = 0; n < count; n++ )
+                {
+                    wxLogMessage(_T("  selected %s %lu: %d"),
+                                 single, (unsigned long)n, sels[n]);
+                }
+            }
+            break;
+
+        default:
+            wxFAIL_MSG( _T("unknown wxGrid selection mode") );
+            break;
+    }
+}
+
 void GridFrame::OnSelectCell( wxGridEvent& ev )
 {
     logBuf = wxEmptyString;
@@ -875,6 +966,18 @@ void GridFrame::OnCellValueChanged( wxGridEvent& ev )
     ev.Skip();
 }
 
+void GridFrame::OnCellBeginDrag( wxGridEvent& ev )
+{
+    logBuf = wxEmptyString;
+    logBuf  << _T("Got request to drag cell at")
+            << _T(" row ") << ev.GetRow()
+            << _T(" col ") << ev.GetCol();
+
+    wxLogMessage( wxT("%s"), logBuf.c_str() );
+
+    ev.Skip();
+}
+
 void GridFrame::OnEditorShown( wxGridEvent& ev )
 {
 
@@ -912,8 +1015,7 @@ void GridFrame::OnEditorHidden( wxGridEvent& ev )
 void GridFrame::About(  wxCommandEvent& WXUNUSED(ev) )
 {
     (void)wxMessageBox( _T("\n\nwxGrid demo \n\n")
-                        _T("Michael Bedward \n")
-                        _T("mbedward@ozemail.com.au \n\n"),
+                        _T("Michael Bedward, Julian Smart, Vadim Zeitlin"),
                         _T("About"),
                         wxOK );
 }
@@ -1174,10 +1276,13 @@ wxString BugsGridTable::GetValue( int row, int col )
     switch ( col )
     {
         case Col_Id:
+            return wxString::Format(_T("%d"), gd.id);
+
         case Col_Priority:
+            return wxString::Format(_T("%d"), gd.prio);
+
         case Col_Opened:
-            wxFAIL_MSG(_T("unexpected column"));
-            break;
+            return gd.opened ? _T("1") : _T("0");
 
         case Col_Severity:
             return severities[gd.severity];
@@ -1235,7 +1340,10 @@ void BugsGridTable::SetValue( int row, int col, const wxString& value )
     }
 }
 
-bool BugsGridTable::CanGetValueAs( int WXUNUSED(row), int col, const wxString& typeName )
+bool
+BugsGridTable::CanGetValueAs(int WXUNUSED(row),
+                             int col,
+                             const wxString& typeName)
 {
     if ( typeName == wxGRID_VALUE_STRING )
     {
@@ -1327,10 +1435,6 @@ wxString BugsGridTable::GetColLabelValue( int col )
     return headers[col];
 }
 
-BugsGridTable::BugsGridTable()
-{
-}
-
 // ----------------------------------------------------------------------------
 // BugsGridFrame
 // ----------------------------------------------------------------------------