]> git.saurik.com Git - wxWidgets.git/blob - samples/grid/griddemo.cpp
default to using graphics context on osx
[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, wxString::Format(wxT("%g"), 3.1415));
340 grid->SetCellValue(1, 6, wxString::Format(wxT("%g"), 1415.0));
341 grid->SetCellValue(2, 6, wxString::Format(wxT("%g"), 12345.67890));
342
343 grid->SetColFormatFloat(7, 6, 2);
344 grid->SetCellValue(0, 7, wxString::Format(wxT("%g"), 3.1415));
345 grid->SetCellValue(1, 7, wxString::Format(wxT("%g"), 1415.0));
346 grid->SetCellValue(2, 7, wxString::Format(wxT("%g"), 12345.67890));
347
348 grid->SetColFormatNumber(8);
349 grid->SetCellValue(0, 8, "17");
350 grid->SetCellValue(1, 8, "0");
351 grid->SetCellValue(2, 8, "-666");
352
353 const wxString choices[] =
354 {
355 _T("Please select a choice"),
356 _T("This takes two cells"),
357 _T("Another choice"),
358 };
359 grid->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices), choices));
360 grid->SetCellSize(4, 0, 1, 2);
361 grid->SetCellValue(4, 0, choices[0]);
362 grid->SetCellOverflow(4, 0, false);
363
364 grid->SetCellSize(7, 1, 3, 4);
365 grid->SetCellAlignment(7, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
366 grid->SetCellValue(7, 1, _T("Big box!"));
367
368 // this does exactly nothing except testing that SetAttr() handles NULL
369 // attributes and does reference counting correctly
370 grid->SetAttr(11, 11, NULL);
371 grid->SetAttr(11, 11, new wxGridCellAttr);
372 grid->SetAttr(11, 11, NULL);
373
374 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
375 topSizer->Add( grid,
376 1,
377 wxEXPAND );
378
379 #if wxUSE_LOG
380 topSizer->Add( logWin,
381 0,
382 wxEXPAND );
383 #endif // wxUSE_LOG
384
385 SetSizerAndFit( topSizer );
386
387 Centre();
388 SetDefaults();
389 }
390
391
392 GridFrame::~GridFrame()
393 {
394 #if wxUSE_LOG
395 delete wxLog::SetActiveTarget(m_logOld);
396 #endif // wxUSE_LOG
397 }
398
399
400 void GridFrame::SetDefaults()
401 {
402 GetMenuBar()->Check( ID_TOGGLEROWLABELS, true );
403 GetMenuBar()->Check( ID_TOGGLECOLLABELS, true );
404 GetMenuBar()->Check( ID_TOGGLEEDIT, true );
405 GetMenuBar()->Check( ID_TOGGLEROWSIZING, true );
406 GetMenuBar()->Check( ID_TOGGLECOLSIZING, true );
407 GetMenuBar()->Check( ID_TOGGLECOLMOVING, false );
408 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, true );
409 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL, false );
410 GetMenuBar()->Check( ID_TOGGLEGRIDLINES, true );
411 GetMenuBar()->Check( ID_CELLOVERFLOW, true );
412 }
413
414
415 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
416 {
417 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
418 {
419 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
420 }
421 else
422 {
423 grid->SetRowLabelSize( 0 );
424 }
425 }
426
427
428 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
429 {
430 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
431 {
432 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
433 }
434 else
435 {
436 grid->SetColLabelSize( 0 );
437 }
438 }
439
440
441 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
442 {
443 grid->EnableEditing(
444 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
445 }
446
447
448 void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
449 {
450 grid->EnableDragRowSize(
451 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
452 }
453
454
455 void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
456 {
457 grid->EnableDragColSize(
458 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
459 }
460
461 void GridFrame::ToggleColMoving( wxCommandEvent& WXUNUSED(ev) )
462 {
463 grid->EnableDragColMove(
464 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING ) );
465 }
466
467 void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
468 {
469 grid->EnableDragGridSize(
470 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
471 }
472
473 void GridFrame::ToggleGridDragCell( wxCommandEvent& WXUNUSED(ev) )
474 {
475 grid->EnableDragCell(
476 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL ) );
477 }
478
479 void GridFrame::ToggleGridLines( wxCommandEvent& WXUNUSED(ev) )
480 {
481 grid->EnableGridLines(
482 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES ) );
483 }
484
485 void GridFrame::OnSetHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
486 {
487 wxString choices[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
488
489 wxSingleChoiceDialog dlg(this, _T("Choose the thickness of the highlight pen:"),
490 _T("Pen Width"), 11, choices);
491
492 int current = grid->GetCellHighlightPenWidth();
493 dlg.SetSelection(current);
494 if (dlg.ShowModal() == wxID_OK) {
495 grid->SetCellHighlightPenWidth(dlg.GetSelection());
496 }
497 }
498
499 void GridFrame::OnSetROHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
500 {
501 wxString choices[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
502
503 wxSingleChoiceDialog dlg(this, _T("Choose the thickness of the highlight pen:"),
504 _T("Pen Width"), 11, choices);
505
506 int current = grid->GetCellHighlightROPenWidth();
507 dlg.SetSelection(current);
508 if (dlg.ShowModal() == wxID_OK) {
509 grid->SetCellHighlightROPenWidth(dlg.GetSelection());
510 }
511 }
512
513
514
515 void GridFrame::AutoSizeCols( wxCommandEvent& WXUNUSED(ev) )
516 {
517 grid->AutoSizeColumns();
518 grid->Refresh();
519 }
520
521 void GridFrame::CellOverflow( wxCommandEvent& ev )
522 {
523 grid->SetDefaultCellOverflow(ev.IsChecked());
524 grid->Refresh();
525 }
526
527 void GridFrame::ResizeCell( wxCommandEvent& ev )
528 {
529 if (ev.IsChecked())
530 grid->SetCellSize( 7, 1, 5, 5 );
531 else
532 grid->SetCellSize( 7, 1, 1, 5 );
533 grid->Refresh();
534 }
535
536 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
537 {
538 wxColourDialog dlg( NULL );
539 if ( dlg.ShowModal() == wxID_OK )
540 {
541 wxColourData retData;
542 retData = dlg.GetColourData();
543 wxColour colour = retData.GetColour();
544
545 grid->SetLabelBackgroundColour( colour );
546 }
547 }
548
549
550 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
551 {
552 wxColourDialog dlg( NULL );
553 if ( dlg.ShowModal() == wxID_OK )
554 {
555 wxColourData retData;
556 retData = dlg.GetColourData();
557 wxColour colour = retData.GetColour();
558
559 grid->SetLabelTextColour( colour );
560 }
561 }
562
563 void GridFrame::SetLabelFont( wxCommandEvent& WXUNUSED(ev) )
564 {
565 wxFont font = wxGetFontFromUser(this);
566 if ( font.Ok() )
567 {
568 grid->SetLabelFont(font);
569 }
570 }
571
572 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
573 {
574 int horiz, vert;
575 grid->GetRowLabelAlignment( &horiz, &vert );
576
577 switch ( horiz )
578 {
579 case wxALIGN_LEFT:
580 horiz = wxALIGN_CENTRE;
581 break;
582
583 case wxALIGN_CENTRE:
584 horiz = wxALIGN_RIGHT;
585 break;
586
587 case wxALIGN_RIGHT:
588 horiz = wxALIGN_LEFT;
589 break;
590 }
591
592 grid->SetRowLabelAlignment( horiz, vert );
593 }
594
595 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
596 {
597 int horiz, vert;
598 grid->GetRowLabelAlignment( &horiz, &vert );
599
600 switch ( vert )
601 {
602 case wxALIGN_TOP:
603 vert = wxALIGN_CENTRE;
604 break;
605
606 case wxALIGN_CENTRE:
607 vert = wxALIGN_BOTTOM;
608 break;
609
610 case wxALIGN_BOTTOM:
611 vert = wxALIGN_TOP;
612 break;
613 }
614
615 grid->SetRowLabelAlignment( horiz, vert );
616 }
617
618
619 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
620 {
621 int horiz, vert;
622 grid->GetColLabelAlignment( &horiz, &vert );
623
624 switch ( horiz )
625 {
626 case wxALIGN_LEFT:
627 horiz = wxALIGN_CENTRE;
628 break;
629
630 case wxALIGN_CENTRE:
631 horiz = wxALIGN_RIGHT;
632 break;
633
634 case wxALIGN_RIGHT:
635 horiz = wxALIGN_LEFT;
636 break;
637 }
638
639 grid->SetColLabelAlignment( horiz, vert );
640 }
641
642
643 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
644 {
645 int horiz, vert;
646 grid->GetColLabelAlignment( &horiz, &vert );
647
648 switch ( vert )
649 {
650 case wxALIGN_TOP:
651 vert = wxALIGN_CENTRE;
652 break;
653
654 case wxALIGN_CENTRE:
655 vert = wxALIGN_BOTTOM;
656 break;
657
658 case wxALIGN_BOTTOM:
659 vert = wxALIGN_TOP;
660 break;
661 }
662
663 grid->SetColLabelAlignment( horiz, vert );
664 }
665
666
667 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
668 {
669 wxColourDialog dlg( NULL );
670 if ( dlg.ShowModal() == wxID_OK )
671 {
672 wxColourData retData;
673 retData = dlg.GetColourData();
674 wxColour colour = retData.GetColour();
675
676 grid->SetGridLineColour( colour );
677 }
678 }
679
680
681 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
682 {
683 grid->InsertRows( grid->GetGridCursorRow(), 1 );
684 }
685
686
687 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
688 {
689 grid->InsertCols( grid->GetGridCursorCol(), 1 );
690 }
691
692
693 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
694 {
695 if ( grid->IsSelection() )
696 {
697 wxGridUpdateLocker locker(grid);
698 for ( int n = 0; n < grid->GetNumberRows(); )
699 {
700 if ( grid->IsInSelection( n , 0 ) )
701 grid->DeleteRows( n, 1 );
702 else
703 n++;
704 }
705 }
706 }
707
708
709 void GridFrame::AutoSizeRow(wxCommandEvent& WXUNUSED(event))
710 {
711 wxGridUpdateLocker locker(grid);
712 const wxArrayInt sels = grid->GetSelectedRows();
713 for ( size_t n = 0, count = sels.size(); n < count; n++ )
714 {
715 grid->AutoSizeRow( sels[n], false );
716 }
717 }
718
719 void GridFrame::AutoSizeCol(wxCommandEvent& WXUNUSED(event))
720 {
721 wxGridUpdateLocker locker(grid);
722 const wxArrayInt sels = grid->GetSelectedCols();
723 for ( size_t n = 0, count = sels.size(); n < count; n++ )
724 {
725 grid->AutoSizeColumn( sels[n], false );
726 }
727 }
728
729 void GridFrame::AutoSizeRowLabel(wxCommandEvent& WXUNUSED(event))
730 {
731 wxGridUpdateLocker locker(grid);
732 const wxArrayInt sels = grid->GetSelectedRows();
733 for ( size_t n = 0, count = sels.size(); n < count; n++ )
734 {
735 grid->AutoSizeRowLabelSize( sels[n] );
736 }
737 }
738
739 void GridFrame::AutoSizeColLabel(wxCommandEvent& WXUNUSED(event))
740 {
741 wxGridUpdateLocker locker(grid);
742 const wxArrayInt sels = grid->GetSelectedCols();
743 for ( size_t n = 0, count = sels.size(); n < count; n++ )
744 {
745 grid->AutoSizeColLabelSize( sels[n] );
746 }
747 }
748
749 void GridFrame::AutoSizeLabelsCol(wxCommandEvent& WXUNUSED(event))
750 {
751 grid->SetColLabelSize( wxGRID_AUTOSIZE );
752 }
753
754 void GridFrame::AutoSizeLabelsRow(wxCommandEvent& WXUNUSED(event))
755 {
756 grid->SetRowLabelSize( wxGRID_AUTOSIZE );
757 }
758
759 void GridFrame::AutoSizeTable(wxCommandEvent& WXUNUSED(event))
760 {
761 grid->AutoSize();
762 }
763
764
765 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
766 {
767 if ( grid->IsSelection() )
768 {
769 wxGridUpdateLocker locker(grid);
770 for ( int n = 0; n < grid->GetNumberCols(); )
771 {
772 if ( grid->IsInSelection( 0 , n ) )
773 grid->DeleteCols( n, 1 );
774 else
775 n++;
776 }
777 }
778 }
779
780
781 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
782 {
783 grid->ClearGrid();
784 }
785
786 void GridFrame::SelectCells( wxCommandEvent& WXUNUSED(ev) )
787 {
788 grid->SetSelectionMode( wxGrid::wxGridSelectCells );
789 }
790
791 void GridFrame::SelectRows( wxCommandEvent& WXUNUSED(ev) )
792 {
793 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
794 }
795
796 void GridFrame::SelectCols( wxCommandEvent& WXUNUSED(ev) )
797 {
798 grid->SetSelectionMode( wxGrid::wxGridSelectColumns );
799 }
800
801 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
802 {
803 wxColour col = wxGetColourFromUser(this);
804 if ( col.Ok() )
805 {
806 grid->SetDefaultCellTextColour(col);
807 grid->Refresh();
808 }
809 }
810
811 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
812 {
813 wxColour col = wxGetColourFromUser(this);
814 if ( col.Ok() )
815 {
816 // Check the new Refresh function by passing it a rectangle
817 // which exactly fits the grid.
818 wxPoint pt(0, 0);
819 wxRect r(pt, grid->GetSize());
820 grid->SetDefaultCellBackgroundColour(col);
821 grid->Refresh(true, &r);
822 }
823 }
824
825 void GridFrame::DeselectCell(wxCommandEvent& WXUNUSED(event))
826 {
827 grid->DeselectCell(3, 1);
828 }
829
830 void GridFrame::DeselectCol(wxCommandEvent& WXUNUSED(event))
831 {
832 grid->DeselectCol(2);
833 }
834
835 void GridFrame::DeselectRow(wxCommandEvent& WXUNUSED(event))
836 {
837 grid->DeselectRow(2);
838 }
839
840 void GridFrame::DeselectAll(wxCommandEvent& WXUNUSED(event))
841 {
842 grid->ClearSelection();
843 }
844
845 void GridFrame::SelectCell(wxCommandEvent& WXUNUSED(event))
846 {
847 grid->SelectBlock(3, 1, 3, 1, m_addToSel);
848 }
849
850 void GridFrame::SelectCol(wxCommandEvent& WXUNUSED(event))
851 {
852 grid->SelectCol(2, m_addToSel);
853 }
854
855 void GridFrame::SelectRow(wxCommandEvent& WXUNUSED(event))
856 {
857 grid->SelectRow(2, m_addToSel);
858 }
859
860 void GridFrame::SelectAll(wxCommandEvent& WXUNUSED(event))
861 {
862 grid->SelectAll();
863 }
864
865 void GridFrame::OnAddToSelectToggle(wxCommandEvent& event)
866 {
867 m_addToSel = event.IsChecked();
868 }
869
870 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
871 {
872 wxString logBuf;
873 if ( ev.GetRow() != -1 )
874 {
875 logBuf << _T("Left click on row label ") << ev.GetRow();
876 }
877 else if ( ev.GetCol() != -1 )
878 {
879 logBuf << _T("Left click on col label ") << ev.GetCol();
880 }
881 else
882 {
883 logBuf << _T("Left click on corner label");
884 }
885
886 if ( ev.ShiftDown() )
887 logBuf << _T(" (shift down)");
888 if ( ev.ControlDown() )
889 logBuf << _T(" (control down)");
890 wxLogMessage( wxT("%s"), logBuf.c_str() );
891
892 // you must call event skip if you want default grid processing
893 //
894 ev.Skip();
895 }
896
897
898 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
899 {
900 wxLogMessage(_T("Left click at row %d, col %d"), ev.GetRow(), ev.GetCol());
901
902 // you must call event skip if you want default grid processing
903 // (cell highlighting etc.)
904 //
905 ev.Skip();
906 }
907
908
909 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
910 {
911 wxLogMessage(_T("Resized row %d"), ev.GetRowOrCol());
912
913 ev.Skip();
914 }
915
916
917 void GridFrame::OnColSize( wxGridSizeEvent& ev )
918 {
919 wxLogMessage(_T("Resized col %d"), ev.GetRowOrCol());
920
921 ev.Skip();
922 }
923
924
925 void GridFrame::OnShowSelection(wxCommandEvent& WXUNUSED(event))
926 {
927 // max number of elements to dump -- otherwise it can take too much time
928 static const size_t countMax = 100;
929
930 bool rows = false;
931
932 switch ( grid->GetSelectionMode() )
933 {
934 case wxGrid::wxGridSelectCells:
935 {
936 const wxGridCellCoordsArray cells(grid->GetSelectedCells());
937 size_t count = cells.size();
938 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count);
939 if ( count > countMax )
940 {
941 wxLogMessage(_T("[too many selected cells, ")
942 _T("showing only the first %lu]"),
943 (unsigned long)countMax);
944 count = countMax;
945 }
946
947 for ( size_t n = 0; n < count; n++ )
948 {
949 const wxGridCellCoords& c = cells[n];
950 wxLogMessage(_T(" selected cell %lu: (%d, %d)"),
951 (unsigned long)n, c.GetCol(), c.GetRow());
952 }
953 }
954 break;
955
956 case wxGrid::wxGridSelectRows:
957 rows = true;
958 // fall through
959
960 case wxGrid::wxGridSelectColumns:
961 {
962 const wxChar *plural, *single;
963 if ( rows )
964 {
965 plural = _T("rows");
966 single = _T("row");
967 }
968 else // columns
969 {
970 plural = _T("columns");
971 single = _T("column");
972 }
973
974 const wxArrayInt sels((const wxArrayInt)(rows ? grid->GetSelectedRows()
975 : grid->GetSelectedCols()));
976 size_t count = sels.size();
977 wxLogMessage(_T("%lu %s selected:"),
978 (unsigned long)count, plural);
979 if ( count > countMax )
980 {
981 wxLogMessage(_T("[too many selected %s, ")
982 _T("showing only the first %lu]"),
983 plural, (unsigned long)countMax);
984 count = countMax;
985 }
986
987 for ( size_t n = 0; n < count; n++ )
988 {
989 wxLogMessage(_T(" selected %s %lu: %d"),
990 single, (unsigned long)n, sels[n]);
991 }
992 }
993 break;
994
995 default:
996 wxFAIL_MSG( _T("unknown wxGrid selection mode") );
997 break;
998 }
999 }
1000
1001 void GridFrame::OnSelectCell( wxGridEvent& ev )
1002 {
1003 wxString logBuf;
1004 if ( ev.Selecting() )
1005 logBuf << _T("Selected ");
1006 else
1007 logBuf << _T("Deselected ");
1008 logBuf << _T("cell at row ") << ev.GetRow()
1009 << _T(" col ") << ev.GetCol()
1010 << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
1011 << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
1012 << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
1013 << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
1014
1015 //Indicate whether this column was moved
1016 if ( ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() ) != ev.GetCol() )
1017 logBuf << _T(" *** Column moved, current position: ") << ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() );
1018
1019 wxLogMessage( wxT("%s"), logBuf.c_str() );
1020
1021 // you must call Skip() if you want the default processing
1022 // to occur in wxGrid
1023 ev.Skip();
1024 }
1025
1026 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
1027 {
1028 wxString logBuf;
1029 if ( ev.Selecting() )
1030 logBuf << _T("Selected ");
1031 else
1032 logBuf << _T("Deselected ");
1033 logBuf << _T("cells from row ") << ev.GetTopRow()
1034 << _T(" col ") << ev.GetLeftCol()
1035 << _T(" to row ") << ev.GetBottomRow()
1036 << _T(" col ") << ev.GetRightCol()
1037 << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
1038 << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
1039 << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
1040 << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
1041 wxLogMessage( wxT("%s"), logBuf.c_str() );
1042
1043 ev.Skip();
1044 }
1045
1046 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
1047 {
1048 int row = ev.GetRow(),
1049 col = ev.GetCol();
1050
1051 wxLogMessage(_T("Value changed for cell at row %d, col %d: now \"%s\""),
1052 row, col, grid->GetCellValue(row, col).c_str());
1053
1054 ev.Skip();
1055 }
1056
1057 void GridFrame::OnCellBeginDrag( wxGridEvent& ev )
1058 {
1059 wxLogMessage(_T("Got request to drag cell at row %d, col %d"),
1060 ev.GetRow(), ev.GetCol());
1061
1062 ev.Skip();
1063 }
1064
1065 void GridFrame::OnEditorShown( wxGridEvent& ev )
1066 {
1067
1068 if ( (ev.GetCol() == 4) &&
1069 (ev.GetRow() == 0) &&
1070 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
1071 _T("Checking"),wxYES_NO) == wxNO ) ) {
1072
1073 ev.Veto();
1074 return;
1075 }
1076
1077 wxLogMessage( wxT("Cell editor shown.") );
1078
1079 ev.Skip();
1080 }
1081
1082 void GridFrame::OnEditorHidden( wxGridEvent& ev )
1083 {
1084
1085 if ( (ev.GetCol() == 4) &&
1086 (ev.GetRow() == 0) &&
1087 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
1088 _T("Checking"),wxYES_NO) == wxNO ) ) {
1089
1090 ev.Veto();
1091 return;
1092 }
1093
1094 wxLogMessage( wxT("Cell editor hidden.") );
1095
1096 ev.Skip();
1097 }
1098
1099 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
1100 {
1101 (void)wxMessageBox( _T("\n\nwxGrid demo \n\n")
1102 _T("Michael Bedward, Julian Smart, Vadim Zeitlin"),
1103 _T("About"),
1104 wxOK );
1105 }
1106
1107
1108 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
1109 {
1110 Close( true );
1111 }
1112
1113 void GridFrame::OnBugsTable(wxCommandEvent& )
1114 {
1115 BugsGridFrame *frame = new BugsGridFrame;
1116 frame->Show(true);
1117 }
1118
1119 void GridFrame::OnSmallGrid(wxCommandEvent& )
1120 {
1121 wxFrame* frame = new wxFrame(NULL, wxID_ANY, _T("A Small Grid"),
1122 wxDefaultPosition, wxSize(640, 480));
1123 wxPanel* panel = new wxPanel(frame, wxID_ANY);
1124 wxGrid* grid = new wxGrid(panel, wxID_ANY, wxPoint(10,10), wxSize(400,400),
1125 wxWANTS_CHARS | wxSIMPLE_BORDER);
1126 grid->CreateGrid(3,3);
1127 frame->Show(true);
1128 }
1129
1130 // ----------------------------------------------------------------------------
1131 // MyGridCellAttrProvider
1132 // ----------------------------------------------------------------------------
1133
1134 MyGridCellAttrProvider::MyGridCellAttrProvider()
1135 {
1136 m_attrForOddRows = new wxGridCellAttr;
1137 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
1138 }
1139
1140 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1141 {
1142 m_attrForOddRows->DecRef();
1143 }
1144
1145 wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col,
1146 wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const
1147 {
1148 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
1149
1150 if ( row % 2 )
1151 {
1152 if ( !attr )
1153 {
1154 attr = m_attrForOddRows;
1155 attr->IncRef();
1156 }
1157 else
1158 {
1159 if ( !attr->HasBackgroundColour() )
1160 {
1161 wxGridCellAttr *attrNew = attr->Clone();
1162 attr->DecRef();
1163 attr = attrNew;
1164 attr->SetBackgroundColour(*wxLIGHT_GREY);
1165 }
1166 }
1167 }
1168
1169 return attr;
1170 }
1171
1172 // ----------------------------------------------------------------------------
1173
1174 void GridFrame::OnTabularGrid(wxCommandEvent& )
1175 {
1176 wxFrame* frame = new wxFrame(NULL, wxID_ANY, _T("A small tabular Grid"),
1177 wxDefaultPosition, wxSize(640, 480));
1178 wxGrid* grid = new wxGrid(frame, wxID_ANY, wxPoint(10,10), wxSize(40,40),
1179 wxWANTS_CHARS | wxBORDER_SUNKEN);
1180 grid->SetRowLabelSize( 0 );
1181 grid->DisableDragRowSize();
1182 grid->SetUseNativeColLabels();
1183 grid->CreateGrid(10,10);
1184 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
1185
1186 frame->Show(true);
1187 }
1188
1189 void GridFrame::OnVTable(wxCommandEvent& )
1190 {
1191 static long s_sizeGrid = 10000;
1192
1193 #ifdef __WXMOTIF__
1194 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
1195 wxString s;
1196 s << s_sizeGrid;
1197 s = wxGetTextFromUser( _T("Size of the table to create"),
1198 _T("Size:"),
1199 s );
1200
1201 s.ToLong( &s_sizeGrid );
1202
1203 #else
1204 s_sizeGrid = wxGetNumberFromUser(_T("Size of the table to create"),
1205 _T("Size: "),
1206 _T("wxGridDemo question"),
1207 s_sizeGrid,
1208 0, 32000, this);
1209 #endif
1210
1211 if ( s_sizeGrid != -1 )
1212 {
1213 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
1214 win->Show(true);
1215 }
1216 }
1217
1218 // ----------------------------------------------------------------------------
1219 // MyGridCellRenderer
1220 // ----------------------------------------------------------------------------
1221
1222 // do something that the default renderer doesn't here just to show that it is
1223 // possible to alter the appearance of the cell beyond what the attributes
1224 // allow
1225 void MyGridCellRenderer::Draw(wxGrid& grid,
1226 wxGridCellAttr& attr,
1227 wxDC& dc,
1228 const wxRect& rect,
1229 int row, int col,
1230 bool isSelected)
1231 {
1232 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1233
1234 dc.SetPen(*wxGREEN_PEN);
1235 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1236 dc.DrawEllipse(rect);
1237 }
1238
1239 // ============================================================================
1240 // BigGridFrame and BigGridTable: Sample of a non-standard table
1241 // ============================================================================
1242
1243 BigGridFrame::BigGridFrame(long sizeGrid)
1244 : wxFrame(NULL, wxID_ANY, _T("Plugin Virtual Table"),
1245 wxDefaultPosition, wxSize(500, 450))
1246 {
1247 m_grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
1248 m_table = new BigGridTable(sizeGrid);
1249
1250 // VZ: I don't understand why this slows down the display that much,
1251 // must profile it...
1252 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1253
1254 m_grid->SetTable(m_table, true);
1255
1256 #if defined __WXMOTIF__
1257 // MB: the grid isn't getting a sensible default size under wxMotif
1258 int cw, ch;
1259 GetClientSize( &cw, &ch );
1260 m_grid->SetSize( cw, ch );
1261 #endif
1262 }
1263
1264 // ============================================================================
1265 // BugsGridFrame: a "realistic" table
1266 // ============================================================================
1267
1268 // ----------------------------------------------------------------------------
1269 // bugs table data
1270 // ----------------------------------------------------------------------------
1271
1272 enum Columns
1273 {
1274 Col_Id,
1275 Col_Summary,
1276 Col_Severity,
1277 Col_Priority,
1278 Col_Platform,
1279 Col_Opened,
1280 Col_Max
1281 };
1282
1283 enum Severity
1284 {
1285 Sev_Wish,
1286 Sev_Minor,
1287 Sev_Normal,
1288 Sev_Major,
1289 Sev_Critical,
1290 Sev_Max
1291 };
1292
1293 static const wxString severities[] =
1294 {
1295 _T("wishlist"),
1296 _T("minor"),
1297 _T("normal"),
1298 _T("major"),
1299 _T("critical"),
1300 };
1301
1302 static struct BugsGridData
1303 {
1304 int id;
1305 wxChar summary[80];
1306 Severity severity;
1307 int prio;
1308 wxChar platform[12];
1309 bool opened;
1310 } gs_dataBugsGrid [] =
1311 {
1312 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), true },
1313 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), false },
1314 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), true },
1315 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), false },
1316 };
1317
1318 static const wxChar *headers[Col_Max] =
1319 {
1320 _T("Id"),
1321 _T("Summary"),
1322 _T("Severity"),
1323 _T("Priority"),
1324 _T("Platform"),
1325 _T("Opened?"),
1326 };
1327
1328 // ----------------------------------------------------------------------------
1329 // BugsGridTable
1330 // ----------------------------------------------------------------------------
1331
1332 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
1333 {
1334 switch ( col )
1335 {
1336 case Col_Id:
1337 case Col_Priority:
1338 return wxGRID_VALUE_NUMBER;;
1339
1340 case Col_Severity:
1341 // fall thorugh (TODO should be a list)
1342
1343 case Col_Summary:
1344 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
1345
1346 case Col_Platform:
1347 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
1348
1349 case Col_Opened:
1350 return wxGRID_VALUE_BOOL;
1351 }
1352
1353 wxFAIL_MSG(_T("unknown column"));
1354
1355 return wxEmptyString;
1356 }
1357
1358 int BugsGridTable::GetNumberRows()
1359 {
1360 return WXSIZEOF(gs_dataBugsGrid);
1361 }
1362
1363 int BugsGridTable::GetNumberCols()
1364 {
1365 return Col_Max;
1366 }
1367
1368 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row), int WXUNUSED(col) )
1369 {
1370 return false;
1371 }
1372
1373 wxString BugsGridTable::GetValue( int row, int col )
1374 {
1375 const BugsGridData& gd = gs_dataBugsGrid[row];
1376
1377 switch ( col )
1378 {
1379 case Col_Id:
1380 return wxString::Format(_T("%d"), gd.id);
1381
1382 case Col_Priority:
1383 return wxString::Format(_T("%d"), gd.prio);
1384
1385 case Col_Opened:
1386 return gd.opened ? _T("1") : _T("0");
1387
1388 case Col_Severity:
1389 return severities[gd.severity];
1390
1391 case Col_Summary:
1392 return gd.summary;
1393
1394 case Col_Platform:
1395 return gd.platform;
1396 }
1397
1398 return wxEmptyString;
1399 }
1400
1401 void BugsGridTable::SetValue( int row, int col, const wxString& value )
1402 {
1403 BugsGridData& gd = gs_dataBugsGrid[row];
1404
1405 switch ( col )
1406 {
1407 case Col_Id:
1408 case Col_Priority:
1409 case Col_Opened:
1410 wxFAIL_MSG(_T("unexpected column"));
1411 break;
1412
1413 case Col_Severity:
1414 {
1415 size_t n;
1416 for ( n = 0; n < WXSIZEOF(severities); n++ )
1417 {
1418 if ( severities[n] == value )
1419 {
1420 gd.severity = (Severity)n;
1421 break;
1422 }
1423 }
1424
1425 if ( n == WXSIZEOF(severities) )
1426 {
1427 wxLogWarning(_T("Invalid severity value '%s'."),
1428 value.c_str());
1429 gd.severity = Sev_Normal;
1430 }
1431 }
1432 break;
1433
1434 case Col_Summary:
1435 wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
1436 break;
1437
1438 case Col_Platform:
1439 wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
1440 break;
1441 }
1442 }
1443
1444 bool
1445 BugsGridTable::CanGetValueAs(int WXUNUSED(row),
1446 int col,
1447 const wxString& typeName)
1448 {
1449 if ( typeName == wxGRID_VALUE_STRING )
1450 {
1451 return true;
1452 }
1453 else if ( typeName == wxGRID_VALUE_BOOL )
1454 {
1455 return col == Col_Opened;
1456 }
1457 else if ( typeName == wxGRID_VALUE_NUMBER )
1458 {
1459 return col == Col_Id || col == Col_Priority || col == Col_Severity;
1460 }
1461 else
1462 {
1463 return false;
1464 }
1465 }
1466
1467 bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
1468 {
1469 return CanGetValueAs(row, col, typeName);
1470 }
1471
1472 long BugsGridTable::GetValueAsLong( int row, int col )
1473 {
1474 const BugsGridData& gd = gs_dataBugsGrid[row];
1475
1476 switch ( col )
1477 {
1478 case Col_Id:
1479 return gd.id;
1480
1481 case Col_Priority:
1482 return gd.prio;
1483
1484 case Col_Severity:
1485 return gd.severity;
1486
1487 default:
1488 wxFAIL_MSG(_T("unexpected column"));
1489 return -1;
1490 }
1491 }
1492
1493 bool BugsGridTable::GetValueAsBool( int row, int col )
1494 {
1495 if ( col == Col_Opened )
1496 {
1497 return gs_dataBugsGrid[row].opened;
1498 }
1499 else
1500 {
1501 wxFAIL_MSG(_T("unexpected column"));
1502
1503 return false;
1504 }
1505 }
1506
1507 void BugsGridTable::SetValueAsLong( int row, int col, long value )
1508 {
1509 BugsGridData& gd = gs_dataBugsGrid[row];
1510
1511 switch ( col )
1512 {
1513 case Col_Priority:
1514 gd.prio = value;
1515 break;
1516
1517 default:
1518 wxFAIL_MSG(_T("unexpected column"));
1519 }
1520 }
1521
1522 void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1523 {
1524 if ( col == Col_Opened )
1525 {
1526 gs_dataBugsGrid[row].opened = value;
1527 }
1528 else
1529 {
1530 wxFAIL_MSG(_T("unexpected column"));
1531 }
1532 }
1533
1534 wxString BugsGridTable::GetColLabelValue( int col )
1535 {
1536 return headers[col];
1537 }
1538
1539 // ----------------------------------------------------------------------------
1540 // BugsGridFrame
1541 // ----------------------------------------------------------------------------
1542
1543 BugsGridFrame::BugsGridFrame()
1544 : wxFrame(NULL, wxID_ANY, _T("Bugs table"))
1545 {
1546 wxGrid *grid = new wxGrid(this, wxID_ANY, wxDefaultPosition);
1547 wxGridTableBase *table = new BugsGridTable();
1548 table->SetAttrProvider(new MyGridCellAttrProvider);
1549 grid->SetTable(table, true);
1550
1551 wxGridCellAttr *attrRO = new wxGridCellAttr,
1552 *attrRangeEditor = new wxGridCellAttr,
1553 *attrCombo = new wxGridCellAttr;
1554
1555 attrRO->SetReadOnly();
1556 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
1557 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1558 severities));
1559
1560 grid->SetColAttr(Col_Id, attrRO);
1561 grid->SetColAttr(Col_Priority, attrRangeEditor);
1562 grid->SetColAttr(Col_Severity, attrCombo);
1563
1564 grid->Fit();
1565 SetClientSize(grid->GetSize());
1566 }
1567
1568