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