]> git.saurik.com Git - wxWidgets.git/blame - samples/newgrid/griddemo.cpp
Added functions to enable/disable drag-resizing of rows and cols.
[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 10
ab79958a
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
e442cc0d 19#ifdef __GNUG__
ab79958a
VZ
20 #pragma implementation
21 #pragma interface
e442cc0d
MB
22#endif
23
24// For compilers that support precompilation, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28#pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32#include "wx/wx.h"
33#endif
34
35#include "wx/colordlg.h"
36
37#include "wx/grid.h"
38
39#include "griddemo.h"
40
ab79958a
VZ
41// ----------------------------------------------------------------------------
42// wxWin macros
43// ----------------------------------------------------------------------------
44
e442cc0d
MB
45IMPLEMENT_APP( GridApp )
46
ab79958a
VZ
47// ============================================================================
48// implementation
49// ============================================================================
e442cc0d 50
ab79958a
VZ
51// ----------------------------------------------------------------------------
52// GridApp
53// ----------------------------------------------------------------------------
e442cc0d
MB
54
55bool GridApp::OnInit()
56{
57 GridFrame *frame = new GridFrame;
58 frame->Show( TRUE );
f0102d2a 59
e442cc0d
MB
60 return TRUE;
61}
62
ab79958a
VZ
63// ----------------------------------------------------------------------------
64// GridFrame
65// ----------------------------------------------------------------------------
e442cc0d
MB
66
67BEGIN_EVENT_TABLE( GridFrame, wxFrame )
68 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
f0102d2a 69 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
f445b853 70 EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
f0102d2a
VZ
71 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
72 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
73 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
74 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
75 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
76 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
e442cc0d 77 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
0eee5283
MB
78 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
79 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
f445b853
MB
80 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
81 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
e442cc0d 82 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
f0102d2a 83
ab79958a
VZ
84 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
85 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
86
e442cc0d
MB
87 EVT_MENU( ID_ABOUT, GridFrame::About )
88 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
f97c9b5b 89 EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
35f97e95 90 EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
e442cc0d 91
5795d034
MB
92 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
93 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
94 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
95 EVT_GRID_COL_SIZE( GridFrame::OnColSize )
c336585e 96 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
5795d034
MB
97 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
98 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
b54ba671
VZ
99
100 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
101 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
e442cc0d
MB
102END_EVENT_TABLE()
103
f0102d2a 104
e442cc0d
MB
105GridFrame::GridFrame()
106 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
107 wxDefaultPosition,
108 wxDefaultSize )
109{
110 int gridW = 600, gridH = 300;
07296f0b 111 int logW = gridW, logH = 100;
f0102d2a 112
e442cc0d 113 wxMenu *fileMenu = new wxMenu;
43947979 114 fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V");
35f97e95 115 fileMenu->Append( ID_BUGS_TABLE, "&Bugs table test\tCtrl-B");
f97c9b5b 116 fileMenu->AppendSeparator();
f0102d2a
VZ
117 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
118
e442cc0d
MB
119 wxMenu *viewMenu = new wxMenu;
120 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
121 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
f445b853 122 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
f0102d2a 123
e442cc0d
MB
124 wxMenu *rowLabelMenu = new wxMenu;
125
126 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
127 rowLabelMenu,
128 "Change alignment of row labels" );
129
130 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
f0102d2a
VZ
131 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
132
e442cc0d
MB
133 wxMenu *colLabelMenu = new wxMenu;
134
135 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
136 colLabelMenu,
137 "Change alignment of col labels" );
138
139 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
140 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
141
ab79958a
VZ
142 wxMenu *colMenu = new wxMenu;
143 colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
144 colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
145 colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
146 colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour" );
147 colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour" );
e442cc0d 148
0eee5283
MB
149 wxMenu *editMenu = new wxMenu;
150 editMenu->Append( ID_INSERTROW, "Insert &row" );
151 editMenu->Append( ID_INSERTCOL, "Insert &column" );
f445b853
MB
152 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
153 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
0eee5283 154 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
f0102d2a 155
e442cc0d
MB
156 wxMenu *helpMenu = new wxMenu;
157 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
f0102d2a 158
e442cc0d
MB
159 wxMenuBar *menuBar = new wxMenuBar;
160 menuBar->Append( fileMenu, "&File" );
161 menuBar->Append( viewMenu, "&View" );
ab79958a 162 menuBar->Append( colMenu, "&Colours" );
0eee5283 163 menuBar->Append( editMenu, "&Edit" );
e442cc0d
MB
164 menuBar->Append( helpMenu, "&Help" );
165
166 SetMenuBar( menuBar );
167
168 grid = new wxGrid( this,
169 -1,
170 wxPoint( 0, 0 ),
171 wxSize( 400, 300 ) );
f0102d2a 172
e442cc0d
MB
173 logWin = new wxTextCtrl( this,
174 -1,
175 wxEmptyString,
176 wxPoint( 0, gridH + 20 ),
177 wxSize( logW, logH ),
178 wxTE_MULTILINE );
f0102d2a 179
e442cc0d
MB
180 logger = new wxLogTextCtrl( logWin );
181 logger->SetActiveTarget( logger );
182 logger->SetTimestamp( NULL );
183
184 // this will create a grid and, by default, an associated grid
185 // table for string data
186 //
f2d76237
RD
187 //grid->CreateGrid( 100, 100 );
188 grid->SetTable(new SimpleTable(100, 100), TRUE);
189
190 // VZ: cell borders don't look nice otherwise :-) (for now...)
191 grid->SetDefaultCellBackgroundColour(wxColour(200, 200, 180));
e442cc0d 192
e442cc0d
MB
193 grid->SetRowSize( 0, 60 );
194 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
f0102d2a 195
e442cc0d
MB
196 grid->SetCellValue( 0, 1, "Blah" );
197 grid->SetCellValue( 0, 2, "Blah" );
283b7808
VZ
198 grid->SetCellValue( 0, 3, "Read only" );
199 grid->SetReadOnly( 0, 3 );
e442cc0d
MB
200
201 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
202
203 grid->SetRowSize( 99, 60 );
204 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
f445b853 205
b99be8fb 206 grid->SetCellValue(2, 2, "red");
ab79958a 207
b99be8fb 208 grid->SetCellTextColour(2, 2, *wxRED);
2e9a6788 209 grid->SetCellValue(3, 3, "green on grey");
b99be8fb 210 grid->SetCellTextColour(3, 3, *wxGREEN);
2e9a6788 211 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
b99be8fb 212
ab79958a
VZ
213 grid->SetCellValue(4, 4, "a weird looking cell");
214 grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
215 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
216
508011ce
VZ
217 grid->SetCellValue(3, 0, "1");
218 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
219 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
220
758cbedf
VZ
221 wxGridCellAttr *attr;
222 attr = new wxGridCellAttr;
223 attr->SetTextColour(*wxBLUE);
224 grid->SetColAttr(5, attr);
225 attr = new wxGridCellAttr;
226 attr->SetBackgroundColour(*wxBLUE);
227 grid->SetRowAttr(5, attr);
228
43947979
VZ
229 grid->SetCellValue(2, 4, "a wider column");
230 grid->SetColSize(4, 120);
231 grid->SetColMinimalWidth(4, 120);
283b7808 232
e442cc0d
MB
233 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
234 topSizer->Add( grid,
235 1,
236 wxEXPAND );
f0102d2a 237
e442cc0d 238 topSizer->Add( logWin,
f0102d2a 239 0,
e442cc0d 240 wxEXPAND );
f0102d2a 241
e442cc0d
MB
242 SetAutoLayout( TRUE );
243 SetSizer( topSizer );
f0102d2a 244
e442cc0d
MB
245 topSizer->Fit( this );
246 topSizer->SetSizeHints( this );
f0102d2a 247
e442cc0d
MB
248 Centre();
249 SetDefaults();
250}
251
252
253GridFrame::~GridFrame()
254{
255}
256
f445b853 257
e442cc0d
MB
258void GridFrame::SetDefaults()
259{
260 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
261 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
f445b853 262 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
e442cc0d
MB
263}
264
265
266void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
267{
268 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
269 {
270 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
271 }
272 else
273 {
274 grid->SetRowLabelSize( 0 );
275 }
276}
277
278
279void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
280{
281 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
282 {
283 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
284 }
285 else
286 {
287 grid->SetColLabelSize( 0 );
288 }
289}
290
291
f445b853 292void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
e442cc0d 293{
f445b853
MB
294 grid->EnableEditing(
295 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
e442cc0d
MB
296}
297
298
299void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
300{
301 wxColourDialog dlg( NULL );
302 if ( dlg.ShowModal() == wxID_OK )
303 {
304 wxColourData retData;
305 retData = dlg.GetColourData();
306 wxColour colour = retData.GetColour();
307
308 grid->SetLabelBackgroundColour( colour );
309 }
310}
311
312
313void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
314{
315 wxColourDialog dlg( NULL );
316 if ( dlg.ShowModal() == wxID_OK )
317 {
318 wxColourData retData;
319 retData = dlg.GetColourData();
320 wxColour colour = retData.GetColour();
321
322 grid->SetLabelTextColour( colour );
323 }
324}
325
326
327void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
328{
329 int horiz, vert;
330 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 331
e442cc0d
MB
332 switch ( horiz )
333 {
334 case wxLEFT:
335 horiz = wxCENTRE;
336 break;
f0102d2a 337
e442cc0d
MB
338 case wxCENTRE:
339 horiz = wxRIGHT;
340 break;
341
342 case wxRIGHT:
343 horiz = wxLEFT;
344 break;
345 }
346
347 grid->SetRowLabelAlignment( horiz, -1 );
348}
349
350void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
351{
352 int horiz, vert;
353 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 354
e442cc0d
MB
355 switch ( vert )
356 {
357 case wxTOP:
358 vert = wxCENTRE;
359 break;
f0102d2a 360
e442cc0d
MB
361 case wxCENTRE:
362 vert = wxBOTTOM;
363 break;
364
365 case wxBOTTOM:
366 vert = wxTOP;
367 break;
368 }
369
370 grid->SetRowLabelAlignment( -1, vert );
371}
372
373
374void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
375{
376 int horiz, vert;
377 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 378
e442cc0d
MB
379 switch ( horiz )
380 {
381 case wxLEFT:
382 horiz = wxCENTRE;
383 break;
f0102d2a 384
e442cc0d
MB
385 case wxCENTRE:
386 horiz = wxRIGHT;
387 break;
388
389 case wxRIGHT:
390 horiz = wxLEFT;
391 break;
392 }
393
394 grid->SetColLabelAlignment( horiz, -1 );
395}
396
397
398void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
399{
400 int horiz, vert;
401 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 402
e442cc0d
MB
403 switch ( vert )
404 {
405 case wxTOP:
406 vert = wxCENTRE;
407 break;
f0102d2a 408
e442cc0d
MB
409 case wxCENTRE:
410 vert = wxBOTTOM;
411 break;
412
413 case wxBOTTOM:
414 vert = wxTOP;
415 break;
416 }
417
418 grid->SetColLabelAlignment( -1, vert );
419}
420
421
422void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
423{
424 wxColourDialog dlg( NULL );
425 if ( dlg.ShowModal() == wxID_OK )
426 {
427 wxColourData retData;
428 retData = dlg.GetColourData();
429 wxColour colour = retData.GetColour();
430
431 grid->SetGridLineColour( colour );
432 }
433}
434
435
0eee5283
MB
436void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
437{
4e5b5abb 438 grid->InsertRows( grid->GetGridCursorRow(), 1 );
0eee5283
MB
439}
440
441
442void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
443{
4e5b5abb 444 grid->InsertCols( grid->GetGridCursorCol(), 1 );
0eee5283
MB
445}
446
447
f445b853 448void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
0eee5283 449{
f445b853
MB
450 if ( grid->IsSelection() )
451 {
452 int topRow, bottomRow, leftCol, rightCol;
453 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
454 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
455 }
0eee5283
MB
456}
457
458
f445b853 459void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
0eee5283 460{
f445b853
MB
461 if ( grid->IsSelection() )
462 {
463 int topRow, bottomRow, leftCol, rightCol;
464 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
465 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
466 }
0eee5283
MB
467}
468
469
e442cc0d
MB
470void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
471{
472 grid->ClearGrid();
473}
474
ab79958a
VZ
475void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
476{
477 wxColour col = wxGetColourFromUser(this);
478 if ( col.Ok() )
758cbedf 479 {
ab79958a 480 grid->SetDefaultCellTextColour(col);
758cbedf
VZ
481 grid->Refresh();
482 }
ab79958a
VZ
483}
484
485void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
486{
487 wxColour col = wxGetColourFromUser(this);
488 if ( col.Ok() )
758cbedf 489 {
ab79958a 490 grid->SetDefaultCellBackgroundColour(col);
758cbedf
VZ
491 grid->Refresh();
492 }
ab79958a 493}
e442cc0d 494
e442cc0d
MB
495void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
496{
497 logBuf = "";
498 if ( ev.GetRow() != -1 )
499 {
c336585e 500 logBuf << "Left click on row label " << ev.GetRow();
e442cc0d
MB
501 }
502 else if ( ev.GetCol() != -1 )
503 {
c336585e 504 logBuf << "Left click on col label " << ev.GetCol();
e442cc0d
MB
505 }
506 else
507 {
c336585e 508 logBuf << "Left click on corner label";
e442cc0d
MB
509 }
510
511 if ( ev.ShiftDown() ) logBuf << " (shift down)";
512 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 513
c336585e
MB
514 // you must call event skip if you want default grid processing
515 //
e442cc0d
MB
516 ev.Skip();
517}
518
519
520void GridFrame::OnCellLeftClick( wxGridEvent& ev )
521{
522 logBuf = "";
c336585e 523 logBuf << "Left click at row " << ev.GetRow()
e442cc0d
MB
524 << " col " << ev.GetCol();
525 wxLogMessage( "%s", logBuf.c_str() );
0eee5283 526
e442cc0d
MB
527 // you must call event skip if you want default grid processing
528 // (cell highlighting etc.)
529 //
530 ev.Skip();
531}
532
533
534void GridFrame::OnRowSize( wxGridSizeEvent& ev )
535{
536 logBuf = "";
537 logBuf << "Resized row " << ev.GetRowOrCol();
538 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 539
e442cc0d
MB
540 ev.Skip();
541}
542
543
544void GridFrame::OnColSize( wxGridSizeEvent& ev )
545{
546 logBuf = "";
547 logBuf << "Resized col " << ev.GetRowOrCol();
548 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 549
e442cc0d
MB
550 ev.Skip();
551}
552
c336585e
MB
553
554void GridFrame::OnSelectCell( wxGridEvent& ev )
555{
556 logBuf = "";
557 logBuf << "Selected cell at row " << ev.GetRow()
558 << " col " << ev.GetCol();
559 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 560
c336585e
MB
561 // you must call Skip() if you want the default processing
562 // to occur in wxGrid
563 ev.Skip();
564}
565
e442cc0d
MB
566void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
567{
568 logBuf = "";
569 logBuf << "Selected cells from row " << ev.GetTopRow()
570 << " col " << ev.GetLeftCol()
571 << " to row " << ev.GetBottomRow()
572 << " col " << ev.GetRightCol();
f0102d2a 573
e442cc0d 574 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 575
e442cc0d
MB
576 ev.Skip();
577}
578
579void GridFrame::OnCellValueChanged( wxGridEvent& ev )
580{
581 logBuf = "";
582 logBuf << "Value changed for cell at"
f0102d2a 583 << " row " << ev.GetRow()
e442cc0d 584 << " col " << ev.GetCol();
f0102d2a 585
e442cc0d 586 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 587
e442cc0d
MB
588 ev.Skip();
589}
590
b54ba671
VZ
591void GridFrame::OnEditorShown( wxGridEvent& ev )
592{
593 wxLogMessage( "Cell editor shown." );
594
595 ev.Skip();
596}
597
598void GridFrame::OnEditorHidden( wxGridEvent& ev )
599{
600 wxLogMessage( "Cell editor hidden." );
601
602 ev.Skip();
603}
e442cc0d 604
f445b853
MB
605void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
606{
607 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
608 "Michael Bedward \n"
609 "mbedward@ozemail.com.au \n\n",
610 "About",
611 wxOK );
612}
613
614
615void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
616{
617 Close( TRUE );
618}
619
35f97e95
VZ
620void GridFrame::OnBugsTable(wxCommandEvent& )
621{
622 BugsGridFrame *frame = new BugsGridFrame;
623 frame->Show(TRUE);
624}
625
f97c9b5b
RD
626void GridFrame::OnVTable(wxCommandEvent& )
627{
1618dca7
VZ
628 static long s_sizeGrid = 10000;
629
630 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
631 "Size: ",
632 "wxGridDemo question",
633 s_sizeGrid,
634 0, 32000, this);
635 if ( s_sizeGrid != -1 )
636 {
637 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
638 win->Show(TRUE);
639 }
f97c9b5b
RD
640}
641
ab79958a
VZ
642// ----------------------------------------------------------------------------
643// MyGridCellRenderer
644// ----------------------------------------------------------------------------
645
758cbedf
VZ
646// do something that the default renderer doesn't here just to show that it is
647// possible to alter the appearance of the cell beyond what the attributes
648// allow
ab79958a 649void MyGridCellRenderer::Draw(wxGrid& grid,
f97c9b5b 650 wxGridCellAttr& attr,
ab79958a
VZ
651 wxDC& dc,
652 const wxRect& rect,
653 int row, int col,
654 bool isSelected)
655{
f97c9b5b 656 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
ab79958a
VZ
657
658 dc.SetPen(*wxGREEN_PEN);
659 dc.SetBrush(*wxTRANSPARENT_BRUSH);
660 dc.DrawEllipse(rect);
661}
f97c9b5b
RD
662
663
664// ----------------------------------------------------------------------------
665// BigGridFrame and BigGridTable: Sample of a non-standard table
666// ----------------------------------------------------------------------------
667
1618dca7
VZ
668BigGridFrame::BigGridFrame(long sizeGrid)
669 : wxFrame(NULL, -1, "Plugin Virtual Table",
670 wxDefaultPosition, wxSize(500, 450))
f97c9b5b
RD
671{
672 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
1618dca7 673 m_table = new BigGridTable(sizeGrid);
f97c9b5b
RD
674 m_grid->SetTable(m_table, TRUE);
675}
35f97e95
VZ
676
677// ----------------------------------------------------------------------------
678// BugsGridFrame: a "realistic" table
679// ----------------------------------------------------------------------------
680
681BugsGridFrame::BugsGridFrame()
682 : wxFrame(NULL, -1, "Bugs table",
683 wxDefaultPosition, wxSize(500, 300))
684{
685 enum Severity
686 {
687 Wish,
688 Minor,
689 Normal,
690 Major,
691 Critical
692 };
693
694 static const wxChar* severities[] =
695 {
696 _T("wishlist"),
697 _T("minor"),
698 _T("normal"),
699 _T("major"),
700 _T("critical"),
701 };
702
703 static const struct GridData
704 {
705 int id;
706 const wxChar *summary;
707 Severity severity;
708 int prio;
709 const wxChar *platform;
710 bool opened;
711 } data [] =
712 {
713 { 18, _T("foo doesn't work"), Major, 1, _T("wxMSW"), TRUE },
714 { 27, _T("bar crashes"), Critical, 1, _T("all"), FALSE },
715 { 45, _T("printing is slow"), Minor, 3, _T("wxMSW"), TRUE },
716 { 68, _T("Rectangle() fails"), Normal, 1, _T("wxMSW"), FALSE },
717 };
718
719 static const wxChar *headers[] =
720 {
721 _T("Id"),
722 _T("Summary"),
723 _T("Severity"),
724 _T("Priority"),
725 _T("Platform"),
726 _T("Opened?"),
727 };
728
729 // TODO the correct data type must be used for each column
730
f2d76237 731 wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
35f97e95
VZ
732 wxGridTableBase *table =
733 new wxGridStringTable(WXSIZEOF(data), WXSIZEOF(headers));
734 for ( size_t row = 0; row < WXSIZEOF(data); row++ )
735 {
736 const GridData& gd = data[row];
737 table->SetValue(row, 0, wxString::Format("%d", gd.id));
738 table->SetValue(row, 1, gd.summary);
739 table->SetValue(row, 2, severities[gd.severity]);
740 table->SetValue(row, 3, wxString::Format("%d", gd.prio));
741 table->SetValue(row, 4, gd.platform);
742 table->SetValue(row, 5, gd.opened ? _T("True") : wxEmptyString);
743 }
744
745 for ( size_t col = 0; col < WXSIZEOF(headers); col++ )
746 {
747 table->SetColLabelValue(col, headers[col]);
748 }
749
750 grid->SetTable(table, TRUE);
751}