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