home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2233.zip / wxOS2-2_3_3.zip / wxWindows-2.3.3 / samples / newgrid / griddemo.cpp next >
C/C++ Source or Header  |  2002-09-08  |  39KB  |  1,356 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name:        griddemo.cpp
  3. // Purpose:     Grid control wxWindows sample
  4. // Author:      Michael Bedward
  5. // Modified by:
  6. // RCS-ID:      $Id: griddemo.cpp,v 1.58 2002/09/07 20:16:02 SN Exp $
  7. // Copyright:   (c) Michael Bedward, Julian Smart
  8. // Licence:     wxWindows license
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. // ============================================================================
  12. // declarations
  13. // ============================================================================
  14.  
  15. // ----------------------------------------------------------------------------
  16. // headers
  17. // ----------------------------------------------------------------------------
  18.  
  19. #ifdef __GNUG__
  20.     #pragma implementation
  21.     #pragma interface
  22. #endif
  23.  
  24. // For compilers that support precompilation, includes "wx/wx.h".
  25. #include "wx/wxprec.h"
  26.  
  27. #ifdef __BORLANDC__
  28. #pragma hdrstop
  29. #endif
  30.  
  31. #ifndef WX_PRECOMP
  32.     #include "wx/wx.h"
  33. #endif
  34.  
  35. #include "wx/colordlg.h"
  36. #include "wx/fontdlg.h"
  37.  
  38. #include "wx/grid.h"
  39. #include "wx/generic/gridctrl.h"
  40.  
  41. #include "griddemo.h"
  42.  
  43. // ----------------------------------------------------------------------------
  44. // wxWin macros
  45. // ----------------------------------------------------------------------------
  46.  
  47. IMPLEMENT_APP( GridApp )
  48.  
  49. // ============================================================================
  50. // implementation
  51. // ============================================================================
  52.  
  53. // ----------------------------------------------------------------------------
  54. // GridApp
  55. // ----------------------------------------------------------------------------
  56.  
  57. bool GridApp::OnInit()
  58. {
  59.     GridFrame *frame = new GridFrame;
  60.     frame->Show( TRUE );
  61.  
  62.     return TRUE;
  63. }
  64.  
  65. // ----------------------------------------------------------------------------
  66. // GridFrame
  67. // ----------------------------------------------------------------------------
  68.  
  69. BEGIN_EVENT_TABLE( GridFrame, wxFrame )
  70.     EVT_MENU( ID_TOGGLEROWLABELS,  GridFrame::ToggleRowLabels )
  71.     EVT_MENU( ID_TOGGLECOLLABELS,  GridFrame::ToggleColLabels )
  72.     EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
  73.     EVT_MENU( ID_TOGGLEROWSIZING, GridFrame::ToggleRowSizing )
  74.     EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing )
  75.     EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
  76.     EVT_MENU( ID_TOGGLEGRIDLINES, GridFrame::ToggleGridLines )
  77.     EVT_MENU( ID_AUTOSIZECOLS, GridFrame::AutoSizeCols )
  78.     EVT_MENU( ID_CELLOVERFLOW, GridFrame::CellOverflow )
  79.     EVT_MENU( ID_RESIZECELL, GridFrame::ResizeCell )
  80.     EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
  81.     EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
  82.     EVT_MENU( ID_SETLABEL_FONT, GridFrame::SetLabelFont )
  83.     EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
  84.     EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
  85.     EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
  86.     EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
  87.     EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
  88.     EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
  89.     EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
  90.     EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
  91.     EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
  92.     EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
  93.     EVT_MENU( ID_SELCELLS,  GridFrame::SelectCells )
  94.     EVT_MENU( ID_SELROWS,  GridFrame::SelectRows )
  95.     EVT_MENU( ID_SELCOLS,  GridFrame::SelectCols )
  96.  
  97.     EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
  98.     EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
  99.  
  100.     EVT_MENU( ID_ABOUT, GridFrame::About )
  101.     EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
  102.     EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
  103.     EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
  104.     EVT_MENU( ID_SMALL_GRID, GridFrame::OnSmallGrid)
  105.  
  106.     EVT_MENU( ID_DESELECT_CELL, GridFrame::DeselectCell)
  107.     EVT_MENU( ID_DESELECT_COL, GridFrame::DeselectCol)
  108.     EVT_MENU( ID_DESELECT_ROW, GridFrame::DeselectRow)
  109.     EVT_MENU( ID_DESELECT_ALL, GridFrame::DeselectAll)
  110.     EVT_MENU( ID_SELECT_CELL, GridFrame::SelectCell)
  111.     EVT_MENU( ID_SELECT_COL, GridFrame::SelectCol)
  112.     EVT_MENU( ID_SELECT_ROW, GridFrame::SelectRow)
  113.     EVT_MENU( ID_SELECT_ALL, GridFrame::SelectAll)
  114.     EVT_MENU( ID_SELECT_UNSELECT, GridFrame::OnAddToSelectToggle)
  115.  
  116.     EVT_MENU( ID_SET_HIGHLIGHT_WIDTH, GridFrame::OnSetHighlightWidth)
  117.     EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH, GridFrame::OnSetROHighlightWidth)
  118.  
  119.     EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
  120.     EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
  121.     EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
  122.     EVT_GRID_COL_SIZE( GridFrame::OnColSize )
  123.     EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
  124.     EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
  125.     EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
  126.  
  127.     EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
  128.     EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
  129. END_EVENT_TABLE()
  130.  
  131.  
  132. GridFrame::GridFrame()
  133.         : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
  134.                    wxDefaultPosition,
  135.                    wxDefaultSize )
  136. {
  137.     int gridW = 600, gridH = 300;
  138.     int logW = gridW, logH = 100;
  139.  
  140.     wxMenu *fileMenu = new wxMenu;
  141.     fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V");
  142.     fileMenu->Append( ID_BUGS_TABLE, "&Bugs table test\tCtrl-B");
  143.     fileMenu->Append( ID_SMALL_GRID, "&Small Grid test\tCtrl-S");
  144.     fileMenu->AppendSeparator();
  145.     fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
  146.  
  147.     wxMenu *viewMenu = new wxMenu;
  148.     viewMenu->Append( ID_TOGGLEROWLABELS,  "&Row labels", "", TRUE );
  149.     viewMenu->Append( ID_TOGGLECOLLABELS,  "&Col labels", "", TRUE );
  150.     viewMenu->Append( ID_TOGGLEEDIT,  "&Editable", "", TRUE );
  151.     viewMenu->Append( ID_TOGGLEROWSIZING, "Ro&w drag-resize", "", TRUE );
  152.     viewMenu->Append( ID_TOGGLECOLSIZING, "C&ol drag-resize", "", TRUE );
  153.     viewMenu->Append( ID_TOGGLEGRIDSIZING, "&Grid drag-resize", "", TRUE );
  154.     viewMenu->Append( ID_TOGGLEGRIDLINES, "&Grid Lines", "", TRUE );
  155.     viewMenu->Append( ID_SET_HIGHLIGHT_WIDTH, "&Set Cell Highlight Width...", "" );
  156.     viewMenu->Append( ID_SET_RO_HIGHLIGHT_WIDTH, "&Set Cell RO Highlight Width...", "" );
  157.     viewMenu->Append( ID_AUTOSIZECOLS, "&Auto-size cols" );
  158.     viewMenu->Append( ID_CELLOVERFLOW, "&Overflow cells", "", TRUE );
  159.     viewMenu->Append( ID_RESIZECELL, "&Resize cell (7,1)", "", TRUE );
  160.  
  161.     wxMenu *rowLabelMenu = new wxMenu;
  162.  
  163.     viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
  164.                       rowLabelMenu,
  165.                       "Change alignment of row labels" );
  166.  
  167.     rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
  168.     rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
  169.  
  170.     wxMenu *colLabelMenu = new wxMenu;
  171.  
  172.     viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
  173.                       colLabelMenu,
  174.                       "Change alignment of col labels" );
  175.  
  176.     colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
  177.     colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
  178.  
  179.     wxMenu *colMenu = new wxMenu;
  180.     colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour..." );
  181.     colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour..." );
  182.     colMenu->Append( ID_SETLABEL_FONT, "Set label fo&nt..." );
  183.     colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour..." );
  184.     colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour..." );
  185.     colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour..." );
  186.  
  187.     wxMenu *editMenu = new wxMenu;
  188.     editMenu->Append( ID_INSERTROW, "Insert &row" );
  189.     editMenu->Append( ID_INSERTCOL, "Insert &column" );
  190.     editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
  191.     editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
  192.     editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
  193.  
  194.     wxMenu *selectMenu = new wxMenu;
  195.     selectMenu->Append( ID_SELECT_UNSELECT, "Add new cells to the selection",
  196.                         "When off, old selection is deselected before "
  197.                         "selecting the new cells", TRUE );
  198.     selectMenu->Append( ID_SELECT_ALL, "Select all");
  199.     selectMenu->Append( ID_SELECT_ROW, "Select row 2");
  200.     selectMenu->Append( ID_SELECT_COL, "Select col 2");
  201.     selectMenu->Append( ID_SELECT_CELL, "Select cell (3, 1)");
  202.     selectMenu->Append( ID_DESELECT_ALL, "Deselect all");
  203.     selectMenu->Append( ID_DESELECT_ROW, "Deselect row 2");
  204.     selectMenu->Append( ID_DESELECT_COL, "Deselect col 2");
  205.     selectMenu->Append( ID_DESELECT_CELL, "Deselect cell (3, 1)");
  206.     wxMenu *selectionMenu = new wxMenu;
  207.     selectMenu->Append( ID_CHANGESEL, "Change &selection mode",
  208.                       selectionMenu,
  209.                       "Change selection mode" );
  210.  
  211.     selectionMenu->Append( ID_SELCELLS, "Select &Cells" );
  212.     selectionMenu->Append( ID_SELROWS, "Select &Rows" );
  213.     selectionMenu->Append( ID_SELCOLS, "Select C&ols" );
  214.  
  215.  
  216.     wxMenu *helpMenu = new wxMenu;
  217.     helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
  218.  
  219.     wxMenuBar *menuBar = new wxMenuBar;
  220.     menuBar->Append( fileMenu, "&File" );
  221.     menuBar->Append( viewMenu, "&View" );
  222.     menuBar->Append( colMenu,  "&Colours" );
  223.     menuBar->Append( editMenu, "&Edit" );
  224.     menuBar->Append( selectMenu, "&Select" );
  225.     menuBar->Append( helpMenu, "&Help" );
  226.  
  227.     SetMenuBar( menuBar );
  228.  
  229.     m_addToSel = FALSE;
  230.  
  231.     grid = new wxGrid( this,
  232.                        -1,
  233.                        wxPoint( 0, 0 ),
  234.                        wxSize( 400, 300 ) );
  235.  
  236.     logWin = new wxTextCtrl( this,
  237.                              -1,
  238.                              wxEmptyString,
  239.                              wxPoint( 0, gridH + 20 ),
  240.                              wxSize( logW, logH ),
  241.                              wxTE_MULTILINE );
  242.  
  243.     logger = new wxLogTextCtrl( logWin );
  244.     m_logOld = logger->SetActiveTarget( logger );
  245.     logger->SetTimestamp( NULL );
  246.  
  247.     // this will create a grid and, by default, an associated grid
  248.     // table for strings
  249.     grid->CreateGrid( 100, 100 );
  250.  
  251.     grid->SetRowSize( 0, 60 );
  252.     grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
  253.  
  254.     grid->SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." );
  255.     grid->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer);
  256.     grid->SetCellEditor( 0,  1 , new wxGridCellAutoWrapStringEditor);
  257.  
  258.     grid->SetCellValue( 0, 2, "Blah" );
  259.     grid->SetCellValue( 0, 3, "Read only" );
  260.     grid->SetReadOnly( 0, 3 );
  261.  
  262.     grid->SetCellValue( 0, 4, "Can veto edit this cell" );
  263.  
  264.     grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
  265.  
  266.     grid->SetRowSize( 99, 60 );
  267.     grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
  268.     grid->SetCellValue( 1, 0, "This default cell will overflow into neighboring cells, but not if you turn overflow off.");
  269.  
  270.     grid->SetCellTextColour(1, 2, *wxRED);
  271.     grid->SetCellBackgroundColour(1, 2, *wxGREEN);
  272.  
  273.     grid->SetCellValue( 1, 4, "I'm in the middle");
  274.  
  275.     grid->SetCellValue(2, 2, "red");
  276.  
  277.     grid->SetCellTextColour(2, 2, *wxRED);
  278.     grid->SetCellValue(3, 3, "green on grey");
  279.     grid->SetCellTextColour(3, 3, *wxGREEN);
  280.     grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
  281.  
  282.     grid->SetCellValue(4, 4, "a weird looking cell");
  283.     grid->SetCellAlignment(4, 4, wxALIGN_CENTRE, wxALIGN_CENTRE);
  284.     grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
  285.  
  286.     grid->SetCellValue(3, 0, "0");
  287.     grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
  288.     grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
  289.  
  290.     wxGridCellAttr *attr;
  291.     attr = new wxGridCellAttr;
  292.     attr->SetTextColour(*wxBLUE);
  293.     grid->SetColAttr(5, attr);
  294.     attr = new wxGridCellAttr;
  295.     attr->SetBackgroundColour(*wxRED);
  296.     grid->SetRowAttr(5, attr);
  297.  
  298.     grid->SetCellValue(2, 4, "a wider column");
  299.     grid->SetColSize(4, 120);
  300.     grid->SetColMinimalWidth(4, 120);
  301.  
  302.     grid->SetCellTextColour(5, 8, *wxGREEN);
  303.     grid->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
  304.     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");
  305.  
  306.     grid->SetColFormatFloat(6);
  307.     grid->SetCellValue(0, 6, "3.1415");
  308.     grid->SetCellValue(1, 6, "1415");
  309.     grid->SetCellValue(2, 6, "12345.67890");
  310.  
  311.     grid->SetColFormatFloat(7, 6, 2);
  312.     grid->SetCellValue(0, 7, "3.1415");
  313.     grid->SetCellValue(1, 7, "1415");
  314.     grid->SetCellValue(2, 7, "12345.67890");
  315.  
  316.     const wxString choices[] =
  317.     {
  318.         _T("Please select a choice"),
  319.         _T("This takes two cells"),
  320.         _T("Another choice"),
  321.     };
  322.     grid->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices), choices));
  323.     grid->SetCellSize(4, 0, 1, 2);
  324.     grid->SetCellValue(4, 0, choices[0]);
  325.     grid->SetCellOverflow(4, 0, FALSE);
  326.  
  327.     grid->SetCellSize(7, 1, 3, 4);
  328.     grid->SetCellAlignment(7, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
  329.     grid->SetCellValue(7, 1, "Big box!");
  330.  
  331.     wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
  332.     topSizer->Add( grid,
  333.                    1,
  334.                    wxEXPAND );
  335.  
  336.     topSizer->Add( logWin,
  337.                    0,
  338.                    wxEXPAND );
  339.  
  340.     SetAutoLayout( TRUE );
  341.     SetSizer( topSizer );
  342.  
  343.     topSizer->Fit( this );
  344.     topSizer->SetSizeHints( this );
  345.  
  346.     Centre();
  347.     SetDefaults();
  348. }
  349.  
  350.  
  351. GridFrame::~GridFrame()
  352. {
  353.     delete wxLog::SetActiveTarget(m_logOld);
  354. }
  355.  
  356.  
  357. void GridFrame::SetDefaults()
  358. {
  359.     GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
  360.     GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
  361.     GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
  362.     GetMenuBar()->Check( ID_TOGGLEROWSIZING, TRUE );
  363.     GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE );
  364.     GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, TRUE );
  365.     GetMenuBar()->Check( ID_TOGGLEGRIDLINES, TRUE );
  366.     GetMenuBar()->Check( ID_CELLOVERFLOW, TRUE );
  367. }
  368.  
  369.  
  370. void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
  371. {
  372.     if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
  373.     {
  374.         grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
  375.     }
  376.     else
  377.     {
  378.         grid->SetRowLabelSize( 0 );
  379.     }
  380. }
  381.  
  382.  
  383. void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
  384. {
  385.     if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
  386.     {
  387.         grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
  388.     }
  389.     else
  390.     {
  391.         grid->SetColLabelSize( 0 );
  392.     }
  393. }
  394.  
  395.  
  396. void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
  397. {
  398.     grid->EnableEditing(
  399.         GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
  400. }
  401.  
  402.  
  403. void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
  404. {
  405.     grid->EnableDragRowSize(
  406.         GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
  407. }
  408.  
  409.  
  410. void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
  411. {
  412.     grid->EnableDragColSize(
  413.         GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
  414. }
  415.  
  416. void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
  417. {
  418.     grid->EnableDragGridSize(
  419.         GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
  420. }
  421.  
  422.  
  423. void GridFrame::ToggleGridLines( wxCommandEvent& WXUNUSED(ev) )
  424. {
  425.     grid->EnableGridLines(
  426.         GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES ) );
  427. }
  428.  
  429. void GridFrame::OnSetHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
  430. {
  431.     wxString choices[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  432.  
  433.     wxSingleChoiceDialog dlg(this, "Choose the thickness of the highlight pen:",
  434.                              "Pen Width", 11, choices);
  435.  
  436.     int current = grid->GetCellHighlightPenWidth();
  437.     dlg.SetSelection(current);
  438.     if (dlg.ShowModal() == wxID_OK) {
  439.         grid->SetCellHighlightPenWidth(dlg.GetSelection());
  440.     }
  441. }
  442.  
  443. void GridFrame::OnSetROHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
  444. {
  445.     wxString choices[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  446.  
  447.     wxSingleChoiceDialog dlg(this, "Choose the thickness of the highlight pen:",
  448.                              "Pen Width", 11, choices);
  449.  
  450.     int current = grid->GetCellHighlightROPenWidth();
  451.     dlg.SetSelection(current);
  452.     if (dlg.ShowModal() == wxID_OK) {
  453.         grid->SetCellHighlightROPenWidth(dlg.GetSelection());
  454.     }
  455. }
  456.  
  457.  
  458.  
  459. void GridFrame::AutoSizeCols( wxCommandEvent& WXUNUSED(ev) )
  460. {
  461.     grid->AutoSizeColumns();
  462.     grid->Refresh();
  463. }
  464.  
  465. void GridFrame::CellOverflow( wxCommandEvent& ev )
  466. {
  467.     grid->SetDefaultCellOverflow(ev.IsChecked());
  468.     grid->Refresh();
  469. }
  470.  
  471. void GridFrame::ResizeCell( wxCommandEvent& ev )
  472. {
  473.     if (ev.IsChecked())
  474.         grid->SetCellSize( 7, 1, 5, 5 );
  475.     else
  476.         grid->SetCellSize( 7, 1, 1, 5 );
  477.     grid->Refresh();
  478. }
  479.  
  480. void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
  481. {
  482.     wxColourDialog dlg( NULL );
  483.     if ( dlg.ShowModal() == wxID_OK )
  484.     {
  485.         wxColourData retData;
  486.         retData = dlg.GetColourData();
  487.         wxColour colour = retData.GetColour();
  488.  
  489.         grid->SetLabelBackgroundColour( colour );
  490.     }
  491. }
  492.  
  493.  
  494. void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
  495. {
  496.     wxColourDialog dlg( NULL );
  497.     if ( dlg.ShowModal() == wxID_OK )
  498.     {
  499.         wxColourData retData;
  500.         retData = dlg.GetColourData();
  501.         wxColour colour = retData.GetColour();
  502.  
  503.         grid->SetLabelTextColour( colour );
  504.     }
  505. }
  506.  
  507. void GridFrame::SetLabelFont( wxCommandEvent& WXUNUSED(ev) )
  508. {
  509.     wxFont font = wxGetFontFromUser(this);
  510.     if ( font.Ok() )
  511.     {
  512.         grid->SetLabelFont(font);
  513.     }
  514. }
  515.  
  516. void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
  517. {
  518.     int horiz, vert;
  519.     grid->GetRowLabelAlignment( &horiz, &vert );
  520.  
  521.     switch ( horiz )
  522.     {
  523.         case wxALIGN_LEFT:
  524.             horiz = wxALIGN_CENTRE;
  525.             break;
  526.  
  527.         case wxALIGN_CENTRE:
  528.             horiz = wxALIGN_RIGHT;
  529.             break;
  530.  
  531.         case wxALIGN_RIGHT:
  532.             horiz = wxALIGN_LEFT;
  533.             break;
  534.     }
  535.  
  536.     grid->SetRowLabelAlignment( horiz, -1 );
  537. }
  538.  
  539. void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
  540. {
  541.     int horiz, vert;
  542.     grid->GetRowLabelAlignment( &horiz, &vert );
  543.  
  544.     switch ( vert )
  545.     {
  546.         case wxALIGN_TOP:
  547.             vert = wxALIGN_CENTRE;
  548.             break;
  549.  
  550.         case wxALIGN_CENTRE:
  551.             vert = wxALIGN_BOTTOM;
  552.             break;
  553.  
  554.         case wxALIGN_BOTTOM:
  555.             vert = wxALIGN_TOP;
  556.             break;
  557.     }
  558.  
  559.     grid->SetRowLabelAlignment( -1, vert );
  560. }
  561.  
  562.  
  563. void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
  564. {
  565.     int horiz, vert;
  566.     grid->GetColLabelAlignment( &horiz, &vert );
  567.  
  568.     switch ( horiz )
  569.     {
  570.         case wxALIGN_LEFT:
  571.             horiz = wxALIGN_CENTRE;
  572.             break;
  573.  
  574.         case wxALIGN_CENTRE:
  575.             horiz = wxALIGN_RIGHT;
  576.             break;
  577.  
  578.         case wxALIGN_RIGHT:
  579.             horiz = wxALIGN_LEFT;
  580.             break;
  581.     }
  582.  
  583.     grid->SetColLabelAlignment( horiz, -1 );
  584. }
  585.  
  586.  
  587. void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
  588. {
  589.     int horiz, vert;
  590.     grid->GetColLabelAlignment( &horiz, &vert );
  591.  
  592.     switch ( vert )
  593.     {
  594.         case wxALIGN_TOP:
  595.             vert = wxALIGN_CENTRE;
  596.             break;
  597.  
  598.         case wxALIGN_CENTRE:
  599.             vert = wxALIGN_BOTTOM;
  600.             break;
  601.  
  602.         case wxALIGN_BOTTOM:
  603.             vert = wxALIGN_TOP;
  604.             break;
  605.     }
  606.  
  607.     grid->SetColLabelAlignment( -1, vert );
  608. }
  609.  
  610.  
  611. void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
  612. {
  613.     wxColourDialog dlg( NULL );
  614.     if ( dlg.ShowModal() == wxID_OK )
  615.     {
  616.         wxColourData retData;
  617.         retData = dlg.GetColourData();
  618.         wxColour colour = retData.GetColour();
  619.  
  620.         grid->SetGridLineColour( colour );
  621.     }
  622. }
  623.  
  624.  
  625. void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
  626. {
  627.     grid->InsertRows( grid->GetGridCursorRow(), 1 );
  628. }
  629.  
  630.  
  631. void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
  632. {
  633.     grid->InsertCols( grid->GetGridCursorCol(), 1 );
  634. }
  635.  
  636.  
  637. void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
  638. {
  639.     if ( grid->IsSelection() )
  640.     {
  641.         grid->BeginBatch();
  642.         for ( int n = 0; n < grid->GetNumberRows(); )
  643.             if ( grid->IsInSelection( n , 0 ) )
  644.                 grid->DeleteRows( n, 1 );
  645.         else
  646.             n++;
  647.         grid->EndBatch();
  648.     }
  649. }
  650.  
  651.  
  652. void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
  653. {
  654.     if ( grid->IsSelection() )
  655.     {
  656.         grid->BeginBatch();
  657.         for ( int n = 0; n < grid->GetNumberCols(); )
  658.             if ( grid->IsInSelection( 0 , n ) )
  659.                 grid->DeleteCols( n, 1 );
  660.         else
  661.             n++;
  662.         grid->EndBatch();
  663.     }
  664. }
  665.  
  666.  
  667. void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
  668. {
  669.     grid->ClearGrid();
  670. }
  671.  
  672. void GridFrame::SelectCells( wxCommandEvent& WXUNUSED(ev) )
  673. {
  674.     grid->SetSelectionMode( wxGrid::wxGridSelectCells );
  675. }
  676.  
  677. void GridFrame::SelectRows( wxCommandEvent& WXUNUSED(ev) )
  678. {
  679.     grid->SetSelectionMode( wxGrid::wxGridSelectRows );
  680. }
  681.  
  682. void GridFrame::SelectCols( wxCommandEvent& WXUNUSED(ev) )
  683. {
  684.     grid->SetSelectionMode( wxGrid::wxGridSelectColumns );
  685. }
  686.  
  687. void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
  688. {
  689.     wxColour col = wxGetColourFromUser(this);
  690.     if ( col.Ok() )
  691.     {
  692.         grid->SetDefaultCellTextColour(col);
  693.         grid->Refresh();
  694.     }
  695. }
  696.  
  697. void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
  698. {
  699.     wxColour col = wxGetColourFromUser(this);
  700.     if ( col.Ok() )
  701.     {
  702.         // Check the new Refresh function by passing it a rectangle
  703.         // which exactly fits the grid.
  704.         wxRect r(wxPoint(0, 0), grid->GetSize());
  705.         grid->SetDefaultCellBackgroundColour(col);
  706.         grid->Refresh(TRUE, &r);
  707.     }
  708. }
  709.  
  710. void GridFrame::DeselectCell(wxCommandEvent& WXUNUSED(event))
  711. {
  712.       grid->DeselectCell(3, 1);
  713. }
  714.  
  715. void GridFrame::DeselectCol(wxCommandEvent& WXUNUSED(event))
  716. {
  717.       grid->DeselectCol(2);
  718. }
  719.  
  720. void GridFrame::DeselectRow(wxCommandEvent& WXUNUSED(event))
  721. {
  722.       grid->DeselectRow(2);
  723. }
  724.  
  725. void GridFrame::DeselectAll(wxCommandEvent& WXUNUSED(event))
  726. {
  727.       grid->ClearSelection();
  728. }
  729.  
  730. void GridFrame::SelectCell(wxCommandEvent& WXUNUSED(event))
  731. {
  732.       grid->SelectBlock(3, 1, 3, 1, m_addToSel);
  733. }
  734.  
  735. void GridFrame::SelectCol(wxCommandEvent& WXUNUSED(event))
  736. {
  737.       grid->SelectCol(2, m_addToSel);
  738. }
  739.  
  740. void GridFrame::SelectRow(wxCommandEvent& WXUNUSED(event))
  741. {
  742.       grid->SelectRow(2, m_addToSel);
  743. }
  744.  
  745. void GridFrame::SelectAll(wxCommandEvent& WXUNUSED(event))
  746. {
  747.       grid->SelectAll();
  748. }
  749.  
  750. void GridFrame::OnAddToSelectToggle(wxCommandEvent& event)
  751. {
  752.     m_addToSel = event.IsChecked();
  753. }
  754.  
  755. void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
  756. {
  757.     logBuf = "";
  758.     if ( ev.GetRow() != -1 )
  759.     {
  760.         logBuf << "Left click on row label " << ev.GetRow();
  761.     }
  762.     else if ( ev.GetCol() != -1 )
  763.     {
  764.         logBuf << "Left click on col label " << ev.GetCol();
  765.     }
  766.     else
  767.     {
  768.         logBuf << "Left click on corner label";
  769.     }
  770.  
  771.     if ( ev.ShiftDown() ) logBuf << " (shift down)";
  772.     if ( ev.ControlDown() ) logBuf << " (control down)";
  773.     wxLogMessage( wxT("%s"), logBuf.c_str() );
  774.  
  775.     // you must call event skip if you want default grid processing
  776.     //
  777.     ev.Skip();
  778. }
  779.  
  780.  
  781. void GridFrame::OnCellLeftClick( wxGridEvent& ev )
  782. {
  783.     logBuf = "";
  784.     logBuf << "Left click at row " << ev.GetRow()
  785.            << " col " << ev.GetCol();
  786.     wxLogMessage( wxT("%s"), logBuf.c_str() );
  787.  
  788.     // you must call event skip if you want default grid processing
  789.     // (cell highlighting etc.)
  790.     //
  791.     ev.Skip();
  792. }
  793.  
  794.  
  795. void GridFrame::OnRowSize( wxGridSizeEvent& ev )
  796. {
  797.     logBuf = "";
  798.     logBuf << "Resized row " << ev.GetRowOrCol();
  799.     wxLogMessage( wxT("%s"), logBuf.c_str() );
  800.  
  801.     ev.Skip();
  802. }
  803.  
  804.  
  805. void GridFrame::OnColSize( wxGridSizeEvent& ev )
  806. {
  807.     logBuf = "";
  808.     logBuf << "Resized col " << ev.GetRowOrCol();
  809.     wxLogMessage( wxT("%s"), logBuf.c_str() );
  810.  
  811.     ev.Skip();
  812. }
  813.  
  814.  
  815. void GridFrame::OnSelectCell( wxGridEvent& ev )
  816. {
  817.     logBuf = "";
  818.     if ( ev.Selecting() )
  819.         logBuf << "Selected ";
  820.     else
  821.         logBuf << "Deselected ";
  822.     logBuf << "cell at row " << ev.GetRow()
  823.            << " col " << ev.GetCol()
  824.            << " ( ControlDown: "<< (ev.ControlDown() ? 'T':'F')
  825.            << ", ShiftDown: "<< (ev.ShiftDown() ? 'T':'F')
  826.            << ", AltDown: "<< (ev.AltDown() ? 'T':'F')
  827.            << ", MetaDown: "<< (ev.MetaDown() ? 'T':'F') << " )";
  828.     wxLogMessage( wxT("%s"), logBuf.c_str() );
  829.  
  830.     // you must call Skip() if you want the default processing
  831.     // to occur in wxGrid
  832.     ev.Skip();
  833. }
  834.  
  835. void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
  836. {
  837.     logBuf = "";
  838.     if ( ev.Selecting() )
  839.         logBuf << "Selected ";
  840.     else
  841.         logBuf << "Deselected ";
  842.     logBuf << "cells from row " << ev.GetTopRow()
  843.            << " col " << ev.GetLeftCol()
  844.            << " to row " << ev.GetBottomRow()
  845.            << " col " << ev.GetRightCol()
  846.            << " ( ControlDown: "<< (ev.ControlDown() ? 'T':'F')
  847.            << ", ShiftDown: "<< (ev.ShiftDown() ? 'T':'F')
  848.            << ", AltDown: "<< (ev.AltDown() ? 'T':'F')
  849.            << ", MetaDown: "<< (ev.MetaDown() ? 'T':'F') << " )";
  850.     wxLogMessage( wxT("%s"), logBuf.c_str() );
  851.  
  852.     ev.Skip();
  853. }
  854.  
  855. void GridFrame::OnCellValueChanged( wxGridEvent& ev )
  856. {
  857.     logBuf = "";
  858.     logBuf  << "Value changed for cell at"
  859.             << " row " << ev.GetRow()
  860.             << " col " << ev.GetCol();
  861.  
  862.     wxLogMessage( wxT("%s"), logBuf.c_str() );
  863.  
  864.     ev.Skip();
  865. }
  866.  
  867. void GridFrame::OnEditorShown( wxGridEvent& ev )
  868. {
  869.  
  870.     if ( (ev.GetCol() == 4) &&
  871.          (ev.GetRow() == 0) &&
  872.      (wxMessageBox(_T("Are you sure you wish to edit this cell"),
  873.                    _T("Checking"),wxYES_NO) == wxNO ) ) {
  874.  
  875.      ev.Veto();
  876.      return;
  877.     }
  878.  
  879.     wxLogMessage( wxT("Cell editor shown.") );
  880.  
  881.     ev.Skip();
  882. }
  883.  
  884. void GridFrame::OnEditorHidden( wxGridEvent& ev )
  885. {
  886.  
  887.     if ( (ev.GetCol() == 4) &&
  888.          (ev.GetRow() == 0) &&
  889.      (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
  890.                    _T("Checking"),wxYES_NO) == wxNO ) ) {
  891.  
  892.         ev.Veto();
  893.         return;
  894.     }
  895.  
  896.     wxLogMessage( wxT("Cell editor hidden.") );
  897.  
  898.     ev.Skip();
  899. }
  900.  
  901. void GridFrame::About(  wxCommandEvent& WXUNUSED(ev) )
  902. {
  903.     (void)wxMessageBox( "\n\nwxGrid demo \n\n"
  904.                         "Michael Bedward \n"
  905.                         "mbedward@ozemail.com.au \n\n",
  906.                         "About",
  907.                         wxOK );
  908. }
  909.  
  910.  
  911. void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
  912. {
  913.     Close( TRUE );
  914. }
  915.  
  916. void GridFrame::OnBugsTable(wxCommandEvent& )
  917. {
  918.     BugsGridFrame *frame = new BugsGridFrame;
  919.     frame->Show(TRUE);
  920. }
  921.  
  922. void GridFrame::OnSmallGrid(wxCommandEvent& )
  923. {
  924.     wxFrame* frame = new wxFrame(NULL, -1, "A Small Grid",
  925.                                  wxDefaultPosition, wxSize(640, 480));
  926.     wxPanel* panel = new wxPanel(frame, -1);
  927.     wxGrid* grid = new wxGrid(panel, -1, wxPoint(10,10), wxSize(400,400),
  928.                               wxWANTS_CHARS | wxSIMPLE_BORDER);
  929.     grid->CreateGrid(3,3);
  930.     frame->Show(TRUE);
  931. }
  932.  
  933. void GridFrame::OnVTable(wxCommandEvent& )
  934. {
  935.     static long s_sizeGrid = 10000;
  936.  
  937. #ifdef __WXMOTIF__
  938.     // MB: wxGetNumberFromUser doesn't work properly for wxMotif
  939.     wxString s;
  940.     s << s_sizeGrid;
  941.     s = wxGetTextFromUser( "Size of the table to create",
  942.                            "Size:",
  943.                            s );
  944.  
  945.     s.ToLong( &s_sizeGrid );
  946.  
  947. #else
  948.     s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
  949.                                      "Size: ",
  950.                                      "wxGridDemo question",
  951.                                      s_sizeGrid,
  952.                                      0, 32000, this);
  953. #endif
  954.  
  955.     if ( s_sizeGrid != -1 )
  956.     {
  957.         BigGridFrame* win = new BigGridFrame(s_sizeGrid);
  958.         win->Show(TRUE);
  959.     }
  960. }
  961.  
  962. // ----------------------------------------------------------------------------
  963. // MyGridCellRenderer
  964. // ----------------------------------------------------------------------------
  965.  
  966. // do something that the default renderer doesn't here just to show that it is
  967. // possible to alter the appearance of the cell beyond what the attributes
  968. // allow
  969. void MyGridCellRenderer::Draw(wxGrid& grid,
  970.                               wxGridCellAttr& attr,
  971.                               wxDC& dc,
  972.                               const wxRect& rect,
  973.                               int row, int col,
  974.                               bool isSelected)
  975. {
  976.     wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
  977.  
  978.     dc.SetPen(*wxGREEN_PEN);
  979.     dc.SetBrush(*wxTRANSPARENT_BRUSH);
  980.     dc.DrawEllipse(rect);
  981. }
  982.  
  983. // ----------------------------------------------------------------------------
  984. // MyGridCellAttrProvider
  985. // ----------------------------------------------------------------------------
  986.  
  987. MyGridCellAttrProvider::MyGridCellAttrProvider()
  988. {
  989.     m_attrForOddRows = new wxGridCellAttr;
  990.     m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
  991. }
  992.  
  993. MyGridCellAttrProvider::~MyGridCellAttrProvider()
  994. {
  995.     m_attrForOddRows->DecRef();
  996. }
  997.  
  998. wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col,
  999.                            wxGridCellAttr::wxAttrKind  kind /* = wxGridCellAttr::Any */) const
  1000. {
  1001.     wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
  1002.  
  1003.     if ( row % 2 )
  1004.     {
  1005.         if ( !attr )
  1006.         {
  1007.             attr = m_attrForOddRows;
  1008.             attr->IncRef();
  1009.         }
  1010.         else
  1011.         {
  1012.             if ( !attr->HasBackgroundColour() )
  1013.             {
  1014.                 wxGridCellAttr *attrNew = attr->Clone();
  1015.                 attr->DecRef();
  1016.                 attr = attrNew;
  1017.                 attr->SetBackgroundColour(*wxLIGHT_GREY);
  1018.             }
  1019.         }
  1020.     }
  1021.  
  1022.     return attr;
  1023. }
  1024.  
  1025. // ============================================================================
  1026. // BigGridFrame and BigGridTable:  Sample of a non-standard table
  1027. // ============================================================================
  1028.  
  1029. BigGridFrame::BigGridFrame(long sizeGrid)
  1030.             : wxFrame(NULL, -1, "Plugin Virtual Table",
  1031.                       wxDefaultPosition, wxSize(500, 450))
  1032. {
  1033.     m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
  1034.     m_table = new BigGridTable(sizeGrid);
  1035.  
  1036.     // VZ: I don't understand why this slows down the display that much,
  1037.     //     must profile it...
  1038.     //m_table->SetAttrProvider(new MyGridCellAttrProvider);
  1039.  
  1040.     m_grid->SetTable(m_table, TRUE);
  1041.  
  1042. #if defined __WXMOTIF__
  1043.     // MB: the grid isn't getting a sensible default size under wxMotif
  1044.     int cw, ch;
  1045.     GetClientSize( &cw, &ch );
  1046.     m_grid->SetSize( cw, ch );
  1047. #endif
  1048. }
  1049.  
  1050. // ============================================================================
  1051. // BugsGridFrame: a "realistic" table
  1052. // ============================================================================
  1053.  
  1054. // ----------------------------------------------------------------------------
  1055. // bugs table data
  1056. // ----------------------------------------------------------------------------
  1057.  
  1058. enum Columns
  1059. {
  1060.     Col_Id,
  1061.     Col_Summary,
  1062.     Col_Severity,
  1063.     Col_Priority,
  1064.     Col_Platform,
  1065.     Col_Opened,
  1066.     Col_Max
  1067. };
  1068.  
  1069. enum Severity
  1070. {
  1071.     Sev_Wish,
  1072.     Sev_Minor,
  1073.     Sev_Normal,
  1074.     Sev_Major,
  1075.     Sev_Critical,
  1076.     Sev_Max
  1077. };
  1078.  
  1079. static const wxString severities[] =
  1080. {
  1081.     _T("wishlist"),
  1082.     _T("minor"),
  1083.     _T("normal"),
  1084.     _T("major"),
  1085.     _T("critical"),
  1086. };
  1087.  
  1088. static struct BugsGridData
  1089. {
  1090.     int id;
  1091.     wxChar summary[80];
  1092.     Severity severity;
  1093.     int prio;
  1094.     wxChar platform[12];
  1095.     bool opened;
  1096. } gs_dataBugsGrid [] =
  1097. {
  1098.     { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), TRUE },
  1099.     { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), FALSE },
  1100.     { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), TRUE },
  1101.     { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), FALSE },
  1102. };
  1103.  
  1104. static const wxChar *headers[Col_Max] =
  1105. {
  1106.     _T("Id"),
  1107.     _T("Summary"),
  1108.     _T("Severity"),
  1109.     _T("Priority"),
  1110.     _T("Platform"),
  1111.     _T("Opened?"),
  1112. };
  1113.  
  1114. // ----------------------------------------------------------------------------
  1115. // BugsGridTable
  1116. // ----------------------------------------------------------------------------
  1117.  
  1118. wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
  1119. {
  1120.     switch ( col )
  1121.     {
  1122.         case Col_Id:
  1123.         case Col_Priority:
  1124.             return wxGRID_VALUE_NUMBER;;
  1125.  
  1126.         case Col_Severity:
  1127.             // fall thorugh (TODO should be a list)
  1128.  
  1129.         case Col_Summary:
  1130.             return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
  1131.  
  1132.         case Col_Platform:
  1133.             return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
  1134.  
  1135.         case Col_Opened:
  1136.             return wxGRID_VALUE_BOOL;
  1137.     }
  1138.  
  1139.     wxFAIL_MSG(_T("unknown column"));
  1140.  
  1141.     return wxEmptyString;
  1142. }
  1143.  
  1144. int BugsGridTable::GetNumberRows()
  1145. {
  1146.     return WXSIZEOF(gs_dataBugsGrid);
  1147. }
  1148.  
  1149. int BugsGridTable::GetNumberCols()
  1150. {
  1151.     return Col_Max;
  1152. }
  1153.  
  1154. bool BugsGridTable::IsEmptyCell( int row, int col )
  1155. {
  1156.     return FALSE;
  1157. }
  1158.  
  1159. wxString BugsGridTable::GetValue( int row, int col )
  1160. {
  1161.     const BugsGridData& gd = gs_dataBugsGrid[row];
  1162.  
  1163.     switch ( col )
  1164.     {
  1165.         case Col_Id:
  1166.         case Col_Priority:
  1167.         case Col_Opened:
  1168.             wxFAIL_MSG(_T("unexpected column"));
  1169.             break;
  1170.  
  1171.         case Col_Severity:
  1172.             return severities[gd.severity];
  1173.  
  1174.         case Col_Summary:
  1175.             return gd.summary;
  1176.  
  1177.         case Col_Platform:
  1178.             return gd.platform;
  1179.     }
  1180.  
  1181.     return wxEmptyString;
  1182. }
  1183.  
  1184. void BugsGridTable::SetValue( int row, int col, const wxString& value )
  1185. {
  1186.     BugsGridData& gd = gs_dataBugsGrid[row];
  1187.  
  1188.     switch ( col )
  1189.     {
  1190.         case Col_Id:
  1191.         case Col_Priority:
  1192.         case Col_Opened:
  1193.             wxFAIL_MSG(_T("unexpected column"));
  1194.             break;
  1195.  
  1196.         case Col_Severity:
  1197.             {
  1198.                 size_t n;
  1199.                 for ( n = 0; n < WXSIZEOF(severities); n++ )
  1200.                 {
  1201.                     if ( severities[n] == value )
  1202.                     {
  1203.                         gd.severity = (Severity)n;
  1204.                         break;
  1205.                     }
  1206.                 }
  1207.  
  1208.                 if ( n == WXSIZEOF(severities) )
  1209.                 {
  1210.                     wxLogWarning(_T("Invalid severity value '%s'."),
  1211.                                  value.c_str());
  1212.                     gd.severity = Sev_Normal;
  1213.                 }
  1214.             }
  1215.             break;
  1216.  
  1217.         case Col_Summary:
  1218.             wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
  1219.             break;
  1220.  
  1221.         case Col_Platform:
  1222.             wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
  1223.             break;
  1224.     }
  1225. }
  1226.  
  1227. bool BugsGridTable::CanGetValueAs( int WXUNUSED(row), int col, const wxString& typeName )
  1228. {
  1229.     if ( typeName == wxGRID_VALUE_STRING )
  1230.     {
  1231.         return TRUE;
  1232.     }
  1233.     else if ( typeName == wxGRID_VALUE_BOOL )
  1234.     {
  1235.         return col == Col_Opened;
  1236.     }
  1237.     else if ( typeName == wxGRID_VALUE_NUMBER )
  1238.     {
  1239.         return col == Col_Id || col == Col_Priority || col == Col_Severity;
  1240.     }
  1241.     else
  1242.     {
  1243.         return FALSE;
  1244.     }
  1245. }
  1246.  
  1247. bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
  1248. {
  1249.     return CanGetValueAs(row, col, typeName);
  1250. }
  1251.  
  1252. long BugsGridTable::GetValueAsLong( int row, int col )
  1253. {
  1254.     const BugsGridData& gd = gs_dataBugsGrid[row];
  1255.  
  1256.     switch ( col )
  1257.     {
  1258.         case Col_Id:
  1259.             return gd.id;
  1260.  
  1261.         case Col_Priority:
  1262.             return gd.prio;
  1263.  
  1264.         case Col_Severity:
  1265.             return gd.severity;
  1266.  
  1267.         default:
  1268.             wxFAIL_MSG(_T("unexpected column"));
  1269.             return -1;
  1270.     }
  1271. }
  1272.  
  1273. bool BugsGridTable::GetValueAsBool( int row, int col )
  1274. {
  1275.     if ( col == Col_Opened )
  1276.     {
  1277.         return gs_dataBugsGrid[row].opened;
  1278.     }
  1279.     else
  1280.     {
  1281.         wxFAIL_MSG(_T("unexpected column"));
  1282.  
  1283.         return FALSE;
  1284.     }
  1285. }
  1286.  
  1287. void BugsGridTable::SetValueAsLong( int row, int col, long value )
  1288. {
  1289.     BugsGridData& gd = gs_dataBugsGrid[row];
  1290.  
  1291.     switch ( col )
  1292.     {
  1293.         case Col_Priority:
  1294.             gd.prio = value;
  1295.             break;
  1296.  
  1297.         default:
  1298.             wxFAIL_MSG(_T("unexpected column"));
  1299.     }
  1300. }
  1301.  
  1302. void BugsGridTable::SetValueAsBool( int row, int col, bool value )
  1303. {
  1304.     if ( col == Col_Opened )
  1305.     {
  1306.         gs_dataBugsGrid[row].opened = value;
  1307.     }
  1308.     else
  1309.     {
  1310.         wxFAIL_MSG(_T("unexpected column"));
  1311.     }
  1312. }
  1313.  
  1314. wxString BugsGridTable::GetColLabelValue( int col )
  1315. {
  1316.     return headers[col];
  1317. }
  1318.  
  1319. BugsGridTable::BugsGridTable()
  1320. {
  1321. }
  1322.  
  1323. // ----------------------------------------------------------------------------
  1324. // BugsGridFrame
  1325. // ----------------------------------------------------------------------------
  1326.  
  1327. BugsGridFrame::BugsGridFrame()
  1328.              : wxFrame(NULL, -1, "Bugs table",
  1329.                        wxDefaultPosition, wxSize(500, 300))
  1330. {
  1331.     wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
  1332.     wxGridTableBase *table = new BugsGridTable();
  1333.     table->SetAttrProvider(new MyGridCellAttrProvider);
  1334.     grid->SetTable(table, TRUE);
  1335.  
  1336.     wxGridCellAttr *attrRO = new wxGridCellAttr,
  1337.                    *attrRangeEditor = new wxGridCellAttr,
  1338.                    *attrCombo = new wxGridCellAttr;
  1339.  
  1340.     attrRO->SetReadOnly();
  1341.     attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
  1342.     attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
  1343.                                                     severities));
  1344.  
  1345.     grid->SetColAttr(Col_Id, attrRO);
  1346.     grid->SetColAttr(Col_Priority, attrRangeEditor);
  1347.     grid->SetColAttr(Col_Severity, attrCombo);
  1348.  
  1349.     grid->SetMargins(0, 0);
  1350.  
  1351.     grid->Fit();
  1352.     SetClientSize(grid->GetSize());
  1353. }
  1354.  
  1355.  
  1356.