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