]> git.saurik.com Git - wxWidgets.git/blame - samples/newgrid/griddemo.cpp
fixed wxDataInputStream::ReadString for empty strings
[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 71 EVT_MENU( ID_TOGGLEROWSIZING, GridFrame::ToggleRowSizing )
ea179c7b 72 EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing )
4cfa5de6 73 EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
f0102d2a
VZ
74 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
75 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
76 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
77 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
78 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
79 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
e442cc0d 80 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
0eee5283
MB
81 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
82 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
f445b853
MB
83 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
84 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
e442cc0d 85 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
043d16b2
SN
86 EVT_MENU( ID_SELCELLS, GridFrame::SelectCells )
87 EVT_MENU( ID_SELROWS, GridFrame::SelectRows )
88 EVT_MENU( ID_SELCOLS, GridFrame::SelectCols )
f0102d2a 89
ab79958a
VZ
90 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
91 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
92
e442cc0d
MB
93 EVT_MENU( ID_ABOUT, GridFrame::About )
94 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
f97c9b5b 95 EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
35f97e95 96 EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
e442cc0d 97
5795d034
MB
98 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
99 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
100 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
101 EVT_GRID_COL_SIZE( GridFrame::OnColSize )
c336585e 102 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
5795d034
MB
103 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
104 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
b54ba671
VZ
105
106 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
107 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
e442cc0d
MB
108END_EVENT_TABLE()
109
f0102d2a 110
e442cc0d
MB
111GridFrame::GridFrame()
112 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
113 wxDefaultPosition,
114 wxDefaultSize )
115{
116 int gridW = 600, gridH = 300;
07296f0b 117 int logW = gridW, logH = 100;
f0102d2a 118
e442cc0d 119 wxMenu *fileMenu = new wxMenu;
43947979 120 fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V");
35f97e95 121 fileMenu->Append( ID_BUGS_TABLE, "&Bugs table test\tCtrl-B");
f97c9b5b 122 fileMenu->AppendSeparator();
f0102d2a
VZ
123 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
124
e442cc0d
MB
125 wxMenu *viewMenu = new wxMenu;
126 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
127 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
f445b853 128 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
dd16fdae
MB
129 viewMenu->Append( ID_TOGGLEROWSIZING, "Ro&w drag-resize", "", TRUE );
130 viewMenu->Append( ID_TOGGLECOLSIZING, "C&ol drag-resize", "", TRUE );
4cfa5de6 131 viewMenu->Append( ID_TOGGLEGRIDSIZING, "&Grid drag-resize", "", TRUE );
f0102d2a 132
e442cc0d
MB
133 wxMenu *rowLabelMenu = new wxMenu;
134
135 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
136 rowLabelMenu,
137 "Change alignment of row labels" );
138
139 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
f0102d2a
VZ
140 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
141
e442cc0d
MB
142 wxMenu *colLabelMenu = new wxMenu;
143
144 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
145 colLabelMenu,
146 "Change alignment of col labels" );
147
148 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
149 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
150
ab79958a
VZ
151 wxMenu *colMenu = new wxMenu;
152 colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
153 colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
154 colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
155 colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour" );
156 colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour" );
e442cc0d 157
0eee5283
MB
158 wxMenu *editMenu = new wxMenu;
159 editMenu->Append( ID_INSERTROW, "Insert &row" );
160 editMenu->Append( ID_INSERTCOL, "Insert &column" );
f445b853
MB
161 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
162 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
0eee5283 163 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
f0102d2a 164
043d16b2
SN
165 wxMenu *selectionMenu = new wxMenu;
166
167 editMenu->Append( ID_CHANGESEL, "Change &selection mode",
168 selectionMenu,
169 "Change selection mode" );
170
171 selectionMenu->Append( ID_SELCELLS, "Select &Cells" );
172 selectionMenu->Append( ID_SELROWS, "Select &Rows" );
173 selectionMenu->Append( ID_SELCOLS, "Select C&ols" );
174
e442cc0d
MB
175 wxMenu *helpMenu = new wxMenu;
176 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
f0102d2a 177
e442cc0d
MB
178 wxMenuBar *menuBar = new wxMenuBar;
179 menuBar->Append( fileMenu, "&File" );
180 menuBar->Append( viewMenu, "&View" );
ab79958a 181 menuBar->Append( colMenu, "&Colours" );
0eee5283 182 menuBar->Append( editMenu, "&Edit" );
e442cc0d
MB
183 menuBar->Append( helpMenu, "&Help" );
184
185 SetMenuBar( menuBar );
186
187 grid = new wxGrid( this,
188 -1,
189 wxPoint( 0, 0 ),
190 wxSize( 400, 300 ) );
f0102d2a 191
e442cc0d
MB
192 logWin = new wxTextCtrl( this,
193 -1,
194 wxEmptyString,
195 wxPoint( 0, gridH + 20 ),
196 wxSize( logW, logH ),
197 wxTE_MULTILINE );
f0102d2a 198
e442cc0d 199 logger = new wxLogTextCtrl( logWin );
66b3609e 200 m_logOld = logger->SetActiveTarget( logger );
e442cc0d
MB
201 logger->SetTimestamp( NULL );
202
203 // this will create a grid and, by default, an associated grid
984ef9dc 204 // table for strings
816be743 205 grid->CreateGrid( 100, 100 );
f2d76237 206
e442cc0d
MB
207 grid->SetRowSize( 0, 60 );
208 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
f0102d2a 209
e442cc0d
MB
210 grid->SetCellValue( 0, 1, "Blah" );
211 grid->SetCellValue( 0, 2, "Blah" );
283b7808
VZ
212 grid->SetCellValue( 0, 3, "Read only" );
213 grid->SetReadOnly( 0, 3 );
e442cc0d
MB
214
215 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
216
217 grid->SetRowSize( 99, 60 );
218 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
f445b853 219
b99be8fb 220 grid->SetCellValue(2, 2, "red");
ab79958a 221
b99be8fb 222 grid->SetCellTextColour(2, 2, *wxRED);
2e9a6788 223 grid->SetCellValue(3, 3, "green on grey");
b99be8fb 224 grid->SetCellTextColour(3, 3, *wxGREEN);
2e9a6788 225 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
b99be8fb 226
ab79958a
VZ
227 grid->SetCellValue(4, 4, "a weird looking cell");
228 grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
229 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
230
508011ce
VZ
231 grid->SetCellValue(3, 0, "1");
232 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
233 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
234
758cbedf
VZ
235 wxGridCellAttr *attr;
236 attr = new wxGridCellAttr;
237 attr->SetTextColour(*wxBLUE);
238 grid->SetColAttr(5, attr);
239 attr = new wxGridCellAttr;
240 attr->SetBackgroundColour(*wxBLUE);
241 grid->SetRowAttr(5, attr);
242
43947979
VZ
243 grid->SetCellValue(2, 4, "a wider column");
244 grid->SetColSize(4, 120);
245 grid->SetColMinimalWidth(4, 120);
283b7808 246
0b190b0f
VZ
247 grid->SetColFormatFloat(5);
248 grid->SetCellValue(0, 5, "3.1415");
249 grid->SetCellValue(1, 5, "1415");
250 grid->SetCellValue(2, 5, "12345.67890");
251
252 grid->SetColFormatFloat(6, 6, 2);
253 grid->SetCellValue(0, 6, "3.1415");
254 grid->SetCellValue(1, 6, "1415");
255 grid->SetCellValue(2, 6, "12345.67890");
256
e442cc0d
MB
257 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
258 topSizer->Add( grid,
259 1,
260 wxEXPAND );
f0102d2a 261
e442cc0d 262 topSizer->Add( logWin,
f0102d2a 263 0,
e442cc0d 264 wxEXPAND );
f0102d2a 265
e442cc0d
MB
266 SetAutoLayout( TRUE );
267 SetSizer( topSizer );
f0102d2a 268
e442cc0d
MB
269 topSizer->Fit( this );
270 topSizer->SetSizeHints( this );
f0102d2a 271
e442cc0d
MB
272 Centre();
273 SetDefaults();
274}
275
276
277GridFrame::~GridFrame()
278{
66b3609e 279 delete wxLog::SetActiveTarget(m_logOld);
e442cc0d
MB
280}
281
f445b853 282
e442cc0d
MB
283void GridFrame::SetDefaults()
284{
285 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
286 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
f445b853 287 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
dd16fdae
MB
288 GetMenuBar()->Check( ID_TOGGLEROWSIZING, TRUE );
289 GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE );
4cfa5de6 290 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, TRUE );
e442cc0d
MB
291}
292
293
294void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
295{
296 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
297 {
298 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
299 }
300 else
301 {
302 grid->SetRowLabelSize( 0 );
303 }
304}
305
306
307void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
308{
309 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
310 {
311 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
312 }
313 else
314 {
315 grid->SetColLabelSize( 0 );
316 }
317}
318
319
f445b853 320void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
e442cc0d 321{
f445b853
MB
322 grid->EnableEditing(
323 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
e442cc0d
MB
324}
325
326
dd16fdae
MB
327void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
328{
ea179c7b 329 grid->EnableDragRowSize(
dd16fdae
MB
330 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
331}
332
333
334void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
335{
ea179c7b 336 grid->EnableDragColSize(
dd16fdae
MB
337 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
338}
339
4cfa5de6
RD
340void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
341{
342 grid->EnableDragGridSize(
343 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
344}
345
dd16fdae 346
e442cc0d
MB
347void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
348{
349 wxColourDialog dlg( NULL );
350 if ( dlg.ShowModal() == wxID_OK )
351 {
352 wxColourData retData;
353 retData = dlg.GetColourData();
354 wxColour colour = retData.GetColour();
355
356 grid->SetLabelBackgroundColour( colour );
357 }
358}
359
360
361void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
362{
363 wxColourDialog dlg( NULL );
364 if ( dlg.ShowModal() == wxID_OK )
365 {
366 wxColourData retData;
367 retData = dlg.GetColourData();
368 wxColour colour = retData.GetColour();
369
370 grid->SetLabelTextColour( colour );
371 }
372}
373
374
375void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
376{
377 int horiz, vert;
378 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 379
e442cc0d
MB
380 switch ( horiz )
381 {
382 case wxLEFT:
383 horiz = wxCENTRE;
384 break;
f0102d2a 385
e442cc0d
MB
386 case wxCENTRE:
387 horiz = wxRIGHT;
388 break;
389
390 case wxRIGHT:
391 horiz = wxLEFT;
392 break;
393 }
394
395 grid->SetRowLabelAlignment( horiz, -1 );
396}
397
398void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
399{
400 int horiz, vert;
401 grid->GetRowLabelAlignment( &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->SetRowLabelAlignment( -1, vert );
419}
420
421
422void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
423{
424 int horiz, vert;
425 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 426
e442cc0d
MB
427 switch ( horiz )
428 {
429 case wxLEFT:
430 horiz = wxCENTRE;
431 break;
f0102d2a 432
e442cc0d
MB
433 case wxCENTRE:
434 horiz = wxRIGHT;
435 break;
436
437 case wxRIGHT:
438 horiz = wxLEFT;
439 break;
440 }
441
442 grid->SetColLabelAlignment( horiz, -1 );
443}
444
445
446void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
447{
448 int horiz, vert;
449 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 450
e442cc0d
MB
451 switch ( vert )
452 {
453 case wxTOP:
454 vert = wxCENTRE;
455 break;
f0102d2a 456
e442cc0d
MB
457 case wxCENTRE:
458 vert = wxBOTTOM;
459 break;
460
461 case wxBOTTOM:
462 vert = wxTOP;
463 break;
464 }
465
466 grid->SetColLabelAlignment( -1, vert );
467}
468
469
470void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
471{
472 wxColourDialog dlg( NULL );
473 if ( dlg.ShowModal() == wxID_OK )
474 {
475 wxColourData retData;
476 retData = dlg.GetColourData();
477 wxColour colour = retData.GetColour();
478
479 grid->SetGridLineColour( colour );
480 }
481}
482
483
0eee5283
MB
484void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
485{
4e5b5abb 486 grid->InsertRows( grid->GetGridCursorRow(), 1 );
0eee5283
MB
487}
488
489
490void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
491{
4e5b5abb 492 grid->InsertCols( grid->GetGridCursorCol(), 1 );
0eee5283
MB
493}
494
495
f445b853 496void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
0eee5283 497{
f445b853
MB
498 if ( grid->IsSelection() )
499 {
043d16b2
SN
500 for ( int n = 0; n < grid->GetNumberRows(); n++ )
501 if ( grid->IsInSelection( n , 0 ) )
502 grid->DeleteRows( n, 1 );
f445b853 503 }
0eee5283
MB
504}
505
506
f445b853 507void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
0eee5283 508{
f445b853
MB
509 if ( grid->IsSelection() )
510 {
043d16b2
SN
511 for ( int n = 0; n < grid->GetNumberCols(); n++ )
512 if ( grid->IsInSelection( 0 , n ) )
513 grid->DeleteCols( n, 1 );
f445b853 514 }
0eee5283
MB
515}
516
517
e442cc0d
MB
518void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
519{
520 grid->ClearGrid();
521}
522
043d16b2
SN
523void GridFrame::SelectCells( wxCommandEvent& WXUNUSED(ev) )
524{
525 grid->SetSelectionMode( wxGrid::wxGridSelectCells );
526}
527
528void GridFrame::SelectRows( wxCommandEvent& WXUNUSED(ev) )
529{
530 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
531}
532
533void GridFrame::SelectCols( wxCommandEvent& WXUNUSED(ev) )
534{
535 grid->SetSelectionMode( wxGrid::wxGridSelectColumns );
536}
537
ab79958a
VZ
538void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
539{
540 wxColour col = wxGetColourFromUser(this);
541 if ( col.Ok() )
758cbedf 542 {
ab79958a 543 grid->SetDefaultCellTextColour(col);
758cbedf
VZ
544 grid->Refresh();
545 }
ab79958a
VZ
546}
547
548void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
549{
550 wxColour col = wxGetColourFromUser(this);
551 if ( col.Ok() )
758cbedf 552 {
ab79958a 553 grid->SetDefaultCellBackgroundColour(col);
758cbedf
VZ
554 grid->Refresh();
555 }
ab79958a 556}
e442cc0d 557
e442cc0d
MB
558void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
559{
560 logBuf = "";
561 if ( ev.GetRow() != -1 )
562 {
c336585e 563 logBuf << "Left click on row label " << ev.GetRow();
e442cc0d
MB
564 }
565 else if ( ev.GetCol() != -1 )
566 {
c336585e 567 logBuf << "Left click on col label " << ev.GetCol();
e442cc0d
MB
568 }
569 else
570 {
c336585e 571 logBuf << "Left click on corner label";
e442cc0d
MB
572 }
573
574 if ( ev.ShiftDown() ) logBuf << " (shift down)";
575 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 576
c336585e
MB
577 // you must call event skip if you want default grid processing
578 //
e442cc0d
MB
579 ev.Skip();
580}
581
582
583void GridFrame::OnCellLeftClick( wxGridEvent& ev )
584{
585 logBuf = "";
c336585e 586 logBuf << "Left click at row " << ev.GetRow()
e442cc0d
MB
587 << " col " << ev.GetCol();
588 wxLogMessage( "%s", logBuf.c_str() );
0eee5283 589
e442cc0d
MB
590 // you must call event skip if you want default grid processing
591 // (cell highlighting etc.)
592 //
593 ev.Skip();
594}
595
596
597void GridFrame::OnRowSize( wxGridSizeEvent& ev )
598{
599 logBuf = "";
600 logBuf << "Resized row " << ev.GetRowOrCol();
601 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 602
e442cc0d
MB
603 ev.Skip();
604}
605
606
607void GridFrame::OnColSize( wxGridSizeEvent& ev )
608{
609 logBuf = "";
610 logBuf << "Resized col " << ev.GetRowOrCol();
611 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 612
e442cc0d
MB
613 ev.Skip();
614}
615
c336585e
MB
616
617void GridFrame::OnSelectCell( wxGridEvent& ev )
618{
619 logBuf = "";
620 logBuf << "Selected cell at row " << ev.GetRow()
621 << " col " << ev.GetCol();
622 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 623
c336585e
MB
624 // you must call Skip() if you want the default processing
625 // to occur in wxGrid
626 ev.Skip();
627}
628
e442cc0d
MB
629void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
630{
631 logBuf = "";
632 logBuf << "Selected cells from row " << ev.GetTopRow()
633 << " col " << ev.GetLeftCol()
634 << " to row " << ev.GetBottomRow()
635 << " col " << ev.GetRightCol();
f0102d2a 636
e442cc0d 637 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 638
e442cc0d
MB
639 ev.Skip();
640}
641
642void GridFrame::OnCellValueChanged( wxGridEvent& ev )
643{
644 logBuf = "";
645 logBuf << "Value changed for cell at"
f0102d2a 646 << " row " << ev.GetRow()
e442cc0d 647 << " col " << ev.GetCol();
f0102d2a 648
e442cc0d 649 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 650
e442cc0d
MB
651 ev.Skip();
652}
653
b54ba671
VZ
654void GridFrame::OnEditorShown( wxGridEvent& ev )
655{
656 wxLogMessage( "Cell editor shown." );
657
658 ev.Skip();
659}
660
661void GridFrame::OnEditorHidden( wxGridEvent& ev )
662{
663 wxLogMessage( "Cell editor hidden." );
664
665 ev.Skip();
666}
e442cc0d 667
f445b853
MB
668void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
669{
670 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
671 "Michael Bedward \n"
672 "mbedward@ozemail.com.au \n\n",
673 "About",
674 wxOK );
675}
676
677
678void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
679{
680 Close( TRUE );
681}
682
35f97e95
VZ
683void GridFrame::OnBugsTable(wxCommandEvent& )
684{
685 BugsGridFrame *frame = new BugsGridFrame;
686 frame->Show(TRUE);
687}
688
f97c9b5b
RD
689void GridFrame::OnVTable(wxCommandEvent& )
690{
1618dca7
VZ
691 static long s_sizeGrid = 10000;
692
b0c8fc35
MB
693#ifdef __WXMOTIF__
694 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
695 wxString s;
696 s << s_sizeGrid;
697 s = wxGetTextFromUser( "Size of the table to create",
698 "Size:",
699 s );
c4608a8a 700
b0c8fc35 701 s.ToLong( &s_sizeGrid );
c4608a8a 702
b0c8fc35 703#else
1618dca7
VZ
704 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
705 "Size: ",
706 "wxGridDemo question",
707 s_sizeGrid,
708 0, 32000, this);
b0c8fc35 709#endif
c4608a8a 710
1618dca7
VZ
711 if ( s_sizeGrid != -1 )
712 {
713 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
714 win->Show(TRUE);
715 }
f97c9b5b
RD
716}
717
ab79958a
VZ
718// ----------------------------------------------------------------------------
719// MyGridCellRenderer
720// ----------------------------------------------------------------------------
721
758cbedf
VZ
722// do something that the default renderer doesn't here just to show that it is
723// possible to alter the appearance of the cell beyond what the attributes
724// allow
ab79958a 725void MyGridCellRenderer::Draw(wxGrid& grid,
f97c9b5b 726 wxGridCellAttr& attr,
ab79958a
VZ
727 wxDC& dc,
728 const wxRect& rect,
729 int row, int col,
730 bool isSelected)
731{
f97c9b5b 732 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
ab79958a
VZ
733
734 dc.SetPen(*wxGREEN_PEN);
735 dc.SetBrush(*wxTRANSPARENT_BRUSH);
736 dc.DrawEllipse(rect);
737}
f97c9b5b 738
f97c9b5b 739// ----------------------------------------------------------------------------
a68c1246 740// MyGridCellAttrProvider
f97c9b5b
RD
741// ----------------------------------------------------------------------------
742
a68c1246
VZ
743MyGridCellAttrProvider::MyGridCellAttrProvider()
744{
745 m_attrForOddRows = new wxGridCellAttr;
746 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
747}
748
749MyGridCellAttrProvider::~MyGridCellAttrProvider()
750{
751 m_attrForOddRows->DecRef();
752}
753
754wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col) const
755{
756 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col);
757
758 if ( row % 2 )
759 {
760 if ( !attr )
761 {
762 attr = m_attrForOddRows;
763 attr->IncRef();
764 }
765 else
766 {
767 if ( !attr->HasBackgroundColour() )
768 {
769 wxGridCellAttr *attrNew = attr->Clone();
770 attr->DecRef();
771 attr = attrNew;
772 attr->SetBackgroundColour(*wxLIGHT_GREY);
773 }
774 }
775 }
776
777 return attr;
778}
779
780// ============================================================================
781// BigGridFrame and BigGridTable: Sample of a non-standard table
782// ============================================================================
783
1618dca7
VZ
784BigGridFrame::BigGridFrame(long sizeGrid)
785 : wxFrame(NULL, -1, "Plugin Virtual Table",
786 wxDefaultPosition, wxSize(500, 450))
f97c9b5b
RD
787{
788 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
1618dca7 789 m_table = new BigGridTable(sizeGrid);
a68c1246
VZ
790
791 // VZ: I don't understand why this slows down the display that much,
792 // must profile it...
793 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
794
f97c9b5b 795 m_grid->SetTable(m_table, TRUE);
b0c8fc35
MB
796
797#if defined __WXMOTIF__
798 // MB: the grid isn't getting a sensible default size under wxMotif
799 int cw, ch;
800 GetClientSize( &cw, &ch );
801 m_grid->SetSize( cw, ch );
802#endif
f97c9b5b 803}
35f97e95 804
a68c1246 805// ============================================================================
35f97e95 806// BugsGridFrame: a "realistic" table
a68c1246
VZ
807// ============================================================================
808
809// ----------------------------------------------------------------------------
810// bugs table data
35f97e95
VZ
811// ----------------------------------------------------------------------------
812
816be743
VZ
813enum Columns
814{
815 Col_Id,
816 Col_Summary,
817 Col_Severity,
818 Col_Priority,
819 Col_Platform,
820 Col_Opened,
821 Col_Max
822};
823
824enum Severity
825{
826 Sev_Wish,
827 Sev_Minor,
828 Sev_Normal,
829 Sev_Major,
830 Sev_Critical,
831 Sev_Max
832};
833
834static const wxChar* severities[] =
835{
836 _T("wishlist"),
837 _T("minor"),
838 _T("normal"),
839 _T("major"),
840 _T("critical"),
841};
842
843static struct BugsGridData
844{
845 int id;
9121bed2 846 wxChar summary[80];
816be743
VZ
847 Severity severity;
848 int prio;
9121bed2 849 wxChar platform[12];
816be743 850 bool opened;
c4608a8a 851} gs_dataBugsGrid [] =
816be743
VZ
852{
853 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), TRUE },
854 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), FALSE },
855 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), TRUE },
856 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), FALSE },
857};
858
859static const wxChar *headers[Col_Max] =
860{
861 _T("Id"),
862 _T("Summary"),
863 _T("Severity"),
864 _T("Priority"),
865 _T("Platform"),
866 _T("Opened?"),
867};
868
a68c1246
VZ
869// ----------------------------------------------------------------------------
870// BugsGridTable
871// ----------------------------------------------------------------------------
872
816be743
VZ
873wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
874{
875 switch ( col )
876 {
877 case Col_Id:
878 case Col_Priority:
879 return wxGRID_VALUE_NUMBER;;
880
881 case Col_Severity:
882 // fall thorugh (TODO should be a list)
883
884 case Col_Summary:
c4608a8a
VZ
885 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
886
816be743 887 case Col_Platform:
c4608a8a 888 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
816be743
VZ
889
890 case Col_Opened:
891 return wxGRID_VALUE_BOOL;
892 }
893
894 wxFAIL_MSG(_T("unknown column"));
895
896 return wxEmptyString;
897}
898
899long BugsGridTable::GetNumberRows()
900{
901 return WXSIZEOF(gs_dataBugsGrid);
902}
903
904long BugsGridTable::GetNumberCols()
35f97e95 905{
816be743
VZ
906 return Col_Max;
907}
908
909bool BugsGridTable::IsEmptyCell( int row, int col )
910{
911 return FALSE;
912}
913
914wxString BugsGridTable::GetValue( int row, int col )
915{
916 const BugsGridData& gd = gs_dataBugsGrid[row];
917
918 switch ( col )
35f97e95 919 {
816be743
VZ
920 case Col_Id:
921 case Col_Priority:
922 case Col_Opened:
923 wxFAIL_MSG(_T("unexpected column"));
924 break;
925
926 case Col_Severity:
927 return severities[gd.severity];
928
929 case Col_Summary:
930 return gd.summary;
931
932 case Col_Platform:
933 return gd.platform;
934 }
935
936 return wxEmptyString;
937}
938
939void BugsGridTable::SetValue( int row, int col, const wxString& value )
940{
941 BugsGridData& gd = gs_dataBugsGrid[row];
35f97e95 942
816be743 943 switch ( col )
35f97e95 944 {
816be743
VZ
945 case Col_Id:
946 case Col_Priority:
947 case Col_Opened:
948 wxFAIL_MSG(_T("unexpected column"));
949 break;
950
951 case Col_Severity:
952 {
953 size_t n;
954 for ( n = 0; n < WXSIZEOF(severities); n++ )
955 {
956 if ( severities[n] == value )
957 {
958 gd.severity = (Severity)n;
959 break;
960 }
961 }
962
963 if ( n == WXSIZEOF(severities) )
964 {
965 wxLogWarning(_T("Invalid severity value '%s'."),
966 value.c_str());
967 gd.severity = Sev_Normal;
968 }
969 }
ed23853b 970 break;
816be743
VZ
971
972 case Col_Summary:
9121bed2 973 wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
816be743 974 break;
35f97e95 975
816be743 976 case Col_Platform:
9121bed2 977 wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
816be743
VZ
978 break;
979 }
980}
981
982bool BugsGridTable::CanGetValueAs( int WXUNUSED(row), int col, const wxString& typeName )
983{
984 if ( typeName == wxGRID_VALUE_STRING )
35f97e95 985 {
816be743
VZ
986 return TRUE;
987 }
988 else if ( typeName == wxGRID_VALUE_BOOL )
989 {
990 return col == Col_Opened;
991 }
992 else if ( typeName == wxGRID_VALUE_NUMBER )
993 {
994 return col == Col_Id || col == Col_Priority || col == Col_Severity;
995 }
996 else
35f97e95 997 {
816be743
VZ
998 return FALSE;
999 }
1000}
1001
1002bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
1003{
1004 return CanGetValueAs(row, col, typeName);
1005}
1006
1007long BugsGridTable::GetValueAsLong( int row, int col )
1008{
1009 const BugsGridData& gd = gs_dataBugsGrid[row];
35f97e95 1010
816be743 1011 switch ( col )
35f97e95 1012 {
816be743
VZ
1013 case Col_Id:
1014 return gd.id;
35f97e95 1015
816be743
VZ
1016 case Col_Priority:
1017 return gd.prio;
35f97e95 1018
816be743
VZ
1019 case Col_Severity:
1020 return gd.severity;
1021
1022 default:
1023 wxFAIL_MSG(_T("unexpected column"));
1024 return -1;
1025 }
1026}
1027
1028bool BugsGridTable::GetValueAsBool( int row, int col )
1029{
1030 if ( col == Col_Opened )
1031 {
1032 return gs_dataBugsGrid[row].opened;
1033 }
1034 else
35f97e95 1035 {
816be743
VZ
1036 wxFAIL_MSG(_T("unexpected column"));
1037
1038 return FALSE;
35f97e95 1039 }
816be743 1040}
35f97e95 1041
816be743
VZ
1042void BugsGridTable::SetValueAsLong( int row, int col, long value )
1043{
1044 BugsGridData& gd = gs_dataBugsGrid[row];
1045
1046 switch ( col )
35f97e95 1047 {
816be743
VZ
1048 case Col_Priority:
1049 gd.prio = value;
1050 break;
1051
1052 default:
1053 wxFAIL_MSG(_T("unexpected column"));
35f97e95 1054 }
816be743 1055}
35f97e95 1056
816be743
VZ
1057void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1058{
1059 if ( col == Col_Opened )
1060 {
1061 gs_dataBugsGrid[row].opened = value;
1062 }
1063 else
1064 {
1065 wxFAIL_MSG(_T("unexpected column"));
1066 }
1067}
1068
1069wxString BugsGridTable::GetColLabelValue( int col )
1070{
1071 return headers[col];
1072}
1073
1074BugsGridTable::BugsGridTable()
1075{
1076}
1077
a68c1246
VZ
1078// ----------------------------------------------------------------------------
1079// BugsGridFrame
1080// ----------------------------------------------------------------------------
1081
816be743
VZ
1082BugsGridFrame::BugsGridFrame()
1083 : wxFrame(NULL, -1, "Bugs table",
1084 wxDefaultPosition, wxSize(500, 300))
1085{
1086 wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
1087 wxGridTableBase *table = new BugsGridTable();
a68c1246 1088 table->SetAttrProvider(new MyGridCellAttrProvider);
35f97e95 1089 grid->SetTable(table, TRUE);
816be743 1090
65e4e78e 1091 wxGridCellAttr *attrRO = new wxGridCellAttr,
4ee5fc9c
VZ
1092 *attrRangeEditor = new wxGridCellAttr,
1093 *attrCombo = new wxGridCellAttr;
1094
65e4e78e
VZ
1095 attrRO->SetReadOnly();
1096 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
4ee5fc9c
VZ
1097 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1098 severities));
65e4e78e
VZ
1099
1100 grid->SetColAttr(Col_Id, attrRO);
1101 grid->SetColAttr(Col_Priority, attrRangeEditor);
4ee5fc9c 1102 grid->SetColAttr(Col_Severity, attrCombo);
65e4e78e 1103
266e8367 1104 grid->SetMargins(0, 0);
b0c8fc35 1105
266e8367 1106 grid->Fit();
0b190b0f
VZ
1107 wxSize size = grid->GetSize();
1108 size.x += 10;
1109 size.y += 10;
1110 SetClientSize(size);
35f97e95 1111}