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