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