]> git.saurik.com Git - wxWidgets.git/blob - samples/grid/griddemo.cpp
removed more extraneous semicolons
[wxWidgets.git] / samples / grid / griddemo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: griddemo.cpp
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
5 // Modified by:
6 // RCS-ID: $Id$
7 // Copyright: (c) Michael Bedward, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29
30 #include "wx/colordlg.h"
31 #include "wx/fontdlg.h"
32 #include "wx/numdlg.h"
33
34 #include "wx/grid.h"
35 #include "wx/generic/gridctrl.h"
36
37 #include "griddemo.h"
38
39 // ----------------------------------------------------------------------------
40 // wxWin macros
41 // ----------------------------------------------------------------------------
42
43 IMPLEMENT_APP( GridApp )
44
45 // ============================================================================
46 // implementation
47 // ============================================================================
48
49 // ----------------------------------------------------------------------------
50 // GridApp
51 // ----------------------------------------------------------------------------
52
53 bool GridApp::OnInit()
54 {
55 GridFrame *frame = new GridFrame;
56 frame->Show(true);
57
58 return true;
59 }
60
61 // ----------------------------------------------------------------------------
62 // GridFrame
63 // ----------------------------------------------------------------------------
64
65 BEGIN_EVENT_TABLE( GridFrame, wxFrame )
66 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
67 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
68 EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
69 EVT_MENU( ID_TOGGLEROWSIZING, GridFrame::ToggleRowSizing )
70 EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing )
71 EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
72 EVT_MENU( ID_TOGGLEGRIDDRAGCELL, GridFrame::ToggleGridDragCell )
73 EVT_MENU( ID_TOGGLEGRIDLINES, GridFrame::ToggleGridLines )
74 EVT_MENU( ID_AUTOSIZECOLS, GridFrame::AutoSizeCols )
75 EVT_MENU( ID_CELLOVERFLOW, GridFrame::CellOverflow )
76 EVT_MENU( ID_RESIZECELL, GridFrame::ResizeCell )
77 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
78 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
79 EVT_MENU( ID_SETLABEL_FONT, GridFrame::SetLabelFont )
80 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
81 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
82 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
83 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
84 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
85 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
86 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
87 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
88 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
89 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
90 EVT_MENU( ID_SELCELLS, GridFrame::SelectCells )
91 EVT_MENU( ID_SELROWS, GridFrame::SelectRows )
92 EVT_MENU( ID_SELCOLS, GridFrame::SelectCols )
93
94 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
95 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
96
97 EVT_MENU( wxID_ABOUT, GridFrame::About )
98 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
99 EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
100 EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
101 EVT_MENU( ID_SMALL_GRID, GridFrame::OnSmallGrid)
102
103 EVT_MENU( ID_DESELECT_CELL, GridFrame::DeselectCell)
104 EVT_MENU( ID_DESELECT_COL, GridFrame::DeselectCol)
105 EVT_MENU( ID_DESELECT_ROW, GridFrame::DeselectRow)
106 EVT_MENU( ID_DESELECT_ALL, GridFrame::DeselectAll)
107 EVT_MENU( ID_SELECT_CELL, GridFrame::SelectCell)
108 EVT_MENU( ID_SELECT_COL, GridFrame::SelectCol)
109 EVT_MENU( ID_SELECT_ROW, GridFrame::SelectRow)
110 EVT_MENU( ID_SELECT_ALL, GridFrame::SelectAll)
111 EVT_MENU( ID_SELECT_UNSELECT, GridFrame::OnAddToSelectToggle)
112
113 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH, GridFrame::OnSetHighlightWidth)
114 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH, GridFrame::OnSetROHighlightWidth)
115
116 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
117 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
118 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
119 EVT_GRID_COL_SIZE( GridFrame::OnColSize )
120 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
121 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
122 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
123 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag )
124
125 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
126 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
127 END_EVENT_TABLE()
128
129
130 GridFrame::GridFrame()
131 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxWidgets grid class demo"),
132 wxDefaultPosition,
133 wxDefaultSize )
134 {
135 wxMenu *fileMenu = new wxMenu;
136 fileMenu->Append( ID_VTABLE, _T("&Virtual table test\tCtrl-V"));
137 fileMenu->Append( ID_BUGS_TABLE, _T("&Bugs table test\tCtrl-B"));
138 fileMenu->Append( ID_SMALL_GRID, _T("&Small Grid test\tCtrl-S"));
139 fileMenu->AppendSeparator();
140 fileMenu->Append( wxID_EXIT, _T("E&xit\tAlt-X") );
141
142 wxMenu *viewMenu = new wxMenu;
143 viewMenu->Append( ID_TOGGLEROWLABELS, _T("&Row labels"), wxEmptyString, wxITEM_CHECK );
144 viewMenu->Append( ID_TOGGLECOLLABELS, _T("&Col labels"), wxEmptyString, wxITEM_CHECK );
145 viewMenu->Append( ID_TOGGLEEDIT, _T("&Editable"), wxEmptyString, wxITEM_CHECK );
146 viewMenu->Append( ID_TOGGLEROWSIZING, _T("Ro&w drag-resize"), wxEmptyString, wxITEM_CHECK );
147 viewMenu->Append( ID_TOGGLECOLSIZING, _T("C&ol drag-resize"), wxEmptyString, wxITEM_CHECK );
148 viewMenu->Append( ID_TOGGLEGRIDSIZING, _T("&Grid drag-resize"), wxEmptyString, wxITEM_CHECK );
149 viewMenu->Append( ID_TOGGLEGRIDDRAGCELL, _T("&Grid drag-cell"), wxEmptyString, wxITEM_CHECK );
150 viewMenu->Append( ID_TOGGLEGRIDLINES, _T("&Grid Lines"), wxEmptyString, wxITEM_CHECK );
151 viewMenu->Append( ID_SET_HIGHLIGHT_WIDTH, _T("&Set Cell Highlight Width...") );
152 viewMenu->Append( ID_SET_RO_HIGHLIGHT_WIDTH, _T("&Set Cell RO Highlight Width...") );
153 viewMenu->Append( ID_AUTOSIZECOLS, _T("&Auto-size cols") );
154 viewMenu->Append( ID_CELLOVERFLOW, _T("&Overflow cells"), wxEmptyString, wxITEM_CHECK );
155 viewMenu->Append( ID_RESIZECELL, _T("&Resize cell (7,1)"), wxEmptyString, wxITEM_CHECK );
156
157 wxMenu *rowLabelMenu = new wxMenu;
158
159 viewMenu->Append( ID_ROWLABELALIGN, _T("R&ow label alignment"),
160 rowLabelMenu,
161 _T("Change alignment of row labels") );
162
163 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, _T("&Horizontal") );
164 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, _T("&Vertical") );
165
166 wxMenu *colLabelMenu = new wxMenu;
167
168 viewMenu->Append( ID_COLLABELALIGN, _T("Col l&abel alignment"),
169 colLabelMenu,
170 _T("Change alignment of col labels") );
171
172 colLabelMenu->Append( ID_COLLABELHORIZALIGN, _T("&Horizontal") );
173 colLabelMenu->Append( ID_COLLABELVERTALIGN, _T("&Vertical") );
174
175 wxMenu *colMenu = new wxMenu;
176 colMenu->Append( ID_SETLABELCOLOUR, _T("Set &label colour...") );
177 colMenu->Append( ID_SETLABELTEXTCOLOUR, _T("Set label &text colour...") );
178 colMenu->Append( ID_SETLABEL_FONT, _T("Set label fo&nt...") );
179 colMenu->Append( ID_GRIDLINECOLOUR, _T("&Grid line colour...") );
180 colMenu->Append( ID_SET_CELL_FG_COLOUR, _T("Set cell &foreground colour...") );
181 colMenu->Append( ID_SET_CELL_BG_COLOUR, _T("Set cell &background colour...") );
182
183 wxMenu *editMenu = new wxMenu;
184 editMenu->Append( ID_INSERTROW, _T("Insert &row") );
185 editMenu->Append( ID_INSERTCOL, _T("Insert &column") );
186 editMenu->Append( ID_DELETEROW, _T("Delete selected ro&ws") );
187 editMenu->Append( ID_DELETECOL, _T("Delete selected co&ls") );
188 editMenu->Append( ID_CLEARGRID, _T("Cl&ear grid cell contents") );
189
190 wxMenu *selectMenu = new wxMenu;
191 selectMenu->Append( ID_SELECT_UNSELECT, _T("Add new cells to the selection"),
192 _T("When off, old selection is deselected before ")
193 _T("selecting the new cells"), wxITEM_CHECK );
194 selectMenu->Append( ID_SELECT_ALL, _T("Select all"));
195 selectMenu->Append( ID_SELECT_ROW, _T("Select row 2"));
196 selectMenu->Append( ID_SELECT_COL, _T("Select col 2"));
197 selectMenu->Append( ID_SELECT_CELL, _T("Select cell (3, 1)"));
198 selectMenu->Append( ID_DESELECT_ALL, _T("Deselect all"));
199 selectMenu->Append( ID_DESELECT_ROW, _T("Deselect row 2"));
200 selectMenu->Append( ID_DESELECT_COL, _T("Deselect col 2"));
201 selectMenu->Append( ID_DESELECT_CELL, _T("Deselect cell (3, 1)"));
202 wxMenu *selectionMenu = new wxMenu;
203 selectMenu->Append( ID_CHANGESEL, _T("Change &selection mode"),
204 selectionMenu,
205 _T("Change selection mode") );
206
207 selectionMenu->Append( ID_SELCELLS, _T("Select &Cells") );
208 selectionMenu->Append( ID_SELROWS, _T("Select &Rows") );
209 selectionMenu->Append( ID_SELCOLS, _T("Select C&ols") );
210
211
212 wxMenu *helpMenu = new wxMenu;
213 helpMenu->Append( wxID_ABOUT, _T("&About wxGrid demo") );
214
215 wxMenuBar *menuBar = new wxMenuBar;
216 menuBar->Append( fileMenu, _T("&File") );
217 menuBar->Append( viewMenu, _T("&View") );
218 menuBar->Append( colMenu, _T("&Colours") );
219 menuBar->Append( editMenu, _T("&Edit") );
220 menuBar->Append( selectMenu, _T("&Select") );
221 menuBar->Append( helpMenu, _T("&Help") );
222
223 SetMenuBar( menuBar );
224
225 m_addToSel = false;
226
227 grid = new wxGrid( this,
228 wxID_ANY,
229 wxPoint( 0, 0 ),
230 wxSize( 400, 300 ) );
231
232 #if wxUSE_LOG
233 int gridW = 600, gridH = 300;
234 int logW = gridW, logH = 100;
235
236 logWin = new wxTextCtrl( this,
237 wxID_ANY,
238 wxEmptyString,
239 wxPoint( 0, gridH + 20 ),
240 wxSize( logW, logH ),
241 wxTE_MULTILINE );
242
243 logger = new wxLogTextCtrl( logWin );
244 m_logOld = wxLog::SetActiveTarget( logger );
245 wxLog::SetTimestamp( NULL );
246 #endif // wxUSE_LOG
247
248 // this will create a grid and, by default, an associated grid
249 // table for strings
250 grid->CreateGrid( 0, 0 );
251 grid->AppendRows(100);
252 grid->AppendCols(100);
253
254 int ir = grid->GetNumberRows();
255 grid->DeleteRows(0, ir);
256 grid->AppendRows(ir);
257
258 grid->SetRowSize( 0, 60 );
259 grid->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
260
261 grid->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
262 grid->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer);
263 grid->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor);
264
265 grid->SetCellValue( 0, 2, _T("Blah") );
266 grid->SetCellValue( 0, 3, _T("Read only") );
267 grid->SetReadOnly( 0, 3 );
268
269 grid->SetCellValue( 0, 4, _T("Can veto edit this cell") );
270
271 grid->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
272
273 grid->SetRowSize( 99, 60 );
274 grid->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
275 grid->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
276
277 grid->SetCellTextColour(1, 2, *wxRED);
278 grid->SetCellBackgroundColour(1, 2, *wxGREEN);
279
280 grid->SetCellValue( 1, 4, _T("I'm in the middle"));
281
282 grid->SetCellValue(2, 2, _T("red"));
283
284 grid->SetCellTextColour(2, 2, *wxRED);
285 grid->SetCellValue(3, 3, _T("green on grey"));
286 grid->SetCellTextColour(3, 3, *wxGREEN);
287 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
288
289 grid->SetCellValue(4, 4, _T("a weird looking cell"));
290 grid->SetCellAlignment(4, 4, wxALIGN_CENTRE, wxALIGN_CENTRE);
291 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
292
293 grid->SetCellValue(3, 0, _T("0"));
294 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
295 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
296
297 wxGridCellAttr *attr;
298 attr = new wxGridCellAttr;
299 attr->SetTextColour(*wxBLUE);
300 grid->SetColAttr(5, attr);
301 attr = new wxGridCellAttr;
302 attr->SetBackgroundColour(*wxRED);
303 grid->SetRowAttr(5, attr);
304
305 grid->SetCellValue(2, 4, _T("a wider column"));
306 grid->SetColSize(4, 120);
307 grid->SetColMinimalWidth(4, 120);
308
309 grid->SetCellTextColour(5, 8, *wxGREEN);
310 grid->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
311 grid->SetCellValue(5, 5, _T("Bg from row attr Text col from col attr and this text is so long that it covers over many many empty cells but is broken by one that isn't"));
312
313 grid->SetColFormatFloat(6);
314 grid->SetCellValue(0, 6, _T("3.1415"));
315 grid->SetCellValue(1, 6, _T("1415"));
316 grid->SetCellValue(2, 6, _T("12345.67890"));
317
318 grid->SetColFormatFloat(7, 6, 2);
319 grid->SetCellValue(0, 7, _T("3.1415"));
320 grid->SetCellValue(1, 7, _T("1415"));
321 grid->SetCellValue(2, 7, _T("12345.67890"));
322
323 const wxString choices[] =
324 {
325 _T("Please select a choice"),
326 _T("This takes two cells"),
327 _T("Another choice"),
328 };
329 grid->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices), choices));
330 grid->SetCellSize(4, 0, 1, 2);
331 grid->SetCellValue(4, 0, choices[0]);
332 grid->SetCellOverflow(4, 0, false);
333
334 grid->SetCellSize(7, 1, 3, 4);
335 grid->SetCellAlignment(7, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
336 grid->SetCellValue(7, 1, _T("Big box!"));
337
338 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
339 topSizer->Add( grid,
340 1,
341 wxEXPAND );
342
343 #if wxUSE_LOG
344 topSizer->Add( logWin,
345 0,
346 wxEXPAND );
347 #endif // wxUSE_LOG
348
349 SetAutoLayout(true);
350 SetSizer( topSizer );
351
352 topSizer->Fit( this );
353
354 Centre();
355 SetDefaults();
356 }
357
358
359 GridFrame::~GridFrame()
360 {
361 #if wxUSE_LOG
362 delete wxLog::SetActiveTarget(m_logOld);
363 #endif // wxUSE_LOG
364 }
365
366
367 void GridFrame::SetDefaults()
368 {
369 GetMenuBar()->Check( ID_TOGGLEROWLABELS, true );
370 GetMenuBar()->Check( ID_TOGGLECOLLABELS, true );
371 GetMenuBar()->Check( ID_TOGGLEEDIT, true );
372 GetMenuBar()->Check( ID_TOGGLEROWSIZING, true );
373 GetMenuBar()->Check( ID_TOGGLECOLSIZING, true );
374 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, true );
375 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL, false );
376 GetMenuBar()->Check( ID_TOGGLEGRIDLINES, true );
377 GetMenuBar()->Check( ID_CELLOVERFLOW, true );
378 }
379
380
381 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
382 {
383 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
384 {
385 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
386 }
387 else
388 {
389 grid->SetRowLabelSize( 0 );
390 }
391 }
392
393
394 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
395 {
396 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
397 {
398 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
399 }
400 else
401 {
402 grid->SetColLabelSize( 0 );
403 }
404 }
405
406
407 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
408 {
409 grid->EnableEditing(
410 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
411 }
412
413
414 void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
415 {
416 grid->EnableDragRowSize(
417 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
418 }
419
420
421 void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
422 {
423 grid->EnableDragColSize(
424 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
425 }
426
427 void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
428 {
429 grid->EnableDragGridSize(
430 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
431 }
432
433 void GridFrame::ToggleGridDragCell( wxCommandEvent& WXUNUSED(ev) )
434 {
435 grid->EnableDragCell(
436 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL ) );
437 }
438
439 void GridFrame::ToggleGridLines( wxCommandEvent& WXUNUSED(ev) )
440 {
441 grid->EnableGridLines(
442 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES ) );
443 }
444
445 void GridFrame::OnSetHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
446 {
447 wxString choices[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
448
449 wxSingleChoiceDialog dlg(this, _T("Choose the thickness of the highlight pen:"),
450 _T("Pen Width"), 11, choices);
451
452 int current = grid->GetCellHighlightPenWidth();
453 dlg.SetSelection(current);
454 if (dlg.ShowModal() == wxID_OK) {
455 grid->SetCellHighlightPenWidth(dlg.GetSelection());
456 }
457 }
458
459 void GridFrame::OnSetROHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
460 {
461 wxString choices[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
462
463 wxSingleChoiceDialog dlg(this, _T("Choose the thickness of the highlight pen:"),
464 _T("Pen Width"), 11, choices);
465
466 int current = grid->GetCellHighlightROPenWidth();
467 dlg.SetSelection(current);
468 if (dlg.ShowModal() == wxID_OK) {
469 grid->SetCellHighlightROPenWidth(dlg.GetSelection());
470 }
471 }
472
473
474
475 void GridFrame::AutoSizeCols( wxCommandEvent& WXUNUSED(ev) )
476 {
477 grid->AutoSizeColumns();
478 grid->Refresh();
479 }
480
481 void GridFrame::CellOverflow( wxCommandEvent& ev )
482 {
483 grid->SetDefaultCellOverflow(ev.IsChecked());
484 grid->Refresh();
485 }
486
487 void GridFrame::ResizeCell( wxCommandEvent& ev )
488 {
489 if (ev.IsChecked())
490 grid->SetCellSize( 7, 1, 5, 5 );
491 else
492 grid->SetCellSize( 7, 1, 1, 5 );
493 grid->Refresh();
494 }
495
496 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
497 {
498 wxColourDialog dlg( NULL );
499 if ( dlg.ShowModal() == wxID_OK )
500 {
501 wxColourData retData;
502 retData = dlg.GetColourData();
503 wxColour colour = retData.GetColour();
504
505 grid->SetLabelBackgroundColour( colour );
506 }
507 }
508
509
510 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
511 {
512 wxColourDialog dlg( NULL );
513 if ( dlg.ShowModal() == wxID_OK )
514 {
515 wxColourData retData;
516 retData = dlg.GetColourData();
517 wxColour colour = retData.GetColour();
518
519 grid->SetLabelTextColour( colour );
520 }
521 }
522
523 void GridFrame::SetLabelFont( wxCommandEvent& WXUNUSED(ev) )
524 {
525 wxFont font = wxGetFontFromUser(this);
526 if ( font.Ok() )
527 {
528 grid->SetLabelFont(font);
529 }
530 }
531
532 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
533 {
534 int horiz, vert;
535 grid->GetRowLabelAlignment( &horiz, &vert );
536
537 switch ( horiz )
538 {
539 case wxALIGN_LEFT:
540 horiz = wxALIGN_CENTRE;
541 break;
542
543 case wxALIGN_CENTRE:
544 horiz = wxALIGN_RIGHT;
545 break;
546
547 case wxALIGN_RIGHT:
548 horiz = wxALIGN_LEFT;
549 break;
550 }
551
552 grid->SetRowLabelAlignment( horiz, vert );
553 }
554
555 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
556 {
557 int horiz, vert;
558 grid->GetRowLabelAlignment( &horiz, &vert );
559
560 switch ( vert )
561 {
562 case wxALIGN_TOP:
563 vert = wxALIGN_CENTRE;
564 break;
565
566 case wxALIGN_CENTRE:
567 vert = wxALIGN_BOTTOM;
568 break;
569
570 case wxALIGN_BOTTOM:
571 vert = wxALIGN_TOP;
572 break;
573 }
574
575 grid->SetRowLabelAlignment( horiz, vert );
576 }
577
578
579 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
580 {
581 int horiz, vert;
582 grid->GetColLabelAlignment( &horiz, &vert );
583
584 switch ( horiz )
585 {
586 case wxALIGN_LEFT:
587 horiz = wxALIGN_CENTRE;
588 break;
589
590 case wxALIGN_CENTRE:
591 horiz = wxALIGN_RIGHT;
592 break;
593
594 case wxALIGN_RIGHT:
595 horiz = wxALIGN_LEFT;
596 break;
597 }
598
599 grid->SetColLabelAlignment( horiz, vert );
600 }
601
602
603 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
604 {
605 int horiz, vert;
606 grid->GetColLabelAlignment( &horiz, &vert );
607
608 switch ( vert )
609 {
610 case wxALIGN_TOP:
611 vert = wxALIGN_CENTRE;
612 break;
613
614 case wxALIGN_CENTRE:
615 vert = wxALIGN_BOTTOM;
616 break;
617
618 case wxALIGN_BOTTOM:
619 vert = wxALIGN_TOP;
620 break;
621 }
622
623 grid->SetColLabelAlignment( horiz, vert );
624 }
625
626
627 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
628 {
629 wxColourDialog dlg( NULL );
630 if ( dlg.ShowModal() == wxID_OK )
631 {
632 wxColourData retData;
633 retData = dlg.GetColourData();
634 wxColour colour = retData.GetColour();
635
636 grid->SetGridLineColour( colour );
637 }
638 }
639
640
641 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
642 {
643 grid->InsertRows( grid->GetGridCursorRow(), 1 );
644 }
645
646
647 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
648 {
649 grid->InsertCols( grid->GetGridCursorCol(), 1 );
650 }
651
652
653 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
654 {
655 if ( grid->IsSelection() )
656 {
657 grid->BeginBatch();
658 for ( int n = 0; n < grid->GetNumberRows(); )
659 {
660 if ( grid->IsInSelection( n , 0 ) )
661 grid->DeleteRows( n, 1 );
662 else
663 n++;
664 }
665 grid->EndBatch();
666 }
667 }
668
669
670 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
671 {
672 if ( grid->IsSelection() )
673 {
674 grid->BeginBatch();
675 for ( int n = 0; n < grid->GetNumberCols(); )
676 {
677 if ( grid->IsInSelection( 0 , n ) )
678 grid->DeleteCols( n, 1 );
679 else
680 n++;
681 }
682 grid->EndBatch();
683 }
684 }
685
686
687 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
688 {
689 grid->ClearGrid();
690 }
691
692 void GridFrame::SelectCells( wxCommandEvent& WXUNUSED(ev) )
693 {
694 grid->SetSelectionMode( wxGrid::wxGridSelectCells );
695 }
696
697 void GridFrame::SelectRows( wxCommandEvent& WXUNUSED(ev) )
698 {
699 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
700 }
701
702 void GridFrame::SelectCols( wxCommandEvent& WXUNUSED(ev) )
703 {
704 grid->SetSelectionMode( wxGrid::wxGridSelectColumns );
705 }
706
707 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
708 {
709 wxColour col = wxGetColourFromUser(this);
710 if ( col.Ok() )
711 {
712 grid->SetDefaultCellTextColour(col);
713 grid->Refresh();
714 }
715 }
716
717 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
718 {
719 wxColour col = wxGetColourFromUser(this);
720 if ( col.Ok() )
721 {
722 // Check the new Refresh function by passing it a rectangle
723 // which exactly fits the grid.
724 wxPoint pt(0, 0);
725 wxRect r(pt, grid->GetSize());
726 grid->SetDefaultCellBackgroundColour(col);
727 grid->Refresh(true, &r);
728 }
729 }
730
731 void GridFrame::DeselectCell(wxCommandEvent& WXUNUSED(event))
732 {
733 grid->DeselectCell(3, 1);
734 }
735
736 void GridFrame::DeselectCol(wxCommandEvent& WXUNUSED(event))
737 {
738 grid->DeselectCol(2);
739 }
740
741 void GridFrame::DeselectRow(wxCommandEvent& WXUNUSED(event))
742 {
743 grid->DeselectRow(2);
744 }
745
746 void GridFrame::DeselectAll(wxCommandEvent& WXUNUSED(event))
747 {
748 grid->ClearSelection();
749 }
750
751 void GridFrame::SelectCell(wxCommandEvent& WXUNUSED(event))
752 {
753 grid->SelectBlock(3, 1, 3, 1, m_addToSel);
754 }
755
756 void GridFrame::SelectCol(wxCommandEvent& WXUNUSED(event))
757 {
758 grid->SelectCol(2, m_addToSel);
759 }
760
761 void GridFrame::SelectRow(wxCommandEvent& WXUNUSED(event))
762 {
763 grid->SelectRow(2, m_addToSel);
764 }
765
766 void GridFrame::SelectAll(wxCommandEvent& WXUNUSED(event))
767 {
768 grid->SelectAll();
769 }
770
771 void GridFrame::OnAddToSelectToggle(wxCommandEvent& event)
772 {
773 m_addToSel = event.IsChecked();
774 }
775
776 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
777 {
778 logBuf = wxEmptyString;
779 if ( ev.GetRow() != -1 )
780 {
781 logBuf << _T("Left click on row label ") << ev.GetRow();
782 }
783 else if ( ev.GetCol() != -1 )
784 {
785 logBuf << _T("Left click on col label ") << ev.GetCol();
786 }
787 else
788 {
789 logBuf << _T("Left click on corner label");
790 }
791
792 if ( ev.ShiftDown() ) logBuf << _T(" (shift down)");
793 if ( ev.ControlDown() ) logBuf << _T(" (control down)");
794 wxLogMessage( wxT("%s"), logBuf.c_str() );
795
796 // you must call event skip if you want default grid processing
797 //
798 ev.Skip();
799 }
800
801
802 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
803 {
804 logBuf = wxEmptyString;
805 logBuf << _T("Left click at row ") << ev.GetRow()
806 << _T(" col ") << ev.GetCol();
807 wxLogMessage( wxT("%s"), logBuf.c_str() );
808
809 // you must call event skip if you want default grid processing
810 // (cell highlighting etc.)
811 //
812 ev.Skip();
813 }
814
815
816 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
817 {
818 logBuf = wxEmptyString;
819 logBuf << _T("Resized row ") << ev.GetRowOrCol();
820 wxLogMessage( wxT("%s"), logBuf.c_str() );
821
822 ev.Skip();
823 }
824
825
826 void GridFrame::OnColSize( wxGridSizeEvent& ev )
827 {
828 logBuf = wxEmptyString;
829 logBuf << _T("Resized col ") << ev.GetRowOrCol();
830 wxLogMessage( wxT("%s"), logBuf.c_str() );
831
832 ev.Skip();
833 }
834
835
836 void GridFrame::OnSelectCell( wxGridEvent& ev )
837 {
838 logBuf = wxEmptyString;
839 if ( ev.Selecting() )
840 logBuf << _T("Selected ");
841 else
842 logBuf << _T("Deselected ");
843 logBuf << _T("cell at row ") << ev.GetRow()
844 << _T(" col ") << ev.GetCol()
845 << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
846 << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
847 << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
848 << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
849 wxLogMessage( wxT("%s"), logBuf.c_str() );
850
851 // you must call Skip() if you want the default processing
852 // to occur in wxGrid
853 ev.Skip();
854 }
855
856 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
857 {
858 logBuf = wxEmptyString;
859 if ( ev.Selecting() )
860 logBuf << _T("Selected ");
861 else
862 logBuf << _T("Deselected ");
863 logBuf << _T("cells from row ") << ev.GetTopRow()
864 << _T(" col ") << ev.GetLeftCol()
865 << _T(" to row ") << ev.GetBottomRow()
866 << _T(" col ") << ev.GetRightCol()
867 << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
868 << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
869 << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
870 << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
871 wxLogMessage( wxT("%s"), logBuf.c_str() );
872
873 ev.Skip();
874 }
875
876 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
877 {
878 logBuf = wxEmptyString;
879 logBuf << _T("Value changed for cell at")
880 << _T(" row ") << ev.GetRow()
881 << _T(" col ") << ev.GetCol();
882
883 wxLogMessage( wxT("%s"), logBuf.c_str() );
884
885 ev.Skip();
886 }
887
888 void GridFrame::OnCellBeginDrag( wxGridEvent& ev )
889 {
890 logBuf = wxEmptyString;
891 logBuf << _T("Got request to drag cell at")
892 << _T(" row ") << ev.GetRow()
893 << _T(" col ") << ev.GetCol();
894
895 wxLogMessage( wxT("%s"), logBuf.c_str() );
896
897 ev.Skip();
898 }
899
900 void GridFrame::OnEditorShown( wxGridEvent& ev )
901 {
902
903 if ( (ev.GetCol() == 4) &&
904 (ev.GetRow() == 0) &&
905 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
906 _T("Checking"),wxYES_NO) == wxNO ) ) {
907
908 ev.Veto();
909 return;
910 }
911
912 wxLogMessage( wxT("Cell editor shown.") );
913
914 ev.Skip();
915 }
916
917 void GridFrame::OnEditorHidden( wxGridEvent& ev )
918 {
919
920 if ( (ev.GetCol() == 4) &&
921 (ev.GetRow() == 0) &&
922 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
923 _T("Checking"),wxYES_NO) == wxNO ) ) {
924
925 ev.Veto();
926 return;
927 }
928
929 wxLogMessage( wxT("Cell editor hidden.") );
930
931 ev.Skip();
932 }
933
934 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
935 {
936 (void)wxMessageBox( _T("\n\nwxGrid demo \n\n")
937 _T("Michael Bedward, Julian Smart, Vadim Zeitlin"),
938 _T("About"),
939 wxOK );
940 }
941
942
943 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
944 {
945 Close( true );
946 }
947
948 void GridFrame::OnBugsTable(wxCommandEvent& )
949 {
950 BugsGridFrame *frame = new BugsGridFrame;
951 frame->Show(true);
952 }
953
954 void GridFrame::OnSmallGrid(wxCommandEvent& )
955 {
956 wxFrame* frame = new wxFrame(NULL, wxID_ANY, _T("A Small Grid"),
957 wxDefaultPosition, wxSize(640, 480));
958 wxPanel* panel = new wxPanel(frame, wxID_ANY);
959 wxGrid* grid = new wxGrid(panel, wxID_ANY, wxPoint(10,10), wxSize(400,400),
960 wxWANTS_CHARS | wxSIMPLE_BORDER);
961 grid->CreateGrid(3,3);
962 frame->Show(true);
963 }
964
965 void GridFrame::OnVTable(wxCommandEvent& )
966 {
967 static long s_sizeGrid = 10000;
968
969 #ifdef __WXMOTIF__
970 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
971 wxString s;
972 s << s_sizeGrid;
973 s = wxGetTextFromUser( _T("Size of the table to create"),
974 _T("Size:"),
975 s );
976
977 s.ToLong( &s_sizeGrid );
978
979 #else
980 s_sizeGrid = wxGetNumberFromUser(_T("Size of the table to create"),
981 _T("Size: "),
982 _T("wxGridDemo question"),
983 s_sizeGrid,
984 0, 32000, this);
985 #endif
986
987 if ( s_sizeGrid != -1 )
988 {
989 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
990 win->Show(true);
991 }
992 }
993
994 // ----------------------------------------------------------------------------
995 // MyGridCellRenderer
996 // ----------------------------------------------------------------------------
997
998 // do something that the default renderer doesn't here just to show that it is
999 // possible to alter the appearance of the cell beyond what the attributes
1000 // allow
1001 void MyGridCellRenderer::Draw(wxGrid& grid,
1002 wxGridCellAttr& attr,
1003 wxDC& dc,
1004 const wxRect& rect,
1005 int row, int col,
1006 bool isSelected)
1007 {
1008 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1009
1010 dc.SetPen(*wxGREEN_PEN);
1011 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1012 dc.DrawEllipse(rect);
1013 }
1014
1015 // ----------------------------------------------------------------------------
1016 // MyGridCellAttrProvider
1017 // ----------------------------------------------------------------------------
1018
1019 MyGridCellAttrProvider::MyGridCellAttrProvider()
1020 {
1021 m_attrForOddRows = new wxGridCellAttr;
1022 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
1023 }
1024
1025 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1026 {
1027 m_attrForOddRows->DecRef();
1028 }
1029
1030 wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col,
1031 wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const
1032 {
1033 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
1034
1035 if ( row % 2 )
1036 {
1037 if ( !attr )
1038 {
1039 attr = m_attrForOddRows;
1040 attr->IncRef();
1041 }
1042 else
1043 {
1044 if ( !attr->HasBackgroundColour() )
1045 {
1046 wxGridCellAttr *attrNew = attr->Clone();
1047 attr->DecRef();
1048 attr = attrNew;
1049 attr->SetBackgroundColour(*wxLIGHT_GREY);
1050 }
1051 }
1052 }
1053
1054 return attr;
1055 }
1056
1057 // ============================================================================
1058 // BigGridFrame and BigGridTable: Sample of a non-standard table
1059 // ============================================================================
1060
1061 BigGridFrame::BigGridFrame(long sizeGrid)
1062 : wxFrame(NULL, wxID_ANY, _T("Plugin Virtual Table"),
1063 wxDefaultPosition, wxSize(500, 450))
1064 {
1065 m_grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
1066 m_table = new BigGridTable(sizeGrid);
1067
1068 // VZ: I don't understand why this slows down the display that much,
1069 // must profile it...
1070 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1071
1072 m_grid->SetTable(m_table, true);
1073
1074 #if defined __WXMOTIF__
1075 // MB: the grid isn't getting a sensible default size under wxMotif
1076 int cw, ch;
1077 GetClientSize( &cw, &ch );
1078 m_grid->SetSize( cw, ch );
1079 #endif
1080 }
1081
1082 // ============================================================================
1083 // BugsGridFrame: a "realistic" table
1084 // ============================================================================
1085
1086 // ----------------------------------------------------------------------------
1087 // bugs table data
1088 // ----------------------------------------------------------------------------
1089
1090 enum Columns
1091 {
1092 Col_Id,
1093 Col_Summary,
1094 Col_Severity,
1095 Col_Priority,
1096 Col_Platform,
1097 Col_Opened,
1098 Col_Max
1099 };
1100
1101 enum Severity
1102 {
1103 Sev_Wish,
1104 Sev_Minor,
1105 Sev_Normal,
1106 Sev_Major,
1107 Sev_Critical,
1108 Sev_Max
1109 };
1110
1111 static const wxString severities[] =
1112 {
1113 _T("wishlist"),
1114 _T("minor"),
1115 _T("normal"),
1116 _T("major"),
1117 _T("critical"),
1118 };
1119
1120 static struct BugsGridData
1121 {
1122 int id;
1123 wxChar summary[80];
1124 Severity severity;
1125 int prio;
1126 wxChar platform[12];
1127 bool opened;
1128 } gs_dataBugsGrid [] =
1129 {
1130 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), true },
1131 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), false },
1132 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), true },
1133 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), false },
1134 };
1135
1136 static const wxChar *headers[Col_Max] =
1137 {
1138 _T("Id"),
1139 _T("Summary"),
1140 _T("Severity"),
1141 _T("Priority"),
1142 _T("Platform"),
1143 _T("Opened?"),
1144 };
1145
1146 // ----------------------------------------------------------------------------
1147 // BugsGridTable
1148 // ----------------------------------------------------------------------------
1149
1150 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
1151 {
1152 switch ( col )
1153 {
1154 case Col_Id:
1155 case Col_Priority:
1156 return wxGRID_VALUE_NUMBER;;
1157
1158 case Col_Severity:
1159 // fall thorugh (TODO should be a list)
1160
1161 case Col_Summary:
1162 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
1163
1164 case Col_Platform:
1165 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
1166
1167 case Col_Opened:
1168 return wxGRID_VALUE_BOOL;
1169 }
1170
1171 wxFAIL_MSG(_T("unknown column"));
1172
1173 return wxEmptyString;
1174 }
1175
1176 int BugsGridTable::GetNumberRows()
1177 {
1178 return WXSIZEOF(gs_dataBugsGrid);
1179 }
1180
1181 int BugsGridTable::GetNumberCols()
1182 {
1183 return Col_Max;
1184 }
1185
1186 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row), int WXUNUSED(col) )
1187 {
1188 return false;
1189 }
1190
1191 wxString BugsGridTable::GetValue( int row, int col )
1192 {
1193 const BugsGridData& gd = gs_dataBugsGrid[row];
1194
1195 switch ( col )
1196 {
1197 case Col_Id:
1198 return wxString::Format(_T("%d"), gd.id);
1199
1200 case Col_Priority:
1201 return wxString::Format(_T("%d"), gd.prio);
1202
1203 case Col_Opened:
1204 return gd.opened ? _T("1") : _T("0");
1205
1206 case Col_Severity:
1207 return severities[gd.severity];
1208
1209 case Col_Summary:
1210 return gd.summary;
1211
1212 case Col_Platform:
1213 return gd.platform;
1214 }
1215
1216 return wxEmptyString;
1217 }
1218
1219 void BugsGridTable::SetValue( int row, int col, const wxString& value )
1220 {
1221 BugsGridData& gd = gs_dataBugsGrid[row];
1222
1223 switch ( col )
1224 {
1225 case Col_Id:
1226 case Col_Priority:
1227 case Col_Opened:
1228 wxFAIL_MSG(_T("unexpected column"));
1229 break;
1230
1231 case Col_Severity:
1232 {
1233 size_t n;
1234 for ( n = 0; n < WXSIZEOF(severities); n++ )
1235 {
1236 if ( severities[n] == value )
1237 {
1238 gd.severity = (Severity)n;
1239 break;
1240 }
1241 }
1242
1243 if ( n == WXSIZEOF(severities) )
1244 {
1245 wxLogWarning(_T("Invalid severity value '%s'."),
1246 value.c_str());
1247 gd.severity = Sev_Normal;
1248 }
1249 }
1250 break;
1251
1252 case Col_Summary:
1253 wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
1254 break;
1255
1256 case Col_Platform:
1257 wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
1258 break;
1259 }
1260 }
1261
1262 bool
1263 BugsGridTable::CanGetValueAs(int WXUNUSED(row),
1264 int col,
1265 const wxString& typeName)
1266 {
1267 if ( typeName == wxGRID_VALUE_STRING )
1268 {
1269 return true;
1270 }
1271 else if ( typeName == wxGRID_VALUE_BOOL )
1272 {
1273 return col == Col_Opened;
1274 }
1275 else if ( typeName == wxGRID_VALUE_NUMBER )
1276 {
1277 return col == Col_Id || col == Col_Priority || col == Col_Severity;
1278 }
1279 else
1280 {
1281 return false;
1282 }
1283 }
1284
1285 bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
1286 {
1287 return CanGetValueAs(row, col, typeName);
1288 }
1289
1290 long BugsGridTable::GetValueAsLong( int row, int col )
1291 {
1292 const BugsGridData& gd = gs_dataBugsGrid[row];
1293
1294 switch ( col )
1295 {
1296 case Col_Id:
1297 return gd.id;
1298
1299 case Col_Priority:
1300 return gd.prio;
1301
1302 case Col_Severity:
1303 return gd.severity;
1304
1305 default:
1306 wxFAIL_MSG(_T("unexpected column"));
1307 return -1;
1308 }
1309 }
1310
1311 bool BugsGridTable::GetValueAsBool( int row, int col )
1312 {
1313 if ( col == Col_Opened )
1314 {
1315 return gs_dataBugsGrid[row].opened;
1316 }
1317 else
1318 {
1319 wxFAIL_MSG(_T("unexpected column"));
1320
1321 return false;
1322 }
1323 }
1324
1325 void BugsGridTable::SetValueAsLong( int row, int col, long value )
1326 {
1327 BugsGridData& gd = gs_dataBugsGrid[row];
1328
1329 switch ( col )
1330 {
1331 case Col_Priority:
1332 gd.prio = value;
1333 break;
1334
1335 default:
1336 wxFAIL_MSG(_T("unexpected column"));
1337 }
1338 }
1339
1340 void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1341 {
1342 if ( col == Col_Opened )
1343 {
1344 gs_dataBugsGrid[row].opened = value;
1345 }
1346 else
1347 {
1348 wxFAIL_MSG(_T("unexpected column"));
1349 }
1350 }
1351
1352 wxString BugsGridTable::GetColLabelValue( int col )
1353 {
1354 return headers[col];
1355 }
1356
1357 // ----------------------------------------------------------------------------
1358 // BugsGridFrame
1359 // ----------------------------------------------------------------------------
1360
1361 BugsGridFrame::BugsGridFrame()
1362 : wxFrame(NULL, wxID_ANY, _T("Bugs table"),
1363 wxDefaultPosition, wxSize(500, 300))
1364 {
1365 wxGrid *grid = new wxGrid(this, wxID_ANY, wxDefaultPosition);
1366 wxGridTableBase *table = new BugsGridTable();
1367 table->SetAttrProvider(new MyGridCellAttrProvider);
1368 grid->SetTable(table, true);
1369
1370 wxGridCellAttr *attrRO = new wxGridCellAttr,
1371 *attrRangeEditor = new wxGridCellAttr,
1372 *attrCombo = new wxGridCellAttr;
1373
1374 attrRO->SetReadOnly();
1375 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
1376 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1377 severities));
1378
1379 grid->SetColAttr(Col_Id, attrRO);
1380 grid->SetColAttr(Col_Priority, attrRangeEditor);
1381 grid->SetColAttr(Col_Severity, attrCombo);
1382
1383 grid->SetMargins(0, 0);
1384
1385 grid->Fit();
1386 SetClientSize(grid->GetSize());
1387 }
1388
1389