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