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