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