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