use wxAboutBox() to make it evident a wxGrid refreshing bug under wxMSW (probably...
[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 #include "wx/aboutdlg.h"
34
35 #include "wx/grid.h"
36 #include "wx/generic/gridctrl.h"
37
38 #include "griddemo.h"
39
40 // ----------------------------------------------------------------------------
41 // wxWin macros
42 // ----------------------------------------------------------------------------
43
44 IMPLEMENT_APP( GridApp )
45
46 // ============================================================================
47 // implementation
48 // ============================================================================
49
50 // ----------------------------------------------------------------------------
51 // GridApp
52 // ----------------------------------------------------------------------------
53
54 bool GridApp::OnInit()
55 {
56 GridFrame *frame = new GridFrame;
57 frame->Show(true);
58
59 return true;
60 }
61
62 // ----------------------------------------------------------------------------
63 // GridFrame
64 // ----------------------------------------------------------------------------
65
66 BEGIN_EVENT_TABLE( GridFrame, wxFrame )
67 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
68 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
69 EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
70 EVT_MENU( ID_TOGGLEROWSIZING, GridFrame::ToggleRowSizing )
71 EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing )
72 EVT_MENU( ID_TOGGLECOLMOVING, GridFrame::ToggleColMoving )
73 EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
74 EVT_MENU( ID_TOGGLEGRIDDRAGCELL, GridFrame::ToggleGridDragCell )
75 EVT_MENU( ID_TOGGLENATIVEHEADER, GridFrame::ToggleNativeHeader )
76 EVT_MENU( ID_TOGGLEGRIDLINES, GridFrame::ToggleGridLines )
77 EVT_MENU( ID_AUTOSIZECOLS, GridFrame::AutoSizeCols )
78 EVT_MENU( ID_CELLOVERFLOW, GridFrame::CellOverflow )
79 EVT_MENU( ID_RESIZECELL, GridFrame::ResizeCell )
80 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
81 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
82 EVT_MENU( ID_SETLABEL_FONT, GridFrame::SetLabelFont )
83 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
84 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
85 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
86 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
87 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
88 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
89 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
90 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
91 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
92 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
93 EVT_MENU( ID_SELCELLS, GridFrame::SelectCells )
94 EVT_MENU( ID_SELROWS, GridFrame::SelectRows )
95 EVT_MENU( ID_SELCOLS, GridFrame::SelectCols )
96 EVT_MENU( ID_SELROWSORCOLS, GridFrame::SelectRowsOrCols )
97
98 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
99 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
100
101 EVT_MENU( wxID_ABOUT, GridFrame::About )
102 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
103 EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
104 EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
105 EVT_MENU( ID_TABULAR_TABLE, GridFrame::OnTabularTable)
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_TABULAR_TABLE, _T("&Tabular table 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 col&umns") );
229 selectionMenu->Append( ID_SELROWSORCOLS, _T("Select rows &or columns") );
230
231 wxMenu *autosizeMenu = new wxMenu;
232 autosizeMenu->Append( ID_SIZE_ROW, _T("Selected &row data") );
233 autosizeMenu->Append( ID_SIZE_COL, _T("Selected &column data") );
234 autosizeMenu->Append( ID_SIZE_ROW_LABEL, _T("Selected row la&bel") );
235 autosizeMenu->Append( ID_SIZE_COL_LABEL, _T("Selected column &label") );
236 autosizeMenu->Append( ID_SIZE_LABELS_COL, _T("Column la&bels") );
237 autosizeMenu->Append( ID_SIZE_LABELS_ROW, _T("Row label&s") );
238 autosizeMenu->Append( ID_SIZE_GRID, _T("Entire &grid") );
239
240 wxMenu *helpMenu = new wxMenu;
241 helpMenu->Append( wxID_ABOUT, _T("&About wxGrid demo") );
242
243 wxMenuBar *menuBar = new wxMenuBar;
244 menuBar->Append( fileMenu, _T("&File") );
245 menuBar->Append( viewMenu, _T("&Grid") );
246 menuBar->Append( colMenu, _T("&Colours") );
247 menuBar->Append( editMenu, _T("&Edit") );
248 menuBar->Append( selectMenu, _T("&Select") );
249 menuBar->Append( autosizeMenu, _T("&Autosize") );
250 menuBar->Append( helpMenu, _T("&Help") );
251
252 SetMenuBar( menuBar );
253
254 m_addToSel = false;
255
256 grid = new wxGrid( this,
257 wxID_ANY,
258 wxPoint( 0, 0 ),
259 wxSize( 400, 300 ) );
260
261 #if wxUSE_LOG
262 int gridW = 600, gridH = 300;
263 int logW = gridW, logH = 100;
264
265 logWin = new wxTextCtrl( this,
266 wxID_ANY,
267 wxEmptyString,
268 wxPoint( 0, gridH + 20 ),
269 wxSize( logW, logH ),
270 wxTE_MULTILINE );
271
272 logger = new wxLogTextCtrl( logWin );
273 m_logOld = wxLog::SetActiveTarget( logger );
274 wxLog::DisableTimestamp();
275 #endif // wxUSE_LOG
276
277 // this will create a grid and, by default, an associated grid
278 // table for strings
279 grid->CreateGrid( 0, 0 );
280 grid->AppendRows(100);
281 grid->AppendCols(100);
282
283 int ir = grid->GetNumberRows();
284 grid->DeleteRows(0, ir);
285 grid->AppendRows(ir);
286
287 grid->SetRowSize( 0, 60 );
288 grid->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
289
290 grid->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
291 grid->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer);
292 grid->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor);
293
294 grid->SetCellValue( 0, 2, _T("Blah") );
295 grid->SetCellValue( 0, 3, _T("Read only") );
296 grid->SetReadOnly( 0, 3 );
297
298 grid->SetCellValue( 0, 4, _T("Can veto edit this cell") );
299
300 grid->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
301
302 grid->SetRowSize( 99, 60 );
303 grid->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
304 grid->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
305
306 grid->SetCellTextColour(1, 2, *wxRED);
307 grid->SetCellBackgroundColour(1, 2, *wxGREEN);
308
309 grid->SetCellValue( 1, 4, _T("I'm in the middle"));
310
311 grid->SetCellValue(2, 2, _T("red"));
312
313 grid->SetCellTextColour(2, 2, *wxRED);
314 grid->SetCellValue(3, 3, _T("green on grey"));
315 grid->SetCellTextColour(3, 3, *wxGREEN);
316 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
317
318 grid->SetCellValue(4, 4, _T("a weird looking cell"));
319 grid->SetCellAlignment(4, 4, wxALIGN_CENTRE, wxALIGN_CENTRE);
320 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
321
322 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
323 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
324
325 wxGridCellAttr *attr;
326 attr = new wxGridCellAttr;
327 attr->SetTextColour(*wxBLUE);
328 grid->SetColAttr(5, attr);
329 attr = new wxGridCellAttr;
330 attr->SetBackgroundColour(*wxRED);
331 grid->SetRowAttr(5, attr);
332
333 grid->SetCellValue(2, 4, _T("a wider column"));
334 grid->SetColSize(4, 120);
335 grid->SetColMinimalWidth(4, 120);
336
337 grid->SetCellTextColour(5, 8, *wxGREEN);
338 grid->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
339 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"));
340
341 grid->SetColFormatFloat(6);
342 grid->SetCellValue(0, 6, wxString::Format(wxT("%g"), 3.1415));
343 grid->SetCellValue(1, 6, wxString::Format(wxT("%g"), 1415.0));
344 grid->SetCellValue(2, 6, wxString::Format(wxT("%g"), 12345.67890));
345
346 grid->SetColFormatFloat(7, 6, 2);
347 grid->SetCellValue(0, 7, wxString::Format(wxT("%g"), 3.1415));
348 grid->SetCellValue(1, 7, wxString::Format(wxT("%g"), 1415.0));
349 grid->SetCellValue(2, 7, wxString::Format(wxT("%g"), 12345.67890));
350
351 grid->SetColFormatNumber(8);
352 grid->SetCellValue(0, 8, "17");
353 grid->SetCellValue(1, 8, "0");
354 grid->SetCellValue(2, 8, "-666");
355
356 const wxString choices[] =
357 {
358 _T("Please select a choice"),
359 _T("This takes two cells"),
360 _T("Another choice"),
361 };
362 grid->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices), choices));
363 grid->SetCellSize(4, 0, 1, 2);
364 grid->SetCellValue(4, 0, choices[0]);
365 grid->SetCellOverflow(4, 0, false);
366
367 grid->SetCellSize(7, 1, 3, 4);
368 grid->SetCellAlignment(7, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
369 grid->SetCellValue(7, 1, _T("Big box!"));
370
371 // this does exactly nothing except testing that SetAttr() handles NULL
372 // attributes and does reference counting correctly
373 grid->SetAttr(11, 11, NULL);
374 grid->SetAttr(11, 11, new wxGridCellAttr);
375 grid->SetAttr(11, 11, NULL);
376
377 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
378 topSizer->Add( grid,
379 1,
380 wxEXPAND );
381
382 #if wxUSE_LOG
383 topSizer->Add( logWin,
384 0,
385 wxEXPAND );
386 #endif // wxUSE_LOG
387
388 SetSizerAndFit( topSizer );
389
390 Centre();
391 SetDefaults();
392 }
393
394
395 GridFrame::~GridFrame()
396 {
397 #if wxUSE_LOG
398 delete wxLog::SetActiveTarget(m_logOld);
399 #endif // wxUSE_LOG
400 }
401
402
403 void GridFrame::SetDefaults()
404 {
405 GetMenuBar()->Check( ID_TOGGLEROWLABELS, true );
406 GetMenuBar()->Check( ID_TOGGLECOLLABELS, true );
407 GetMenuBar()->Check( ID_TOGGLEEDIT, true );
408 GetMenuBar()->Check( ID_TOGGLEROWSIZING, true );
409 GetMenuBar()->Check( ID_TOGGLECOLSIZING, true );
410 GetMenuBar()->Check( ID_TOGGLECOLMOVING, false );
411 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, true );
412 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL, false );
413 GetMenuBar()->Check( ID_TOGGLENATIVEHEADER, false );
414 GetMenuBar()->Check( ID_TOGGLEGRIDLINES, true );
415 GetMenuBar()->Check( ID_CELLOVERFLOW, true );
416 }
417
418
419 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
420 {
421 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
422 {
423 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
424 }
425 else
426 {
427 grid->SetRowLabelSize( 0 );
428 }
429 }
430
431
432 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
433 {
434 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
435 {
436 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
437 }
438 else
439 {
440 grid->SetColLabelSize( 0 );
441 }
442 }
443
444
445 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
446 {
447 grid->EnableEditing(
448 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
449 }
450
451
452 void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
453 {
454 grid->EnableDragRowSize(
455 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
456 }
457
458
459 void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
460 {
461 grid->EnableDragColSize(
462 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
463 }
464
465 void GridFrame::ToggleColMoving( wxCommandEvent& WXUNUSED(ev) )
466 {
467 grid->EnableDragColMove(
468 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING ) );
469 }
470
471 void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
472 {
473 grid->EnableDragGridSize(
474 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
475 }
476
477 void GridFrame::ToggleGridDragCell( wxCommandEvent& WXUNUSED(ev) )
478 {
479 grid->EnableDragCell(
480 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL ) );
481 }
482
483 void GridFrame::ToggleNativeHeader( wxCommandEvent& WXUNUSED(ev) )
484 {
485 grid->SetUseNativeColLabels(
486 GetMenuBar()->IsChecked( ID_TOGGLENATIVEHEADER ) );
487 }
488
489 void GridFrame::ToggleGridLines( wxCommandEvent& WXUNUSED(ev) )
490 {
491 grid->EnableGridLines(
492 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES ) );
493 }
494
495 void GridFrame::OnSetHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
496 {
497 wxString choices[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
498
499 wxSingleChoiceDialog dlg(this, _T("Choose the thickness of the highlight pen:"),
500 _T("Pen Width"), 11, choices);
501
502 int current = grid->GetCellHighlightPenWidth();
503 dlg.SetSelection(current);
504 if (dlg.ShowModal() == wxID_OK) {
505 grid->SetCellHighlightPenWidth(dlg.GetSelection());
506 }
507 }
508
509 void GridFrame::OnSetROHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
510 {
511 wxString choices[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
512
513 wxSingleChoiceDialog dlg(this, _T("Choose the thickness of the highlight pen:"),
514 _T("Pen Width"), 11, choices);
515
516 int current = grid->GetCellHighlightROPenWidth();
517 dlg.SetSelection(current);
518 if (dlg.ShowModal() == wxID_OK) {
519 grid->SetCellHighlightROPenWidth(dlg.GetSelection());
520 }
521 }
522
523
524
525 void GridFrame::AutoSizeCols( wxCommandEvent& WXUNUSED(ev) )
526 {
527 grid->AutoSizeColumns();
528 grid->Refresh();
529 }
530
531 void GridFrame::CellOverflow( wxCommandEvent& ev )
532 {
533 grid->SetDefaultCellOverflow(ev.IsChecked());
534 grid->Refresh();
535 }
536
537 void GridFrame::ResizeCell( wxCommandEvent& ev )
538 {
539 if (ev.IsChecked())
540 grid->SetCellSize( 7, 1, 5, 5 );
541 else
542 grid->SetCellSize( 7, 1, 1, 5 );
543 grid->Refresh();
544 }
545
546 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
547 {
548 wxColourDialog dlg( NULL );
549 if ( dlg.ShowModal() == wxID_OK )
550 {
551 wxColourData retData;
552 retData = dlg.GetColourData();
553 wxColour colour = retData.GetColour();
554
555 grid->SetLabelBackgroundColour( colour );
556 }
557 }
558
559
560 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
561 {
562 wxColourDialog dlg( NULL );
563 if ( dlg.ShowModal() == wxID_OK )
564 {
565 wxColourData retData;
566 retData = dlg.GetColourData();
567 wxColour colour = retData.GetColour();
568
569 grid->SetLabelTextColour( colour );
570 }
571 }
572
573 void GridFrame::SetLabelFont( wxCommandEvent& WXUNUSED(ev) )
574 {
575 wxFont font = wxGetFontFromUser(this);
576 if ( font.Ok() )
577 {
578 grid->SetLabelFont(font);
579 }
580 }
581
582 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
583 {
584 int horiz, vert;
585 grid->GetRowLabelAlignment( &horiz, &vert );
586
587 switch ( horiz )
588 {
589 case wxALIGN_LEFT:
590 horiz = wxALIGN_CENTRE;
591 break;
592
593 case wxALIGN_CENTRE:
594 horiz = wxALIGN_RIGHT;
595 break;
596
597 case wxALIGN_RIGHT:
598 horiz = wxALIGN_LEFT;
599 break;
600 }
601
602 grid->SetRowLabelAlignment( horiz, vert );
603 }
604
605 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
606 {
607 int horiz, vert;
608 grid->GetRowLabelAlignment( &horiz, &vert );
609
610 switch ( vert )
611 {
612 case wxALIGN_TOP:
613 vert = wxALIGN_CENTRE;
614 break;
615
616 case wxALIGN_CENTRE:
617 vert = wxALIGN_BOTTOM;
618 break;
619
620 case wxALIGN_BOTTOM:
621 vert = wxALIGN_TOP;
622 break;
623 }
624
625 grid->SetRowLabelAlignment( horiz, vert );
626 }
627
628
629 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
630 {
631 int horiz, vert;
632 grid->GetColLabelAlignment( &horiz, &vert );
633
634 switch ( horiz )
635 {
636 case wxALIGN_LEFT:
637 horiz = wxALIGN_CENTRE;
638 break;
639
640 case wxALIGN_CENTRE:
641 horiz = wxALIGN_RIGHT;
642 break;
643
644 case wxALIGN_RIGHT:
645 horiz = wxALIGN_LEFT;
646 break;
647 }
648
649 grid->SetColLabelAlignment( horiz, vert );
650 }
651
652
653 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
654 {
655 int horiz, vert;
656 grid->GetColLabelAlignment( &horiz, &vert );
657
658 switch ( vert )
659 {
660 case wxALIGN_TOP:
661 vert = wxALIGN_CENTRE;
662 break;
663
664 case wxALIGN_CENTRE:
665 vert = wxALIGN_BOTTOM;
666 break;
667
668 case wxALIGN_BOTTOM:
669 vert = wxALIGN_TOP;
670 break;
671 }
672
673 grid->SetColLabelAlignment( horiz, vert );
674 }
675
676
677 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
678 {
679 wxColourDialog dlg( NULL );
680 if ( dlg.ShowModal() == wxID_OK )
681 {
682 wxColourData retData;
683 retData = dlg.GetColourData();
684 wxColour colour = retData.GetColour();
685
686 grid->SetGridLineColour( colour );
687 }
688 }
689
690
691 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
692 {
693 grid->InsertRows( grid->GetGridCursorRow(), 1 );
694 }
695
696
697 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
698 {
699 grid->InsertCols( grid->GetGridCursorCol(), 1 );
700 }
701
702
703 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
704 {
705 if ( grid->IsSelection() )
706 {
707 wxGridUpdateLocker locker(grid);
708 for ( int n = 0; n < grid->GetNumberRows(); )
709 {
710 if ( grid->IsInSelection( n , 0 ) )
711 grid->DeleteRows( n, 1 );
712 else
713 n++;
714 }
715 }
716 }
717
718
719 void GridFrame::AutoSizeRow(wxCommandEvent& WXUNUSED(event))
720 {
721 wxGridUpdateLocker locker(grid);
722 const wxArrayInt sels = grid->GetSelectedRows();
723 for ( size_t n = 0, count = sels.size(); n < count; n++ )
724 {
725 grid->AutoSizeRow( sels[n], false );
726 }
727 }
728
729 void GridFrame::AutoSizeCol(wxCommandEvent& WXUNUSED(event))
730 {
731 wxGridUpdateLocker locker(grid);
732 const wxArrayInt sels = grid->GetSelectedCols();
733 for ( size_t n = 0, count = sels.size(); n < count; n++ )
734 {
735 grid->AutoSizeColumn( sels[n], false );
736 }
737 }
738
739 void GridFrame::AutoSizeRowLabel(wxCommandEvent& WXUNUSED(event))
740 {
741 wxGridUpdateLocker locker(grid);
742 const wxArrayInt sels = grid->GetSelectedRows();
743 for ( size_t n = 0, count = sels.size(); n < count; n++ )
744 {
745 grid->AutoSizeRowLabelSize( sels[n] );
746 }
747 }
748
749 void GridFrame::AutoSizeColLabel(wxCommandEvent& WXUNUSED(event))
750 {
751 wxGridUpdateLocker locker(grid);
752 const wxArrayInt sels = grid->GetSelectedCols();
753 for ( size_t n = 0, count = sels.size(); n < count; n++ )
754 {
755 grid->AutoSizeColLabelSize( sels[n] );
756 }
757 }
758
759 void GridFrame::AutoSizeLabelsCol(wxCommandEvent& WXUNUSED(event))
760 {
761 grid->SetColLabelSize( wxGRID_AUTOSIZE );
762 }
763
764 void GridFrame::AutoSizeLabelsRow(wxCommandEvent& WXUNUSED(event))
765 {
766 grid->SetRowLabelSize( wxGRID_AUTOSIZE );
767 }
768
769 void GridFrame::AutoSizeTable(wxCommandEvent& WXUNUSED(event))
770 {
771 grid->AutoSize();
772 }
773
774
775 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
776 {
777 if ( grid->IsSelection() )
778 {
779 wxGridUpdateLocker locker(grid);
780 for ( int n = 0; n < grid->GetNumberCols(); )
781 {
782 if ( grid->IsInSelection( 0 , n ) )
783 grid->DeleteCols( n, 1 );
784 else
785 n++;
786 }
787 }
788 }
789
790
791 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
792 {
793 grid->ClearGrid();
794 }
795
796 void GridFrame::SelectCells( wxCommandEvent& WXUNUSED(ev) )
797 {
798 grid->SetSelectionMode( wxGrid::wxGridSelectCells );
799 }
800
801 void GridFrame::SelectRows( wxCommandEvent& WXUNUSED(ev) )
802 {
803 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
804 }
805
806 void GridFrame::SelectCols( wxCommandEvent& WXUNUSED(ev) )
807 {
808 grid->SetSelectionMode( wxGrid::wxGridSelectColumns );
809 }
810
811 void GridFrame::SelectRowsOrCols( wxCommandEvent& WXUNUSED(ev) )
812 {
813 grid->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns );
814 }
815
816 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
817 {
818 wxColour col = wxGetColourFromUser(this);
819 if ( col.Ok() )
820 {
821 grid->SetDefaultCellTextColour(col);
822 grid->Refresh();
823 }
824 }
825
826 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
827 {
828 wxColour col = wxGetColourFromUser(this);
829 if ( col.Ok() )
830 {
831 // Check the new Refresh function by passing it a rectangle
832 // which exactly fits the grid.
833 wxPoint pt(0, 0);
834 wxRect r(pt, grid->GetSize());
835 grid->SetDefaultCellBackgroundColour(col);
836 grid->Refresh(true, &r);
837 }
838 }
839
840 void GridFrame::DeselectCell(wxCommandEvent& WXUNUSED(event))
841 {
842 grid->DeselectCell(3, 1);
843 }
844
845 void GridFrame::DeselectCol(wxCommandEvent& WXUNUSED(event))
846 {
847 grid->DeselectCol(2);
848 }
849
850 void GridFrame::DeselectRow(wxCommandEvent& WXUNUSED(event))
851 {
852 grid->DeselectRow(2);
853 }
854
855 void GridFrame::DeselectAll(wxCommandEvent& WXUNUSED(event))
856 {
857 grid->ClearSelection();
858 }
859
860 void GridFrame::SelectCell(wxCommandEvent& WXUNUSED(event))
861 {
862 grid->SelectBlock(3, 1, 3, 1, m_addToSel);
863 }
864
865 void GridFrame::SelectCol(wxCommandEvent& WXUNUSED(event))
866 {
867 grid->SelectCol(2, m_addToSel);
868 }
869
870 void GridFrame::SelectRow(wxCommandEvent& WXUNUSED(event))
871 {
872 grid->SelectRow(2, m_addToSel);
873 }
874
875 void GridFrame::SelectAll(wxCommandEvent& WXUNUSED(event))
876 {
877 grid->SelectAll();
878 }
879
880 void GridFrame::OnAddToSelectToggle(wxCommandEvent& event)
881 {
882 m_addToSel = event.IsChecked();
883 }
884
885 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
886 {
887 wxString logBuf;
888 if ( ev.GetRow() != -1 )
889 {
890 logBuf << _T("Left click on row label ") << ev.GetRow();
891 }
892 else if ( ev.GetCol() != -1 )
893 {
894 logBuf << _T("Left click on col label ") << ev.GetCol();
895 }
896 else
897 {
898 logBuf << _T("Left click on corner label");
899 }
900
901 if ( ev.ShiftDown() )
902 logBuf << _T(" (shift down)");
903 if ( ev.ControlDown() )
904 logBuf << _T(" (control down)");
905 wxLogMessage( wxT("%s"), logBuf.c_str() );
906
907 // you must call event skip if you want default grid processing
908 //
909 ev.Skip();
910 }
911
912
913 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
914 {
915 wxLogMessage(_T("Left click at row %d, col %d"), ev.GetRow(), ev.GetCol());
916
917 // you must call event skip if you want default grid processing
918 // (cell highlighting etc.)
919 //
920 ev.Skip();
921 }
922
923
924 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
925 {
926 wxLogMessage(_T("Resized row %d"), ev.GetRowOrCol());
927
928 ev.Skip();
929 }
930
931
932 void GridFrame::OnColSize( wxGridSizeEvent& ev )
933 {
934 wxLogMessage(_T("Resized col %d"), ev.GetRowOrCol());
935
936 ev.Skip();
937 }
938
939
940 void GridFrame::OnShowSelection(wxCommandEvent& WXUNUSED(event))
941 {
942 // max number of elements to dump -- otherwise it can take too much time
943 static const size_t countMax = 100;
944
945 bool rows = false;
946
947 switch ( grid->GetSelectionMode() )
948 {
949 case wxGrid::wxGridSelectCells:
950 {
951 const wxGridCellCoordsArray cells(grid->GetSelectedCells());
952 size_t count = cells.size();
953 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count);
954 if ( count > countMax )
955 {
956 wxLogMessage(_T("[too many selected cells, ")
957 _T("showing only the first %lu]"),
958 (unsigned long)countMax);
959 count = countMax;
960 }
961
962 for ( size_t n = 0; n < count; n++ )
963 {
964 const wxGridCellCoords& c = cells[n];
965 wxLogMessage(_T(" selected cell %lu: (%d, %d)"),
966 (unsigned long)n, c.GetCol(), c.GetRow());
967 }
968 }
969 break;
970
971 case wxGrid::wxGridSelectRows:
972 rows = true;
973 // fall through
974
975 case wxGrid::wxGridSelectColumns:
976 {
977 const wxChar *plural, *single;
978 if ( rows )
979 {
980 plural = _T("rows");
981 single = _T("row");
982 }
983 else // columns
984 {
985 plural = _T("columns");
986 single = _T("column");
987 }
988
989 const wxArrayInt sels((const wxArrayInt)(rows ? grid->GetSelectedRows()
990 : grid->GetSelectedCols()));
991 size_t count = sels.size();
992 wxLogMessage(_T("%lu %s selected:"),
993 (unsigned long)count, plural);
994 if ( count > countMax )
995 {
996 wxLogMessage(_T("[too many selected %s, ")
997 _T("showing only the first %lu]"),
998 plural, (unsigned long)countMax);
999 count = countMax;
1000 }
1001
1002 for ( size_t n = 0; n < count; n++ )
1003 {
1004 wxLogMessage(_T(" selected %s %lu: %d"),
1005 single, (unsigned long)n, sels[n]);
1006 }
1007 }
1008 break;
1009
1010 default:
1011 wxFAIL_MSG( _T("unknown wxGrid selection mode") );
1012 break;
1013 }
1014 }
1015
1016 void GridFrame::OnSelectCell( wxGridEvent& ev )
1017 {
1018 wxString logBuf;
1019 if ( ev.Selecting() )
1020 logBuf << _T("Selected ");
1021 else
1022 logBuf << _T("Deselected ");
1023 logBuf << _T("cell at row ") << ev.GetRow()
1024 << _T(" col ") << ev.GetCol()
1025 << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
1026 << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
1027 << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
1028 << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
1029
1030 //Indicate whether this column was moved
1031 if ( ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() ) != ev.GetCol() )
1032 logBuf << _T(" *** Column moved, current position: ") << ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() );
1033
1034 wxLogMessage( wxT("%s"), logBuf.c_str() );
1035
1036 // you must call Skip() if you want the default processing
1037 // to occur in wxGrid
1038 ev.Skip();
1039 }
1040
1041 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
1042 {
1043 wxString logBuf;
1044 if ( ev.Selecting() )
1045 logBuf << _T("Selected ");
1046 else
1047 logBuf << _T("Deselected ");
1048 logBuf << _T("cells from row ") << ev.GetTopRow()
1049 << _T(" col ") << ev.GetLeftCol()
1050 << _T(" to row ") << ev.GetBottomRow()
1051 << _T(" col ") << ev.GetRightCol()
1052 << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
1053 << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
1054 << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
1055 << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )");
1056 wxLogMessage( wxT("%s"), logBuf.c_str() );
1057
1058 ev.Skip();
1059 }
1060
1061 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
1062 {
1063 int row = ev.GetRow(),
1064 col = ev.GetCol();
1065
1066 wxLogMessage(_T("Value changed for cell at row %d, col %d: now \"%s\""),
1067 row, col, grid->GetCellValue(row, col).c_str());
1068
1069 ev.Skip();
1070 }
1071
1072 void GridFrame::OnCellBeginDrag( wxGridEvent& ev )
1073 {
1074 wxLogMessage(_T("Got request to drag cell at row %d, col %d"),
1075 ev.GetRow(), ev.GetCol());
1076
1077 ev.Skip();
1078 }
1079
1080 void GridFrame::OnEditorShown( wxGridEvent& ev )
1081 {
1082
1083 if ( (ev.GetCol() == 4) &&
1084 (ev.GetRow() == 0) &&
1085 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
1086 _T("Checking"),wxYES_NO) == wxNO ) ) {
1087
1088 ev.Veto();
1089 return;
1090 }
1091
1092 wxLogMessage( wxT("Cell editor shown.") );
1093
1094 ev.Skip();
1095 }
1096
1097 void GridFrame::OnEditorHidden( wxGridEvent& ev )
1098 {
1099
1100 if ( (ev.GetCol() == 4) &&
1101 (ev.GetRow() == 0) &&
1102 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
1103 _T("Checking"),wxYES_NO) == wxNO ) ) {
1104
1105 ev.Veto();
1106 return;
1107 }
1108
1109 wxLogMessage( wxT("Cell editor hidden.") );
1110
1111 ev.Skip();
1112 }
1113
1114 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
1115 {
1116 wxAboutDialogInfo aboutInfo;
1117 aboutInfo.SetName(wxT("wxGrid demo"));
1118 aboutInfo.SetDescription(_("wxGrid sample program"));
1119 aboutInfo.AddDeveloper(wxT("Michael Bedward"));
1120 aboutInfo.AddDeveloper(wxT("Julian Smart"));
1121 aboutInfo.AddDeveloper(wxT("Vadim Zeitlin"));
1122
1123 wxAboutBox(aboutInfo);
1124 }
1125
1126
1127 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
1128 {
1129 Close( true );
1130 }
1131
1132 void GridFrame::OnBugsTable(wxCommandEvent& )
1133 {
1134 BugsGridFrame *frame = new BugsGridFrame;
1135 frame->Show(true);
1136 }
1137
1138 // ----------------------------------------------------------------------------
1139 // MyGridCellAttrProvider
1140 // ----------------------------------------------------------------------------
1141
1142 MyGridCellAttrProvider::MyGridCellAttrProvider()
1143 {
1144 m_attrForOddRows = new wxGridCellAttr;
1145 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
1146 }
1147
1148 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1149 {
1150 m_attrForOddRows->DecRef();
1151 }
1152
1153 wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col,
1154 wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const
1155 {
1156 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
1157
1158 if ( row % 2 )
1159 {
1160 if ( !attr )
1161 {
1162 attr = m_attrForOddRows;
1163 attr->IncRef();
1164 }
1165 else
1166 {
1167 if ( !attr->HasBackgroundColour() )
1168 {
1169 wxGridCellAttr *attrNew = attr->Clone();
1170 attr->DecRef();
1171 attr = attrNew;
1172 attr->SetBackgroundColour(*wxLIGHT_GREY);
1173 }
1174 }
1175 }
1176
1177 return attr;
1178 }
1179
1180 void GridFrame::OnVTable(wxCommandEvent& )
1181 {
1182 static long s_sizeGrid = 10000;
1183
1184 s_sizeGrid = wxGetNumberFromUser(_T("Size of the table to create"),
1185 _T("Size: "),
1186 _T("wxGridDemo question"),
1187 s_sizeGrid,
1188 0, 32000, this);
1189
1190 if ( s_sizeGrid != -1 )
1191 {
1192 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
1193 win->Show(true);
1194 }
1195 }
1196
1197 // ----------------------------------------------------------------------------
1198 // MyGridCellRenderer
1199 // ----------------------------------------------------------------------------
1200
1201 // do something that the default renderer doesn't here just to show that it is
1202 // possible to alter the appearance of the cell beyond what the attributes
1203 // allow
1204 void MyGridCellRenderer::Draw(wxGrid& grid,
1205 wxGridCellAttr& attr,
1206 wxDC& dc,
1207 const wxRect& rect,
1208 int row, int col,
1209 bool isSelected)
1210 {
1211 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1212
1213 dc.SetPen(*wxGREEN_PEN);
1214 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1215 dc.DrawEllipse(rect);
1216 }
1217
1218 // ============================================================================
1219 // BigGridFrame and BigGridTable: Sample of a non-standard table
1220 // ============================================================================
1221
1222 BigGridFrame::BigGridFrame(long sizeGrid)
1223 : wxFrame(NULL, wxID_ANY, _T("Plugin Virtual Table"),
1224 wxDefaultPosition, wxSize(500, 450))
1225 {
1226 m_grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
1227 m_table = new BigGridTable(sizeGrid);
1228
1229 // VZ: I don't understand why this slows down the display that much,
1230 // must profile it...
1231 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1232
1233 m_grid->SetTable(m_table, true);
1234
1235 #if defined __WXMOTIF__
1236 // MB: the grid isn't getting a sensible default size under wxMotif
1237 int cw, ch;
1238 GetClientSize( &cw, &ch );
1239 m_grid->SetSize( cw, ch );
1240 #endif
1241 }
1242
1243 // ============================================================================
1244 // BugsGridFrame: a "realistic" table
1245 // ============================================================================
1246
1247 // ----------------------------------------------------------------------------
1248 // bugs table data
1249 // ----------------------------------------------------------------------------
1250
1251 enum Columns
1252 {
1253 Col_Id,
1254 Col_Summary,
1255 Col_Severity,
1256 Col_Priority,
1257 Col_Platform,
1258 Col_Opened,
1259 Col_Max
1260 };
1261
1262 enum Severity
1263 {
1264 Sev_Wish,
1265 Sev_Minor,
1266 Sev_Normal,
1267 Sev_Major,
1268 Sev_Critical,
1269 Sev_Max
1270 };
1271
1272 static const wxString severities[] =
1273 {
1274 _T("wishlist"),
1275 _T("minor"),
1276 _T("normal"),
1277 _T("major"),
1278 _T("critical"),
1279 };
1280
1281 static struct BugsGridData
1282 {
1283 int id;
1284 wxChar summary[80];
1285 Severity severity;
1286 int prio;
1287 wxChar platform[12];
1288 bool opened;
1289 } gs_dataBugsGrid [] =
1290 {
1291 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), true },
1292 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), false },
1293 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), true },
1294 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), false },
1295 };
1296
1297 static const wxChar *headers[Col_Max] =
1298 {
1299 _T("Id"),
1300 _T("Summary"),
1301 _T("Severity"),
1302 _T("Priority"),
1303 _T("Platform"),
1304 _T("Opened?"),
1305 };
1306
1307 // ----------------------------------------------------------------------------
1308 // BugsGridTable
1309 // ----------------------------------------------------------------------------
1310
1311 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
1312 {
1313 switch ( col )
1314 {
1315 case Col_Id:
1316 case Col_Priority:
1317 return wxGRID_VALUE_NUMBER;;
1318
1319 case Col_Severity:
1320 // fall thorugh (TODO should be a list)
1321
1322 case Col_Summary:
1323 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
1324
1325 case Col_Platform:
1326 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
1327
1328 case Col_Opened:
1329 return wxGRID_VALUE_BOOL;
1330 }
1331
1332 wxFAIL_MSG(_T("unknown column"));
1333
1334 return wxEmptyString;
1335 }
1336
1337 int BugsGridTable::GetNumberRows()
1338 {
1339 return WXSIZEOF(gs_dataBugsGrid);
1340 }
1341
1342 int BugsGridTable::GetNumberCols()
1343 {
1344 return Col_Max;
1345 }
1346
1347 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row), int WXUNUSED(col) )
1348 {
1349 return false;
1350 }
1351
1352 wxString BugsGridTable::GetValue( int row, int col )
1353 {
1354 const BugsGridData& gd = gs_dataBugsGrid[row];
1355
1356 switch ( col )
1357 {
1358 case Col_Id:
1359 return wxString::Format(_T("%d"), gd.id);
1360
1361 case Col_Priority:
1362 return wxString::Format(_T("%d"), gd.prio);
1363
1364 case Col_Opened:
1365 return gd.opened ? _T("1") : _T("0");
1366
1367 case Col_Severity:
1368 return severities[gd.severity];
1369
1370 case Col_Summary:
1371 return gd.summary;
1372
1373 case Col_Platform:
1374 return gd.platform;
1375 }
1376
1377 return wxEmptyString;
1378 }
1379
1380 void BugsGridTable::SetValue( int row, int col, const wxString& value )
1381 {
1382 BugsGridData& gd = gs_dataBugsGrid[row];
1383
1384 switch ( col )
1385 {
1386 case Col_Id:
1387 case Col_Priority:
1388 case Col_Opened:
1389 wxFAIL_MSG(_T("unexpected column"));
1390 break;
1391
1392 case Col_Severity:
1393 {
1394 size_t n;
1395 for ( n = 0; n < WXSIZEOF(severities); n++ )
1396 {
1397 if ( severities[n] == value )
1398 {
1399 gd.severity = (Severity)n;
1400 break;
1401 }
1402 }
1403
1404 if ( n == WXSIZEOF(severities) )
1405 {
1406 wxLogWarning(_T("Invalid severity value '%s'."),
1407 value.c_str());
1408 gd.severity = Sev_Normal;
1409 }
1410 }
1411 break;
1412
1413 case Col_Summary:
1414 wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
1415 break;
1416
1417 case Col_Platform:
1418 wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
1419 break;
1420 }
1421 }
1422
1423 bool
1424 BugsGridTable::CanGetValueAs(int WXUNUSED(row),
1425 int col,
1426 const wxString& typeName)
1427 {
1428 if ( typeName == wxGRID_VALUE_STRING )
1429 {
1430 return true;
1431 }
1432 else if ( typeName == wxGRID_VALUE_BOOL )
1433 {
1434 return col == Col_Opened;
1435 }
1436 else if ( typeName == wxGRID_VALUE_NUMBER )
1437 {
1438 return col == Col_Id || col == Col_Priority || col == Col_Severity;
1439 }
1440 else
1441 {
1442 return false;
1443 }
1444 }
1445
1446 bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
1447 {
1448 return CanGetValueAs(row, col, typeName);
1449 }
1450
1451 long BugsGridTable::GetValueAsLong( int row, int col )
1452 {
1453 const BugsGridData& gd = gs_dataBugsGrid[row];
1454
1455 switch ( col )
1456 {
1457 case Col_Id:
1458 return gd.id;
1459
1460 case Col_Priority:
1461 return gd.prio;
1462
1463 case Col_Severity:
1464 return gd.severity;
1465
1466 default:
1467 wxFAIL_MSG(_T("unexpected column"));
1468 return -1;
1469 }
1470 }
1471
1472 bool BugsGridTable::GetValueAsBool( int row, int col )
1473 {
1474 if ( col == Col_Opened )
1475 {
1476 return gs_dataBugsGrid[row].opened;
1477 }
1478 else
1479 {
1480 wxFAIL_MSG(_T("unexpected column"));
1481
1482 return false;
1483 }
1484 }
1485
1486 void BugsGridTable::SetValueAsLong( int row, int col, long value )
1487 {
1488 BugsGridData& gd = gs_dataBugsGrid[row];
1489
1490 switch ( col )
1491 {
1492 case Col_Priority:
1493 gd.prio = value;
1494 break;
1495
1496 default:
1497 wxFAIL_MSG(_T("unexpected column"));
1498 }
1499 }
1500
1501 void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1502 {
1503 if ( col == Col_Opened )
1504 {
1505 gs_dataBugsGrid[row].opened = value;
1506 }
1507 else
1508 {
1509 wxFAIL_MSG(_T("unexpected column"));
1510 }
1511 }
1512
1513 wxString BugsGridTable::GetColLabelValue( int col )
1514 {
1515 return headers[col];
1516 }
1517
1518 // ----------------------------------------------------------------------------
1519 // BugsGridFrame
1520 // ----------------------------------------------------------------------------
1521
1522 BugsGridFrame::BugsGridFrame()
1523 : wxFrame(NULL, wxID_ANY, _T("Bugs table"))
1524 {
1525 wxGrid *grid = new wxGrid(this, wxID_ANY);
1526 wxGridTableBase *table = new BugsGridTable();
1527 table->SetAttrProvider(new MyGridCellAttrProvider);
1528 grid->SetTable(table, true);
1529
1530 wxGridCellAttr *attrRO = new wxGridCellAttr,
1531 *attrRangeEditor = new wxGridCellAttr,
1532 *attrCombo = new wxGridCellAttr;
1533
1534 attrRO->SetReadOnly();
1535 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
1536 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1537 severities));
1538
1539 grid->SetColAttr(Col_Id, attrRO);
1540 grid->SetColAttr(Col_Priority, attrRangeEditor);
1541 grid->SetColAttr(Col_Severity, attrCombo);
1542
1543 grid->Fit();
1544 SetClientSize(grid->GetSize());
1545 }
1546
1547 // ============================================================================
1548 // TabularGrid: grid used for display of tabular data
1549 // ============================================================================
1550
1551 class TabularGridTable : public wxGridTableBase
1552 {
1553 public:
1554 enum
1555 {
1556 COL_NAME,
1557 COL_EXT,
1558 COL_SIZE,
1559 COL_DATE,
1560 COL_MAX
1561 };
1562
1563 enum
1564 {
1565 ROW_MAX = 3
1566 };
1567
1568 virtual int GetNumberRows() { return ROW_MAX; }
1569 virtual int GetNumberCols() { return COL_MAX; }
1570
1571 virtual wxString GetValue(int row, int col)
1572 {
1573 // notice that column parameter here always refers to the internal
1574 // column index, independently of its position on the screen
1575 static const char *filedata[][COL_MAX] =
1576 {
1577 { "autoexec", "bat", "412", "Apr 17 2004" },
1578 { "boot", "ini", "604", "May 27 2006" },
1579 { "io", "sys", "40774", "May 31 1994" },
1580 };
1581 wxCOMPILE_TIME_ASSERT( WXSIZEOF(filedata) == ROW_MAX, Mismatch );
1582
1583 return filedata[row][col];
1584 }
1585
1586 virtual void SetValue(int, int, const wxString&)
1587 {
1588 wxFAIL_MSG( "shouldn't be called" );
1589 }
1590
1591 virtual wxString GetColLabelValue(int col)
1592 {
1593 static const char *labels[] = { "Name", "Extension", "Size", "Date" };
1594 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels) == COL_MAX, Mismatch );
1595
1596 return labels[col];
1597 }
1598
1599 virtual void SetColLabelValue(int, const wxString&)
1600 {
1601 wxFAIL_MSG( "shouldn't be called" );
1602 }
1603 };
1604
1605 // specialized text control for column indexes entry
1606 class ColIndexEntry : public wxTextCtrl
1607 {
1608 public:
1609 ColIndexEntry(wxWindow *parent)
1610 : wxTextCtrl(parent, wxID_ANY, "")
1611 {
1612 SetValidator(wxTextValidator(wxFILTER_NUMERIC));
1613 }
1614
1615 int GetCol()
1616 {
1617 unsigned long col;
1618 if ( !GetValue().ToULong(&col) || col > TabularGridTable::COL_MAX )
1619 {
1620 SetFocus();
1621 return -1;
1622 }
1623
1624 return col;
1625 }
1626
1627 protected:
1628 virtual wxSize DoGetBestSize() const
1629 {
1630 wxSize size = wxTextCtrl::DoGetBestSize();
1631 size.x = 3*GetCharWidth();
1632 return size;
1633 }
1634 };
1635
1636 class TabularGridFrame : public wxFrame
1637 {
1638 public:
1639 TabularGridFrame();
1640
1641 private:
1642 enum // control ids
1643 {
1644 Id_Check_UseNative,
1645 Id_Check_EnableColMove
1646 };
1647
1648 // event handlers
1649
1650 void OnToggleUseNativeHeader(wxCommandEvent&)
1651 {
1652 m_grid->SetUseNativeColLabels(m_chkUseNative->IsChecked());
1653 }
1654
1655 void OnToggleColMove(wxCommandEvent&)
1656 {
1657 m_grid->EnableDragColMove(m_chkEnableColMove->IsChecked());
1658 }
1659
1660 void OnMoveColumn(wxCommandEvent&)
1661 {
1662 int col = m_txtColIndex->GetCol();
1663 int pos = m_txtColPos->GetCol();
1664 if ( col == -1 || pos == -1 )
1665 return;
1666
1667 m_grid->SetColPos(col, pos);
1668
1669 UpdateOrder();
1670 }
1671
1672 void OnGridColMove(wxGridEvent& event)
1673 {
1674 UpdateOrder();
1675
1676 event.Skip();
1677 }
1678
1679 void UpdateOrder()
1680 {
1681 wxString s;
1682 for ( int pos = 0; pos < TabularGridTable::COL_MAX; pos++ )
1683 s << m_grid->GetColAt(pos) << ' ';
1684
1685 m_statOrder->SetLabel(s);
1686 }
1687
1688 // controls
1689 wxGrid *m_grid;
1690 wxCheckBox *m_chkUseNative,
1691 *m_chkEnableColMove;
1692
1693 ColIndexEntry *m_txtColIndex,
1694 *m_txtColPos;
1695
1696 wxStaticText *m_statOrder;
1697
1698 DECLARE_NO_COPY_CLASS(TabularGridFrame)
1699 DECLARE_EVENT_TABLE()
1700 };
1701
1702 BEGIN_EVENT_TABLE(TabularGridFrame, wxFrame)
1703 EVT_CHECKBOX(Id_Check_UseNative, TabularGridFrame::OnToggleUseNativeHeader)
1704 EVT_CHECKBOX(Id_Check_EnableColMove, TabularGridFrame::OnToggleColMove)
1705
1706 EVT_BUTTON(wxID_APPLY, TabularGridFrame::OnMoveColumn)
1707
1708 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove)
1709 END_EVENT_TABLE()
1710
1711 TabularGridFrame::TabularGridFrame()
1712 : wxFrame(NULL, wxID_ANY, "Tabular table")
1713 {
1714 // create and initialize the grid with the specified data
1715 m_grid = new wxGrid(this, wxID_ANY);
1716 m_grid->SetTable(new TabularGridTable, true, wxGrid::wxGridSelectRows);
1717
1718 m_grid->SetUseNativeColLabels();
1719 m_grid->EnableDragColMove();
1720
1721 // add it and the other controls to the frame
1722 wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
1723 sizerTop->Add(m_grid, wxSizerFlags(1).Expand().Border());
1724
1725 wxSizer * const sizerControls = new wxBoxSizer(wxHORIZONTAL);
1726
1727 wxSizer * const sizerStyles = new wxBoxSizer(wxVERTICAL);
1728 m_chkUseNative = new wxCheckBox(this, Id_Check_UseNative,
1729 "&Use native header");
1730 m_chkUseNative->SetValue(true);
1731 sizerStyles->Add(m_chkUseNative, wxSizerFlags().Border());
1732
1733 m_chkEnableColMove = new wxCheckBox(this, Id_Check_EnableColMove,
1734 "Allow column re&ordering");
1735 m_chkEnableColMove->SetValue(true);
1736 sizerStyles->Add(m_chkEnableColMove, wxSizerFlags().Border());
1737 sizerControls->Add(sizerStyles);
1738
1739 sizerControls->AddSpacer(10);
1740
1741 wxSizer * const sizerColumns = new wxBoxSizer(wxVERTICAL);
1742 wxSizer * const sizerMoveCols = new wxBoxSizer(wxHORIZONTAL);
1743 const wxSizerFlags
1744 flagsHorz(wxSizerFlags().Border(wxLEFT | wxRIGHT).Centre());
1745 sizerMoveCols->Add(new wxStaticText(this, wxID_ANY, "&Move column"),
1746 flagsHorz);
1747 m_txtColIndex = new ColIndexEntry(this);
1748 sizerMoveCols->Add(m_txtColIndex, flagsHorz);
1749 sizerMoveCols->Add(new wxStaticText(this, wxID_ANY, "&to"), flagsHorz);
1750 m_txtColPos = new ColIndexEntry(this);
1751 sizerMoveCols->Add(m_txtColPos, flagsHorz);
1752 sizerMoveCols->Add(new wxButton(this, wxID_APPLY), flagsHorz);
1753
1754 sizerColumns->Add(sizerMoveCols, wxSizerFlags().Expand().Border(wxBOTTOM));
1755
1756 wxSizer * const sizerShowCols = new wxBoxSizer(wxHORIZONTAL);
1757 sizerShowCols->Add(new wxStaticText(this, wxID_ANY, "Current order:"),
1758 flagsHorz);
1759 m_statOrder = new wxStaticText(this, wxID_ANY, "<default>");
1760 sizerShowCols->Add(m_statOrder, flagsHorz);
1761 sizerColumns->Add(sizerShowCols, wxSizerFlags().Expand().Border(wxTOP));
1762
1763 sizerControls->Add(sizerColumns, wxSizerFlags(1).Expand().Border());
1764
1765 sizerTop->Add(sizerControls, wxSizerFlags().Expand().Border());
1766
1767 SetSizerAndFit(sizerTop);
1768 SetBackgroundColour(*wxWHITE);
1769 Show();
1770 }
1771
1772 void GridFrame::OnTabularTable(wxCommandEvent&)
1773 {
1774 new TabularGridFrame;
1775 }
1776