1998-05-20 14:01:55 +00:00
/*
2002-03-17 14:16:03 +00:00
* File : grid . cpp
* Purpose : wxGrid test
2003-01-04 11:04:39 +00:00
PLEASE NOTE : this sample is deprecated . See
newgrid for a sample based on the newer wxGrid API .
2002-03-17 14:16:03 +00:00
* Author : Julian Smart
* Created : 1995
* Updated :
1998-05-20 14:01:55 +00:00
* Copyright : ( c ) 1995 , AIAI , University of Edinburgh
*/
static const char sccsid [ ] = " %W% %G% " ;
// For compilers that support precompilation, includes "wx/wx.h".
# include "wx/wxprec.h"
# ifdef __BORLANDC__
# pragma hdrstop
# endif
# ifndef WX_PRECOMP
# include "wx/wx.h"
# endif
# include "wx/grid.h"
# include "wx/colordlg.h"
// Define a new application type
class MyApp : public wxApp
2002-03-17 14:16:03 +00:00
{
public :
virtual bool OnInit ( void ) ;
virtual int OnExit ( ) ;
1998-05-20 14:01:55 +00:00
} ;
1999-06-23 11:26:19 +00:00
1998-05-20 14:01:55 +00:00
// Define a new frame type
class MyFrame : public wxFrame
{ public :
wxGrid * grid ;
MyFrame ( wxFrame * frame , const wxString & title , const wxPoint & pos , const wxSize & size ) ;
void ToggleEditable ( wxCommandEvent & event ) ;
1999-06-23 11:26:19 +00:00
void ToggleEditInPlace ( wxCommandEvent & event ) ;
1998-05-20 14:01:55 +00:00
void ToggleRowLabel ( wxCommandEvent & event ) ;
void ToggleColLabel ( wxCommandEvent & event ) ;
void ToggleDividers ( wxCommandEvent & event ) ;
void LeftCell ( wxCommandEvent & event ) ;
void CentreCell ( wxCommandEvent & event ) ;
void RightCell ( wxCommandEvent & event ) ;
void ColourLabelBackground ( wxCommandEvent & event ) ;
void ColourLabelText ( wxCommandEvent & event ) ;
void NormalLabelColouring ( wxCommandEvent & event ) ;
void ColourCellBackground ( wxCommandEvent & event ) ;
void ColourCellText ( wxCommandEvent & event ) ;
void NormalCellColouring ( wxCommandEvent & event ) ;
void Quit ( wxCommandEvent & event ) ;
void OnActivate ( wxActivateEvent & event ) ;
DECLARE_EVENT_TABLE ( )
} ;
1998-08-23 03:22:56 +00:00
wxBitmap * cellBitmap1 = ( wxBitmap * ) NULL ;
wxBitmap * cellBitmap2 = ( wxBitmap * ) NULL ;
1998-05-20 14:01:55 +00:00
// ID for the menu quit command
# define GRID_QUIT 1
# define GRID_TOGGLE_EDITABLE 2
1999-06-23 11:26:19 +00:00
# define GRID_TOGGLE_EDITINPLACE 22
1998-05-20 14:01:55 +00:00
# define GRID_LEFT_CELL 3
# define GRID_CENTRE_CELL 4
# define GRID_RIGHT_CELL 5
# define GRID_TOGGLE_ROW_LABEL 6
# define GRID_TOGGLE_COL_LABEL 7
# define GRID_COLOUR_LABEL_BACKGROUND 8
# define GRID_COLOUR_LABEL_TEXT 9
# define GRID_NORMAL_LABEL_COLOURING 10
# define GRID_COLOUR_CELL_BACKGROUND 11
# define GRID_COLOUR_CELL_TEXT 12
# define GRID_NORMAL_CELL_COLOURING 13
# define GRID_TOGGLE_DIVIDERS 14
// Main proc
IMPLEMENT_APP ( MyApp )
// `Main program' equivalent, creating windows and returning main app frame
bool MyApp : : OnInit ( void )
{
1998-07-10 14:15:17 +00:00
# ifdef __WXMSW__
2002-12-14 18:13:27 +00:00
cellBitmap1 = new wxBitmap ( _T ( " bitmap1 " ) ) ;
cellBitmap2 = new wxBitmap ( _T ( " bitmap2 " ) ) ;
1998-05-20 14:01:55 +00:00
# endif
2002-03-17 14:16:03 +00:00
// Create the main frame window
2002-12-14 18:13:27 +00:00
MyFrame * frame = new MyFrame ( NULL , _T ( " wxGrid Sample " ) , wxPoint ( 50 , 50 ) , wxSize ( 450 , 300 ) ) ;
2002-03-17 14:16:03 +00:00
// Give it an icon
1998-07-10 14:15:17 +00:00
# ifdef __WXMSW__
2002-12-14 18:13:27 +00:00
frame - > SetIcon ( wxIcon ( _T ( " mondrian " ) ) ) ;
1998-05-20 14:01:55 +00:00
# endif
2002-03-17 14:16:03 +00:00
// Make a menubar
wxMenu * file_menu = new wxMenu ;
2002-12-14 18:13:27 +00:00
file_menu - > Append ( GRID_QUIT , _T ( " E&xit " ) ) ;
2002-03-17 14:16:03 +00:00
wxMenu * settings_menu = new wxMenu ;
2002-12-14 18:13:27 +00:00
settings_menu - > Append ( GRID_TOGGLE_EDITABLE , _T ( " &Toggle editable " ) ) ;
settings_menu - > Append ( GRID_TOGGLE_EDITINPLACE , _T ( " &Toggle edit in place " ) ) ;
settings_menu - > Append ( GRID_TOGGLE_ROW_LABEL , _T ( " Toggle ro&w label " ) ) ;
settings_menu - > Append ( GRID_TOGGLE_COL_LABEL , _T ( " Toggle co&l label " ) ) ;
settings_menu - > Append ( GRID_TOGGLE_DIVIDERS , _T ( " Toggle ÷rs " ) ) ;
2002-03-17 14:16:03 +00:00
settings_menu - > AppendSeparator ( ) ;
2002-12-14 18:13:27 +00:00
settings_menu - > Append ( GRID_LEFT_CELL , _T ( " &Left cell alignment " ) ) ;
settings_menu - > Append ( GRID_CENTRE_CELL , _T ( " &Centre cell alignment " ) ) ;
settings_menu - > Append ( GRID_RIGHT_CELL , _T ( " &Right cell alignment " ) ) ;
2002-03-17 14:16:03 +00:00
settings_menu - > AppendSeparator ( ) ;
2002-12-14 18:13:27 +00:00
settings_menu - > Append ( GRID_COLOUR_LABEL_BACKGROUND , _T ( " Choose a label &background colour " ) ) ;
settings_menu - > Append ( GRID_COLOUR_LABEL_TEXT , _T ( " Choose a label fore&ground colour " ) ) ;
settings_menu - > Append ( GRID_NORMAL_LABEL_COLOURING , _T ( " &Normal label colouring " ) ) ;
2002-03-17 14:16:03 +00:00
settings_menu - > AppendSeparator ( ) ;
2002-12-14 18:13:27 +00:00
settings_menu - > Append ( GRID_COLOUR_CELL_BACKGROUND , _T ( " Choo&se a cell &background colour " ) ) ;
settings_menu - > Append ( GRID_COLOUR_CELL_TEXT , _T ( " Choose &a cell foreground colour " ) ) ;
settings_menu - > Append ( GRID_NORMAL_CELL_COLOURING , _T ( " N&ormal cell colouring " ) ) ;
2002-03-17 14:16:03 +00:00
wxMenuBar * menu_bar = new wxMenuBar ;
2002-12-14 18:13:27 +00:00
menu_bar - > Append ( file_menu , _T ( " &File " ) ) ;
menu_bar - > Append ( settings_menu , _T ( " &Settings " ) ) ;
2002-03-17 14:16:03 +00:00
frame - > SetMenuBar ( menu_bar ) ;
// Make a grid
frame - > grid = new wxGrid ( frame , 0 , 0 , 400 , 400 ) ;
frame - > grid - > CreateGrid ( 10 , 8 ) ;
frame - > grid - > SetColumnWidth ( 3 , 200 ) ;
frame - > grid - > SetRowHeight ( 4 , 45 ) ;
2002-12-14 18:13:27 +00:00
frame - > grid - > SetCellValue ( _T ( " First cell " ) , 0 , 0 ) ;
frame - > grid - > SetCellValue ( _T ( " Another cell " ) , 1 , 1 ) ;
frame - > grid - > SetCellValue ( _T ( " Yet another cell " ) , 2 , 2 ) ;
2002-03-17 14:16:03 +00:00
frame - > grid - > SetCellTextFont ( wxFont ( 10 , wxROMAN , wxITALIC , wxNORMAL ) , 0 , 0 ) ;
frame - > grid - > SetCellTextColour ( * wxRED , 1 , 1 ) ;
frame - > grid - > SetCellBackgroundColour ( * wxCYAN , 2 , 2 ) ;
if ( cellBitmap1 & & cellBitmap2 )
{
frame - > grid - > SetCellAlignment ( wxCENTRE , 5 , 0 ) ;
frame - > grid - > SetCellAlignment ( wxCENTRE , 6 , 0 ) ;
frame - > grid - > SetCellBitmap ( cellBitmap1 , 5 , 0 ) ;
frame - > grid - > SetCellBitmap ( cellBitmap2 , 6 , 0 ) ;
}
frame - > grid - > UpdateDimensions ( ) ;
// Show the frame
frame - > Show ( TRUE ) ;
2003-01-04 11:04:39 +00:00
wxMessageBox ( wxT ( " Please note: this is an obsolete sample using the old wxGrid API. \n Please compile newgrid instead. " ) , wxT ( " wxGrid " ) , wxICON_INFORMATION | wxOK , frame ) ;
2002-03-17 14:16:03 +00:00
SetTopWindow ( frame ) ;
return TRUE ;
}
int MyApp : : OnExit ( )
{
if ( cellBitmap1 )
{
delete cellBitmap1 ;
cellBitmap1 = ( wxBitmap * ) NULL ;
}
if ( cellBitmap2 )
{
delete cellBitmap2 ;
cellBitmap1 = ( wxBitmap * ) NULL ;
}
// exit code is 0, everything is ok
return 0 ;
1998-05-20 14:01:55 +00:00
}
2002-03-17 14:16:03 +00:00
1998-05-20 14:01:55 +00:00
// My frame constructor
2002-03-17 14:16:03 +00:00
MyFrame : : MyFrame ( wxFrame * frame , const wxString & title ,
const wxPoint & pos , const wxSize & size ) :
wxFrame ( frame , - 1 , title , pos , size )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
grid = ( wxGrid * ) NULL ;
1998-05-20 14:01:55 +00:00
}
BEGIN_EVENT_TABLE ( MyFrame , wxFrame )
2002-03-17 14:16:03 +00:00
EVT_MENU ( GRID_TOGGLE_EDITABLE , MyFrame : : ToggleEditable )
EVT_MENU ( GRID_TOGGLE_EDITINPLACE , MyFrame : : ToggleEditInPlace )
EVT_MENU ( GRID_TOGGLE_ROW_LABEL , MyFrame : : ToggleRowLabel )
EVT_MENU ( GRID_TOGGLE_COL_LABEL , MyFrame : : ToggleColLabel )
EVT_MENU ( GRID_TOGGLE_DIVIDERS , MyFrame : : ToggleDividers )
EVT_MENU ( GRID_LEFT_CELL , MyFrame : : LeftCell )
EVT_MENU ( GRID_CENTRE_CELL , MyFrame : : CentreCell )
EVT_MENU ( GRID_RIGHT_CELL , MyFrame : : RightCell )
EVT_MENU ( GRID_COLOUR_LABEL_BACKGROUND , MyFrame : : ColourLabelBackground )
EVT_MENU ( GRID_COLOUR_LABEL_TEXT , MyFrame : : ColourLabelText )
EVT_MENU ( GRID_NORMAL_LABEL_COLOURING , MyFrame : : NormalLabelColouring )
EVT_MENU ( GRID_COLOUR_CELL_BACKGROUND , MyFrame : : ColourCellBackground )
EVT_MENU ( GRID_COLOUR_CELL_TEXT , MyFrame : : ColourCellText )
EVT_MENU ( GRID_NORMAL_CELL_COLOURING , MyFrame : : NormalCellColouring )
EVT_MENU ( GRID_QUIT , MyFrame : : Quit )
1998-05-20 14:01:55 +00:00
END_EVENT_TABLE ( )
1998-07-25 08:31:39 +00:00
void MyFrame : : ToggleEditable ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
grid - > SetEditable ( ! grid - > GetEditable ( ) ) ;
grid - > Refresh ( ) ;
1998-05-20 14:01:55 +00:00
}
1999-06-23 11:26:19 +00:00
void MyFrame : : ToggleEditInPlace ( wxCommandEvent & WXUNUSED ( event ) )
{
2002-03-17 14:16:03 +00:00
grid - > SetEditInPlace ( ! grid - > GetEditInPlace ( ) ) ;
grid - > Refresh ( ) ;
1999-06-23 11:26:19 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : ToggleRowLabel ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
if ( grid - > GetLabelSize ( wxVERTICAL ) > 0 )
1998-05-20 14:01:55 +00:00
grid - > SetLabelSize ( wxVERTICAL , 0 ) ;
2002-03-17 14:16:03 +00:00
else
1998-05-20 14:01:55 +00:00
grid - > SetLabelSize ( wxVERTICAL , 40 ) ;
2002-03-17 14:16:03 +00:00
grid - > Refresh ( ) ;
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : ToggleColLabel ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
if ( grid - > GetLabelSize ( wxHORIZONTAL ) > 0 )
1998-05-20 14:01:55 +00:00
grid - > SetLabelSize ( wxHORIZONTAL , 0 ) ;
2002-03-17 14:16:03 +00:00
else
1998-05-20 14:01:55 +00:00
grid - > SetLabelSize ( wxHORIZONTAL , 20 ) ;
2002-03-17 14:16:03 +00:00
1998-05-20 14:01:55 +00:00
grid - > Refresh ( ) ;
}
1998-07-25 08:31:39 +00:00
void MyFrame : : ToggleDividers ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
if ( ! grid - > GetDividerPen ( ) . Ok ( ) )
2001-11-22 09:11:33 +00:00
grid - > SetDividerPen ( wxPen ( wxT ( " LIGHT GREY " ) , 1 , wxSOLID ) ) ;
2002-03-17 14:16:03 +00:00
else
1998-12-08 23:26:18 +00:00
grid - > SetDividerPen ( wxNullPen ) ;
2002-03-17 14:16:03 +00:00
2003-01-04 11:04:39 +00:00
grid - > Refresh ( ) ;
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : LeftCell ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
grid - > SetCellAlignment ( wxLEFT ) ;
grid - > Refresh ( ) ;
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : CentreCell ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
grid - > SetCellAlignment ( wxCENTRE ) ;
grid - > Refresh ( ) ;
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : RightCell ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
grid - > SetCellAlignment ( wxRIGHT ) ;
grid - > Refresh ( ) ;
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : ColourLabelBackground ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
wxColourData data ;
data . SetChooseFull ( TRUE ) ;
wxColourDialog dialog ( this , & data ) ;
if ( dialog . ShowModal ( ) ! = wxID_CANCEL )
{
1998-05-20 14:01:55 +00:00
wxColourData retData = dialog . GetColourData ( ) ;
wxColour col = retData . GetColour ( ) ;
grid - > SetLabelBackgroundColour ( col ) ;
grid - > Refresh ( ) ;
2002-03-17 14:16:03 +00:00
}
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : ColourLabelText ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
wxColourData data ;
data . SetChooseFull ( TRUE ) ;
wxColourDialog dialog ( this , & data ) ;
if ( dialog . ShowModal ( ) ! = wxID_CANCEL )
{
1998-05-20 14:01:55 +00:00
wxColourData retData = dialog . GetColourData ( ) ;
wxColour col = retData . GetColour ( ) ;
grid - > SetLabelTextColour ( col ) ;
grid - > Refresh ( ) ;
2002-03-17 14:16:03 +00:00
}
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : NormalLabelColouring ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
grid - > SetLabelBackgroundColour ( * wxLIGHT_GREY ) ;
grid - > SetLabelTextColour ( * wxBLACK ) ;
grid - > Refresh ( ) ;
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : ColourCellBackground ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
wxColourData data ;
data . SetChooseFull ( TRUE ) ;
wxColourDialog dialog ( this , & data ) ;
if ( dialog . ShowModal ( ) ! = wxID_CANCEL )
{
1998-05-20 14:01:55 +00:00
wxColourData retData = dialog . GetColourData ( ) ;
wxColour col = retData . GetColour ( ) ;
grid - > SetCellBackgroundColour ( col ) ;
grid - > Refresh ( ) ;
2002-03-17 14:16:03 +00:00
}
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : ColourCellText ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
wxColourData data ;
data . SetChooseFull ( TRUE ) ;
wxColourDialog dialog ( this , & data ) ;
if ( dialog . ShowModal ( ) ! = wxID_CANCEL )
{
1998-05-20 14:01:55 +00:00
wxColourData retData = dialog . GetColourData ( ) ;
wxColour col = retData . GetColour ( ) ;
grid - > SetCellTextColour ( col ) ;
grid - > Refresh ( ) ;
2002-03-17 14:16:03 +00:00
}
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : NormalCellColouring ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
grid - > SetCellBackgroundColour ( * wxWHITE ) ;
grid - > SetCellTextColour ( * wxBLACK ) ;
grid - > Refresh ( ) ;
1998-05-20 14:01:55 +00:00
}
1998-07-25 08:31:39 +00:00
void MyFrame : : Quit ( wxCommandEvent & WXUNUSED ( event ) )
1998-05-20 14:01:55 +00:00
{
2002-03-17 14:16:03 +00:00
this - > Close ( TRUE ) ;
1998-05-20 14:01:55 +00:00
}
// Ensure that the grid's edit control always has the focus.
void MyFrame : : OnActivate ( wxActivateEvent & event )
{
2002-03-17 14:16:03 +00:00
if ( grid ) grid - > OnActivate ( event . GetActive ( ) ) ;
1998-05-20 14:01:55 +00:00
}