1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWindows sample
4 // Author: Michael Bedward
7 // Copyright: (c) Michael Bedward, Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/colordlg.h"
33 IMPLEMENT_APP( GridApp
)
37 bool GridApp::OnInit()
39 GridFrame
*frame
= new GridFrame
;
47 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
48 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
49 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
50 EVT_MENU( ID_TOGGLECONTROLPANEL
, GridFrame::ToggleControlPanel
)
51 EVT_MENU( ID_TOGGLECELLEDIT
, GridFrame::ToggleCellEdit
)
52 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
53 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
54 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
55 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
56 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
57 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
58 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
59 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
60 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
61 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteRow
)
62 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteCol
)
63 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
64 EVT_MENU( ID_ABOUT
, GridFrame::About
)
65 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
67 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
68 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
69 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
70 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
71 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
72 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
76 GridFrame::GridFrame()
77 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
81 int gridW
= 600, gridH
= 300;
82 int logW
= gridW
, logH
= 80;
84 wxMenu
*fileMenu
= new wxMenu
;
85 fileMenu
->Append( wxID_EXIT
, "E&xit" );
87 wxMenu
*viewMenu
= new wxMenu
;
88 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
89 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
90 viewMenu
->Append( ID_TOGGLECONTROLPANEL
, "To&p controls", "", TRUE
);
91 viewMenu
->Append( ID_TOGGLECELLEDIT
, "&In-place editing", "", TRUE
);
92 viewMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
93 viewMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
95 wxMenu
*rowLabelMenu
= new wxMenu
;
97 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
99 "Change alignment of row labels" );
101 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
102 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
104 wxMenu
*colLabelMenu
= new wxMenu
;
106 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
108 "Change alignment of col labels" );
110 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
111 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
113 viewMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
115 wxMenu
*editMenu
= new wxMenu
;
116 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
117 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
118 editMenu
->Append( ID_DELETEROW
, "Delete ro&w" );
119 editMenu
->Append( ID_DELETECOL
, "Delete co&l" );
120 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
122 wxMenu
*helpMenu
= new wxMenu
;
123 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
125 wxMenuBar
*menuBar
= new wxMenuBar
;
126 menuBar
->Append( fileMenu
, "&File" );
127 menuBar
->Append( viewMenu
, "&View" );
128 menuBar
->Append( editMenu
, "&Edit" );
129 menuBar
->Append( helpMenu
, "&Help" );
131 SetMenuBar( menuBar
);
133 grid
= new wxGrid( this,
136 wxSize( 400, 300 ) );
138 logWin
= new wxTextCtrl( this,
141 wxPoint( 0, gridH
+ 20 ),
142 wxSize( logW
, logH
),
145 logger
= new wxLogTextCtrl( logWin
);
146 logger
->SetActiveTarget( logger
);
147 logger
->SetTimestamp( NULL
);
149 // this will create a grid and, by default, an associated grid
150 // table for string data
152 grid
->CreateGrid( 100, 100 );
154 grid
->EnableTopEditControl( TRUE
);
156 grid
->SetRowSize( 0, 60 );
157 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
159 grid
->SetCellValue( 0, 1, "Blah" );
160 grid
->SetCellValue( 0, 2, "Blah" );
162 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
164 grid
->SetRowSize( 99, 60 );
165 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
167 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
172 topSizer
->Add( logWin
,
176 SetAutoLayout( TRUE
);
177 SetSizer( topSizer
);
179 topSizer
->Fit( this );
180 topSizer
->SetSizeHints( this );
187 GridFrame::~GridFrame()
191 void GridFrame::SetDefaults()
193 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
194 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
195 GetMenuBar()->Check( ID_TOGGLECONTROLPANEL
, TRUE
);
196 GetMenuBar()->Check( ID_TOGGLECELLEDIT
, TRUE
);
200 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
202 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
204 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
208 grid
->SetRowLabelSize( 0 );
213 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
215 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
217 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
221 grid
->SetColLabelSize( 0 );
226 void GridFrame::ToggleControlPanel( wxCommandEvent
& WXUNUSED(ev
) )
228 grid
->EnableTopEditControl(GetMenuBar()->IsChecked(ID_TOGGLECONTROLPANEL
));
232 void GridFrame::ToggleCellEdit( wxCommandEvent
& WXUNUSED(ev
) )
234 grid
->EnableCellEditControl(
235 GetMenuBar()->IsChecked( ID_TOGGLECELLEDIT
) );
239 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
241 wxColourDialog
dlg( NULL
);
242 if ( dlg
.ShowModal() == wxID_OK
)
244 wxColourData retData
;
245 retData
= dlg
.GetColourData();
246 wxColour colour
= retData
.GetColour();
248 grid
->SetLabelBackgroundColour( colour
);
253 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
255 wxColourDialog
dlg( NULL
);
256 if ( dlg
.ShowModal() == wxID_OK
)
258 wxColourData retData
;
259 retData
= dlg
.GetColourData();
260 wxColour colour
= retData
.GetColour();
262 grid
->SetLabelTextColour( colour
);
267 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
270 grid
->GetRowLabelAlignment( &horiz
, &vert
);
287 grid
->SetRowLabelAlignment( horiz
, -1 );
290 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
293 grid
->GetRowLabelAlignment( &horiz
, &vert
);
310 grid
->SetRowLabelAlignment( -1, vert
);
314 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
317 grid
->GetColLabelAlignment( &horiz
, &vert
);
334 grid
->SetColLabelAlignment( horiz
, -1 );
338 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
341 grid
->GetColLabelAlignment( &horiz
, &vert
);
358 grid
->SetColLabelAlignment( -1, vert
);
362 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
364 wxColourDialog
dlg( NULL
);
365 if ( dlg
.ShowModal() == wxID_OK
)
367 wxColourData retData
;
368 retData
= dlg
.GetColourData();
369 wxColour colour
= retData
.GetColour();
371 grid
->SetGridLineColour( colour
);
376 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
378 grid
->InsertRows( 0, 1 );
382 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
384 grid
->InsertCols( 0, 1 );
388 void GridFrame::DeleteRow( wxCommandEvent
& WXUNUSED(ev
) )
390 grid
->DeleteRows( 0, 1 );
394 void GridFrame::DeleteCol( wxCommandEvent
& WXUNUSED(ev
) )
396 grid
->DeleteCols( 0, 1 );
400 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
406 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
408 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
410 "mbedward@ozemail.com.au \n\n",
416 void GridFrame::OnSize( wxSizeEvent
& WXUNUSED(ev
) )
418 if ( grid
&& logWin
)
421 GetClientSize( &cw
, &ch
);
430 grid
->SetSize( 0, 0, cw
, gridH
);
431 logWin
->SetSize( 0, gridH
+ 10, cw
, logH
);
435 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
441 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
444 if ( ev
.GetRow() != -1 )
446 logBuf
<< "row label " << ev
.GetRow();
448 else if ( ev
.GetCol() != -1 )
450 logBuf
<< "col label " << ev
.GetCol();
454 logBuf
<< "corner label";
457 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
458 wxLogMessage( "%s", logBuf
.c_str() );
464 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
467 logBuf
<< "Cell at row " << ev
.GetRow()
468 << " col " << ev
.GetCol();
469 wxLogMessage( "%s", logBuf
.c_str() );
471 // you must call event skip if you want default grid processing
472 // (cell highlighting etc.)
478 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
481 logBuf
<< "Resized row " << ev
.GetRowOrCol();
482 wxLogMessage( "%s", logBuf
.c_str() );
488 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
491 logBuf
<< "Resized col " << ev
.GetRowOrCol();
492 wxLogMessage( "%s", logBuf
.c_str() );
497 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
500 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
501 << " col " << ev
.GetLeftCol()
502 << " to row " << ev
.GetBottomRow()
503 << " col " << ev
.GetRightCol();
505 wxLogMessage( "%s", logBuf
.c_str() );
510 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
513 logBuf
<< "Value changed for cell at"
514 << " row " << ev
.GetRow()
515 << " col " << ev
.GetCol();
517 wxLogMessage( "%s", logBuf
.c_str() );