]> git.saurik.com Git - wxWidgets.git/blame - samples/newgrid/griddemo.cpp
cosemtic fixes
[wxWidgets.git] / samples / newgrid / griddemo.cpp
CommitLineData
10434f3c
MB
1/////////////////////////////////////////////////////////////////////////////
2// Name: griddemo.cpp
3// Purpose: Grid control wxWindows sample
4// Author: Michael Bedward
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Michael Bedward, Julian Smart
8// Licence: wxWindows license
9/////////////////////////////////////////////////////////////////////////////
e442cc0d
MB
10
11#ifdef __GNUG__
12#pragma implementation
13#pragma interface
14#endif
15
16// For compilers that support precompilation, includes "wx/wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/wx.h"
25#endif
26
27#include "wx/colordlg.h"
28
29#include "wx/grid.h"
30
31#include "griddemo.h"
32
33IMPLEMENT_APP( GridApp )
34
35
36
37bool GridApp::OnInit()
38{
39 GridFrame *frame = new GridFrame;
40 frame->Show( TRUE );
f0102d2a 41
e442cc0d
MB
42 return TRUE;
43}
44
45
46
47BEGIN_EVENT_TABLE( GridFrame, wxFrame )
48 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
f0102d2a 49 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
f445b853 50 EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
f0102d2a
VZ
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 )
e442cc0d 57 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
0eee5283
MB
58 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
59 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
f445b853
MB
60 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
61 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
e442cc0d 62 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
f0102d2a 63
e442cc0d
MB
64 EVT_MENU( ID_ABOUT, GridFrame::About )
65 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
66
5795d034
MB
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 )
c336585e 71 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
5795d034
MB
72 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
73 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
f0102d2a 74
e442cc0d
MB
75END_EVENT_TABLE()
76
f0102d2a 77
e442cc0d
MB
78GridFrame::GridFrame()
79 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
80 wxDefaultPosition,
81 wxDefaultSize )
82{
83 int gridW = 600, gridH = 300;
84 int logW = gridW, logH = 80;
f0102d2a 85
e442cc0d 86 wxMenu *fileMenu = new wxMenu;
f0102d2a
VZ
87 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
88
e442cc0d
MB
89 wxMenu *viewMenu = new wxMenu;
90 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
91 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
f445b853 92 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
e442cc0d
MB
93 viewMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
94 viewMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
f0102d2a 95
e442cc0d
MB
96 wxMenu *rowLabelMenu = new wxMenu;
97
98 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
99 rowLabelMenu,
100 "Change alignment of row labels" );
101
102 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
f0102d2a
VZ
103 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
104
e442cc0d
MB
105 wxMenu *colLabelMenu = new wxMenu;
106
107 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
108 colLabelMenu,
109 "Change alignment of col labels" );
110
111 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
112 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
113
114 viewMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
e442cc0d 115
0eee5283
MB
116 wxMenu *editMenu = new wxMenu;
117 editMenu->Append( ID_INSERTROW, "Insert &row" );
118 editMenu->Append( ID_INSERTCOL, "Insert &column" );
f445b853
MB
119 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
120 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
0eee5283 121 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
f0102d2a 122
e442cc0d
MB
123 wxMenu *helpMenu = new wxMenu;
124 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
f0102d2a 125
e442cc0d
MB
126 wxMenuBar *menuBar = new wxMenuBar;
127 menuBar->Append( fileMenu, "&File" );
128 menuBar->Append( viewMenu, "&View" );
0eee5283 129 menuBar->Append( editMenu, "&Edit" );
e442cc0d
MB
130 menuBar->Append( helpMenu, "&Help" );
131
132 SetMenuBar( menuBar );
133
134 grid = new wxGrid( this,
135 -1,
136 wxPoint( 0, 0 ),
137 wxSize( 400, 300 ) );
f0102d2a 138
e442cc0d
MB
139 logWin = new wxTextCtrl( this,
140 -1,
141 wxEmptyString,
142 wxPoint( 0, gridH + 20 ),
143 wxSize( logW, logH ),
144 wxTE_MULTILINE );
f0102d2a 145
e442cc0d
MB
146 logger = new wxLogTextCtrl( logWin );
147 logger->SetActiveTarget( logger );
148 logger->SetTimestamp( NULL );
149
150 // this will create a grid and, by default, an associated grid
151 // table for string data
152 //
153 grid->CreateGrid( 100, 100 );
154
e442cc0d
MB
155 grid->SetRowSize( 0, 60 );
156 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
f0102d2a 157
e442cc0d
MB
158 grid->SetCellValue( 0, 1, "Blah" );
159 grid->SetCellValue( 0, 2, "Blah" );
160
161 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
162
163 grid->SetRowSize( 99, 60 );
164 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
f445b853 165
e442cc0d
MB
166 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
167 topSizer->Add( grid,
168 1,
169 wxEXPAND );
f0102d2a 170
e442cc0d 171 topSizer->Add( logWin,
f0102d2a 172 0,
e442cc0d 173 wxEXPAND );
f0102d2a 174
e442cc0d
MB
175 SetAutoLayout( TRUE );
176 SetSizer( topSizer );
f0102d2a 177
e442cc0d
MB
178 topSizer->Fit( this );
179 topSizer->SetSizeHints( this );
f0102d2a 180
e442cc0d
MB
181 Centre();
182 SetDefaults();
183}
184
185
186GridFrame::~GridFrame()
187{
188}
189
f445b853 190
e442cc0d
MB
191void GridFrame::SetDefaults()
192{
193 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
194 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
f445b853 195 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
e442cc0d
MB
196}
197
198
199void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
200{
201 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
202 {
203 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
204 }
205 else
206 {
207 grid->SetRowLabelSize( 0 );
208 }
209}
210
211
212void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
213{
214 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
215 {
216 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
217 }
218 else
219 {
220 grid->SetColLabelSize( 0 );
221 }
222}
223
224
f445b853 225void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
e442cc0d 226{
f445b853
MB
227 grid->EnableEditing(
228 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
e442cc0d
MB
229}
230
231
232void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
233{
234 wxColourDialog dlg( NULL );
235 if ( dlg.ShowModal() == wxID_OK )
236 {
237 wxColourData retData;
238 retData = dlg.GetColourData();
239 wxColour colour = retData.GetColour();
240
241 grid->SetLabelBackgroundColour( colour );
242 }
243}
244
245
246void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
247{
248 wxColourDialog dlg( NULL );
249 if ( dlg.ShowModal() == wxID_OK )
250 {
251 wxColourData retData;
252 retData = dlg.GetColourData();
253 wxColour colour = retData.GetColour();
254
255 grid->SetLabelTextColour( colour );
256 }
257}
258
259
260void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
261{
262 int horiz, vert;
263 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 264
e442cc0d
MB
265 switch ( horiz )
266 {
267 case wxLEFT:
268 horiz = wxCENTRE;
269 break;
f0102d2a 270
e442cc0d
MB
271 case wxCENTRE:
272 horiz = wxRIGHT;
273 break;
274
275 case wxRIGHT:
276 horiz = wxLEFT;
277 break;
278 }
279
280 grid->SetRowLabelAlignment( horiz, -1 );
281}
282
283void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
284{
285 int horiz, vert;
286 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 287
e442cc0d
MB
288 switch ( vert )
289 {
290 case wxTOP:
291 vert = wxCENTRE;
292 break;
f0102d2a 293
e442cc0d
MB
294 case wxCENTRE:
295 vert = wxBOTTOM;
296 break;
297
298 case wxBOTTOM:
299 vert = wxTOP;
300 break;
301 }
302
303 grid->SetRowLabelAlignment( -1, vert );
304}
305
306
307void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
308{
309 int horiz, vert;
310 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 311
e442cc0d
MB
312 switch ( horiz )
313 {
314 case wxLEFT:
315 horiz = wxCENTRE;
316 break;
f0102d2a 317
e442cc0d
MB
318 case wxCENTRE:
319 horiz = wxRIGHT;
320 break;
321
322 case wxRIGHT:
323 horiz = wxLEFT;
324 break;
325 }
326
327 grid->SetColLabelAlignment( horiz, -1 );
328}
329
330
331void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
332{
333 int horiz, vert;
334 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 335
e442cc0d
MB
336 switch ( vert )
337 {
338 case wxTOP:
339 vert = wxCENTRE;
340 break;
f0102d2a 341
e442cc0d
MB
342 case wxCENTRE:
343 vert = wxBOTTOM;
344 break;
345
346 case wxBOTTOM:
347 vert = wxTOP;
348 break;
349 }
350
351 grid->SetColLabelAlignment( -1, vert );
352}
353
354
355void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
356{
357 wxColourDialog dlg( NULL );
358 if ( dlg.ShowModal() == wxID_OK )
359 {
360 wxColourData retData;
361 retData = dlg.GetColourData();
362 wxColour colour = retData.GetColour();
363
364 grid->SetGridLineColour( colour );
365 }
366}
367
368
0eee5283
MB
369void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
370{
371 grid->InsertRows( 0, 1 );
372}
373
374
375void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
376{
377 grid->InsertCols( 0, 1 );
378}
379
380
f445b853 381void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
0eee5283 382{
f445b853
MB
383 if ( grid->IsSelection() )
384 {
385 int topRow, bottomRow, leftCol, rightCol;
386 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
387 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
388 }
0eee5283
MB
389}
390
391
f445b853 392void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
0eee5283 393{
f445b853
MB
394 if ( grid->IsSelection() )
395 {
396 int topRow, bottomRow, leftCol, rightCol;
397 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
398 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
399 }
0eee5283
MB
400}
401
402
e442cc0d
MB
403void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
404{
405 grid->ClearGrid();
406}
407
408
e442cc0d
MB
409void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
410{
411 logBuf = "";
412 if ( ev.GetRow() != -1 )
413 {
c336585e 414 logBuf << "Left click on row label " << ev.GetRow();
e442cc0d
MB
415 }
416 else if ( ev.GetCol() != -1 )
417 {
c336585e 418 logBuf << "Left click on col label " << ev.GetCol();
e442cc0d
MB
419 }
420 else
421 {
c336585e 422 logBuf << "Left click on corner label";
e442cc0d
MB
423 }
424
425 if ( ev.ShiftDown() ) logBuf << " (shift down)";
426 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 427
c336585e
MB
428 // you must call event skip if you want default grid processing
429 //
e442cc0d
MB
430 ev.Skip();
431}
432
433
434void GridFrame::OnCellLeftClick( wxGridEvent& ev )
435{
436 logBuf = "";
c336585e 437 logBuf << "Left click at row " << ev.GetRow()
e442cc0d
MB
438 << " col " << ev.GetCol();
439 wxLogMessage( "%s", logBuf.c_str() );
0eee5283 440
e442cc0d
MB
441 // you must call event skip if you want default grid processing
442 // (cell highlighting etc.)
443 //
444 ev.Skip();
445}
446
447
448void GridFrame::OnRowSize( wxGridSizeEvent& ev )
449{
450 logBuf = "";
451 logBuf << "Resized row " << ev.GetRowOrCol();
452 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 453
e442cc0d
MB
454 ev.Skip();
455}
456
457
458void GridFrame::OnColSize( wxGridSizeEvent& ev )
459{
460 logBuf = "";
461 logBuf << "Resized col " << ev.GetRowOrCol();
462 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 463
e442cc0d
MB
464 ev.Skip();
465}
466
c336585e
MB
467
468void GridFrame::OnSelectCell( wxGridEvent& ev )
469{
470 logBuf = "";
471 logBuf << "Selected cell at row " << ev.GetRow()
472 << " col " << ev.GetCol();
473 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 474
c336585e
MB
475 // you must call Skip() if you want the default processing
476 // to occur in wxGrid
477 ev.Skip();
478}
479
e442cc0d
MB
480void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
481{
482 logBuf = "";
483 logBuf << "Selected cells from row " << ev.GetTopRow()
484 << " col " << ev.GetLeftCol()
485 << " to row " << ev.GetBottomRow()
486 << " col " << ev.GetRightCol();
f0102d2a 487
e442cc0d 488 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 489
e442cc0d
MB
490 ev.Skip();
491}
492
493void GridFrame::OnCellValueChanged( wxGridEvent& ev )
494{
495 logBuf = "";
496 logBuf << "Value changed for cell at"
f0102d2a 497 << " row " << ev.GetRow()
e442cc0d 498 << " col " << ev.GetCol();
f0102d2a 499
e442cc0d 500 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 501
e442cc0d
MB
502 ev.Skip();
503}
504
505
f445b853
MB
506void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
507{
508 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
509 "Michael Bedward \n"
510 "mbedward@ozemail.com.au \n\n",
511 "About",
512 wxOK );
513}
514
515
516void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
517{
518 Close( TRUE );
519}
520