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