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