]> git.saurik.com Git - wxWidgets.git/blame - samples/newgrid/griddemo.cpp
added wxPython control files and a few changes
[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 //
187 grid->CreateGrid( 100, 100 );
188
e442cc0d
MB
189 grid->SetRowSize( 0, 60 );
190 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
f0102d2a 191
e442cc0d
MB
192 grid->SetCellValue( 0, 1, "Blah" );
193 grid->SetCellValue( 0, 2, "Blah" );
283b7808
VZ
194 grid->SetCellValue( 0, 3, "Read only" );
195 grid->SetReadOnly( 0, 3 );
e442cc0d
MB
196
197 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
198
199 grid->SetRowSize( 99, 60 );
200 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
f445b853 201
b99be8fb 202 grid->SetCellValue(2, 2, "red");
ab79958a 203
b99be8fb 204 grid->SetCellTextColour(2, 2, *wxRED);
2e9a6788 205 grid->SetCellValue(3, 3, "green on grey");
b99be8fb 206 grid->SetCellTextColour(3, 3, *wxGREEN);
2e9a6788 207 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
b99be8fb 208
ab79958a
VZ
209 grid->SetCellValue(4, 4, "a weird looking cell");
210 grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
211 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
212
508011ce
VZ
213 grid->SetCellValue(3, 0, "1");
214 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
215 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
216
758cbedf
VZ
217 wxGridCellAttr *attr;
218 attr = new wxGridCellAttr;
219 attr->SetTextColour(*wxBLUE);
220 grid->SetColAttr(5, attr);
221 attr = new wxGridCellAttr;
222 attr->SetBackgroundColour(*wxBLUE);
223 grid->SetRowAttr(5, attr);
224
43947979
VZ
225 grid->SetCellValue(2, 4, "a wider column");
226 grid->SetColSize(4, 120);
227 grid->SetColMinimalWidth(4, 120);
283b7808 228
e442cc0d
MB
229 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
230 topSizer->Add( grid,
231 1,
232 wxEXPAND );
f0102d2a 233
e442cc0d 234 topSizer->Add( logWin,
f0102d2a 235 0,
e442cc0d 236 wxEXPAND );
f0102d2a 237
e442cc0d
MB
238 SetAutoLayout( TRUE );
239 SetSizer( topSizer );
f0102d2a 240
e442cc0d
MB
241 topSizer->Fit( this );
242 topSizer->SetSizeHints( this );
f0102d2a 243
e442cc0d
MB
244 Centre();
245 SetDefaults();
246}
247
248
249GridFrame::~GridFrame()
250{
251}
252
f445b853 253
e442cc0d
MB
254void GridFrame::SetDefaults()
255{
256 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
257 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
f445b853 258 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
e442cc0d
MB
259}
260
261
262void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
263{
264 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
265 {
266 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
267 }
268 else
269 {
270 grid->SetRowLabelSize( 0 );
271 }
272}
273
274
275void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
276{
277 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
278 {
279 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
280 }
281 else
282 {
283 grid->SetColLabelSize( 0 );
284 }
285}
286
287
f445b853 288void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
e442cc0d 289{
f445b853
MB
290 grid->EnableEditing(
291 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
e442cc0d
MB
292}
293
294
295void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
296{
297 wxColourDialog dlg( NULL );
298 if ( dlg.ShowModal() == wxID_OK )
299 {
300 wxColourData retData;
301 retData = dlg.GetColourData();
302 wxColour colour = retData.GetColour();
303
304 grid->SetLabelBackgroundColour( colour );
305 }
306}
307
308
309void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
310{
311 wxColourDialog dlg( NULL );
312 if ( dlg.ShowModal() == wxID_OK )
313 {
314 wxColourData retData;
315 retData = dlg.GetColourData();
316 wxColour colour = retData.GetColour();
317
318 grid->SetLabelTextColour( colour );
319 }
320}
321
322
323void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
324{
325 int horiz, vert;
326 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 327
e442cc0d
MB
328 switch ( horiz )
329 {
330 case wxLEFT:
331 horiz = wxCENTRE;
332 break;
f0102d2a 333
e442cc0d
MB
334 case wxCENTRE:
335 horiz = wxRIGHT;
336 break;
337
338 case wxRIGHT:
339 horiz = wxLEFT;
340 break;
341 }
342
343 grid->SetRowLabelAlignment( horiz, -1 );
344}
345
346void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
347{
348 int horiz, vert;
349 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 350
e442cc0d
MB
351 switch ( vert )
352 {
353 case wxTOP:
354 vert = wxCENTRE;
355 break;
f0102d2a 356
e442cc0d
MB
357 case wxCENTRE:
358 vert = wxBOTTOM;
359 break;
360
361 case wxBOTTOM:
362 vert = wxTOP;
363 break;
364 }
365
366 grid->SetRowLabelAlignment( -1, vert );
367}
368
369
370void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
371{
372 int horiz, vert;
373 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 374
e442cc0d
MB
375 switch ( horiz )
376 {
377 case wxLEFT:
378 horiz = wxCENTRE;
379 break;
f0102d2a 380
e442cc0d
MB
381 case wxCENTRE:
382 horiz = wxRIGHT;
383 break;
384
385 case wxRIGHT:
386 horiz = wxLEFT;
387 break;
388 }
389
390 grid->SetColLabelAlignment( horiz, -1 );
391}
392
393
394void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
395{
396 int horiz, vert;
397 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 398
e442cc0d
MB
399 switch ( vert )
400 {
401 case wxTOP:
402 vert = wxCENTRE;
403 break;
f0102d2a 404
e442cc0d
MB
405 case wxCENTRE:
406 vert = wxBOTTOM;
407 break;
408
409 case wxBOTTOM:
410 vert = wxTOP;
411 break;
412 }
413
414 grid->SetColLabelAlignment( -1, vert );
415}
416
417
418void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
419{
420 wxColourDialog dlg( NULL );
421 if ( dlg.ShowModal() == wxID_OK )
422 {
423 wxColourData retData;
424 retData = dlg.GetColourData();
425 wxColour colour = retData.GetColour();
426
427 grid->SetGridLineColour( colour );
428 }
429}
430
431
0eee5283
MB
432void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
433{
4e5b5abb 434 grid->InsertRows( grid->GetGridCursorRow(), 1 );
0eee5283
MB
435}
436
437
438void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
439{
4e5b5abb 440 grid->InsertCols( grid->GetGridCursorCol(), 1 );
0eee5283
MB
441}
442
443
f445b853 444void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
0eee5283 445{
f445b853
MB
446 if ( grid->IsSelection() )
447 {
448 int topRow, bottomRow, leftCol, rightCol;
449 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
450 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
451 }
0eee5283
MB
452}
453
454
f445b853 455void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
0eee5283 456{
f445b853
MB
457 if ( grid->IsSelection() )
458 {
459 int topRow, bottomRow, leftCol, rightCol;
460 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
461 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
462 }
0eee5283
MB
463}
464
465
e442cc0d
MB
466void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
467{
468 grid->ClearGrid();
469}
470
ab79958a
VZ
471void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
472{
473 wxColour col = wxGetColourFromUser(this);
474 if ( col.Ok() )
758cbedf 475 {
ab79958a 476 grid->SetDefaultCellTextColour(col);
758cbedf
VZ
477 grid->Refresh();
478 }
ab79958a
VZ
479}
480
481void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
482{
483 wxColour col = wxGetColourFromUser(this);
484 if ( col.Ok() )
758cbedf 485 {
ab79958a 486 grid->SetDefaultCellBackgroundColour(col);
758cbedf
VZ
487 grid->Refresh();
488 }
ab79958a 489}
e442cc0d 490
e442cc0d
MB
491void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
492{
493 logBuf = "";
494 if ( ev.GetRow() != -1 )
495 {
c336585e 496 logBuf << "Left click on row label " << ev.GetRow();
e442cc0d
MB
497 }
498 else if ( ev.GetCol() != -1 )
499 {
c336585e 500 logBuf << "Left click on col label " << ev.GetCol();
e442cc0d
MB
501 }
502 else
503 {
c336585e 504 logBuf << "Left click on corner label";
e442cc0d
MB
505 }
506
507 if ( ev.ShiftDown() ) logBuf << " (shift down)";
508 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 509
c336585e
MB
510 // you must call event skip if you want default grid processing
511 //
e442cc0d
MB
512 ev.Skip();
513}
514
515
516void GridFrame::OnCellLeftClick( wxGridEvent& ev )
517{
518 logBuf = "";
c336585e 519 logBuf << "Left click at row " << ev.GetRow()
e442cc0d
MB
520 << " col " << ev.GetCol();
521 wxLogMessage( "%s", logBuf.c_str() );
0eee5283 522
e442cc0d
MB
523 // you must call event skip if you want default grid processing
524 // (cell highlighting etc.)
525 //
526 ev.Skip();
527}
528
529
530void GridFrame::OnRowSize( wxGridSizeEvent& ev )
531{
532 logBuf = "";
533 logBuf << "Resized row " << ev.GetRowOrCol();
534 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 535
e442cc0d
MB
536 ev.Skip();
537}
538
539
540void GridFrame::OnColSize( wxGridSizeEvent& ev )
541{
542 logBuf = "";
543 logBuf << "Resized col " << ev.GetRowOrCol();
544 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 545
e442cc0d
MB
546 ev.Skip();
547}
548
c336585e
MB
549
550void GridFrame::OnSelectCell( wxGridEvent& ev )
551{
552 logBuf = "";
553 logBuf << "Selected cell at row " << ev.GetRow()
554 << " col " << ev.GetCol();
555 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 556
c336585e
MB
557 // you must call Skip() if you want the default processing
558 // to occur in wxGrid
559 ev.Skip();
560}
561
e442cc0d
MB
562void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
563{
564 logBuf = "";
565 logBuf << "Selected cells from row " << ev.GetTopRow()
566 << " col " << ev.GetLeftCol()
567 << " to row " << ev.GetBottomRow()
568 << " col " << ev.GetRightCol();
f0102d2a 569
e442cc0d 570 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 571
e442cc0d
MB
572 ev.Skip();
573}
574
575void GridFrame::OnCellValueChanged( wxGridEvent& ev )
576{
577 logBuf = "";
578 logBuf << "Value changed for cell at"
f0102d2a 579 << " row " << ev.GetRow()
e442cc0d 580 << " col " << ev.GetCol();
f0102d2a 581
e442cc0d 582 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 583
e442cc0d
MB
584 ev.Skip();
585}
586
b54ba671
VZ
587void GridFrame::OnEditorShown( wxGridEvent& ev )
588{
589 wxLogMessage( "Cell editor shown." );
590
591 ev.Skip();
592}
593
594void GridFrame::OnEditorHidden( wxGridEvent& ev )
595{
596 wxLogMessage( "Cell editor hidden." );
597
598 ev.Skip();
599}
e442cc0d 600
f445b853
MB
601void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
602{
603 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
604 "Michael Bedward \n"
605 "mbedward@ozemail.com.au \n\n",
606 "About",
607 wxOK );
608}
609
610
611void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
612{
613 Close( TRUE );
614}
615
35f97e95
VZ
616void GridFrame::OnBugsTable(wxCommandEvent& )
617{
618 BugsGridFrame *frame = new BugsGridFrame;
619 frame->Show(TRUE);
620}
621
f97c9b5b
RD
622void GridFrame::OnVTable(wxCommandEvent& )
623{
1618dca7
VZ
624 static long s_sizeGrid = 10000;
625
626 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
627 "Size: ",
628 "wxGridDemo question",
629 s_sizeGrid,
630 0, 32000, this);
631 if ( s_sizeGrid != -1 )
632 {
633 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
634 win->Show(TRUE);
635 }
f97c9b5b
RD
636}
637
ab79958a
VZ
638// ----------------------------------------------------------------------------
639// MyGridCellRenderer
640// ----------------------------------------------------------------------------
641
758cbedf
VZ
642// do something that the default renderer doesn't here just to show that it is
643// possible to alter the appearance of the cell beyond what the attributes
644// allow
ab79958a 645void MyGridCellRenderer::Draw(wxGrid& grid,
f97c9b5b 646 wxGridCellAttr& attr,
ab79958a
VZ
647 wxDC& dc,
648 const wxRect& rect,
649 int row, int col,
650 bool isSelected)
651{
f97c9b5b 652 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
ab79958a
VZ
653
654 dc.SetPen(*wxGREEN_PEN);
655 dc.SetBrush(*wxTRANSPARENT_BRUSH);
656 dc.DrawEllipse(rect);
657}
f97c9b5b
RD
658
659
660// ----------------------------------------------------------------------------
661// BigGridFrame and BigGridTable: Sample of a non-standard table
662// ----------------------------------------------------------------------------
663
1618dca7
VZ
664BigGridFrame::BigGridFrame(long sizeGrid)
665 : wxFrame(NULL, -1, "Plugin Virtual Table",
666 wxDefaultPosition, wxSize(500, 450))
f97c9b5b
RD
667{
668 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
1618dca7 669 m_table = new BigGridTable(sizeGrid);
f97c9b5b
RD
670 m_grid->SetTable(m_table, TRUE);
671}
35f97e95
VZ
672
673// ----------------------------------------------------------------------------
674// BugsGridFrame: a "realistic" table
675// ----------------------------------------------------------------------------
676
677BugsGridFrame::BugsGridFrame()
678 : wxFrame(NULL, -1, "Bugs table",
679 wxDefaultPosition, wxSize(500, 300))
680{
681 enum Severity
682 {
683 Wish,
684 Minor,
685 Normal,
686 Major,
687 Critical
688 };
689
690 static const wxChar* severities[] =
691 {
692 _T("wishlist"),
693 _T("minor"),
694 _T("normal"),
695 _T("major"),
696 _T("critical"),
697 };
698
699 static const struct GridData
700 {
701 int id;
702 const wxChar *summary;
703 Severity severity;
704 int prio;
705 const wxChar *platform;
706 bool opened;
707 } data [] =
708 {
709 { 18, _T("foo doesn't work"), Major, 1, _T("wxMSW"), TRUE },
710 { 27, _T("bar crashes"), Critical, 1, _T("all"), FALSE },
711 { 45, _T("printing is slow"), Minor, 3, _T("wxMSW"), TRUE },
712 { 68, _T("Rectangle() fails"), Normal, 1, _T("wxMSW"), FALSE },
713 };
714
715 static const wxChar *headers[] =
716 {
717 _T("Id"),
718 _T("Summary"),
719 _T("Severity"),
720 _T("Priority"),
721 _T("Platform"),
722 _T("Opened?"),
723 };
724
725 // TODO the correct data type must be used for each column
726
727 wxGrid *grid = new wxGrid(this, -1);
728 wxGridTableBase *table =
729 new wxGridStringTable(WXSIZEOF(data), WXSIZEOF(headers));
730 for ( size_t row = 0; row < WXSIZEOF(data); row++ )
731 {
732 const GridData& gd = data[row];
733 table->SetValue(row, 0, wxString::Format("%d", gd.id));
734 table->SetValue(row, 1, gd.summary);
735 table->SetValue(row, 2, severities[gd.severity]);
736 table->SetValue(row, 3, wxString::Format("%d", gd.prio));
737 table->SetValue(row, 4, gd.platform);
738 table->SetValue(row, 5, gd.opened ? _T("True") : wxEmptyString);
739 }
740
741 for ( size_t col = 0; col < WXSIZEOF(headers); col++ )
742 {
743 table->SetColLabelValue(col, headers[col]);
744 }
745
746 grid->SetTable(table, TRUE);
747}