]> git.saurik.com Git - wxWidgets.git/blob - samples/newgrid/griddemo.cpp
Some parts rewritten to use wxSocket events instead of callbacks, as the
[wxWidgets.git] / samples / newgrid / griddemo.cpp
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 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #pragma interface
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
41 // ----------------------------------------------------------------------------
42 // wxWin macros
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_APP( GridApp )
46
47 // ============================================================================
48 // implementation
49 // ============================================================================
50
51 // ----------------------------------------------------------------------------
52 // GridApp
53 // ----------------------------------------------------------------------------
54
55 bool GridApp::OnInit()
56 {
57 GridFrame *frame = new GridFrame;
58 frame->Show( TRUE );
59
60 return TRUE;
61 }
62
63 // ----------------------------------------------------------------------------
64 // GridFrame
65 // ----------------------------------------------------------------------------
66
67 BEGIN_EVENT_TABLE( GridFrame, wxFrame )
68 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
69 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
70 EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
71 EVT_MENU( ID_TOGGLEROWSIZING, GridFrame::ToggleRowSizing )
72 EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing )
73 EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
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 )
80 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
81 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
82 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
83 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
84 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
85 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
86 EVT_MENU( ID_SELCELLS, GridFrame::SelectCells )
87 EVT_MENU( ID_SELROWS, GridFrame::SelectRows )
88 EVT_MENU( ID_SELCOLS, GridFrame::SelectCols )
89
90 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
91 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
92
93 EVT_MENU( ID_ABOUT, GridFrame::About )
94 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
95 EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
96 EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
97
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 )
102 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
103 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
104 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
105
106 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
107 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
108 END_EVENT_TABLE()
109
110
111 GridFrame::GridFrame()
112 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
113 wxDefaultPosition,
114 wxDefaultSize )
115 {
116 int gridW = 600, gridH = 300;
117 int logW = gridW, logH = 100;
118
119 wxMenu *fileMenu = new wxMenu;
120 fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V");
121 fileMenu->Append( ID_BUGS_TABLE, "&Bugs table test\tCtrl-B");
122 fileMenu->AppendSeparator();
123 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
124
125 wxMenu *viewMenu = new wxMenu;
126 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
127 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
128 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
129 viewMenu->Append( ID_TOGGLEROWSIZING, "Ro&w drag-resize", "", TRUE );
130 viewMenu->Append( ID_TOGGLECOLSIZING, "C&ol drag-resize", "", TRUE );
131 viewMenu->Append( ID_TOGGLEGRIDSIZING, "&Grid drag-resize", "", TRUE );
132
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" );
140 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
141
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
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" );
157
158 wxMenu *editMenu = new wxMenu;
159 editMenu->Append( ID_INSERTROW, "Insert &row" );
160 editMenu->Append( ID_INSERTCOL, "Insert &column" );
161 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
162 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
163 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
164
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
175 wxMenu *helpMenu = new wxMenu;
176 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
177
178 wxMenuBar *menuBar = new wxMenuBar;
179 menuBar->Append( fileMenu, "&File" );
180 menuBar->Append( viewMenu, "&View" );
181 menuBar->Append( colMenu, "&Colours" );
182 menuBar->Append( editMenu, "&Edit" );
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 ) );
191
192 logWin = new wxTextCtrl( this,
193 -1,
194 wxEmptyString,
195 wxPoint( 0, gridH + 20 ),
196 wxSize( logW, logH ),
197 wxTE_MULTILINE );
198
199 logger = new wxLogTextCtrl( logWin );
200 m_logOld = logger->SetActiveTarget( logger );
201 logger->SetTimestamp( NULL );
202
203 // this will create a grid and, by default, an associated grid
204 // table for strings
205 grid->CreateGrid( 100, 100 );
206
207 grid->SetRowSize( 0, 60 );
208 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
209
210 grid->SetCellValue( 0, 1, "Blah" );
211 grid->SetCellValue( 0, 2, "Blah" );
212 grid->SetCellValue( 0, 3, "Read only" );
213 grid->SetReadOnly( 0, 3 );
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" );
219
220 grid->SetCellValue(2, 2, "red");
221
222 grid->SetCellTextColour(2, 2, *wxRED);
223 grid->SetCellValue(3, 3, "green on grey");
224 grid->SetCellTextColour(3, 3, *wxGREEN);
225 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
226
227 grid->SetCellValue(4, 4, "a weird looking cell");
228 grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
229 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
230
231 grid->SetCellValue(3, 0, "1");
232 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
233 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
234
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
243 grid->SetCellValue(2, 4, "a wider column");
244 grid->SetColSize(4, 120);
245 grid->SetColMinimalWidth(4, 120);
246
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
257 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
258 topSizer->Add( grid,
259 1,
260 wxEXPAND );
261
262 topSizer->Add( logWin,
263 0,
264 wxEXPAND );
265
266 SetAutoLayout( TRUE );
267 SetSizer( topSizer );
268
269 topSizer->Fit( this );
270 topSizer->SetSizeHints( this );
271
272 Centre();
273 SetDefaults();
274 }
275
276
277 GridFrame::~GridFrame()
278 {
279 delete wxLog::SetActiveTarget(m_logOld);
280 }
281
282
283 void GridFrame::SetDefaults()
284 {
285 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
286 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
287 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
288 GetMenuBar()->Check( ID_TOGGLEROWSIZING, TRUE );
289 GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE );
290 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, TRUE );
291 }
292
293
294 void 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
307 void 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
320 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
321 {
322 grid->EnableEditing(
323 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
324 }
325
326
327 void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
328 {
329 grid->EnableDragRowSize(
330 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
331 }
332
333
334 void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
335 {
336 grid->EnableDragColSize(
337 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
338 }
339
340 void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
341 {
342 grid->EnableDragGridSize(
343 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
344 }
345
346
347 void 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
361 void 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
375 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
376 {
377 int horiz, vert;
378 grid->GetRowLabelAlignment( &horiz, &vert );
379
380 switch ( horiz )
381 {
382 case wxLEFT:
383 horiz = wxCENTRE;
384 break;
385
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
398 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
399 {
400 int horiz, vert;
401 grid->GetRowLabelAlignment( &horiz, &vert );
402
403 switch ( vert )
404 {
405 case wxTOP:
406 vert = wxCENTRE;
407 break;
408
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
422 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
423 {
424 int horiz, vert;
425 grid->GetColLabelAlignment( &horiz, &vert );
426
427 switch ( horiz )
428 {
429 case wxLEFT:
430 horiz = wxCENTRE;
431 break;
432
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
446 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
447 {
448 int horiz, vert;
449 grid->GetColLabelAlignment( &horiz, &vert );
450
451 switch ( vert )
452 {
453 case wxTOP:
454 vert = wxCENTRE;
455 break;
456
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
470 void 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
484 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
485 {
486 grid->InsertRows( grid->GetGridCursorRow(), 1 );
487 }
488
489
490 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
491 {
492 grid->InsertCols( grid->GetGridCursorCol(), 1 );
493 }
494
495
496 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
497 {
498 if ( grid->IsSelection() )
499 {
500 for ( int n = 0; n < grid->GetNumberRows(); n++ )
501 if ( grid->IsInSelection( n , 0 ) )
502 grid->DeleteRows( n, 1 );
503 }
504 }
505
506
507 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
508 {
509 if ( grid->IsSelection() )
510 {
511 for ( int n = 0; n < grid->GetNumberCols(); n++ )
512 if ( grid->IsInSelection( 0 , n ) )
513 grid->DeleteCols( n, 1 );
514 }
515 }
516
517
518 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
519 {
520 grid->ClearGrid();
521 }
522
523 void GridFrame::SelectCells( wxCommandEvent& WXUNUSED(ev) )
524 {
525 grid->SetSelectionMode( wxGrid::wxGridSelectCells );
526 }
527
528 void GridFrame::SelectRows( wxCommandEvent& WXUNUSED(ev) )
529 {
530 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
531 }
532
533 void GridFrame::SelectCols( wxCommandEvent& WXUNUSED(ev) )
534 {
535 grid->SetSelectionMode( wxGrid::wxGridSelectColumns );
536 }
537
538 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
539 {
540 wxColour col = wxGetColourFromUser(this);
541 if ( col.Ok() )
542 {
543 grid->SetDefaultCellTextColour(col);
544 grid->Refresh();
545 }
546 }
547
548 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
549 {
550 wxColour col = wxGetColourFromUser(this);
551 if ( col.Ok() )
552 {
553 grid->SetDefaultCellBackgroundColour(col);
554 grid->Refresh();
555 }
556 }
557
558 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
559 {
560 logBuf = "";
561 if ( ev.GetRow() != -1 )
562 {
563 logBuf << "Left click on row label " << ev.GetRow();
564 }
565 else if ( ev.GetCol() != -1 )
566 {
567 logBuf << "Left click on col label " << ev.GetCol();
568 }
569 else
570 {
571 logBuf << "Left click on corner label";
572 }
573
574 if ( ev.ShiftDown() ) logBuf << " (shift down)";
575 wxLogMessage( "%s", logBuf.c_str() );
576
577 // you must call event skip if you want default grid processing
578 //
579 ev.Skip();
580 }
581
582
583 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
584 {
585 logBuf = "";
586 logBuf << "Left click at row " << ev.GetRow()
587 << " col " << ev.GetCol();
588 wxLogMessage( "%s", logBuf.c_str() );
589
590 // you must call event skip if you want default grid processing
591 // (cell highlighting etc.)
592 //
593 ev.Skip();
594 }
595
596
597 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
598 {
599 logBuf = "";
600 logBuf << "Resized row " << ev.GetRowOrCol();
601 wxLogMessage( "%s", logBuf.c_str() );
602
603 ev.Skip();
604 }
605
606
607 void GridFrame::OnColSize( wxGridSizeEvent& ev )
608 {
609 logBuf = "";
610 logBuf << "Resized col " << ev.GetRowOrCol();
611 wxLogMessage( "%s", logBuf.c_str() );
612
613 ev.Skip();
614 }
615
616
617 void GridFrame::OnSelectCell( wxGridEvent& ev )
618 {
619 logBuf = "";
620 if ( ev.Selecting() )
621 logBuf << "Selected ";
622 else
623 logBuf << "Deselected ";
624 logBuf << "cell at row " << ev.GetRow()
625 << " col " << ev.GetCol()
626 << " ( ControlDown: "<< (ev.ControlDown() ? 'T':'F')
627 << ", ShiftDown: "<< (ev.ShiftDown() ? 'T':'F')
628 << ", AltDown: "<< (ev.AltDown() ? 'T':'F')
629 << ", MetaDown: "<< (ev.MetaDown() ? 'T':'F') << " )";
630 wxLogMessage( "%s", logBuf.c_str() );
631
632 // you must call Skip() if you want the default processing
633 // to occur in wxGrid
634 ev.Skip();
635 }
636
637 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
638 {
639 logBuf = "";
640 if ( ev.Selecting() )
641 logBuf << "Selected ";
642 else
643 logBuf << "Deselected ";
644 logBuf << "cells from row " << ev.GetTopRow()
645 << " col " << ev.GetLeftCol()
646 << " to row " << ev.GetBottomRow()
647 << " col " << ev.GetRightCol()
648 << " ( ControlDown: "<< (ev.ControlDown() ? 'T':'F')
649 << ", ShiftDown: "<< (ev.ShiftDown() ? 'T':'F')
650 << ", AltDown: "<< (ev.AltDown() ? 'T':'F')
651 << ", MetaDown: "<< (ev.MetaDown() ? 'T':'F') << " )";
652 wxLogMessage( "%s", logBuf.c_str() );
653
654 ev.Skip();
655 }
656
657 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
658 {
659 logBuf = "";
660 logBuf << "Value changed for cell at"
661 << " row " << ev.GetRow()
662 << " col " << ev.GetCol();
663
664 wxLogMessage( "%s", logBuf.c_str() );
665
666 ev.Skip();
667 }
668
669 void GridFrame::OnEditorShown( wxGridEvent& ev )
670 {
671 wxLogMessage( "Cell editor shown." );
672
673 ev.Skip();
674 }
675
676 void GridFrame::OnEditorHidden( wxGridEvent& ev )
677 {
678 wxLogMessage( "Cell editor hidden." );
679
680 ev.Skip();
681 }
682
683 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
684 {
685 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
686 "Michael Bedward \n"
687 "mbedward@ozemail.com.au \n\n",
688 "About",
689 wxOK );
690 }
691
692
693 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
694 {
695 Close( TRUE );
696 }
697
698 void GridFrame::OnBugsTable(wxCommandEvent& )
699 {
700 BugsGridFrame *frame = new BugsGridFrame;
701 frame->Show(TRUE);
702 }
703
704 void GridFrame::OnVTable(wxCommandEvent& )
705 {
706 static long s_sizeGrid = 10000;
707
708 #ifdef __WXMOTIF__
709 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
710 wxString s;
711 s << s_sizeGrid;
712 s = wxGetTextFromUser( "Size of the table to create",
713 "Size:",
714 s );
715
716 s.ToLong( &s_sizeGrid );
717
718 #else
719 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
720 "Size: ",
721 "wxGridDemo question",
722 s_sizeGrid,
723 0, 32000, this);
724 #endif
725
726 if ( s_sizeGrid != -1 )
727 {
728 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
729 win->Show(TRUE);
730 }
731 }
732
733 // ----------------------------------------------------------------------------
734 // MyGridCellRenderer
735 // ----------------------------------------------------------------------------
736
737 // do something that the default renderer doesn't here just to show that it is
738 // possible to alter the appearance of the cell beyond what the attributes
739 // allow
740 void MyGridCellRenderer::Draw(wxGrid& grid,
741 wxGridCellAttr& attr,
742 wxDC& dc,
743 const wxRect& rect,
744 int row, int col,
745 bool isSelected)
746 {
747 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
748
749 dc.SetPen(*wxGREEN_PEN);
750 dc.SetBrush(*wxTRANSPARENT_BRUSH);
751 dc.DrawEllipse(rect);
752 }
753
754 // ----------------------------------------------------------------------------
755 // MyGridCellAttrProvider
756 // ----------------------------------------------------------------------------
757
758 MyGridCellAttrProvider::MyGridCellAttrProvider()
759 {
760 m_attrForOddRows = new wxGridCellAttr;
761 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
762 }
763
764 MyGridCellAttrProvider::~MyGridCellAttrProvider()
765 {
766 m_attrForOddRows->DecRef();
767 }
768
769 wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col) const
770 {
771 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col);
772
773 if ( row % 2 )
774 {
775 if ( !attr )
776 {
777 attr = m_attrForOddRows;
778 attr->IncRef();
779 }
780 else
781 {
782 if ( !attr->HasBackgroundColour() )
783 {
784 wxGridCellAttr *attrNew = attr->Clone();
785 attr->DecRef();
786 attr = attrNew;
787 attr->SetBackgroundColour(*wxLIGHT_GREY);
788 }
789 }
790 }
791
792 return attr;
793 }
794
795 // ============================================================================
796 // BigGridFrame and BigGridTable: Sample of a non-standard table
797 // ============================================================================
798
799 BigGridFrame::BigGridFrame(long sizeGrid)
800 : wxFrame(NULL, -1, "Plugin Virtual Table",
801 wxDefaultPosition, wxSize(500, 450))
802 {
803 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
804 m_table = new BigGridTable(sizeGrid);
805
806 // VZ: I don't understand why this slows down the display that much,
807 // must profile it...
808 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
809
810 m_grid->SetTable(m_table, TRUE);
811
812 #if defined __WXMOTIF__
813 // MB: the grid isn't getting a sensible default size under wxMotif
814 int cw, ch;
815 GetClientSize( &cw, &ch );
816 m_grid->SetSize( cw, ch );
817 #endif
818 }
819
820 // ============================================================================
821 // BugsGridFrame: a "realistic" table
822 // ============================================================================
823
824 // ----------------------------------------------------------------------------
825 // bugs table data
826 // ----------------------------------------------------------------------------
827
828 enum Columns
829 {
830 Col_Id,
831 Col_Summary,
832 Col_Severity,
833 Col_Priority,
834 Col_Platform,
835 Col_Opened,
836 Col_Max
837 };
838
839 enum Severity
840 {
841 Sev_Wish,
842 Sev_Minor,
843 Sev_Normal,
844 Sev_Major,
845 Sev_Critical,
846 Sev_Max
847 };
848
849 static const wxChar* severities[] =
850 {
851 _T("wishlist"),
852 _T("minor"),
853 _T("normal"),
854 _T("major"),
855 _T("critical"),
856 };
857
858 static struct BugsGridData
859 {
860 int id;
861 wxChar summary[80];
862 Severity severity;
863 int prio;
864 wxChar platform[12];
865 bool opened;
866 } gs_dataBugsGrid [] =
867 {
868 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), TRUE },
869 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), FALSE },
870 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), TRUE },
871 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), FALSE },
872 };
873
874 static const wxChar *headers[Col_Max] =
875 {
876 _T("Id"),
877 _T("Summary"),
878 _T("Severity"),
879 _T("Priority"),
880 _T("Platform"),
881 _T("Opened?"),
882 };
883
884 // ----------------------------------------------------------------------------
885 // BugsGridTable
886 // ----------------------------------------------------------------------------
887
888 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
889 {
890 switch ( col )
891 {
892 case Col_Id:
893 case Col_Priority:
894 return wxGRID_VALUE_NUMBER;;
895
896 case Col_Severity:
897 // fall thorugh (TODO should be a list)
898
899 case Col_Summary:
900 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
901
902 case Col_Platform:
903 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
904
905 case Col_Opened:
906 return wxGRID_VALUE_BOOL;
907 }
908
909 wxFAIL_MSG(_T("unknown column"));
910
911 return wxEmptyString;
912 }
913
914 long BugsGridTable::GetNumberRows()
915 {
916 return WXSIZEOF(gs_dataBugsGrid);
917 }
918
919 long BugsGridTable::GetNumberCols()
920 {
921 return Col_Max;
922 }
923
924 bool BugsGridTable::IsEmptyCell( int row, int col )
925 {
926 return FALSE;
927 }
928
929 wxString BugsGridTable::GetValue( int row, int col )
930 {
931 const BugsGridData& gd = gs_dataBugsGrid[row];
932
933 switch ( col )
934 {
935 case Col_Id:
936 case Col_Priority:
937 case Col_Opened:
938 wxFAIL_MSG(_T("unexpected column"));
939 break;
940
941 case Col_Severity:
942 return severities[gd.severity];
943
944 case Col_Summary:
945 return gd.summary;
946
947 case Col_Platform:
948 return gd.platform;
949 }
950
951 return wxEmptyString;
952 }
953
954 void BugsGridTable::SetValue( int row, int col, const wxString& value )
955 {
956 BugsGridData& gd = gs_dataBugsGrid[row];
957
958 switch ( col )
959 {
960 case Col_Id:
961 case Col_Priority:
962 case Col_Opened:
963 wxFAIL_MSG(_T("unexpected column"));
964 break;
965
966 case Col_Severity:
967 {
968 size_t n;
969 for ( n = 0; n < WXSIZEOF(severities); n++ )
970 {
971 if ( severities[n] == value )
972 {
973 gd.severity = (Severity)n;
974 break;
975 }
976 }
977
978 if ( n == WXSIZEOF(severities) )
979 {
980 wxLogWarning(_T("Invalid severity value '%s'."),
981 value.c_str());
982 gd.severity = Sev_Normal;
983 }
984 }
985 break;
986
987 case Col_Summary:
988 wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
989 break;
990
991 case Col_Platform:
992 wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
993 break;
994 }
995 }
996
997 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row), int col, const wxString& typeName )
998 {
999 if ( typeName == wxGRID_VALUE_STRING )
1000 {
1001 return TRUE;
1002 }
1003 else if ( typeName == wxGRID_VALUE_BOOL )
1004 {
1005 return col == Col_Opened;
1006 }
1007 else if ( typeName == wxGRID_VALUE_NUMBER )
1008 {
1009 return col == Col_Id || col == Col_Priority || col == Col_Severity;
1010 }
1011 else
1012 {
1013 return FALSE;
1014 }
1015 }
1016
1017 bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
1018 {
1019 return CanGetValueAs(row, col, typeName);
1020 }
1021
1022 long BugsGridTable::GetValueAsLong( int row, int col )
1023 {
1024 const BugsGridData& gd = gs_dataBugsGrid[row];
1025
1026 switch ( col )
1027 {
1028 case Col_Id:
1029 return gd.id;
1030
1031 case Col_Priority:
1032 return gd.prio;
1033
1034 case Col_Severity:
1035 return gd.severity;
1036
1037 default:
1038 wxFAIL_MSG(_T("unexpected column"));
1039 return -1;
1040 }
1041 }
1042
1043 bool BugsGridTable::GetValueAsBool( int row, int col )
1044 {
1045 if ( col == Col_Opened )
1046 {
1047 return gs_dataBugsGrid[row].opened;
1048 }
1049 else
1050 {
1051 wxFAIL_MSG(_T("unexpected column"));
1052
1053 return FALSE;
1054 }
1055 }
1056
1057 void BugsGridTable::SetValueAsLong( int row, int col, long value )
1058 {
1059 BugsGridData& gd = gs_dataBugsGrid[row];
1060
1061 switch ( col )
1062 {
1063 case Col_Priority:
1064 gd.prio = value;
1065 break;
1066
1067 default:
1068 wxFAIL_MSG(_T("unexpected column"));
1069 }
1070 }
1071
1072 void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1073 {
1074 if ( col == Col_Opened )
1075 {
1076 gs_dataBugsGrid[row].opened = value;
1077 }
1078 else
1079 {
1080 wxFAIL_MSG(_T("unexpected column"));
1081 }
1082 }
1083
1084 wxString BugsGridTable::GetColLabelValue( int col )
1085 {
1086 return headers[col];
1087 }
1088
1089 BugsGridTable::BugsGridTable()
1090 {
1091 }
1092
1093 // ----------------------------------------------------------------------------
1094 // BugsGridFrame
1095 // ----------------------------------------------------------------------------
1096
1097 BugsGridFrame::BugsGridFrame()
1098 : wxFrame(NULL, -1, "Bugs table",
1099 wxDefaultPosition, wxSize(500, 300))
1100 {
1101 wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
1102 wxGridTableBase *table = new BugsGridTable();
1103 table->SetAttrProvider(new MyGridCellAttrProvider);
1104 grid->SetTable(table, TRUE);
1105
1106 wxGridCellAttr *attrRO = new wxGridCellAttr,
1107 *attrRangeEditor = new wxGridCellAttr,
1108 *attrCombo = new wxGridCellAttr;
1109
1110 attrRO->SetReadOnly();
1111 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
1112 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1113 severities));
1114
1115 grid->SetColAttr(Col_Id, attrRO);
1116 grid->SetColAttr(Col_Priority, attrRangeEditor);
1117 grid->SetColAttr(Col_Severity, attrCombo);
1118
1119 grid->SetMargins(0, 0);
1120
1121 grid->Fit();
1122 wxSize size = grid->GetSize();
1123 size.x += 10;
1124 size.y += 10;
1125 SetClientSize(size);
1126 }