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