Grid.cs

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 // wx.NET/Samples - Grid.cs
00003 //
00004 // wx.NET "grid" sample.
00005 //
00006 // Written by Alexander Olk (xenomorph2@onlinehome.de)
00007 // (C) 2004 by Alexander Olk
00008 // BigGrid extended by Dr. Harald Meyer auf'm Hofe March 2006
00009 //
00010 // Licensed under the wxWidgets license, see LICENSE.txt for details.
00011 //
00012 // $Id: Grid.cs,v 1.18 2009/10/11 16:24:26 harald_meyer Exp $
00013 //
00014 //-----------------------------------------------------------------------------
00015 
00016 using System;
00017 using System.Drawing;
00018 using System.Collections;
00019 using wx;
00020 using wx.GridCtrl;
00021 
00032 namespace wx.SampleGrid
00033 {
00034         public class GridFrame : Frame
00035         {
00036                 enum Cmd  
00037                 {
00038                         ID_TOGGLEROWLABELS = 100,
00039                         ID_TOGGLECOLLABELS,
00040                         ID_TOGGLEEDIT,
00041                         ID_TOGGLEROWSIZING,
00042                         ID_TOGGLECOLSIZING,
00043                         ID_TOGGLEGRIDSIZING,
00044                         ID_TOGGLEGRIDLINES,
00045                         ID_AUTOSIZECOLS,
00046                         ID_CELLOVERFLOW,
00047                         ID_RESIZECELL,
00048                         ID_SETLABELCOLOUR,
00049                         ID_SETLABELTEXTCOLOUR,
00050                         ID_SETLABEL_FONT,
00051                         ID_ROWLABELALIGN,
00052                         ID_ROWLABELHORIZALIGN,
00053                         ID_ROWLABELVERTALIGN,
00054                         ID_COLLABELALIGN,
00055                         ID_COLLABELHORIZALIGN,
00056                         ID_COLLABELVERTALIGN,
00057                         ID_GRIDLINECOLOUR,
00058                         ID_INSERTROW,
00059                         ID_INSERTCOL,
00060                         ID_DELETEROW,
00061                         ID_DELETECOL,
00062                         ID_CLEARGRID,
00063                         ID_CHANGESEL,
00064                         ID_SELCELLS,
00065                         ID_SELROWS,
00066                         ID_SELCOLS,
00067                         ID_SET_CELL_FG_COLOUR,
00068                         ID_SET_CELL_BG_COLOUR,
00069                         ID_ABOUT,
00070                         ID_VTABLE,
00071                         ID_BUGS_TABLE,
00072                         ID_SMALL_GRID,
00073                         ID_SELECT_UNSELECT,
00074                         ID_SELECT_ALL,
00075                         ID_SELECT_ROW,
00076                         ID_SELECT_COL,
00077                         ID_SELECT_CELL,
00078                         ID_DESELECT_ALL,
00079                         ID_DESELECT_ROW,
00080                         ID_DESELECT_COL,
00081                         ID_DESELECT_CELL,
00082                         
00083                         ID_SET_HIGHLIGHT_WIDTH,
00084                         ID_SET_RO_HIGHLIGHT_WIDTH,
00085 
00086                         ID_TESTFUNC,
00087                         ID_EXIT
00088                 }
00089                 
00090                 //-----------------------------------------------------------------------------
00091                 
00092                 private Grid grid;
00093                 private TextCtrl logWin;
00094                 private string logBuf;
00095                 
00096                 public bool m_addToSel = false;
00097                 
00098                 public static long s_sizeGrid = 10000;
00099                 
00100                 //-----------------------------------------------------------------------------
00101                 
00102                 public GridFrame()
00103                         : base( null, -1, "wxWidgets grid class demo", wxDefaultPosition, wxDefaultSize )
00104                 {
00105                         Icon = new wx.Icon( "../Samples/Grid/mondrian.png" );
00106                         
00107                         int gridW = 600;
00108                         int gridH = 300;
00109                         int logW = gridW;
00110                         int logH = 100;
00111                         
00112                         Menu fileMenu = new Menu();
00113                         fileMenu.Append( (int)Cmd.ID_VTABLE, "&Virtual table test\tCtrl-V" );
00114                         fileMenu.Append( (int)Cmd.ID_BUGS_TABLE, "&Bugs table test\tCtrl-B" );
00115                         fileMenu.Append( (int)Cmd.ID_SMALL_GRID, "&Small Grid test\tCtrl-S" );
00116                         fileMenu.AppendSeparator();
00117                         fileMenu.Append( (int)Cmd.ID_EXIT, "E&xit\tAlt-X" );
00118 
00119                         Menu viewMenu = new Menu();
00120                         viewMenu.AppendCheckItem( (int)Cmd.ID_TOGGLEROWLABELS,  "&Row labels", "" );
00121                         viewMenu.AppendCheckItem( (int)Cmd.ID_TOGGLECOLLABELS,  "&Col labels", "" );
00122                         viewMenu.AppendCheckItem( (int)Cmd.ID_TOGGLEEDIT,  "&Editable", "" );
00123                         viewMenu.AppendCheckItem( (int)Cmd.ID_TOGGLEROWSIZING, "Ro&w drag-resize", "" );
00124                         viewMenu.AppendCheckItem( (int)Cmd.ID_TOGGLECOLSIZING, "C&ol drag-resize", "" );
00125                         viewMenu.AppendCheckItem( (int)Cmd.ID_TOGGLEGRIDSIZING, "&Grid drag-resize", "" );
00126                         viewMenu.AppendCheckItem( (int)Cmd.ID_TOGGLEGRIDLINES, "&Grid Lines", "" );
00127                         viewMenu.Append( (int)Cmd.ID_SET_HIGHLIGHT_WIDTH, "&Set Cell Highlight Width...", "" );
00128                         viewMenu.Append( (int)Cmd.ID_SET_RO_HIGHLIGHT_WIDTH, "&Set Cell RO Highlight Width...", "" );
00129                         viewMenu.Append( (int)Cmd.ID_AUTOSIZECOLS, "&Auto-size cols" );
00130                         viewMenu.AppendCheckItem( (int)Cmd.ID_CELLOVERFLOW, "&Overflow cells", "" );
00131                         viewMenu.AppendCheckItem( (int)Cmd.ID_RESIZECELL, "&Resize cell (7,1)", "" );
00132 
00133                         Menu rowLabelMenu = new Menu();
00134 
00135                         viewMenu.Append( (int)Cmd.ID_ROWLABELALIGN, "R&ow label alignment", rowLabelMenu, "Change alignment of row labels" );
00136 
00137                         rowLabelMenu.Append( (int)Cmd.ID_ROWLABELHORIZALIGN, "&Horizontal" );
00138                         rowLabelMenu.Append( (int)Cmd.ID_ROWLABELVERTALIGN, "&Vertical" );
00139 
00140                         Menu colLabelMenu = new Menu();
00141 
00142                         viewMenu.Append( (int)Cmd.ID_COLLABELALIGN, "Col l&abel alignment", colLabelMenu, "Change alignment of col labels" );
00143 
00144                         colLabelMenu.Append( (int)Cmd.ID_COLLABELHORIZALIGN, "&Horizontal" );
00145                         colLabelMenu.Append( (int)Cmd.ID_COLLABELVERTALIGN, "&Vertical" );
00146 
00147                         Menu colMenu = new Menu();
00148                         colMenu.Append( (int)Cmd.ID_SETLABELCOLOUR, "Set &label colour..." );
00149                         colMenu.Append( (int)Cmd.ID_SETLABELTEXTCOLOUR, "Set label &text colour..." );
00150                         colMenu.Append( (int)Cmd.ID_SETLABEL_FONT, "Set label fo&nt..." );
00151                         colMenu.Append( (int)Cmd.ID_GRIDLINECOLOUR, "&Grid line colour..." );
00152                         colMenu.Append( (int)Cmd.ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour..." );
00153                         colMenu.Append( (int)Cmd.ID_SET_CELL_BG_COLOUR, "Set cell &background colour..." );
00154 
00155                         Menu editMenu = new Menu();
00156                         editMenu.Append( (int)Cmd.ID_INSERTROW, "Insert &row" );
00157                         editMenu.Append( (int)Cmd.ID_INSERTCOL, "Insert &column" );
00158                         editMenu.Append( (int)Cmd.ID_DELETEROW, "Delete selected ro&ws" );
00159                         editMenu.Append( (int)Cmd.ID_DELETECOL, "Delete selected co&ls" );
00160                         editMenu.Append( (int)Cmd.ID_CLEARGRID, "Cl&ear grid cell contents" );
00161 
00162                         Menu selectMenu = new Menu();
00163                         selectMenu.AppendCheckItem( (int)Cmd.ID_SELECT_UNSELECT, "Add new cells to the selection",
00164                                 "When off, old selection is deselected before selecting the new cells" );
00165                         selectMenu.Append( (int)Cmd.ID_SELECT_ALL, "Select all" );
00166                         selectMenu.Append( (int)Cmd.ID_SELECT_ROW, "Select row 2" );
00167                         selectMenu.Append( (int)Cmd.ID_SELECT_COL, "Select col 2" );
00168                         selectMenu.Append( (int)Cmd.ID_SELECT_CELL, "Select cell (3, 1)" );
00169                         selectMenu.Append( (int)Cmd.ID_DESELECT_ALL, "Deselect all" );
00170                         selectMenu.Append( (int)Cmd.ID_DESELECT_ROW, "Deselect row 2" );
00171                         selectMenu.Append( (int)Cmd.ID_DESELECT_COL, "Deselect col 2" );
00172                         selectMenu.Append( (int)Cmd.ID_DESELECT_CELL, "Deselect cell (3, 1)" );
00173                         
00174                         Menu selectionMenu = new Menu();
00175                         selectMenu.Append( (int)Cmd.ID_CHANGESEL, "Change &selection mode", selectionMenu, "Change selection mode" );
00176 
00177                         selectionMenu.Append( (int)Cmd.ID_SELCELLS, "Select &Cells" );
00178                         selectionMenu.Append( (int)Cmd.ID_SELROWS, "Select &Rows" );
00179                         selectionMenu.Append( (int)Cmd.ID_SELCOLS, "Select C&ols" );
00180 
00181 
00182                         Menu helpMenu = new Menu();
00183                         helpMenu.Append( (int)Cmd.ID_ABOUT, "&About wxGrid demo" );
00184 
00185                         wx.MenuBar menuBar = new wx.MenuBar();
00186                         menuBar.Append( fileMenu, "&File" );
00187                         menuBar.Append( viewMenu, "&View" );
00188                         menuBar.Append( colMenu,  "&Colours" );
00189                         menuBar.Append( editMenu, "&Edit" );
00190                         menuBar.Append( selectMenu, "&Select" );
00191                         menuBar.Append( helpMenu, "&Help" );
00192 
00193                         MenuBar = menuBar ;     
00194                         
00195                         grid = new Grid( this, -1, new Point( 0, 0 ), new Size( 400, 300 ) );
00196 
00197             logWin = new TextCtrl(this, -1, "", new Point(0, gridH + 20), new Size(logW, logH), wx.WindowStyles.TE_MULTILINE);
00198                         
00199                         Log.SetActiveTarget( logWin ); 
00200                         
00201                         grid.CreateGrid( 0, 0 );
00202                         grid.AppendRows( 100 );
00203                         grid.AppendCols( 100 );
00204                         
00205                         int ir = grid.NumberRows;
00206                         grid.DeleteRows( 0, ir );
00207                         grid.AppendRows( ir );
00208                         
00209                         grid.SetRowSize( 0, 60 );
00210                         grid.SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
00211                         
00212                         grid.SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." );
00213             grid.SetCellRenderer(0, 1, new wx.GridCtrl.Renderers.GridCellAutoWrapStringRenderer());
00214             grid.SetCellEditor(0, 1, new wx.GridCtrl.Editors.GridCellAutoWrapStringEditor());
00215 
00216                         grid.SetCellValue( 0, 2, "Blah" );
00217                         grid.SetCellValue( 0, 3, "Read only" );
00218                         grid.SetReadOnly( 0, 3 );
00219 
00220                         grid.SetCellValue( 0, 4, "Can veto edit this cell" );
00221 
00222                         grid.SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
00223 
00224                         grid.SetRowSize( 99, 60 );
00225                         grid.SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
00226                         grid.SetCellValue( 1, 0, "This default cell will overflow into neighboring cells, but not if you turn overflow off." );
00227 
00228                         grid.SetCellTextColour( 1, 2, Colour.wxRED );
00229                         grid.SetCellBackgroundColour( 1, 2, Colour.wxGREEN );
00230 
00231                         grid.SetCellValue( 1, 4, "I'm in the middle" );
00232 
00233                         grid.SetCellValue( 2, 2, "red" );
00234 
00235                         grid.SetCellTextColour( 2, 2, Colour.wxRED );
00236                         grid.SetCellValue( 3, 3, "green on grey" );
00237                         grid.SetCellTextColour( 3, 3, Colour.wxGREEN );
00238                         grid.SetCellBackgroundColour( 3, 3, Colour.wxLIGHT_GREY );
00239 
00240                         grid.SetCellValue( 4, 4, "a weird looking cell" );
00241                         grid.SetCellAlignment( 4, 4, (int)Alignment.wxALIGN_CENTRE, (int)Alignment.wxALIGN_CENTRE );
00242                         grid.SetCellRenderer( 4, 4, new MyGridCellRenderer() );
00243 
00244                         grid.SetCellValue( 3, 0, "" );
00245             grid.SetCellRenderer(3, 0, new wx.GridCtrl.Renderers.GridCellBoolRenderer());
00246             grid.SetCellEditor(3, 0, new wx.GridCtrl.Editors.GridCellBoolEditor());
00247 
00248             grid.SetCellValue(3, 4, DateTime.Now.ToString());
00249             grid.SetCellEditor(3, 4, new wx.GridCtrl.Editors.GridCellDateTimeEditor("%d ({ddd}) {HH}:{mm}", new DateTime(1967, 9, 27), new DateTime(2050, 9, 27), false));
00250             grid.SetCellRenderer(3, 4, new wx.GridCtrl.Renderers.GridCellDateTimeMaskRenderer("%d ({ddd}) {HH}:{mm}"));
00251 
00252                         GridCellAttr attr = new GridCellAttr();
00253                         attr.TextColour = Colour.wxBLUE;
00254                         grid.SetColAttr( 5, attr );
00255                         attr = new GridCellAttr();      
00256                         attr.BackgroundColour = Colour.wxRED;
00257                         grid.SetRowAttr( 5, attr );
00258 
00259                         grid.SetCellValue( 2, 4, "a wider column" );
00260                         grid.SetColSize( 4, 200 );
00261                         grid.SetColMinimalWidth( 4, 200 );
00262 
00263                         grid.SetCellTextColour( 5, 8, Colour.wxGREEN );
00264                         grid.SetCellValue( 5, 8, "Bg from row attr\nText col from cell attr" );
00265                         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" );
00266 
00267                         grid.SetColFormatFloat( 6 );
00268                         grid.SetCellValue( 0, 6, "3.1415" );
00269                         grid.SetCellValue( 1, 6, "1415" );
00270                         grid.SetCellValue( 2, 6, "12345.67890" );
00271 
00272                         grid.SetColFormatFloat( 7, 6, 2 );
00273                         grid.SetCellValue( 0, 7, "3.1415" );
00274                         grid.SetCellValue( 1, 7, "1415" );
00275                         grid.SetCellValue( 2, 7, "12345.67890" );
00276 
00277                         string[] choices =
00278                         {
00279                                 "Please select a choice",
00280                                 "This takes two cells",
00281                                 "Another choice"
00282                         };
00283 
00284             grid.SetCellEditor(4, 0, new wx.GridCtrl.Editors.GridCellChoiceEditor(choices));
00285                         grid.SetCellSize( 4, 0, 1, 2 );
00286                         grid.SetCellValue( 4, 0, choices[0] );
00287                         grid.SetCellOverflow( 4, 0, false );
00288 
00289                         grid.SetCellSize( 7, 1, 3, 4 );
00290                         grid.SetCellAlignment( 7, 1, (int)Alignment.wxALIGN_CENTRE, (int)Alignment.wxALIGN_CENTRE );
00291                         grid.SetCellValue( 7, 1, "Big box!" );
00292 
00293                         BoxSizer topSizer = new BoxSizer( Orientation.wxVERTICAL );
00294                         topSizer.Add( grid, 1, wx.SizerFlag.wxEXPAND );
00295 
00296                         topSizer.Add( logWin, 0, wx.SizerFlag.wxEXPAND );
00297 
00298                         AutoLayout = true;
00299                         Sizer = topSizer ;
00300 
00301                         topSizer.Fit( this );
00302                         topSizer.SetSizeHints( this );
00303 
00304                         Centre();
00305                         SetDefaults();                  
00306                         
00307                         EVT_MENU( (int)Cmd.ID_TOGGLEROWLABELS,  new EventListener( ToggleRowLabels ) );
00308                         EVT_MENU( (int)Cmd.ID_TOGGLECOLLABELS,  new EventListener( ToggleColLabels ) );
00309                         EVT_MENU( (int)Cmd.ID_TOGGLEEDIT, new EventListener( ToggleEditing ) );
00310                         EVT_MENU( (int)Cmd.ID_TOGGLEROWSIZING, new EventListener( ToggleRowSizing ) );
00311                         EVT_MENU( (int)Cmd.ID_TOGGLECOLSIZING, new EventListener( ToggleColSizing ) );
00312                         EVT_MENU( (int)Cmd.ID_TOGGLEGRIDSIZING, new EventListener( ToggleGridSizing ) );
00313                         EVT_MENU( (int)Cmd.ID_TOGGLEGRIDLINES, new EventListener( ToggleGridLines ) );
00314                         EVT_MENU( (int)Cmd.ID_AUTOSIZECOLS, new EventListener( AutoSizeCols ) );
00315                         EVT_MENU( (int)Cmd.ID_CELLOVERFLOW, new EventListener( CellOverflow ) );
00316                         EVT_MENU( (int)Cmd.ID_RESIZECELL, new EventListener( ResizeCell ) );
00317                         EVT_MENU( (int)Cmd.ID_SETLABELCOLOUR, new EventListener( SetLabelColour ) );
00318                         EVT_MENU( (int)Cmd.ID_SETLABELTEXTCOLOUR, new EventListener( SetLabelTextColour ) );
00319                         EVT_MENU( (int)Cmd.ID_SETLABEL_FONT, new EventListener( SetLabelFont ) );
00320                         EVT_MENU( (int)Cmd.ID_ROWLABELHORIZALIGN, new EventListener( SetRowLabelHorizAlignment ) );
00321                         EVT_MENU( (int)Cmd.ID_ROWLABELVERTALIGN, new EventListener( SetRowLabelVertAlignment ) );
00322                         EVT_MENU( (int)Cmd.ID_COLLABELHORIZALIGN, new EventListener( SetColLabelHorizAlignment ) );
00323                         EVT_MENU( (int)Cmd.ID_COLLABELVERTALIGN, new EventListener( SetColLabelVertAlignment ) );
00324                         EVT_MENU( (int)Cmd.ID_GRIDLINECOLOUR, new EventListener( SetGridLineColour ) );
00325                         EVT_MENU( (int)Cmd.ID_INSERTROW, new EventListener( InsertRow ) );
00326                         EVT_MENU( (int)Cmd.ID_INSERTCOL, new EventListener( InsertCol ) );
00327                         EVT_MENU( (int)Cmd.ID_DELETEROW, new EventListener( DeleteSelectedRows ) );
00328                         EVT_MENU( (int)Cmd.ID_DELETECOL, new EventListener( DeleteSelectedCols ) );
00329                         EVT_MENU( (int)Cmd.ID_CLEARGRID, new EventListener( ClearGrid ) );
00330                         EVT_MENU( (int)Cmd.ID_SELCELLS,  new EventListener( SelectCells ) );
00331                         EVT_MENU( (int)Cmd.ID_SELROWS,  new EventListener( SelectRows ) );
00332                         EVT_MENU( (int)Cmd.ID_SELCOLS,  new EventListener( SelectCols ) );
00333                         
00334                         EVT_MENU( (int)Cmd.ID_SET_CELL_FG_COLOUR, new EventListener( SetCellFgColour ) );
00335                         EVT_MENU( (int)Cmd.ID_SET_CELL_BG_COLOUR, new EventListener( SetCellBgColour ) );
00336                         
00337                         EVT_MENU( (int)Cmd.ID_ABOUT, new EventListener( About ) );
00338                         EVT_MENU( (int)Cmd.ID_EXIT, new EventListener( OnQuit ) );
00339                         EVT_MENU( (int)Cmd.ID_VTABLE, new EventListener( OnVTable) );
00340                         EVT_MENU( (int)Cmd.ID_BUGS_TABLE, new EventListener( OnBugsTable) );
00341                         EVT_MENU( (int)Cmd.ID_SMALL_GRID, new EventListener( OnSmallGrid) );
00342                         
00343                         EVT_MENU( (int)Cmd.ID_DESELECT_CELL, new EventListener( DeselectCell) );
00344                         EVT_MENU( (int)Cmd.ID_DESELECT_COL, new EventListener( DeselectCol) );
00345                         EVT_MENU( (int)Cmd.ID_DESELECT_ROW, new EventListener( DeselectRow) );
00346                         EVT_MENU( (int)Cmd.ID_DESELECT_ALL, new EventListener( DeselectAll) );
00347                         EVT_MENU( (int)Cmd.ID_SELECT_CELL, new EventListener( SelectCell) );
00348                         EVT_MENU( (int)Cmd.ID_SELECT_COL, new EventListener( SelectCol) );
00349                         EVT_MENU( (int)Cmd.ID_SELECT_ROW, new EventListener( SelectRow) );
00350                         EVT_MENU( (int)Cmd.ID_SELECT_ALL, new EventListener( SelectAll) );
00351                         EVT_MENU( (int)Cmd.ID_SELECT_UNSELECT, new EventListener( OnAddToSelectToggle) );
00352                         
00353                         EVT_MENU( (int)Cmd.ID_SET_HIGHLIGHT_WIDTH, new EventListener( OnSetHighlightWidth) );
00354                         EVT_MENU( (int)Cmd.ID_SET_RO_HIGHLIGHT_WIDTH, new EventListener( OnSetROHighlightWidth) );
00355                         
00356                         EVT_GRID_LABEL_LEFT_CLICK( new EventListener( OnLabelLeftClick ) );
00357                         EVT_GRID_CELL_LEFT_CLICK( new EventListener( OnCellLeftClick ) );
00358                         EVT_GRID_ROW_SIZE( new EventListener( OnRowSize ) );
00359                         EVT_GRID_COL_SIZE( new EventListener( OnColSize ) );
00360                         EVT_GRID_SELECT_CELL( new EventListener( OnSelectCell ) );
00361                         EVT_GRID_RANGE_SELECT( new EventListener( OnRangeSelected ) );
00362                         EVT_GRID_CELL_CHANGE( new EventListener( OnCellValueChanged ) );
00363                         
00364                         EVT_GRID_EDITOR_SHOWN( new EventListener( OnEditorShown ) );
00365                         EVT_GRID_EDITOR_HIDDEN( new EventListener( OnEditorHidden ) );                  
00366                         
00367                 }
00368                 
00369                 public void SetDefaults()
00370                 {
00371                         MenuBar.Check( (int)Cmd.ID_TOGGLEROWLABELS, true );
00372                         MenuBar.Check( (int)Cmd.ID_TOGGLECOLLABELS, true );
00373                         MenuBar.Check( (int)Cmd.ID_TOGGLEEDIT, true );
00374                         MenuBar.Check( (int)Cmd.ID_TOGGLEROWSIZING, true );
00375                         MenuBar.Check( (int)Cmd.ID_TOGGLECOLSIZING, true );
00376                         MenuBar.Check( (int)Cmd.ID_TOGGLEGRIDSIZING, true );
00377                         MenuBar.Check( (int)Cmd.ID_TOGGLEGRIDLINES, true );
00378                         MenuBar.Check( (int)Cmd.ID_CELLOVERFLOW, true );
00379                 }
00380                 
00381                 public void ToggleRowLabels( object sender, Event e )
00382                 {
00383                         if ( MenuBar.IsChecked( (int)Cmd.ID_TOGGLEROWLABELS ) )
00384                         {
00385                                 grid.RowLabelSize = grid.DefaultRowLabelSize;
00386                         }
00387                         else
00388                         {
00389                                 grid.RowLabelSize = 0;
00390                         }
00391                 }
00392                 
00393                 public void ToggleColLabels( object sender, Event e )
00394                 {
00395                         if ( MenuBar.IsChecked( (int)Cmd.ID_TOGGLECOLLABELS ) )
00396                         {
00397                                 grid.ColLabelSize = grid.DefaultColLabelSize;
00398                         }
00399                         else
00400                         {
00401                                 grid.ColLabelSize = 0;
00402                         }                       
00403                 }
00404                 
00405                 public void ToggleEditing( object sender, Event e )
00406                 {
00407                         grid.IsEditable = MenuBar.IsChecked( (int)Cmd. ID_TOGGLEEDIT );
00408                 }
00409 
00410                 public void ToggleRowSizing( object sender, Event e )
00411                 {
00412                         grid.DragRowSizeEnabled = MenuBar.IsChecked( (int)Cmd.ID_TOGGLEROWSIZING );                     
00413                 }
00414 
00415                 public void ToggleColSizing( object sender, Event e )
00416                 {
00417                         grid.DragColSizeEnabled = MenuBar.IsChecked( (int)Cmd.ID_TOGGLECOLSIZING );
00418                         
00419                 }
00420 
00421                 public void ToggleGridSizing( object sender, Event e )
00422                 {
00423                         grid.DragGridSizeEnabled = MenuBar.IsChecked( (int)Cmd.ID_TOGGLEGRIDSIZING );                   
00424                 }
00425 
00426                 public void ToggleGridLines( object sender, Event e )
00427                 {
00428                         grid.GridLinesEnabled = MenuBar.IsChecked( (int)Cmd.ID_TOGGLEGRIDLINES );                       
00429                 }
00430 
00431                 public void AutoSizeCols( object sender, Event e )
00432                 {
00433                         grid.AutoSizeColumns();
00434                         grid.Refresh();                 
00435                 }
00436 
00437                 public void CellOverflow( object sender, Event e )
00438                 {
00439                         CommandEvent ce = (CommandEvent) e;
00440                         grid.DefaultCellOverflow = ce.IsChecked;
00441                         grid.Refresh();
00442                 }
00443 
00444                 public void ResizeCell( object sender, Event e )
00445                 {
00446                         CommandEvent ce = (CommandEvent) e;
00447                         
00448                         if ( ce.IsChecked )
00449                                 grid.SetCellSize( 7, 1, 5, 5 );
00450                         else
00451                                 grid.SetCellSize( 7, 1, 1, 5 );
00452                                 
00453                         grid.Refresh();
00454                 }
00455 
00456                 public void SetLabelColour( object sender, Event e )
00457                 {
00458                         ColourDialog dlg = new ColourDialog( null );
00459             if (dlg.ShowModal() == wx.ShowModalResult.OK)
00460                         {
00461                                 ColourData retData = dlg.ColourData;
00462                                 Colour colour = retData.Colour;
00463 
00464                                 grid.LabelBackgroundColour = colour;
00465                         }                       
00466                 }
00467                 
00468                 public void SetLabelTextColour( object sender, Event e )
00469                 {
00470                         ColourDialog dlg = new ColourDialog( null );
00471             if (dlg.ShowModal() == wx.ShowModalResult.OK)
00472                         {
00473                                 ColourData retData = dlg.ColourData;
00474                                 Colour colour = retData.Colour;
00475 
00476                                 grid.LabelTextColour = colour;
00477                         }                               
00478                 }               
00479                 
00480                 public void SetLabelFont( object sender, Event e )
00481                 {
00482                         FontData data = new FontData();
00483                         data.InitialFont = grid.LabelFont;
00484                         
00485                         FontDialog fd = new FontDialog(this, data);
00486 
00487             if (fd.ShowModal() == wx.ShowModalResult.OK)
00488                         {                       
00489                                 Font font = fd.FontData.ChosenFont;
00490                                 if ( font.Ok )
00491                                 {
00492                                         grid.LabelFont = font;
00493                                 }
00494                         }
00495                         
00496                 }               
00497 
00498                 public void SetRowLabelHorizAlignment( object sender, Event e )
00499                 {
00500                         Alignment[] currentAlignment=grid.GetRowLabelAlignment();
00501 
00502                         switch ( currentAlignment[0] )
00503                         {
00504                                 case Alignment.wxALIGN_LEFT:
00505                     currentAlignment[0] = Alignment.wxALIGN_CENTRE;
00506                                 break;
00507 
00508                                 case Alignment.wxALIGN_CENTRE:
00509                     currentAlignment[0] = Alignment.wxALIGN_RIGHT;
00510                                 break;
00511 
00512                                 case Alignment.wxALIGN_RIGHT:
00513                     currentAlignment[0] = Alignment.wxALIGN_LEFT;
00514                                 break;
00515                         }
00516 
00517             grid.SetRowLabelAlignment(currentAlignment[0], currentAlignment[1]);                        
00518                 }               
00519 
00520                 public void SetRowLabelVertAlignment( object sender, Event e )
00521                 {
00522                         Alignment[] align=grid.GetRowLabelAlignment();
00523                         switch ( align[1] )
00524                         {
00525                                 case Alignment.wxALIGN_TOP:
00526                     align[1] = Alignment.wxALIGN_CENTRE;
00527                                 break;
00528 
00529                                 case Alignment.wxALIGN_CENTRE:
00530                     align[1] = Alignment.wxALIGN_BOTTOM;
00531                                 break;
00532 
00533                                 case Alignment.wxALIGN_BOTTOM:
00534                     align[1] = Alignment.wxALIGN_TOP;
00535                                 break;
00536                         }
00537 
00538             grid.SetRowLabelAlignment(align[0], align[1]);                      
00539                 }               
00540 
00541                 public void SetColLabelHorizAlignment( object sender, Event e )
00542                 {
00543             Alignment[] align = grid.GetColLabelAlignment();
00544 
00545                         switch ( align[0] )
00546                         {
00547                                 case Alignment.wxALIGN_LEFT:
00548                     align[0] = Alignment.wxALIGN_CENTRE;
00549                                 break;
00550 
00551                                 case Alignment.wxALIGN_CENTRE:
00552                     align[0] = Alignment.wxALIGN_RIGHT;
00553                                 break;
00554 
00555                                 case Alignment.wxALIGN_RIGHT:
00556                     align[0] = Alignment.wxALIGN_LEFT;
00557                                 break;
00558                         }
00559 
00560             grid.SetColLabelAlignment(align[0], align[1]);                      
00561                 }               
00562 
00563                 public void SetColLabelVertAlignment( object sender, Event e )
00564                 {
00565             Alignment[] align = grid.GetColLabelAlignment();
00566 
00567                         switch ( align[1] )
00568                         {
00569                                 case Alignment.wxALIGN_TOP:
00570                     align[1] = Alignment.wxALIGN_CENTRE;
00571                                 break;
00572 
00573                                 case Alignment.wxALIGN_CENTRE:
00574                     align[1] = Alignment.wxALIGN_BOTTOM;
00575                                 break;
00576 
00577                                 case Alignment.wxALIGN_BOTTOM:
00578                     align[1] = Alignment.wxALIGN_TOP;
00579                                 break;
00580                         }
00581 
00582             grid.SetColLabelAlignment(align[0], align[1]);                      
00583                 }               
00584 
00585                 public void SetGridLineColour( object sender, Event e )
00586                 {
00587                         ColourDialog dlg = new ColourDialog( null );
00588             if (dlg.ShowModal() == wx.ShowModalResult.OK)
00589                         {
00590                                 ColourData retData = dlg.ColourData;
00591                                 Colour colour = retData.Colour;
00592 
00593                                 grid.GridLineColour = colour;
00594                         }                                                       
00595                 }               
00596                 
00597                 public void InsertRow( object sender, Event e )
00598                 {
00599                         grid.InsertRows( grid.GridCursorRow, 1 );                       
00600                 }               
00601 
00602                 public void InsertCol( object sender, Event e )
00603                 {
00604                         grid.InsertCols( grid.GridCursorCol, 1 );
00605                 }               
00606 
00607                 public void DeleteSelectedRows( object sender, Event e )
00608                 {
00609                         if ( grid.IsSelection )
00610                         {
00611                                 grid.BeginBatch();
00612                                 for ( int n = 0; n < grid.NumberRows; )
00613                                         if ( grid.IsInSelection( n , 0 ) )
00614                                                 grid.DeleteRows( n, 1 );
00615                                         else
00616                                                 n++;
00617                                 grid.EndBatch();
00618                         }                       
00619                 }               
00620 
00621                 public void DeleteSelectedCols( object sender, Event e )
00622                 {
00623                         if ( grid.IsSelection )
00624                         {
00625                                 grid.BeginBatch();
00626                                 for ( int n = 0; n < grid.NumberCols; )
00627                                         if ( grid.IsInSelection( 0 , n ) )
00628                                                 grid.DeleteCols( n, 1 );
00629                                         else
00630                                                 n++;
00631                                 grid.EndBatch();
00632                         }                       
00633                 }               
00634                 
00635                 public void ClearGrid( object sender, Event e )
00636                 {
00637                         grid.ClearGrid();
00638                 }               
00639                 
00640                 public void SelectCells( object sender, Event e )
00641                 {
00642             Log.LogMessage("Now {0} managed and {1} unmanaged instances.", Object.SavedInstancesCount, Object.InstancesCount);
00643                         grid.SelectionMode = GridSelectionMode.wxGridSelectCells;
00644                 }               
00645                 
00646                 public void SelectRows( object sender, Event e )
00647                 {
00648                         grid.SelectionMode = GridSelectionMode.wxGridSelectRows;
00649                 }               
00650 
00651                 public void SelectCols( object sender, Event e )
00652                 {
00653                         grid.SelectionMode = GridSelectionMode.wxGridSelectColumns;
00654                 }               
00655 
00656                 public void SetCellFgColour( object sender, Event e )
00657                 {
00658                         ColourDialog dlg = new ColourDialog( null );
00659             if (dlg.ShowModal() == wx.ShowModalResult.OK)
00660                         {
00661                                 ColourData retData = dlg.ColourData;
00662                                 Colour colour = retData.Colour;
00663 
00664                                 if ( colour.Ok() )
00665                                 {
00666                                         grid.DefaultCellTextColour = colour;
00667                                         grid.Refresh();
00668                                 }
00669                         }                                                                               
00670                 }               
00671 
00672                 public void SetCellBgColour( object sender, Event e )
00673                 {
00674                         ColourDialog dlg = new ColourDialog( null );
00675             if (dlg.ShowModal() == wx.ShowModalResult.OK)
00676                         {
00677                                 ColourData retData = dlg.ColourData;
00678                                 Colour colour = retData.Colour;
00679 
00680                                 if ( colour.Ok() )
00681                                 {
00682                                         grid.DefaultCellBackgroundColour = colour;
00683                                         Rectangle r = new Rectangle( 0, 0, grid.Size.Width, grid.Size.Height );
00684                                         grid.Refresh( true, r);
00685                                 }
00686                         }                                                                                                       
00687                 }               
00688         
00689                 public void About( object sender, Event e )
00690                 {
00691                         wx.MessageDialog.ShowModal( "\n\nwxGrid demo \n\n" +
00692                                 "Ported to wx.NET \nby\n" +
00693                                 "Alexander Olk\n" +
00694                                 "Extended for testing wrapped grid editors \nby\n" +
00695                                 "Harald Meyer auf'm Hofe\n",
00696                                 "About",
00697                                 wx.WindowStyles.DIALOG_OK );                    
00698                 }               
00699 
00700                 public void OnQuit( object sender, Event e )
00701                 {
00702                         Close( true );
00703                 }               
00704 
00705                 public void OnVTable( object sender, Event e )
00706                 {
00707                         s_sizeGrid = GetNumberFromUser( "Size of the table to create",
00708                                      "Size: ",
00709                                      "wxGridDemo question",
00710                                      (int)s_sizeGrid,
00711                                      0, 32000, this);
00712                                 
00713                         if ( s_sizeGrid != -1 )
00714                         {
00715                                 BigGridFrame win = new BigGridFrame( s_sizeGrid );
00716                                 win.Show( true );
00717                         }
00718                 }               
00719                 
00720                 public void OnBugsTable( object sender, Event e )
00721                 {
00722                         BugsGridFrame frame = new BugsGridFrame();
00723                         frame.Show( true );
00724                 }               
00725                 
00726                 public void OnSmallGrid( object sender, Event e )
00727                 {
00728                         Frame frame = new Frame( null, -1, "A Small Grid", wxDefaultPosition, new Size( 640, 480 ) );
00729                         Panel panel = new Panel( frame, -1 );
00730             Grid agrid = new Grid(panel, -1, new Point(10, 10), new Size(400, 400), wx.WindowStyles.WANTS_CHARS | wx.WindowStyles.BORDER_SIMPLE);
00731                         agrid.CreateGrid( 3, 3 );
00732                         frame.Show( true );
00733                 }               
00734                 
00735                 public void DeselectCell( object sender, Event e )
00736                 {
00737                         grid.DeselectCell( 3, 1 );
00738                 }               
00739 
00740                 public void DeselectCol( object sender, Event e )
00741                 {
00742                         grid.DeselectCol( 2 );
00743                 }               
00744                 
00745                 public void DeselectRow( object sender, Event e )
00746                 {
00747                         grid.DeselectRow( 2 );
00748                 }               
00749         
00750                 public void DeselectAll( object sender, Event e )
00751                 {
00752                         grid.ClearSelection();
00753                 }               
00754 
00755                 public void SelectCell( object sender, Event e )
00756                 {
00757                         grid.SelectBlock( 3, 1, 3, 1, m_addToSel );
00758                 }               
00759                 
00760                 public void SelectCol( object sender, Event e )
00761                 {
00762                         grid.SelectCol( 2, m_addToSel );
00763                 }               
00764                 
00765                 public void SelectRow( object sender, Event e )
00766                 {
00767                         grid.SelectRow( 2, m_addToSel );
00768                 }               
00769 
00770                 public void SelectAll( object sender, Event e )
00771                 {
00772                         grid.SelectAll();
00773                 }               
00774                 
00775                 public void OnAddToSelectToggle( object sender, Event e )
00776                 {
00777                         CommandEvent ce = (CommandEvent) e;
00778                         m_addToSel = ce.IsChecked;
00779                 }               
00780 
00781                 public void OnSetHighlightWidth( object sender, Event e )
00782                 {
00783                         string[] choices = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
00784 
00785                         SingleChoiceDialog dlg = new SingleChoiceDialog(this, "Choose the thickness of the highlight pen:",
00786                                 "Pen Width", choices);
00787 
00788                         int current = grid.CellHighlightPenWidth;
00789                         dlg.SetSelection( current );
00790             if (dlg.ShowModal() == wx.ShowModalResult.OK) 
00791                         {
00792                                 grid.CellHighlightPenWidth = dlg.GetSelection();
00793                         }                       
00794                 }               
00795 
00796                 public void OnSetROHighlightWidth( object sender, Event e )
00797                 {
00798                         string[] choices = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
00799 
00800                         SingleChoiceDialog dlg = new SingleChoiceDialog(this, "Choose the thickness of the highlight pen:",
00801                                 "Pen Width", choices);
00802 
00803                         int current = grid.CellHighlightROPenWidth;
00804                         dlg.SetSelection( current );
00805             if (dlg.ShowModal() == wx.ShowModalResult.OK) 
00806                         {
00807                                 grid.CellHighlightROPenWidth = dlg.GetSelection();
00808                         }                       
00809                 }               
00810                 
00811                 public void OnLabelLeftClick( object sender, Event e )
00812                 {
00813                         GridEvent ge = (GridEvent) e;
00814                         
00815                         if ( ge.Row != -1 )
00816                         {
00817                                 logBuf = "Left click on row label " + ge.Row;
00818                         }
00819                         else if ( ge.Col != -1 )
00820                         {
00821                                 logBuf += "Left click on col label " + ge.Col;
00822                         }
00823                         else
00824                         {
00825                                 logBuf += "Left click on corner label";
00826                         }
00827 
00828                         if ( ge.ShiftDown ) logBuf += " (shift down)";
00829                         if ( ge.ControlDown ) logBuf += " (control down)";
00830                         Log.LogMessage( logBuf );
00831 
00832                         // you must call event skip if you want default grid processing
00833                         
00834                         ge.Skip();                      
00835                 }               
00836                 
00837                 public void OnCellLeftClick( object sender, Event e )
00838                 {
00839                         GridEvent ge = (GridEvent) e;
00840                         logBuf = "Left click at row " + ge.Row + " col " + ge.Col;
00841                         Log.LogMessage( logBuf );
00842 
00843                         // you must call event skip if you want default grid processing
00844                         // (cell highlighting etc.)
00845                         
00846                         ge.Skip();                      
00847                 }               
00848 
00849                 public void OnRowSize( object sender, Event e )
00850                 {
00851                         GridSizeEvent ge = (GridSizeEvent) e;
00852                         logBuf = "Resized row " + ge.RowOrCol;
00853                         Log.LogMessage( logBuf );
00854 
00855                         ge.Skip();                      
00856                 }               
00857                 
00858                 public void OnColSize( object sender, Event e )
00859                 {
00860                         GridSizeEvent ge = (GridSizeEvent) e;
00861                         logBuf = "Resized col " + ge.RowOrCol;
00862                         Log.LogMessage( logBuf );
00863 
00864                         ge.Skip();                      
00865                 }               
00866 
00867                 public void OnSelectCell( object sender, Event e )
00868                 {
00869                         GridEvent ge = (GridEvent) e;
00870                         if ( ge.Selecting )
00871                                 logBuf = "Selected ";
00872                         else
00873                                 logBuf = "Deselected ";
00874                                 
00875                         logBuf += "cell at row " + ge.Row +
00876                                 " col " + ge.Col +
00877                                 " ( ControlDown: " + ( ge.ControlDown ? 'T':'F' ) +
00878                                 ", ShiftDown: " + (ge.ShiftDown ? 'T':'F') +
00879                                 ", AltDown: " + (ge.AltDown ? 'T':'F') +
00880                                 ", MetaDown: " + (ge.MetaDown ? 'T':'F') + " )";
00881                                 
00882                         Log.LogMessage( logBuf );
00883             Log.LogMessage("Now {0} managed and {1} unmanaged instances.", Object.SavedInstancesCount, Object.InstancesCount);
00884 
00885                         // you must call Skip() if you want the default processing
00886                         // to occur in wxGrid
00887                         ge.Skip();                      
00888                 }               
00889                 
00890                 public void OnRangeSelected( object sender, Event e )
00891                 {
00892                         GridRangeSelectEvent ge = (GridRangeSelectEvent) e;
00893                         
00894                         if ( ge.Selecting )
00895                                 logBuf = "Selected ";
00896                         else
00897                                 logBuf = "Deselected ";
00898                                 
00899                         logBuf += "cells from row " + ge.TopRow +
00900                                 " col " + ge.LeftCol +
00901                                 " to row " + ge.BottomRow +
00902                                 " col " + ge.RightCol +
00903                                 " ( ControlDown: " + (ge.ControlDown ? 'T':'F') +
00904                                 ", ShiftDown: " + (ge.ShiftDown ? 'T':'F') +
00905                                 ", AltDown: " + (ge.AltDown ? 'T':'F') +
00906                                 ", MetaDown: " + (ge.MetaDown ? 'T':'F') + " )";
00907                                 
00908                         Log.LogMessage( logBuf );
00909 
00910                         ge.Skip();                      
00911                 }               
00912 
00913                 public void OnCellValueChanged( object sender, Event e )
00914                 {
00915                         GridEvent ge = (GridEvent) e;
00916                         
00917                         logBuf = "Value changed for cell at" +
00918                                 " row " + ge.Row +
00919                                 " col " + ge.Col;
00920 
00921                                 Log.LogMessage( logBuf );
00922 
00923                         ge.Skip();                      
00924                 }               
00925 
00926                 public void OnEditorShown( object sender, Event e )
00927                 {
00928                         GridEvent ge = (GridEvent) e;
00929                         if ( ( ge.Col == 4 ) &&
00930                                 ( ge.Row == 0 ) &&
00931                                 ( wx.MessageDialog.ShowModal( "Are you sure you wish to edit this cell",
00932                         "Checking", wx.WindowStyles.DIALOG_YES_NO) == wx.ShowModalResult.NO) )
00933                         {
00934 
00935                                 ge.Veto();
00936                                 return;
00937                         }
00938                         
00939                         Log.LogMessage( "Cell editor shown." );
00940 
00941                         ge.Skip();
00942                 }               
00943 
00944                 public void OnEditorHidden( object sender, Event e )
00945                 {
00946                         GridEvent ge = (GridEvent) e;
00947                         if ( ( ge.Col == 4 ) &&
00948                                 ( ge.Row == 0 ) &&
00949                                 ( wx.MessageDialog.ShowModal( "Are you sure you wish to finish editing this cell",
00950                         "Checking", wx.WindowStyles.DIALOG_YES_NO) == wx.ShowModalResult.NO) )
00951                         {
00952 
00953                                 ge.Veto();
00954                                 return;
00955                         }
00956 
00957                         Log.LogMessage( "Cell editor hidden." );
00958 
00959                         ge.Skip();                      
00960                 }                               
00961         }
00962         
00963         //---------------------------------------------------------------------
00964         
00965         public class BugsGridFrame : Frame
00966         {
00967                 public enum Columns
00968                 {
00969                         Col_Id,
00970                         Col_Summary,
00971                         Col_Severity, 
00972                         Col_Priority,
00973                         Col_Platform,
00974                         Col_Opened,
00975                         Col_Max
00976                 }
00977                 
00978                 public string[] severities =
00979                 {
00980                         "wishlist",
00981                         "minor",
00982                         "normal",
00983                         "major",
00984                         "critical"
00985                 };
00986                 
00987                 public BugsGridFrame()
00988                         : base( null, -1, "Bugs table", wxDefaultPosition, new Size( 500, 300 ) )
00989                 {
00990                         Grid grid = new Grid( this, -1, wxDefaultPosition );
00991                         GridTableBase table = new BugsGridTable();
00992                         table.SetAttrProvider( new MyGridCellAttrProvider() );
00993                         grid.SetTable(table, true);
00994 
00995                         GridCellAttr attrRO    = new GridCellAttr();
00996                         GridCellAttr attrRange = new GridCellAttr();
00997                         GridCellAttr attrCombo = new GridCellAttr();
00998 
00999                         attrRO.ReadOnly  = true;
01000             attrRange.SetEditor(new wx.GridCtrl.Editors.GridCellNumberEditor(1, 5));
01001             attrCombo.SetEditor(new wx.GridCtrl.Editors.GridCellChoiceEditor(severities));
01002 
01003                         grid.SetColAttr((int)Columns.Col_Id,       attrRO);
01004                         grid.SetColAttr((int)Columns.Col_Priority, attrRange);
01005                         grid.SetColAttr((int)Columns.Col_Severity, attrCombo);
01006 
01007                         grid.SetMargins(0, 0);
01008 
01009                         grid.Fit();
01010                         ClientSize = grid.Size;                 
01011                 }
01012         }
01013         
01014         //---------------------------------------------------------------------
01015         
01016         public class BugsGridTable : GridTableBase
01017         {
01018                 public enum Columns
01019                 {
01020                         Col_Id,
01021                         Col_Summary,
01022                         Col_Severity,
01023                         Col_Priority,
01024                         Col_Platform,
01025                         Col_Opened,
01026                         Col_Max
01027                 }       
01028                 
01029                 public enum Severity
01030                 {
01031                         Sev_Wish,
01032                         Sev_Minor,
01033                         Sev_Normal,
01034                         Sev_Major,
01035                         Sev_Critical,
01036                         Sev_Max
01037                 }
01038                 
01039                 public struct BugsGridData
01040                 {
01041                         public int id;
01042                         public string summary;
01043                         public Severity severity;
01044                         public int prio;
01045                         public string platform;
01046                         public bool opened;
01047                         
01048             public BugsGridData(int id, string summary, Severity severity, 
01049                                 int prio, string platform, bool opened)
01050                         {
01051                                 this.id = id;
01052                                 this.summary = summary;
01053                                 this.severity = severity;
01054                                 this.prio = prio;
01055                                 this.platform = platform;
01056                                 this.opened = opened;
01057                         }
01058                 }
01059                 
01060                 public static string[] severities =
01061                 {
01062                         "wishlist",
01063                         "minor",
01064                         "normal",
01065                         "major",
01066                         "critical"
01067                 };
01068                 
01069                 public static string[] headers =
01070                 {
01071                         "Id",
01072                         "Summary",
01073                         "Severity",
01074                         "Priority",
01075                         "Platform",
01076                         "Opened?"
01077                 };
01078                 
01079                 public static BugsGridData[] gs_dataBugsGrid = new BugsGridData[4];
01080                 
01081                 public BugsGridTable() 
01082                         : base()
01083                 {
01084                         gs_dataBugsGrid[0] = new BugsGridData( 18, "foo doesn't work", Severity.Sev_Major, 1, "wxMSW", true );
01085                         gs_dataBugsGrid[1] = new BugsGridData( 27, "bar crashes", Severity.Sev_Critical, 1, "all", false );
01086                         gs_dataBugsGrid[2] = new BugsGridData( 45, "printing is slow", Severity.Sev_Minor, 3, "wxMSW", true );
01087                         gs_dataBugsGrid[3] = new BugsGridData( 68, "Rectangle() fails", Severity.Sev_Normal, 1, "wxMSW", false );
01088                 }
01089                 
01090                 public override int GetNumberRows()
01091                 {
01092                         return gs_dataBugsGrid.Length;
01093                 }
01094                 
01095                 public override int GetNumberCols()
01096                 {
01097                         return (int)Columns.Col_Max;
01098                 }
01099                 
01100                 public override bool IsEmptyCell( int row, int col )
01101                 {
01102                         return false;
01103                 }
01104                 
01105                 public override string GetValue( int row, int col )
01106                 {
01107                         BugsGridData gd = gs_dataBugsGrid[row];
01108                         
01109                         switch( col )
01110                         {
01111                                 case (int)Columns.Col_Id:
01112                                 case (int)Columns.Col_Priority:
01113                                 case (int)Columns.Col_Opened:
01114                                         Log.LogError( "unexpected column" );
01115                                 break;
01116 
01117                                 case (int)Columns.Col_Severity:
01118                                         return severities[(int)gd.severity];
01119 
01120                                 case (int)Columns.Col_Summary:
01121                                         return gd.summary;
01122 
01123                                 case (int)Columns.Col_Platform:
01124                                         return gd.platform;
01125                         }
01126 
01127                         return "";                              
01128                 }
01129                 
01130                 public override void SetValue( int row, int col, string value)
01131                 {
01132                         BugsGridData gd = gs_dataBugsGrid[row];
01133 
01134                         switch ( col )
01135                         {
01136                                 case (int)Columns.Col_Id:
01137                                 case (int)Columns.Col_Priority:
01138                                 case (int)Columns.Col_Opened:
01139                                         Log.LogError( "unexpected column" );
01140                                 break;
01141 
01142                                 case (int)Columns.Col_Severity:
01143                                 {
01144                                         int n;
01145                                         for ( n = 0; n < severities.Length; n++ )
01146                                         {
01147                                                 if ( severities[n] == value )
01148                                                 {
01149                                                         gd.severity = (Severity)n;
01150                                                         break;
01151                                                 } 
01152                                         }
01153 
01154                                         if ( n == severities.Length )
01155                                         {
01156                                                 Log.LogWarning( "Invalid severity value '{0}'.", value );
01157                                                 gd.severity = Severity.Sev_Normal;
01158                                         }
01159                                 }
01160                                 break;
01161 
01162                                 case (int)Columns.Col_Summary:
01163                                         gd.summary = value;
01164                                 break;
01165 
01166                                 case (int)Columns.Col_Platform:
01167                                         gd.platform = value;
01168                                 break;
01169                         }                       
01170                 }
01171                 
01172                 public override string GetColLabelValue( int col )
01173                 {
01174                         return headers[col];
01175                 }
01176                 
01177                 public override string GetTypeName( int row, int col )
01178                 {
01179                         switch ( col )
01180                         {
01181                                 case (int)Columns.Col_Id:
01182                                         return "long";
01183                                 
01184                                 case (int)Columns.Col_Priority:
01185                                         return "long";
01186 
01187                                 case (int)Columns.Col_Severity:
01188                                         return "string:80";
01189 
01190                                 case (int)Columns.Col_Summary:
01191                                         return "string:80";
01192 
01193                                 case (int)Columns.Col_Platform:
01194                                         return "choice:all,MSW,GTK,other";
01195 
01196                                 case (int)Columns.Col_Opened:
01197                                         return "bool";
01198                         }
01199 
01200                         return "";                      
01201                 }
01202                 
01203                 public override bool CanGetValueAs( int row, int col, string typeName )
01204                 {
01205                         if ( typeName == "string" )
01206                         {
01207                                 return true;
01208                         }
01209                         else if ( typeName == "bool" )
01210                         {
01211                                 return col == (int)Columns.Col_Opened;
01212                         }
01213                         else if ( typeName == "long" )
01214                         {
01215                                 return col == (int)Columns.Col_Id || col == (int)Columns.Col_Priority || col == (int)Columns.Col_Severity;
01216                         }
01217                         else
01218                         {
01219                                 return false;
01220                         }                       
01221                 }
01222                 
01223                 public override bool CanSetValueAs( int row, int col, string typeName )
01224                 {
01225                         return CanGetValueAs(row, col, typeName);
01226                 }
01227 
01228                 public override long GetValueAsLong( int row, int col )
01229                 {
01230                         BugsGridData gd = gs_dataBugsGrid[row];
01231                         
01232                         switch ( col )
01233                         {
01234                                 case (int)Columns.Col_Id:
01235                                         return gd.id;
01236 
01237                                 case (int)Columns.Col_Priority:
01238                                         return gd.prio;
01239 
01240                                 case (int)Columns.Col_Severity:
01241                                         return (int)gd.severity;
01242 
01243                                 default:
01244                                         Log.LogError( "unexpected column" );
01245                                         return -1;
01246                         }                       
01247                 }
01248                 
01249                 public override bool GetValueAsBool( int row, int col )
01250                 {
01251                         if ( col == (int)Columns.Col_Opened )
01252                         {
01253                                 return gs_dataBugsGrid[row].opened;
01254                         }
01255                         else 
01256                         {
01257                                 Log.LogError( "unexpected column" );
01258 
01259                                 return false;
01260                         }                       
01261                 } 
01262 
01263                 public override void SetValueAsLong( int row, int col, long value )
01264                 {
01265                         BugsGridData gd = gs_dataBugsGrid[row];
01266                         
01267 
01268                         switch ( col )
01269                         {
01270                                 case (int)Columns.Col_Priority:
01271                                         gd.prio = (int)value;
01272                                 break;
01273 
01274                                 default:
01275                                         Log.LogError( "unexpected column" );
01276                                 break;
01277                         }                       
01278                 }
01279                 
01280                 public override void SetValueAsBool( int row, int col, bool value )
01281                 {
01282                         if ( col == (int)Columns.Col_Opened )
01283                         {
01284                                 gs_dataBugsGrid[row].opened = value;
01285                         }
01286                         else
01287                         {
01288                                 Log.LogError( "unexpected column" );
01289                         }                       
01290                 }
01291         } 
01292                 
01293         public class MyGridCellAttrProvider : GridCellAttrProvider
01294         {
01295                 private GridCellAttr m_attrForOddRows;
01296                 
01297                 public MyGridCellAttrProvider()
01298                 {
01299                         m_attrForOddRows = new GridCellAttr();
01300                         m_attrForOddRows.BackgroundColour = Colour.wxLIGHT_GREY;
01301                 }
01302                 
01303                 public override GridCellAttr GetAttr( int row, int col, GridCellAttr.AttrKind kind)
01304                 {
01305                         GridCellAttr attr = base.GetAttr(row, col, kind);
01306                         if ( ( row % 2 ) > 0 )
01307                         {
01308                                 if ( attr == null )
01309                                 {
01310                                         attr = m_attrForOddRows;
01311                                         attr.IncRef();
01312                                 }
01313                                 else
01314                                 {
01315                                         if ( !attr.HasBackgroundColour )
01316                                         {
01317                                                 GridCellAttr attrNew = (GridCellAttr)attr.Clone();
01318                                                 attr.DecRef();
01319                                                 attr = attrNew;
01320                                                 attr.BackgroundColour = Colour.wxLIGHT_GREY;
01321                                         }
01322                                 }
01323                         }
01324                         return attr;                    
01325                 }
01326         }
01327 
01330     public class MyGridCellRenderer : wx.GridCtrl.Renderers.GridCellStringRenderer
01331         {
01332                 public MyGridCellRenderer()
01333                         : base()
01334         {
01335         }
01336                         
01337                 public override void Draw(Grid grid, GridCellAttr attr, DC dc, Rectangle rect, int row, int col, bool isSelected)
01338                 {
01339                         base.Draw(grid, attr, dc, rect, row, col, isSelected);
01340                         
01341                         dc.Pen = GDIPens.wxGREEN_PEN;
01342                         dc.Brush = GDIBrushes.wxTRANSPARENT_BRUSH;
01343                         dc.DrawEllipse( rect.X, rect.Y, rect.Width, rect.Height );
01344                 }
01345 
01346         public override wx.GridCtrl.Renderers.GridCellRenderer Clone()
01347         {
01348             return new MyGridCellRenderer();
01349         }
01350         }
01351         
01352         //---------------------------------------------------------------------
01353 
01361         public class NumericInput : TextCtrl
01362         {
01363         int m_max=int.MaxValue;
01364                 public NumericInput(Window parent, int id, int defaultValue)
01365                         : base(parent, id, defaultValue.ToString(), wxDefaultPosition, wxDefaultSize)
01366                 {
01367                         Char += new EventListener( OnChar );
01368                 }
01369 
01370                 public NumericInput(Window parent, int id, int defaultValue, System.Drawing.Point pos, System.Drawing.Size size)
01371                         : base(parent, id, defaultValue.ToString(), pos, size)
01372                 {
01373                         Char += new EventListener( OnChar );
01374                 }
01375 
01379         public int MaxValue { get { return m_max; } set { m_max=value; } }
01380 
01381                 public void OnChar( object sender, Event e )
01382                 {
01383             if (e is KeyEvent)
01384             {
01385                 KeyEvent kev=(KeyEvent) e;
01386                 if (kev.KeyCode >= '0' && kev.KeyCode <= '9')
01387                 {
01388                   if ((int)InsertionPoint >= Value.Length)
01389                   {
01390                      Value=Value+new string((char) kev.KeyCode, 1);
01391                   }
01392                   else
01393                   {
01394                     Value=Value.Substring(0, (int)InsertionPoint)
01395                         +new string((char) kev.KeyCode, 1)
01396                         +Value.Substring((int)InsertionPoint);
01397                   }
01398                 }
01399                 else if (kev.KeyCode==(int)KeyCode.WXK_DELETE
01400                     || kev.KeyCode==(int)KeyCode.WXK_BACK
01401                     || kev.KeyCode >= 256)
01402                 {
01403                     kev.Skip();
01404                 }
01405                          
01406             }
01407                 }
01408 
01414         public new string Value
01415         {
01416             get
01417             {
01418                 int selFrom, selTo;
01419                 this.GetSelection(out selFrom, out selTo);
01420                 if (selFrom==selTo)
01421                     return base.Value;
01422                 else
01423                     return base.Value.Substring(0, (int)selFrom);
01424             }
01425             set 
01426             {
01427                 string generatedValue="";
01428                 bool isValueOK=true;
01429                 int posLeqMax=-1;
01430                 int numValue=0;
01431                 for(int pos=0; isValueOK && pos < value.Length; ++pos)
01432                 {
01433                     if (value[pos] >= '0' && value[pos] <= '9')
01434                     {
01435                         generatedValue+=new string(value[pos], 1);
01436                         if (posLeqMax < 0)
01437                         {
01438                             numValue=Convert.ToInt32(generatedValue);
01439                             if (numValue > m_max) posLeqMax=pos;
01440                         }
01441                     }
01442                     else
01443                         isValueOK=false;
01444                 }
01445                 if (isValueOK)
01446                 {
01447                     if (generatedValue.Length == 0)
01448                     {
01449                         base.Value="0";
01450                         this.SetInsertionPointEnd();
01451                     }
01452                     else
01453                     {
01454                         int oldInsertionPoint=InsertionPoint;
01455                         base.Value=generatedValue;
01456                         if (posLeqMax >= 0)
01457                         {
01458                             this.SetSelection(posLeqMax, -1);
01459                         }
01460                         else
01461                             InsertionPoint=oldInsertionPoint+1;
01462                     }
01463                 }
01464             }
01465         }
01466         }
01467 
01471     public class NumericGridEditor : wx.GridCtrl.Editors.GridCellEditor
01472         {
01473                 private int m_min;
01474             private int m_max;
01475 
01476         protected NumericInput GetInputCtrl()
01477         {
01478             return (NumericInput) GetControl();
01479         }
01480 
01481                 public NumericGridEditor(int min, int max)
01482             : base()
01483                 {
01484                         m_min=min;
01485                         m_max=max;
01486                 }
01487 
01488         public override wx.GridCtrl.Editors.GridCellEditor Clone()
01489                 {
01490                         return new NumericGridEditor(m_min, m_max);
01491                 }
01492 
01493                 public override void Create(Window parent, int id, EvtHandler evtHandler)
01494                 {
01495                         NumericInput ctrl=new NumericInput(parent, id, m_min);
01496             ctrl.MaxValue=m_max;
01497             SetControl(ctrl);
01498             if (evtHandler!=null)
01499                 ctrl.PushEventHandler(evtHandler);
01500                 }
01501 
01502         public override void StartingKey(KeyEvent e)
01503         {
01504             if (e.KeyCode >= '0' && e.KeyCode <= '9')
01505             {
01506                 NumericInput ctrl=GetInputCtrl();
01507                 if (ctrl!=null)
01508                 {
01509                     ctrl.Value=new string((char) e.KeyCode, 1);
01510                     ctrl.SetInsertionPointEnd();
01511                 }
01512             }
01513             else
01514                 e.Skip();
01515         }
01516 
01517                 public override void BeginEdit(int row, int col, Grid grid)
01518                 {
01519             NumericInput ctrl=GetInputCtrl();
01520             ctrl.Value=grid.GetCellValue(row, col);
01521                         ctrl.SetFocus();
01522                         ctrl.SetSelection(-1, -1);
01523                 }
01524 
01525                 public override bool EndEdit(int row, int col, Grid grid)
01526                 {
01527             NumericInput ctrl=GetInputCtrl();
01528             bool result=ctrl.Value != grid.GetCellValue(row, col);
01529                         grid.SetCellValue(row, col, ctrl.Value);
01530                         return result;
01531                 }
01532 
01533                 public override void Reset()
01534                 {
01535                 }
01536         }
01537 
01538         //---------------------------------------------------------------------
01539 
01546         public class BigGridFrame : Frame
01547         {
01548                 private Grid m_grid;
01549                 private BigGridTable m_table;
01550                 
01551                 public BigGridFrame(long sizeGrid)
01552                         : base(null, -1, "Plugin Virtual Table", wxDefaultPosition, new Size( 500, 450 ) )
01553                 {
01554                         Icon = new wx.Icon( "../Samples/Grid/mondrian.png" );
01555 
01556                         m_grid = new Grid( this, -1, wxDefaultPosition, wxDefaultSize );
01557                         m_grid.SetDefaultRowSize(2*m_grid.GetRowHeight(0), true);
01558                         m_grid.SetDefaultCellAlignment((int)Alignment.wxALIGN_CENTRE, (int)Alignment.wxALIGN_CENTRE );
01559                         wx.Font labelFont=m_grid.LabelFont;
01560                         int x=0;
01561                         int y=0;
01562                         int descent;
01563                         int externalLeading;
01564                         m_grid.GetTextExtent("(22) Label888", out x, out y, out descent, out externalLeading, labelFont);
01565             y *= 2;
01566                         m_grid.ColLabelSize=y+5; // larger, since two lines
01567                         m_grid.SetDefaultColSize(x+5, false);
01568             m_grid.RegisterDataType("num8888", new wx.GridCtrl.Renderers.GridCellStringRenderer(), new NumericGridEditor(0, 8888));
01569             m_grid.RegisterDataType("text", new wx.GridCtrl.Renderers.GridCellAutoWrapStringRenderer(), new wx.GridCtrl.Editors.GridCellAutoWrapStringEditor());
01570                         m_grid.SetCellEditor(1, 1, new NumericGridEditor(10, 20));
01571                         m_table = new BigGridTable( sizeGrid );
01572                         m_grid.SetTable( m_table, true );
01573                 }
01574         }
01575         
01576         //---------------------------------------------------------------------
01577 
01579 
01582    public struct TableCoordinate
01583    {
01584       private int m_row;
01585       private int m_col;
01586       public TableCoordinate(int row, int col)
01587       {
01588          m_row=row;
01589          m_col=col;
01590       }
01591 
01593            public int Row 
01594            {
01595                    get { return m_row; }
01596                    set { m_row=value; }
01597            }
01599            public int Column
01600            {
01601                    get { return m_col; }
01602                    set { m_col=value; }
01603            }
01604 
01605       public override bool Equals(object arg)
01606       {
01607          if (arg is TableCoordinate)
01608          {
01609             TableCoordinate castedArg=(TableCoordinate) arg;
01610             return m_row==castedArg.m_row && m_col==castedArg.m_col;
01611          }
01612          else
01613             return false;
01614       }
01615 
01616            public override int GetHashCode()
01617            {
01618                    return m_row*m_row+m_col;
01619            }
01620 
01621            public override string ToString()
01622            {
01623                    return "(" + m_row + ", " + m_col + ")";
01624            }
01625    }
01626         
01632         public class BigGridTable : GridTableBase
01633         {
01634                 private long m_sizeGrid;
01636                 private IDictionary m_data;
01637 
01639                 static private GridCellAttr s_attrForOddRows=null;
01641                 static private GridCellAttr s_attrForEvenRows=null;
01642 
01644                 public GridCellAttr AttrForOddRows
01645                 {
01646                         get
01647                         {
01648                                 if (s_attrForOddRows==null)
01649                                 {
01650                                         s_attrForOddRows=new GridCellAttr();
01651                                         s_attrForOddRows.BackgroundColour = new Colour(240, 240, 240);
01652                                 }
01653                                 return s_attrForOddRows;
01654                         }
01655                 }
01656 
01658                 public GridCellAttr AttrForEvenRows
01659                 {
01660                         get
01661                         {
01662                                 if (s_attrForEvenRows==null)
01663                                 {
01664                                         s_attrForEvenRows=new GridCellAttr();
01665                                 }
01666                                 return s_attrForEvenRows;
01667                         }
01668                 }
01669 
01670         Font _fontForKnownValues = null;
01671         public Font FontForKnownValues
01672         {
01673             get
01674             {
01675                 if (_fontForKnownValues == null)
01676                 {
01677                     _fontForKnownValues = new Font(GetView().Font);
01678                     _fontForKnownValues.Style = FontStyle.wxSLANT;
01679                     _fontForKnownValues.Weight = FontWeight.wxBOLD;
01680                     _fontForKnownValues.Underlined = false;
01681                 }
01682                 return _fontForKnownValues;
01683             }
01684         }
01685 
01686                 public BigGridTable( long sizeGrid)
01687                         : base()
01688                 {
01689                         m_sizeGrid = sizeGrid;
01690                         m_data=new Hashtable();
01691                 }
01692                 
01696                 public int NoOfInputsCol(int col)
01697                 {
01698                         int result=0;
01699                         foreach(TableCoordinate key in m_data.Keys)
01700                         {
01701                                 if (key.Column==col) ++result;
01702                         }
01703                         return result;
01704                 }
01705 
01709         public int NoOfInputsRow(int row)
01710                 {
01711                         int result=0;
01712                         foreach(TableCoordinate key in m_data.Keys)
01713                         {
01714                                 if (key.Row==row) ++result;
01715                         }
01716                         return result;
01717                 }
01718 
01719                 public override int GetNumberRows()
01720                 {
01721                         return (int)m_sizeGrid;
01722                 }
01723                 
01724                 public override int GetNumberCols()
01725                 {
01726                         return (int)m_sizeGrid;
01727                 }
01728 
01729                 public override string GetColLabelValue(int col)
01730                 {
01731                         return col.ToString()+"\n("+NoOfInputsCol(col)+") "+GetTypeName(0, col);
01732                 }
01733 
01734                 public override string GetRowLabelValue(int row)
01735                 {
01736                         return row.ToString()+"\n("+NoOfInputsRow(row)+")";
01737                 }
01738 
01739                 public override string GetValue( int row, int col )
01740                 {
01741             Log.LogMessage("Now {0} managed and {1} unmanaged instances.", Object.SavedInstancesCount, Object.InstancesCount);
01742                         TableCoordinate coord=new TableCoordinate(row, col);
01743                         if (m_data.Contains(coord))
01744                                 return m_data[coord].ToString();
01745                         else
01746                                 if (row+col > 100)
01747                                         return "";
01748                                 else
01749                                         return coord.ToString();
01750                 }
01751                 
01752                 public override void SetValue( int row, int col, string val )
01753                 {
01754                         TableCoordinate coord=new TableCoordinate(row, col);
01755                         m_data[coord]=val;
01756                         GetView().ForceRefresh();
01757                 }
01758                 
01759                 public override string GetTypeName(int row, int col)
01760                 {
01761             if (col%2==0) return "num8888";
01762             else return "text";
01763                 }
01764 
01765                 public override bool IsEmptyCell( int row, int col )
01766                 {
01767                         if (row+col > 100)
01768                         {
01769                                 return !m_data.Contains(new TableCoordinate(row, col));
01770                         }
01771                         else
01772                                 return false;
01773                 }
01774 
01775                 public override GridCellAttr GetAttr(int row, int col, GridCellAttr.AttrKind kind)
01776                 {
01777                         GridCellAttr attr = base.GetAttr(row, col, kind);
01778                         if ( ( row % 2 ) > 0 )
01779                         {
01780                                 if ( attr == null )
01781                                 {
01782                                         attr = AttrForOddRows;
01783                                         attr.IncRef();
01784                                 }
01785                                 else if ( !attr.HasBackgroundColour )
01786                                 {
01787                                         GridCellAttr attrNew = (GridCellAttr)attr.Clone();
01788                                         attr.DecRef();
01789                                         attr = attrNew;
01790                     attr.BackgroundColour = AttrForOddRows.BackgroundColour;
01791                 }
01792                         }
01793                         if (m_data.Contains(new TableCoordinate(row, col)))
01794                         {
01795                                 if ( attr == null )
01796                                 {
01797                                         attr = AttrForEvenRows;
01798                                         attr.IncRef();
01799                                 }
01800                                 else if ( !attr.HasFont )
01801                                 {
01802                                         GridCellAttr attrNew = (GridCellAttr)attr.Clone();
01803                                         attr.DecRef();
01804                                         attr = attrNew;
01805                                 }
01806                 attr.Font = FontForKnownValues;
01807                         }
01808                         return attr;
01809                 }
01810                 
01811 
01812                 public override bool CanHaveAttributes()
01813                 {
01814                         return true;
01815                 }
01816         }
01817         
01818         //---------------------------------------------------------------------
01819 
01820         public class GridApp : wx.App
01821         {
01822                 public override bool OnInit()
01823                 {
01824                         GridFrame frame = new GridFrame();
01825                         frame.Show( true );
01826 
01827                         return true;
01828                 }
01829 
01830                 //---------------------------------------------------------------------
01831 
01832                 [STAThread]
01833                 static void Main()
01834                 {
01835                         GridApp app = new GridApp();
01836                         app.Run();
01837                 }
01838         }       
01839 }

Manual of the wx.NET   (c) 2003-2010 the wx.NET project