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_TOGGLEEDIT
, GridFrame::ToggleEditing
)
51 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
52 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
53 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
54 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
55 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
56 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
57 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
58 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
59 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
60 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
61 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
62 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_SELECT_CELL( GridFrame::OnSelectCell
)
72 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
73 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
78 GridFrame::GridFrame()
79 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
83 int gridW
= 600, gridH
= 300;
84 int logW
= gridW
, logH
= 80;
86 wxMenu
*fileMenu
= new wxMenu
;
87 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
89 wxMenu
*viewMenu
= new wxMenu
;
90 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
91 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
92 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
93 viewMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
94 viewMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
96 wxMenu
*rowLabelMenu
= new wxMenu
;
98 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
100 "Change alignment of row labels" );
102 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
103 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
105 wxMenu
*colLabelMenu
= new wxMenu
;
107 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
109 "Change alignment of col labels" );
111 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
112 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
114 viewMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
116 wxMenu
*editMenu
= new wxMenu
;
117 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
118 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
119 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
120 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
121 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
123 wxMenu
*helpMenu
= new wxMenu
;
124 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
126 wxMenuBar
*menuBar
= new wxMenuBar
;
127 menuBar
->Append( fileMenu
, "&File" );
128 menuBar
->Append( viewMenu
, "&View" );
129 menuBar
->Append( editMenu
, "&Edit" );
130 menuBar
->Append( helpMenu
, "&Help" );
132 SetMenuBar( menuBar
);
134 grid
= new wxGrid( this,
137 wxSize( 400, 300 ) );
139 logWin
= new wxTextCtrl( this,
142 wxPoint( 0, gridH
+ 20 ),
143 wxSize( logW
, logH
),
146 logger
= new wxLogTextCtrl( logWin
);
147 logger
->SetActiveTarget( logger
);
148 logger
->SetTimestamp( NULL
);
150 // this will create a grid and, by default, an associated grid
151 // table for string data
153 grid
->CreateGrid( 100, 100 );
155 grid
->SetRowSize( 0, 60 );
156 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
158 grid
->SetCellValue( 0, 1, "Blah" );
159 grid
->SetCellValue( 0, 2, "Blah" );
161 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
163 grid
->SetRowSize( 99, 60 );
164 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
166 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
171 topSizer
->Add( logWin
,
175 SetAutoLayout( TRUE
);
176 SetSizer( topSizer
);
178 topSizer
->Fit( this );
179 topSizer
->SetSizeHints( this );
186 GridFrame::~GridFrame()
191 void GridFrame::SetDefaults()
193 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
194 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
195 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
199 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
201 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
203 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
207 grid
->SetRowLabelSize( 0 );
212 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
214 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
216 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
220 grid
->SetColLabelSize( 0 );
225 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
228 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
232 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
234 wxColourDialog
dlg( NULL
);
235 if ( dlg
.ShowModal() == wxID_OK
)
237 wxColourData retData
;
238 retData
= dlg
.GetColourData();
239 wxColour colour
= retData
.GetColour();
241 grid
->SetLabelBackgroundColour( colour
);
246 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
248 wxColourDialog
dlg( NULL
);
249 if ( dlg
.ShowModal() == wxID_OK
)
251 wxColourData retData
;
252 retData
= dlg
.GetColourData();
253 wxColour colour
= retData
.GetColour();
255 grid
->SetLabelTextColour( colour
);
260 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
263 grid
->GetRowLabelAlignment( &horiz
, &vert
);
280 grid
->SetRowLabelAlignment( horiz
, -1 );
283 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
286 grid
->GetRowLabelAlignment( &horiz
, &vert
);
303 grid
->SetRowLabelAlignment( -1, vert
);
307 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
310 grid
->GetColLabelAlignment( &horiz
, &vert
);
327 grid
->SetColLabelAlignment( horiz
, -1 );
331 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
334 grid
->GetColLabelAlignment( &horiz
, &vert
);
351 grid
->SetColLabelAlignment( -1, vert
);
355 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
357 wxColourDialog
dlg( NULL
);
358 if ( dlg
.ShowModal() == wxID_OK
)
360 wxColourData retData
;
361 retData
= dlg
.GetColourData();
362 wxColour colour
= retData
.GetColour();
364 grid
->SetGridLineColour( colour
);
369 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
371 grid
->InsertRows( 0, 1 );
375 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
377 grid
->InsertCols( 0, 1 );
381 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
383 if ( grid
->IsSelection() )
385 int topRow
, bottomRow
, leftCol
, rightCol
;
386 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
387 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
392 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
394 if ( grid
->IsSelection() )
396 int topRow
, bottomRow
, leftCol
, rightCol
;
397 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
398 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
403 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
409 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
412 if ( ev
.GetRow() != -1 )
414 logBuf
<< "Left click on row label " << ev
.GetRow();
416 else if ( ev
.GetCol() != -1 )
418 logBuf
<< "Left click on col label " << ev
.GetCol();
422 logBuf
<< "Left click on corner label";
425 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
426 wxLogMessage( "%s", logBuf
.c_str() );
428 // you must call event skip if you want default grid processing
434 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
437 logBuf
<< "Left click at row " << ev
.GetRow()
438 << " col " << ev
.GetCol();
439 wxLogMessage( "%s", logBuf
.c_str() );
441 // you must call event skip if you want default grid processing
442 // (cell highlighting etc.)
448 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
451 logBuf
<< "Resized row " << ev
.GetRowOrCol();
452 wxLogMessage( "%s", logBuf
.c_str() );
458 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
461 logBuf
<< "Resized col " << ev
.GetRowOrCol();
462 wxLogMessage( "%s", logBuf
.c_str() );
468 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
471 logBuf
<< "Selected cell at row " << ev
.GetRow()
472 << " col " << ev
.GetCol();
473 wxLogMessage( "%s", logBuf
.c_str() );
475 // you must call Skip() if you want the default processing
476 // to occur in wxGrid
480 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
483 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
484 << " col " << ev
.GetLeftCol()
485 << " to row " << ev
.GetBottomRow()
486 << " col " << ev
.GetRightCol();
488 wxLogMessage( "%s", logBuf
.c_str() );
493 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
496 logBuf
<< "Value changed for cell at"
497 << " row " << ev
.GetRow()
498 << " col " << ev
.GetCol();
500 wxLogMessage( "%s", logBuf
.c_str() );
506 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
508 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
510 "mbedward@ozemail.com.au \n\n",
516 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )