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