1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
5 // Modified by: Santiago Palacios
7 // Copyright: (c) Michael Bedward, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/colordlg.h"
31 #include "wx/fontdlg.h"
32 #include "wx/numdlg.h"
33 #include "wx/aboutdlg.h"
36 #include "wx/headerctrl.h"
37 #include "wx/generic/gridctrl.h"
38 #include "wx/generic/grideditors.h"
43 #include "../sample.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_APP( GridApp
)
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 bool GridApp::OnInit()
62 GridFrame
*frame
= new GridFrame
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
73 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
74 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
75 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
76 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
77 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
78 EVT_MENU( ID_TOGGLECOLMOVING
, GridFrame::ToggleColMoving
)
79 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
80 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
81 EVT_MENU( ID_TOGGLENATIVEHEADER
, GridFrame::ToggleNativeHeader
)
82 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
83 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
84 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
85 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
86 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
87 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
88 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
89 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
90 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
91 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
92 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
93 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
94 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
95 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
96 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
97 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
98 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
99 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
100 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
101 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
102 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
104 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
105 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
107 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
108 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
109 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
110 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
111 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
113 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
114 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
115 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
116 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
117 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
118 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
119 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
120 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
121 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
122 EVT_MENU( ID_SHOW_SELECTION
, GridFrame::OnShowSelection
)
124 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
125 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
126 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
127 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
128 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
129 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
130 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
132 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
133 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
135 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
136 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
137 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
138 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
139 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
140 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
141 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
142 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
143 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
145 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
146 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
150 GridFrame::GridFrame()
151 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets grid class demo"),
155 SetIcon(wxICON(sample
));
157 wxMenu
*fileMenu
= new wxMenu
;
158 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
159 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
160 fileMenu
->Append( ID_TABULAR_TABLE
, _T("&Tabular table test\tCtrl-T"));
161 fileMenu
->AppendSeparator();
162 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
164 wxMenu
*viewMenu
= new wxMenu
;
165 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
166 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
167 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
168 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
169 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
170 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
171 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
172 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
173 viewMenu
->AppendCheckItem(ID_TOGGLENATIVEHEADER
, "&Native column headers");
174 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
175 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
176 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
177 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
178 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
179 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
181 wxMenu
*rowLabelMenu
= new wxMenu
;
183 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
185 _T("Change alignment of row labels") );
187 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
188 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
190 wxMenu
*colLabelMenu
= new wxMenu
;
192 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
194 _T("Change alignment of col labels") );
196 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
197 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
199 wxMenu
*colMenu
= new wxMenu
;
200 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
201 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
202 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
203 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
204 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
205 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
207 wxMenu
*editMenu
= new wxMenu
;
208 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
209 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
210 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
211 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
212 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
214 wxMenu
*selectMenu
= new wxMenu
;
215 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
216 _T("When off, old selection is deselected before ")
217 _T("selecting the new cells"), wxITEM_CHECK
);
218 selectMenu
->Append( ID_SHOW_SELECTION
,
219 _T("&Show current selection\tCtrl-Alt-S"));
220 selectMenu
->AppendSeparator();
221 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
222 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
223 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
224 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
225 selectMenu
->AppendSeparator();
226 selectMenu
->Append( ID_DESELECT_ALL
, _T("Deselect all"));
227 selectMenu
->Append( ID_DESELECT_ROW
, _T("Deselect row 2"));
228 selectMenu
->Append( ID_DESELECT_COL
, _T("Deselect col 2"));
229 selectMenu
->Append( ID_DESELECT_CELL
, _T("Deselect cell (3, 1)"));
230 wxMenu
*selectionMenu
= new wxMenu
;
231 selectMenu
->Append( ID_CHANGESEL
, _T("Change &selection mode"),
233 _T("Change selection mode") );
235 selectionMenu
->Append( ID_SELCELLS
, _T("Select &cells") );
236 selectionMenu
->Append( ID_SELROWS
, _T("Select &rows") );
237 selectionMenu
->Append( ID_SELCOLS
, _T("Select col&umns") );
238 selectionMenu
->Append( ID_SELROWSORCOLS
, _T("Select rows &or columns") );
240 wxMenu
*autosizeMenu
= new wxMenu
;
241 autosizeMenu
->Append( ID_SIZE_ROW
, _T("Selected &row data") );
242 autosizeMenu
->Append( ID_SIZE_COL
, _T("Selected &column data") );
243 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, _T("Selected row la&bel") );
244 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, _T("Selected column &label") );
245 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, _T("Column la&bels") );
246 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, _T("Row label&s") );
247 autosizeMenu
->Append( ID_SIZE_GRID
, _T("Entire &grid") );
249 wxMenu
*helpMenu
= new wxMenu
;
250 helpMenu
->Append( wxID_ABOUT
, _T("&About wxGrid demo") );
252 wxMenuBar
*menuBar
= new wxMenuBar
;
253 menuBar
->Append( fileMenu
, _T("&File") );
254 menuBar
->Append( viewMenu
, _T("&Grid") );
255 menuBar
->Append( colMenu
, _T("&Colours") );
256 menuBar
->Append( editMenu
, _T("&Edit") );
257 menuBar
->Append( selectMenu
, _T("&Select") );
258 menuBar
->Append( autosizeMenu
, _T("&Autosize") );
259 menuBar
->Append( helpMenu
, _T("&Help") );
261 SetMenuBar( menuBar
);
265 grid
= new wxGrid( this,
268 wxSize( 400, 300 ) );
271 int gridW
= 600, gridH
= 300;
272 int logW
= gridW
, logH
= 100;
274 logWin
= new wxTextCtrl( this,
277 wxPoint( 0, gridH
+ 20 ),
278 wxSize( logW
, logH
),
281 logger
= new wxLogTextCtrl( logWin
);
282 m_logOld
= wxLog::SetActiveTarget( logger
);
283 wxLog::DisableTimestamp();
286 // this will create a grid and, by default, an associated grid
288 grid
->CreateGrid( 0, 0 );
289 grid
->AppendRows(100);
290 grid
->AppendCols(100);
292 int ir
= grid
->GetNumberRows();
293 grid
->DeleteRows(0, ir
);
294 grid
->AppendRows(ir
);
296 grid
->SetRowSize( 0, 60 );
297 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
299 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
300 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
301 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
303 grid
->SetCellValue( 0, 2, _T("Blah") );
304 grid
->SetCellValue( 0, 3, _T("Read only") );
305 grid
->SetReadOnly( 0, 3 );
307 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
309 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
311 grid
->SetRowSize( 99, 60 );
312 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
313 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
315 grid
->SetCellTextColour(1, 2, *wxRED
);
316 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
318 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
320 grid
->SetCellValue(2, 2, _T("red"));
322 grid
->SetCellTextColour(2, 2, *wxRED
);
323 grid
->SetCellValue(3, 3, _T("green on grey"));
324 grid
->SetCellTextColour(3, 3, *wxGREEN
);
325 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
327 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
328 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
329 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
331 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
332 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
334 wxGridCellAttr
*attr
;
335 attr
= new wxGridCellAttr
;
336 attr
->SetTextColour(*wxBLUE
);
337 grid
->SetColAttr(5, attr
);
338 attr
= new wxGridCellAttr
;
339 attr
->SetBackgroundColour(*wxRED
);
340 grid
->SetRowAttr(5, attr
);
342 grid
->SetCellValue(2, 4, _T("a wider column"));
343 grid
->SetColSize(4, 120);
344 grid
->SetColMinimalWidth(4, 120);
346 grid
->SetCellTextColour(5, 8, *wxGREEN
);
347 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
348 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"));
350 grid
->SetColFormatFloat(6);
351 grid
->SetCellValue(0, 6, wxString::Format(wxT("%g"), 3.1415));
352 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 1415.0));
353 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 12345.67890));
355 grid
->SetColFormatFloat(7, 6, 2);
356 grid
->SetCellValue(0, 7, wxString::Format(wxT("%g"), 3.1415));
357 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 1415.0));
358 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 12345.67890));
360 grid
->SetColFormatNumber(8);
361 grid
->SetCellValue(0, 8, "17");
362 grid
->SetCellValue(1, 8, "0");
363 grid
->SetCellValue(2, 8, "-666");
365 const wxString choices
[] =
367 _T("Please select a choice"),
368 _T("This takes two cells"),
369 _T("Another choice"),
371 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
372 grid
->SetCellSize(4, 0, 1, 2);
373 grid
->SetCellValue(4, 0, choices
[0]);
374 grid
->SetCellOverflow(4, 0, false);
376 grid
->SetCellSize(7, 1, 3, 4);
377 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
378 grid
->SetCellValue(7, 1, _T("Big box!"));
380 // this does exactly nothing except testing that SetAttr() handles NULL
381 // attributes and does reference counting correctly
382 grid
->SetAttr(11, 11, NULL
);
383 grid
->SetAttr(11, 11, new wxGridCellAttr
);
384 grid
->SetAttr(11, 11, NULL
);
386 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
392 topSizer
->Add( logWin
,
397 SetSizerAndFit( topSizer
);
404 GridFrame::~GridFrame()
407 delete wxLog::SetActiveTarget(m_logOld
);
412 void GridFrame::SetDefaults()
414 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
415 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
416 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
417 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
418 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
419 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
420 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
421 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
422 GetMenuBar()->Check( ID_TOGGLENATIVEHEADER
, false );
423 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
424 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
428 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
430 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
432 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
436 grid
->SetRowLabelSize( 0 );
441 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
443 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
445 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
449 grid
->SetColLabelSize( 0 );
454 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
457 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
461 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
463 grid
->EnableDragRowSize(
464 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
468 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
470 grid
->EnableDragColSize(
471 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
474 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
476 grid
->EnableDragColMove(
477 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
480 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
482 grid
->EnableDragGridSize(
483 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
486 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
488 grid
->EnableDragCell(
489 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
492 void GridFrame::ToggleNativeHeader( wxCommandEvent
& WXUNUSED(ev
) )
494 grid
->SetUseNativeColLabels(
495 GetMenuBar()->IsChecked( ID_TOGGLENATIVEHEADER
) );
498 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
500 grid
->EnableGridLines(
501 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
504 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
506 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
508 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
509 _T("Pen Width"), 11, choices
);
511 int current
= grid
->GetCellHighlightPenWidth();
512 dlg
.SetSelection(current
);
513 if (dlg
.ShowModal() == wxID_OK
) {
514 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
518 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
520 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
522 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
523 _T("Pen Width"), 11, choices
);
525 int current
= grid
->GetCellHighlightROPenWidth();
526 dlg
.SetSelection(current
);
527 if (dlg
.ShowModal() == wxID_OK
) {
528 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
534 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
536 grid
->AutoSizeColumns();
540 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
542 grid
->SetDefaultCellOverflow(ev
.IsChecked());
546 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
549 grid
->SetCellSize( 7, 1, 5, 5 );
551 grid
->SetCellSize( 7, 1, 1, 5 );
555 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
557 wxColourDialog
dlg( NULL
);
558 if ( dlg
.ShowModal() == wxID_OK
)
560 wxColourData retData
;
561 retData
= dlg
.GetColourData();
562 wxColour colour
= retData
.GetColour();
564 grid
->SetLabelBackgroundColour( colour
);
569 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
571 wxColourDialog
dlg( NULL
);
572 if ( dlg
.ShowModal() == wxID_OK
)
574 wxColourData retData
;
575 retData
= dlg
.GetColourData();
576 wxColour colour
= retData
.GetColour();
578 grid
->SetLabelTextColour( colour
);
582 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
584 wxFont font
= wxGetFontFromUser(this);
587 grid
->SetLabelFont(font
);
591 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
594 grid
->GetRowLabelAlignment( &horiz
, &vert
);
599 horiz
= wxALIGN_CENTRE
;
603 horiz
= wxALIGN_RIGHT
;
607 horiz
= wxALIGN_LEFT
;
611 grid
->SetRowLabelAlignment( horiz
, vert
);
614 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
617 grid
->GetRowLabelAlignment( &horiz
, &vert
);
622 vert
= wxALIGN_CENTRE
;
626 vert
= wxALIGN_BOTTOM
;
634 grid
->SetRowLabelAlignment( horiz
, vert
);
638 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
641 grid
->GetColLabelAlignment( &horiz
, &vert
);
646 horiz
= wxALIGN_CENTRE
;
650 horiz
= wxALIGN_RIGHT
;
654 horiz
= wxALIGN_LEFT
;
658 grid
->SetColLabelAlignment( horiz
, vert
);
662 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
665 grid
->GetColLabelAlignment( &horiz
, &vert
);
670 vert
= wxALIGN_CENTRE
;
674 vert
= wxALIGN_BOTTOM
;
682 grid
->SetColLabelAlignment( horiz
, vert
);
686 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
688 wxColourDialog
dlg( NULL
);
689 if ( dlg
.ShowModal() == wxID_OK
)
691 wxColourData retData
;
692 retData
= dlg
.GetColourData();
693 wxColour colour
= retData
.GetColour();
695 grid
->SetGridLineColour( colour
);
700 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
702 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
706 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
708 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
712 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
714 if ( grid
->IsSelection() )
716 wxGridUpdateLocker
locker(grid
);
717 for ( int n
= 0; n
< grid
->GetNumberRows(); )
719 if ( grid
->IsInSelection( n
, 0 ) )
720 grid
->DeleteRows( n
, 1 );
728 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
730 wxGridUpdateLocker
locker(grid
);
731 const wxArrayInt sels
= grid
->GetSelectedRows();
732 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
734 grid
->AutoSizeRow( sels
[n
], false );
738 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
740 wxGridUpdateLocker
locker(grid
);
741 const wxArrayInt sels
= grid
->GetSelectedCols();
742 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
744 grid
->AutoSizeColumn( sels
[n
], false );
748 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
750 wxGridUpdateLocker
locker(grid
);
751 const wxArrayInt sels
= grid
->GetSelectedRows();
752 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
754 grid
->AutoSizeRowLabelSize( sels
[n
] );
758 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
760 wxGridUpdateLocker
locker(grid
);
761 const wxArrayInt sels
= grid
->GetSelectedCols();
762 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
764 grid
->AutoSizeColLabelSize( sels
[n
] );
768 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
770 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
773 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
775 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
778 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
784 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
786 if ( grid
->IsSelection() )
788 wxGridUpdateLocker
locker(grid
);
789 for ( int n
= 0; n
< grid
->GetNumberCols(); )
791 if ( grid
->IsInSelection( 0 , n
) )
792 grid
->DeleteCols( n
, 1 );
800 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
805 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
807 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
810 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
812 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
815 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
817 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
820 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
822 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
825 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
827 wxColour col
= wxGetColourFromUser(this);
830 grid
->SetDefaultCellTextColour(col
);
835 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
837 wxColour col
= wxGetColourFromUser(this);
840 // Check the new Refresh function by passing it a rectangle
841 // which exactly fits the grid.
843 wxRect
r(pt
, grid
->GetSize());
844 grid
->SetDefaultCellBackgroundColour(col
);
845 grid
->Refresh(true, &r
);
849 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
851 grid
->DeselectCell(3, 1);
854 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
856 grid
->DeselectCol(2);
859 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
861 grid
->DeselectRow(2);
864 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
866 grid
->ClearSelection();
869 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
871 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
874 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
876 grid
->SelectCol(2, m_addToSel
);
879 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
881 grid
->SelectRow(2, m_addToSel
);
884 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
889 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
891 m_addToSel
= event
.IsChecked();
894 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
897 if ( ev
.GetRow() != -1 )
899 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
901 else if ( ev
.GetCol() != -1 )
903 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
907 logBuf
<< _T("Left click on corner label");
910 if ( ev
.ShiftDown() )
911 logBuf
<< _T(" (shift down)");
912 if ( ev
.ControlDown() )
913 logBuf
<< _T(" (control down)");
914 wxLogMessage( wxT("%s"), logBuf
.c_str() );
916 // you must call event skip if you want default grid processing
922 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
924 wxLogMessage(_T("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
926 // you must call event skip if you want default grid processing
927 // (cell highlighting etc.)
933 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
935 wxLogMessage(_T("Resized row %d"), ev
.GetRowOrCol());
941 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
943 wxLogMessage(_T("Resized col %d"), ev
.GetRowOrCol());
949 void GridFrame::OnShowSelection(wxCommandEvent
& WXUNUSED(event
))
951 // max number of elements to dump -- otherwise it can take too much time
952 static const size_t countMax
= 100;
956 switch ( grid
->GetSelectionMode() )
958 case wxGrid::wxGridSelectCells
:
960 const wxGridCellCoordsArray
cells(grid
->GetSelectedCells());
961 size_t count
= cells
.size();
962 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count
);
963 if ( count
> countMax
)
965 wxLogMessage(_T("[too many selected cells, ")
966 _T("showing only the first %lu]"),
967 (unsigned long)countMax
);
971 for ( size_t n
= 0; n
< count
; n
++ )
973 const wxGridCellCoords
& c
= cells
[n
];
974 wxLogMessage(_T(" selected cell %lu: (%d, %d)"),
975 (unsigned long)n
, c
.GetCol(), c
.GetRow());
980 case wxGrid::wxGridSelectRows
:
984 case wxGrid::wxGridSelectColumns
:
986 const wxChar
*plural
, *single
;
994 plural
= _T("columns");
995 single
= _T("column");
998 const wxArrayInt
sels((const wxArrayInt
)(rows
? grid
->GetSelectedRows()
999 : grid
->GetSelectedCols()));
1000 size_t count
= sels
.size();
1001 wxLogMessage(_T("%lu %s selected:"),
1002 (unsigned long)count
, plural
);
1003 if ( count
> countMax
)
1005 wxLogMessage(_T("[too many selected %s, ")
1006 _T("showing only the first %lu]"),
1007 plural
, (unsigned long)countMax
);
1011 for ( size_t n
= 0; n
< count
; n
++ )
1013 wxLogMessage(_T(" selected %s %lu: %d"),
1014 single
, (unsigned long)n
, sels
[n
]);
1020 wxFAIL_MSG( _T("unknown wxGrid selection mode") );
1025 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1028 if ( ev
.Selecting() )
1029 logBuf
<< _T("Selected ");
1031 logBuf
<< _T("Deselected ");
1032 logBuf
<< _T("cell at row ") << ev
.GetRow()
1033 << _T(" col ") << ev
.GetCol()
1034 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1035 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1036 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1037 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
1039 //Indicate whether this column was moved
1040 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1041 logBuf
<< _T(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1043 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1045 // you must call Skip() if you want the default processing
1046 // to occur in wxGrid
1050 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1053 if ( ev
.Selecting() )
1054 logBuf
<< _T("Selected ");
1056 logBuf
<< _T("Deselected ");
1057 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
1058 << _T(" col ") << ev
.GetLeftCol()
1059 << _T(" to row ") << ev
.GetBottomRow()
1060 << _T(" col ") << ev
.GetRightCol()
1061 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1062 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1063 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1064 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
1065 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1070 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1072 int row
= ev
.GetRow(),
1075 wxLogMessage("Value of cell at (%d, %d): about to change "
1076 "from \"%s\" to \"%s\"",
1078 grid
->GetCellValue(row
, col
), ev
.GetString());
1080 // test how vetoing works
1081 if ( ev
.GetString() == "42" )
1083 wxLogMessage("Vetoing the change.");
1091 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1093 int row
= ev
.GetRow(),
1096 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1099 grid
->GetCellValue(row
, col
), ev
.GetString());
1104 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1106 wxLogMessage(_T("Got request to drag cell at row %d, col %d"),
1107 ev
.GetRow(), ev
.GetCol());
1112 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1115 if ( (ev
.GetCol() == 4) &&
1116 (ev
.GetRow() == 0) &&
1117 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
1118 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1124 wxLogMessage( wxT("Cell editor shown.") );
1129 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1132 if ( (ev
.GetCol() == 4) &&
1133 (ev
.GetRow() == 0) &&
1134 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
1135 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1141 wxLogMessage( wxT("Cell editor hidden.") );
1146 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1148 wxAboutDialogInfo aboutInfo
;
1149 aboutInfo
.SetName(wxT("wxGrid demo"));
1150 aboutInfo
.SetDescription(_("wxGrid sample program"));
1151 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1152 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1153 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1155 wxAboutBox(aboutInfo
);
1159 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1164 void GridFrame::OnBugsTable(wxCommandEvent
& )
1166 BugsGridFrame
*frame
= new BugsGridFrame
;
1170 // ----------------------------------------------------------------------------
1171 // MyGridCellAttrProvider
1172 // ----------------------------------------------------------------------------
1174 MyGridCellAttrProvider::MyGridCellAttrProvider()
1176 m_attrForOddRows
= new wxGridCellAttr
;
1177 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1180 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1182 m_attrForOddRows
->DecRef();
1185 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1186 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1188 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1194 attr
= m_attrForOddRows
;
1199 if ( !attr
->HasBackgroundColour() )
1201 wxGridCellAttr
*attrNew
= attr
->Clone();
1204 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1212 void GridFrame::OnVTable(wxCommandEvent
& )
1214 static long s_sizeGrid
= 10000;
1216 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
1218 _T("wxGridDemo question"),
1222 if ( s_sizeGrid
!= -1 )
1224 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1229 // ----------------------------------------------------------------------------
1230 // MyGridCellRenderer
1231 // ----------------------------------------------------------------------------
1233 // do something that the default renderer doesn't here just to show that it is
1234 // possible to alter the appearance of the cell beyond what the attributes
1236 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1237 wxGridCellAttr
& attr
,
1243 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1245 dc
.SetPen(*wxGREEN_PEN
);
1246 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1247 dc
.DrawEllipse(rect
);
1250 // ============================================================================
1251 // BigGridFrame and BigGridTable: Sample of a non-standard table
1252 // ============================================================================
1254 BigGridFrame::BigGridFrame(long sizeGrid
)
1255 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1256 wxDefaultPosition
, wxSize(500, 450))
1258 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1259 m_table
= new BigGridTable(sizeGrid
);
1261 // VZ: I don't understand why this slows down the display that much,
1262 // must profile it...
1263 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1265 m_grid
->SetTable(m_table
, true);
1267 #if defined __WXMOTIF__
1268 // MB: the grid isn't getting a sensible default size under wxMotif
1270 GetClientSize( &cw
, &ch
);
1271 m_grid
->SetSize( cw
, ch
);
1275 // ============================================================================
1276 // BugsGridFrame: a "realistic" table
1277 // ============================================================================
1279 // ----------------------------------------------------------------------------
1281 // ----------------------------------------------------------------------------
1304 static const wxString severities
[] =
1313 static struct BugsGridData
1319 wxChar platform
[12];
1321 } gs_dataBugsGrid
[] =
1323 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1324 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1325 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1326 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1329 static const wxChar
*headers
[Col_Max
] =
1339 // ----------------------------------------------------------------------------
1341 // ----------------------------------------------------------------------------
1343 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1349 return wxGRID_VALUE_NUMBER
;;
1352 // fall thorugh (TODO should be a list)
1355 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1358 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1361 return wxGRID_VALUE_BOOL
;
1364 wxFAIL_MSG(_T("unknown column"));
1366 return wxEmptyString
;
1369 int BugsGridTable::GetNumberRows()
1371 return WXSIZEOF(gs_dataBugsGrid
);
1374 int BugsGridTable::GetNumberCols()
1379 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1384 wxString
BugsGridTable::GetValue( int row
, int col
)
1386 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1391 return wxString::Format(_T("%d"), gd
.id
);
1394 return wxString::Format(_T("%d"), gd
.prio
);
1397 return gd
.opened
? _T("1") : _T("0");
1400 return severities
[gd
.severity
];
1409 return wxEmptyString
;
1412 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1414 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1421 wxFAIL_MSG(_T("unexpected column"));
1427 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1429 if ( severities
[n
] == value
)
1431 gd
.severity
= (Severity
)n
;
1436 if ( n
== WXSIZEOF(severities
) )
1438 wxLogWarning(_T("Invalid severity value '%s'."),
1440 gd
.severity
= Sev_Normal
;
1446 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1450 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1456 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1458 const wxString
& typeName
)
1460 if ( typeName
== wxGRID_VALUE_STRING
)
1464 else if ( typeName
== wxGRID_VALUE_BOOL
)
1466 return col
== Col_Opened
;
1468 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1470 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1478 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1480 return CanGetValueAs(row
, col
, typeName
);
1483 long BugsGridTable::GetValueAsLong( int row
, int col
)
1485 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1499 wxFAIL_MSG(_T("unexpected column"));
1504 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1506 if ( col
== Col_Opened
)
1508 return gs_dataBugsGrid
[row
].opened
;
1512 wxFAIL_MSG(_T("unexpected column"));
1518 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1520 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1529 wxFAIL_MSG(_T("unexpected column"));
1533 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1535 if ( col
== Col_Opened
)
1537 gs_dataBugsGrid
[row
].opened
= value
;
1541 wxFAIL_MSG(_T("unexpected column"));
1545 wxString
BugsGridTable::GetColLabelValue( int col
)
1547 return headers
[col
];
1550 // ----------------------------------------------------------------------------
1552 // ----------------------------------------------------------------------------
1554 BugsGridFrame::BugsGridFrame()
1555 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"))
1557 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1558 wxGridTableBase
*table
= new BugsGridTable();
1559 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1560 grid
->SetTable(table
, true);
1562 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1563 *attrRangeEditor
= new wxGridCellAttr
,
1564 *attrCombo
= new wxGridCellAttr
;
1566 attrRO
->SetReadOnly();
1567 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1568 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1571 grid
->SetColAttr(Col_Id
, attrRO
);
1572 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1573 grid
->SetColAttr(Col_Severity
, attrCombo
);
1576 SetClientSize(grid
->GetSize());
1579 // ============================================================================
1580 // TabularGrid: grid used for display of tabular data
1581 // ============================================================================
1583 class TabularGridTable
: public wxGridTableBase
1600 TabularGridTable() { m_sortOrder
= NULL
; }
1602 virtual int GetNumberRows() { return ROW_MAX
; }
1603 virtual int GetNumberCols() { return COL_MAX
; }
1605 virtual wxString
GetValue(int row
, int col
)
1608 row
= m_sortOrder
[row
];
1614 return GetNameOrExt(row
, col
);
1617 return wxString::Format("%lu", GetSize(row
));
1620 return GetDate(row
).FormatDate();
1624 wxFAIL_MSG( "unknown column" );
1630 virtual void SetValue(int, int, const wxString
&)
1632 wxFAIL_MSG( "shouldn't be called" );
1635 virtual wxString
GetColLabelValue(int col
)
1637 // notice that column parameter here always refers to the internal
1638 // column index, independently of its position on the screen
1639 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1640 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1645 virtual void SetColLabelValue(int, const wxString
&)
1647 wxFAIL_MSG( "shouldn't be called" );
1650 void Sort(int col
, bool ascending
)
1652 // we hardcode all sorting orders for simplicity here
1653 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1655 // descending ascending
1656 { { 2, 1, 0 }, { 0, 1, 2 } },
1657 { { 2, 1, 0 }, { 0, 1, 2 } },
1658 { { 2, 1, 0 }, { 0, 1, 2 } },
1659 { { 1, 0, 2 }, { 2, 0, 1 } },
1662 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1666 wxString
GetNameOrExt(int row
, int col
) const
1669 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1670 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1672 const wxString
s(names
[row
]);
1673 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1676 unsigned long GetSize(int row
) const
1678 static const unsigned long
1679 sizes
[] = { 412, 604, 40774 };
1680 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1685 wxDateTime
GetDate(int row
) const
1688 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1689 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1692 dt
.ParseISODate(dates
[row
]);
1699 // specialized text control for column indexes entry
1700 class ColIndexEntry
: public wxTextCtrl
1703 ColIndexEntry(wxWindow
*parent
)
1704 : wxTextCtrl(parent
, wxID_ANY
, "")
1706 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1712 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1722 virtual wxSize
DoGetBestSize() const
1724 wxSize size
= wxTextCtrl::DoGetBestSize();
1725 size
.x
= 3*GetCharWidth();
1730 class TabularGridFrame
: public wxFrame
1738 Id_Check_UseNativeHeader
,
1739 Id_Check_DrawNativeLabels
,
1740 Id_Check_ShowRowLabels
,
1741 Id_Check_EnableColMove
1746 void OnToggleUseNativeHeader(wxCommandEvent
&)
1748 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1751 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1753 // we don't draw labels at all, native or otherwise, if we use the
1754 // native header control
1755 event
.Enable( !m_chkUseNative
->GetValue() );
1758 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1760 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1763 void OnToggleShowRowLabels(wxCommandEvent
&)
1765 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1770 void OnToggleColMove(wxCommandEvent
&)
1772 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1775 void OnShowHideColumn(wxCommandEvent
& event
)
1777 int col
= m_txtColShowHide
->GetCol();
1780 m_grid
->SetColSize(col
,
1781 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1783 UpdateOrderAndVisibility();
1787 void OnMoveColumn(wxCommandEvent
&)
1789 int col
= m_txtColIndex
->GetCol();
1790 int pos
= m_txtColPos
->GetCol();
1791 if ( col
== -1 || pos
== -1 )
1794 m_grid
->SetColPos(col
, pos
);
1796 UpdateOrderAndVisibility();
1799 void OnResetColumnOrder(wxCommandEvent
&)
1801 m_grid
->ResetColPos();
1803 UpdateOrderAndVisibility();
1806 void OnGridColSort(wxGridEvent
& event
)
1808 const int col
= event
.GetCol();
1809 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
1810 m_grid
->IsSortOrderAscending()));
1813 void OnGridColMove(wxGridEvent
& event
)
1815 // can't update it yet as the order hasn't been changed, so do it a bit
1817 m_shouldUpdateOrder
= true;
1822 void OnGridColSize(wxGridSizeEvent
& event
)
1824 // we only catch this event to react to the user showing or hiding this
1825 // column using the header control menu and not because we're
1826 // interested in column resizing
1827 UpdateOrderAndVisibility();
1832 void OnIdle(wxIdleEvent
& event
)
1834 if ( m_shouldUpdateOrder
)
1836 m_shouldUpdateOrder
= false;
1837 UpdateOrderAndVisibility();
1843 void UpdateOrderAndVisibility()
1846 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
1848 const int col
= m_grid
->GetColAt(pos
);
1849 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
1860 m_statOrder
->SetLabel(s
);
1865 TabularGridTable
*m_table
;
1866 wxCheckBox
*m_chkUseNative
,
1868 *m_chkShowRowLabels
,
1869 *m_chkEnableColMove
;
1871 ColIndexEntry
*m_txtColIndex
,
1875 wxStaticText
*m_statOrder
;
1877 // fla for EVT_IDLE handler
1878 bool m_shouldUpdateOrder
;
1880 DECLARE_NO_COPY_CLASS(TabularGridFrame
)
1881 DECLARE_EVENT_TABLE()
1884 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
1885 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
1886 TabularGridFrame::OnToggleUseNativeHeader
)
1887 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
1888 TabularGridFrame::OnToggleDrawNativeLabels
)
1889 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
1890 TabularGridFrame::OnToggleShowRowLabels
)
1891 EVT_CHECKBOX(Id_Check_EnableColMove
,
1892 TabularGridFrame::OnToggleColMove
)
1894 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
1895 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
1897 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
1898 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
1899 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
1900 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
1902 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
1903 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
1904 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
1906 EVT_IDLE(TabularGridFrame::OnIdle
)
1909 TabularGridFrame::TabularGridFrame()
1910 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
1912 m_shouldUpdateOrder
= false;
1914 wxPanel
* const panel
= new wxPanel(this);
1916 // create and initialize the grid with the specified data
1917 m_table
= new TabularGridTable
;
1918 m_grid
= new wxGrid(panel
, wxID_ANY
,
1919 wxDefaultPosition
, wxDefaultSize
,
1920 wxBORDER_STATIC
| wxWANTS_CHARS
);
1921 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
1923 m_grid
->EnableDragColMove();
1924 m_grid
->UseNativeColHeader();
1925 m_grid
->HideRowLabels();
1927 // add it and the other controls to the frame
1928 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
1929 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
1931 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
1933 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
1934 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
1935 "&Use native header");
1936 m_chkUseNative
->SetValue(true);
1937 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
1939 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
1940 "&Draw native column labels");
1941 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
1943 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
1944 "Show &row labels");
1945 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
1947 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
1948 "Allow column re&ordering");
1949 m_chkEnableColMove
->SetValue(true);
1950 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
1951 sizerControls
->Add(sizerStyles
);
1953 sizerControls
->AddSpacer(10);
1955 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
1956 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
1958 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
1959 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
1961 m_txtColIndex
= new ColIndexEntry(panel
);
1962 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
1963 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
1964 m_txtColPos
= new ColIndexEntry(panel
);
1965 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
1966 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
1968 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
1970 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
1971 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
1973 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
1974 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
1975 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
1976 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
1978 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
1979 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
1981 m_txtColShowHide
= new ColIndexEntry(panel
);
1982 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
1983 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
1984 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
1985 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
1987 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
1989 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
1991 panel
->SetSizer(sizerTop
);
1993 SetClientSize(panel
->GetBestSize());
1994 SetSizeHints(GetSize());
1999 void GridFrame::OnTabularTable(wxCommandEvent
&)
2001 new TabularGridFrame
;