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