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_CLEARGRID
, GridFrame::ClearGrid
)
60 EVT_MENU( ID_ABOUT
, GridFrame::About
)
61 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
63 EVT_WXGRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
64 EVT_WXGRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
65 EVT_WXGRID_ROW_SIZE( GridFrame::OnRowSize
)
66 EVT_WXGRID_COL_SIZE( GridFrame::OnColSize
)
67 EVT_WXGRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
68 EVT_WXGRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
72 GridFrame::GridFrame()
73 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
77 int gridW
= 600, gridH
= 300;
78 int logW
= gridW
, logH
= 80;
80 wxMenu
*fileMenu
= new wxMenu
;
81 fileMenu
->Append( wxID_EXIT
, "E&xit" );
83 wxMenu
*viewMenu
= new wxMenu
;
84 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
85 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
86 viewMenu
->Append( ID_TOGGLECONTROLPANEL
, "To&p controls", "", TRUE
);
87 viewMenu
->Append( ID_TOGGLECELLEDIT
, "&In-place editing", "", TRUE
);
88 viewMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
89 viewMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
91 wxMenu
*rowLabelMenu
= new wxMenu
;
93 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
95 "Change alignment of row labels" );
97 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
98 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
100 wxMenu
*colLabelMenu
= new wxMenu
;
102 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
104 "Change alignment of col labels" );
106 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
107 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
109 viewMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
110 viewMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
112 wxMenu
*helpMenu
= new wxMenu
;
113 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
115 wxMenuBar
*menuBar
= new wxMenuBar
;
116 menuBar
->Append( fileMenu
, "&File" );
117 menuBar
->Append( viewMenu
, "&View" );
118 menuBar
->Append( helpMenu
, "&Help" );
120 SetMenuBar( menuBar
);
122 grid
= new wxGrid( this,
125 wxSize( 400, 300 ) );
127 logWin
= new wxTextCtrl( this,
130 wxPoint( 0, gridH
+ 20 ),
131 wxSize( logW
, logH
),
134 logger
= new wxLogTextCtrl( logWin
);
135 logger
->SetActiveTarget( logger
);
136 logger
->SetTimestamp( NULL
);
138 // this will create a grid and, by default, an associated grid
139 // table for string data
141 grid
->CreateGrid( 100, 100 );
143 grid
->EnableTopEditControl( TRUE
);
145 grid
->SetRowSize( 0, 60 );
146 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
148 grid
->SetCellValue( 0, 1, "Blah" );
149 grid
->SetCellValue( 0, 2, "Blah" );
151 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
153 grid
->SetRowSize( 99, 60 );
154 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
156 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
161 topSizer
->Add( logWin
,
165 SetAutoLayout( TRUE
);
166 SetSizer( topSizer
);
168 topSizer
->Fit( this );
169 topSizer
->SetSizeHints( this );
176 GridFrame::~GridFrame()
180 void GridFrame::SetDefaults()
182 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
183 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
184 GetMenuBar()->Check( ID_TOGGLECONTROLPANEL
, TRUE
);
185 GetMenuBar()->Check( ID_TOGGLECELLEDIT
, TRUE
);
189 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
191 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
193 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
197 grid
->SetRowLabelSize( 0 );
202 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
204 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
206 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
210 grid
->SetColLabelSize( 0 );
215 void GridFrame::ToggleControlPanel( wxCommandEvent
& WXUNUSED(ev
) )
217 grid
->EnableTopEditControl(GetMenuBar()->IsChecked(ID_TOGGLECONTROLPANEL
));
221 void GridFrame::ToggleCellEdit( wxCommandEvent
& WXUNUSED(ev
) )
223 grid
->EnableCellEditControl(
224 GetMenuBar()->IsChecked( ID_TOGGLECELLEDIT
) );
228 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
230 wxColourDialog
dlg( NULL
);
231 if ( dlg
.ShowModal() == wxID_OK
)
233 wxColourData retData
;
234 retData
= dlg
.GetColourData();
235 wxColour colour
= retData
.GetColour();
237 grid
->SetLabelBackgroundColour( colour
);
242 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
244 wxColourDialog
dlg( NULL
);
245 if ( dlg
.ShowModal() == wxID_OK
)
247 wxColourData retData
;
248 retData
= dlg
.GetColourData();
249 wxColour colour
= retData
.GetColour();
251 grid
->SetLabelTextColour( colour
);
256 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
259 grid
->GetRowLabelAlignment( &horiz
, &vert
);
276 grid
->SetRowLabelAlignment( horiz
, -1 );
279 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
282 grid
->GetRowLabelAlignment( &horiz
, &vert
);
299 grid
->SetRowLabelAlignment( -1, vert
);
303 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
306 grid
->GetColLabelAlignment( &horiz
, &vert
);
323 grid
->SetColLabelAlignment( horiz
, -1 );
327 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
330 grid
->GetColLabelAlignment( &horiz
, &vert
);
347 grid
->SetColLabelAlignment( -1, vert
);
351 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
353 wxColourDialog
dlg( NULL
);
354 if ( dlg
.ShowModal() == wxID_OK
)
356 wxColourData retData
;
357 retData
= dlg
.GetColourData();
358 wxColour colour
= retData
.GetColour();
360 grid
->SetGridLineColour( colour
);
365 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
371 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
373 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
375 "mbedward@ozemail.com.au \n\n",
381 void GridFrame::OnSize( wxSizeEvent
& WXUNUSED(ev
) )
383 if ( grid
&& logWin
)
386 GetClientSize( &cw
, &ch
);
395 grid
->SetSize( 0, 0, cw
, gridH
);
396 logWin
->SetSize( 0, gridH
+ 10, cw
, logH
);
400 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
406 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
409 if ( ev
.GetRow() != -1 )
411 logBuf
<< "row label " << ev
.GetRow();
413 else if ( ev
.GetCol() != -1 )
415 logBuf
<< "col label " << ev
.GetCol();
419 logBuf
<< "corner label";
422 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
423 wxLogMessage( "%s", logBuf
.c_str() );
429 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
432 logBuf
<< "Cell at row " << ev
.GetRow()
433 << " col " << ev
.GetCol();
434 wxLogMessage( "%s", logBuf
.c_str() );
436 // you must call event skip if you want default grid processing
437 // (cell highlighting etc.)
443 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
446 logBuf
<< "Resized row " << ev
.GetRowOrCol();
447 wxLogMessage( "%s", logBuf
.c_str() );
453 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
456 logBuf
<< "Resized col " << ev
.GetRowOrCol();
457 wxLogMessage( "%s", logBuf
.c_str() );
462 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
465 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
466 << " col " << ev
.GetLeftCol()
467 << " to row " << ev
.GetBottomRow()
468 << " col " << ev
.GetRightCol();
470 wxLogMessage( "%s", logBuf
.c_str() );
475 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
478 logBuf
<< "Value changed for cell at"
479 << " row " << ev
.GetRow()
480 << " col " << ev
.GetCol();
482 wxLogMessage( "%s", logBuf
.c_str() );