]> git.saurik.com Git - wxWidgets.git/blame - samples/newgrid/griddemo.cpp
correction for Fit() which didn't handle properly windows positioned at (-1, -1)
[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
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{
66b3609e 256 delete wxLog::SetActiveTarget(m_logOld);
e442cc0d
MB
257}
258
f445b853 259
e442cc0d
MB
260void GridFrame::SetDefaults()
261{
262 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
263 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
f445b853 264 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
dd16fdae
MB
265 GetMenuBar()->Check( ID_TOGGLEROWSIZING, TRUE );
266 GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE );
4cfa5de6 267 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, TRUE );
e442cc0d
MB
268}
269
270
271void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
272{
273 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
274 {
275 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
276 }
277 else
278 {
279 grid->SetRowLabelSize( 0 );
280 }
281}
282
283
284void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
285{
286 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
287 {
288 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
289 }
290 else
291 {
292 grid->SetColLabelSize( 0 );
293 }
294}
295
296
f445b853 297void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
e442cc0d 298{
f445b853
MB
299 grid->EnableEditing(
300 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
e442cc0d
MB
301}
302
303
dd16fdae
MB
304void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
305{
ea179c7b 306 grid->EnableDragRowSize(
dd16fdae
MB
307 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
308}
309
310
311void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
312{
ea179c7b 313 grid->EnableDragColSize(
dd16fdae
MB
314 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
315}
316
4cfa5de6
RD
317void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
318{
319 grid->EnableDragGridSize(
320 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
321}
322
dd16fdae 323
e442cc0d
MB
324void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
325{
326 wxColourDialog dlg( NULL );
327 if ( dlg.ShowModal() == wxID_OK )
328 {
329 wxColourData retData;
330 retData = dlg.GetColourData();
331 wxColour colour = retData.GetColour();
332
333 grid->SetLabelBackgroundColour( colour );
334 }
335}
336
337
338void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
339{
340 wxColourDialog dlg( NULL );
341 if ( dlg.ShowModal() == wxID_OK )
342 {
343 wxColourData retData;
344 retData = dlg.GetColourData();
345 wxColour colour = retData.GetColour();
346
347 grid->SetLabelTextColour( colour );
348 }
349}
350
351
352void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
353{
354 int horiz, vert;
355 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 356
e442cc0d
MB
357 switch ( horiz )
358 {
359 case wxLEFT:
360 horiz = wxCENTRE;
361 break;
f0102d2a 362
e442cc0d
MB
363 case wxCENTRE:
364 horiz = wxRIGHT;
365 break;
366
367 case wxRIGHT:
368 horiz = wxLEFT;
369 break;
370 }
371
372 grid->SetRowLabelAlignment( horiz, -1 );
373}
374
375void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
376{
377 int horiz, vert;
378 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 379
e442cc0d
MB
380 switch ( vert )
381 {
382 case wxTOP:
383 vert = wxCENTRE;
384 break;
f0102d2a 385
e442cc0d
MB
386 case wxCENTRE:
387 vert = wxBOTTOM;
388 break;
389
390 case wxBOTTOM:
391 vert = wxTOP;
392 break;
393 }
394
395 grid->SetRowLabelAlignment( -1, vert );
396}
397
398
399void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
400{
401 int horiz, vert;
402 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 403
e442cc0d
MB
404 switch ( horiz )
405 {
406 case wxLEFT:
407 horiz = wxCENTRE;
408 break;
f0102d2a 409
e442cc0d
MB
410 case wxCENTRE:
411 horiz = wxRIGHT;
412 break;
413
414 case wxRIGHT:
415 horiz = wxLEFT;
416 break;
417 }
418
419 grid->SetColLabelAlignment( horiz, -1 );
420}
421
422
423void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
424{
425 int horiz, vert;
426 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 427
e442cc0d
MB
428 switch ( vert )
429 {
430 case wxTOP:
431 vert = wxCENTRE;
432 break;
f0102d2a 433
e442cc0d
MB
434 case wxCENTRE:
435 vert = wxBOTTOM;
436 break;
437
438 case wxBOTTOM:
439 vert = wxTOP;
440 break;
441 }
442
443 grid->SetColLabelAlignment( -1, vert );
444}
445
446
447void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
448{
449 wxColourDialog dlg( NULL );
450 if ( dlg.ShowModal() == wxID_OK )
451 {
452 wxColourData retData;
453 retData = dlg.GetColourData();
454 wxColour colour = retData.GetColour();
455
456 grid->SetGridLineColour( colour );
457 }
458}
459
460
0eee5283
MB
461void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
462{
4e5b5abb 463 grid->InsertRows( grid->GetGridCursorRow(), 1 );
0eee5283
MB
464}
465
466
467void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
468{
4e5b5abb 469 grid->InsertCols( grid->GetGridCursorCol(), 1 );
0eee5283
MB
470}
471
472
f445b853 473void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
0eee5283 474{
f445b853
MB
475 if ( grid->IsSelection() )
476 {
477 int topRow, bottomRow, leftCol, rightCol;
478 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
479 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
480 }
0eee5283
MB
481}
482
483
f445b853 484void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
0eee5283 485{
f445b853
MB
486 if ( grid->IsSelection() )
487 {
488 int topRow, bottomRow, leftCol, rightCol;
489 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
490 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
491 }
0eee5283
MB
492}
493
494
e442cc0d
MB
495void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
496{
497 grid->ClearGrid();
498}
499
ab79958a
VZ
500void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
501{
502 wxColour col = wxGetColourFromUser(this);
503 if ( col.Ok() )
758cbedf 504 {
ab79958a 505 grid->SetDefaultCellTextColour(col);
758cbedf
VZ
506 grid->Refresh();
507 }
ab79958a
VZ
508}
509
510void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
511{
512 wxColour col = wxGetColourFromUser(this);
513 if ( col.Ok() )
758cbedf 514 {
ab79958a 515 grid->SetDefaultCellBackgroundColour(col);
758cbedf
VZ
516 grid->Refresh();
517 }
ab79958a 518}
e442cc0d 519
e442cc0d
MB
520void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
521{
522 logBuf = "";
523 if ( ev.GetRow() != -1 )
524 {
c336585e 525 logBuf << "Left click on row label " << ev.GetRow();
e442cc0d
MB
526 }
527 else if ( ev.GetCol() != -1 )
528 {
c336585e 529 logBuf << "Left click on col label " << ev.GetCol();
e442cc0d
MB
530 }
531 else
532 {
c336585e 533 logBuf << "Left click on corner label";
e442cc0d
MB
534 }
535
536 if ( ev.ShiftDown() ) logBuf << " (shift down)";
537 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 538
c336585e
MB
539 // you must call event skip if you want default grid processing
540 //
e442cc0d
MB
541 ev.Skip();
542}
543
544
545void GridFrame::OnCellLeftClick( wxGridEvent& ev )
546{
547 logBuf = "";
c336585e 548 logBuf << "Left click at row " << ev.GetRow()
e442cc0d
MB
549 << " col " << ev.GetCol();
550 wxLogMessage( "%s", logBuf.c_str() );
0eee5283 551
e442cc0d
MB
552 // you must call event skip if you want default grid processing
553 // (cell highlighting etc.)
554 //
555 ev.Skip();
556}
557
558
559void GridFrame::OnRowSize( wxGridSizeEvent& ev )
560{
561 logBuf = "";
562 logBuf << "Resized row " << ev.GetRowOrCol();
563 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 564
e442cc0d
MB
565 ev.Skip();
566}
567
568
569void GridFrame::OnColSize( wxGridSizeEvent& ev )
570{
571 logBuf = "";
572 logBuf << "Resized col " << ev.GetRowOrCol();
573 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 574
e442cc0d
MB
575 ev.Skip();
576}
577
c336585e
MB
578
579void GridFrame::OnSelectCell( wxGridEvent& ev )
580{
581 logBuf = "";
582 logBuf << "Selected cell at row " << ev.GetRow()
583 << " col " << ev.GetCol();
584 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 585
c336585e
MB
586 // you must call Skip() if you want the default processing
587 // to occur in wxGrid
588 ev.Skip();
589}
590
e442cc0d
MB
591void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
592{
593 logBuf = "";
594 logBuf << "Selected cells from row " << ev.GetTopRow()
595 << " col " << ev.GetLeftCol()
596 << " to row " << ev.GetBottomRow()
597 << " col " << ev.GetRightCol();
f0102d2a 598
e442cc0d 599 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 600
e442cc0d
MB
601 ev.Skip();
602}
603
604void GridFrame::OnCellValueChanged( wxGridEvent& ev )
605{
606 logBuf = "";
607 logBuf << "Value changed for cell at"
f0102d2a 608 << " row " << ev.GetRow()
e442cc0d 609 << " col " << ev.GetCol();
f0102d2a 610
e442cc0d 611 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 612
e442cc0d
MB
613 ev.Skip();
614}
615
b54ba671
VZ
616void GridFrame::OnEditorShown( wxGridEvent& ev )
617{
618 wxLogMessage( "Cell editor shown." );
619
620 ev.Skip();
621}
622
623void GridFrame::OnEditorHidden( wxGridEvent& ev )
624{
625 wxLogMessage( "Cell editor hidden." );
626
627 ev.Skip();
628}
e442cc0d 629
f445b853
MB
630void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
631{
632 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
633 "Michael Bedward \n"
634 "mbedward@ozemail.com.au \n\n",
635 "About",
636 wxOK );
637}
638
639
640void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
641{
642 Close( TRUE );
643}
644
35f97e95
VZ
645void GridFrame::OnBugsTable(wxCommandEvent& )
646{
647 BugsGridFrame *frame = new BugsGridFrame;
648 frame->Show(TRUE);
649}
650
f97c9b5b
RD
651void GridFrame::OnVTable(wxCommandEvent& )
652{
1618dca7
VZ
653 static long s_sizeGrid = 10000;
654
b0c8fc35
MB
655#ifdef __WXMOTIF__
656 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
657 wxString s;
658 s << s_sizeGrid;
659 s = wxGetTextFromUser( "Size of the table to create",
660 "Size:",
661 s );
662
663 s.ToLong( &s_sizeGrid );
664
665#else
1618dca7
VZ
666 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
667 "Size: ",
668 "wxGridDemo question",
669 s_sizeGrid,
670 0, 32000, this);
b0c8fc35
MB
671#endif
672
1618dca7
VZ
673 if ( s_sizeGrid != -1 )
674 {
675 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
676 win->Show(TRUE);
677 }
f97c9b5b
RD
678}
679
ab79958a
VZ
680// ----------------------------------------------------------------------------
681// MyGridCellRenderer
682// ----------------------------------------------------------------------------
683
758cbedf
VZ
684// do something that the default renderer doesn't here just to show that it is
685// possible to alter the appearance of the cell beyond what the attributes
686// allow
ab79958a 687void MyGridCellRenderer::Draw(wxGrid& grid,
f97c9b5b 688 wxGridCellAttr& attr,
ab79958a
VZ
689 wxDC& dc,
690 const wxRect& rect,
691 int row, int col,
692 bool isSelected)
693{
f97c9b5b 694 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
ab79958a
VZ
695
696 dc.SetPen(*wxGREEN_PEN);
697 dc.SetBrush(*wxTRANSPARENT_BRUSH);
698 dc.DrawEllipse(rect);
699}
f97c9b5b
RD
700
701
702// ----------------------------------------------------------------------------
703// BigGridFrame and BigGridTable: Sample of a non-standard table
704// ----------------------------------------------------------------------------
705
1618dca7
VZ
706BigGridFrame::BigGridFrame(long sizeGrid)
707 : wxFrame(NULL, -1, "Plugin Virtual Table",
708 wxDefaultPosition, wxSize(500, 450))
f97c9b5b
RD
709{
710 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
1618dca7 711 m_table = new BigGridTable(sizeGrid);
f97c9b5b 712 m_grid->SetTable(m_table, TRUE);
b0c8fc35
MB
713
714#if defined __WXMOTIF__
715 // MB: the grid isn't getting a sensible default size under wxMotif
716 int cw, ch;
717 GetClientSize( &cw, &ch );
718 m_grid->SetSize( cw, ch );
719#endif
f97c9b5b 720}
35f97e95
VZ
721
722// ----------------------------------------------------------------------------
723// BugsGridFrame: a "realistic" table
724// ----------------------------------------------------------------------------
725
816be743
VZ
726enum Columns
727{
728 Col_Id,
729 Col_Summary,
730 Col_Severity,
731 Col_Priority,
732 Col_Platform,
733 Col_Opened,
734 Col_Max
735};
736
737enum Severity
738{
739 Sev_Wish,
740 Sev_Minor,
741 Sev_Normal,
742 Sev_Major,
743 Sev_Critical,
744 Sev_Max
745};
746
747static const wxChar* severities[] =
748{
749 _T("wishlist"),
750 _T("minor"),
751 _T("normal"),
752 _T("major"),
753 _T("critical"),
754};
755
756static struct BugsGridData
757{
758 int id;
266e8367
VZ
759#ifndef __BORLANDC__
760 wxString
761#else
762 const wxChar *
763#endif
764 summary;
816be743
VZ
765 Severity severity;
766 int prio;
266e8367
VZ
767#ifndef __BORLANDC__
768 wxString
769#else
770 const wxChar *
771#endif
772 platform;
816be743
VZ
773 bool opened;
774} gs_dataBugsGrid [] =
775{
776 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), TRUE },
777 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), FALSE },
778 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), TRUE },
779 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), FALSE },
780};
781
782static const wxChar *headers[Col_Max] =
783{
784 _T("Id"),
785 _T("Summary"),
786 _T("Severity"),
787 _T("Priority"),
788 _T("Platform"),
789 _T("Opened?"),
790};
791
792wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
793{
794 switch ( col )
795 {
796 case Col_Id:
797 case Col_Priority:
798 return wxGRID_VALUE_NUMBER;;
799
800 case Col_Severity:
801 // fall thorugh (TODO should be a list)
802
803 case Col_Summary:
804 case Col_Platform:
805 return wxGRID_VALUE_STRING;
806
807 case Col_Opened:
808 return wxGRID_VALUE_BOOL;
809 }
810
811 wxFAIL_MSG(_T("unknown column"));
812
813 return wxEmptyString;
814}
815
816long BugsGridTable::GetNumberRows()
817{
818 return WXSIZEOF(gs_dataBugsGrid);
819}
820
821long BugsGridTable::GetNumberCols()
35f97e95 822{
816be743
VZ
823 return Col_Max;
824}
825
826bool BugsGridTable::IsEmptyCell( int row, int col )
827{
828 return FALSE;
829}
830
831wxString BugsGridTable::GetValue( int row, int col )
832{
833 const BugsGridData& gd = gs_dataBugsGrid[row];
834
835 switch ( col )
35f97e95 836 {
816be743
VZ
837 case Col_Id:
838 case Col_Priority:
839 case Col_Opened:
840 wxFAIL_MSG(_T("unexpected column"));
841 break;
842
843 case Col_Severity:
844 return severities[gd.severity];
845
846 case Col_Summary:
847 return gd.summary;
848
849 case Col_Platform:
850 return gd.platform;
851 }
852
853 return wxEmptyString;
854}
855
856void BugsGridTable::SetValue( int row, int col, const wxString& value )
857{
858 BugsGridData& gd = gs_dataBugsGrid[row];
35f97e95 859
816be743 860 switch ( col )
35f97e95 861 {
816be743
VZ
862 case Col_Id:
863 case Col_Priority:
864 case Col_Opened:
865 wxFAIL_MSG(_T("unexpected column"));
866 break;
867
868 case Col_Severity:
869 {
870 size_t n;
871 for ( n = 0; n < WXSIZEOF(severities); n++ )
872 {
873 if ( severities[n] == value )
874 {
875 gd.severity = (Severity)n;
876 break;
877 }
878 }
879
880 if ( n == WXSIZEOF(severities) )
881 {
882 wxLogWarning(_T("Invalid severity value '%s'."),
883 value.c_str());
884 gd.severity = Sev_Normal;
885 }
886 }
ed23853b 887 break;
816be743
VZ
888
889 case Col_Summary:
890 gd.summary = value;
891 break;
35f97e95 892
816be743
VZ
893 case Col_Platform:
894 gd.platform = value;
895 break;
896 }
897}
898
899bool BugsGridTable::CanGetValueAs( int WXUNUSED(row), int col, const wxString& typeName )
900{
901 if ( typeName == wxGRID_VALUE_STRING )
35f97e95 902 {
816be743
VZ
903 return TRUE;
904 }
905 else if ( typeName == wxGRID_VALUE_BOOL )
906 {
907 return col == Col_Opened;
908 }
909 else if ( typeName == wxGRID_VALUE_NUMBER )
910 {
911 return col == Col_Id || col == Col_Priority || col == Col_Severity;
912 }
913 else
35f97e95 914 {
816be743
VZ
915 return FALSE;
916 }
917}
918
919bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
920{
921 return CanGetValueAs(row, col, typeName);
922}
923
924long BugsGridTable::GetValueAsLong( int row, int col )
925{
926 const BugsGridData& gd = gs_dataBugsGrid[row];
35f97e95 927
816be743 928 switch ( col )
35f97e95 929 {
816be743
VZ
930 case Col_Id:
931 return gd.id;
35f97e95 932
816be743
VZ
933 case Col_Priority:
934 return gd.prio;
35f97e95 935
816be743
VZ
936 case Col_Severity:
937 return gd.severity;
938
939 default:
940 wxFAIL_MSG(_T("unexpected column"));
941 return -1;
942 }
943}
944
945bool BugsGridTable::GetValueAsBool( int row, int col )
946{
947 if ( col == Col_Opened )
948 {
949 return gs_dataBugsGrid[row].opened;
950 }
951 else
35f97e95 952 {
816be743
VZ
953 wxFAIL_MSG(_T("unexpected column"));
954
955 return FALSE;
35f97e95 956 }
816be743 957}
35f97e95 958
816be743
VZ
959void BugsGridTable::SetValueAsLong( int row, int col, long value )
960{
961 BugsGridData& gd = gs_dataBugsGrid[row];
962
963 switch ( col )
35f97e95 964 {
816be743
VZ
965 case Col_Priority:
966 gd.prio = value;
967 break;
968
969 default:
970 wxFAIL_MSG(_T("unexpected column"));
35f97e95 971 }
816be743 972}
35f97e95 973
816be743
VZ
974void BugsGridTable::SetValueAsBool( int row, int col, bool value )
975{
976 if ( col == Col_Opened )
977 {
978 gs_dataBugsGrid[row].opened = value;
979 }
980 else
981 {
982 wxFAIL_MSG(_T("unexpected column"));
983 }
984}
985
986wxString BugsGridTable::GetColLabelValue( int col )
987{
988 return headers[col];
989}
990
991BugsGridTable::BugsGridTable()
992{
993}
994
995BugsGridFrame::BugsGridFrame()
996 : wxFrame(NULL, -1, "Bugs table",
997 wxDefaultPosition, wxSize(500, 300))
998{
999 wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
1000 wxGridTableBase *table = new BugsGridTable();
35f97e95 1001 grid->SetTable(table, TRUE);
816be743 1002
65e4e78e 1003 wxGridCellAttr *attrRO = new wxGridCellAttr,
4ee5fc9c
VZ
1004 *attrRangeEditor = new wxGridCellAttr,
1005 *attrCombo = new wxGridCellAttr;
1006
65e4e78e
VZ
1007 attrRO->SetReadOnly();
1008 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
4ee5fc9c
VZ
1009 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1010 severities));
65e4e78e
VZ
1011
1012 grid->SetColAttr(Col_Id, attrRO);
1013 grid->SetColAttr(Col_Priority, attrRangeEditor);
4ee5fc9c 1014 grid->SetColAttr(Col_Severity, attrCombo);
65e4e78e 1015
266e8367 1016 grid->SetMargins(0, 0);
b0c8fc35 1017
266e8367 1018 grid->Fit();
57c086ef 1019 Fit();
35f97e95 1020}