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"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_APP( GridApp
)
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 bool GridApp::OnInit()
58 GridFrame
*frame
= new GridFrame
;
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
69 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
70 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
71 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
72 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
73 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
74 EVT_MENU( ID_TOGGLECOLMOVING
, GridFrame::ToggleColMoving
)
75 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
76 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
77 EVT_MENU( ID_TOGGLENATIVEHEADER
, GridFrame::ToggleNativeHeader
)
78 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
79 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
80 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
81 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
82 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
83 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
84 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
85 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
86 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
87 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
88 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
89 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
90 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
91 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
92 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
93 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
94 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
95 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
96 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
97 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
98 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
100 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
101 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
103 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
104 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
105 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
106 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
107 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
109 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
110 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
111 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
112 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
113 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
114 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
115 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
116 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
117 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
118 EVT_MENU( ID_SHOW_SELECTION
, GridFrame::OnShowSelection
)
120 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
121 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
122 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
123 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
124 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
125 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
126 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
128 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
129 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
131 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
132 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
133 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
134 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
135 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
136 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
137 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
138 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
139 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
141 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
142 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
146 GridFrame::GridFrame()
147 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets grid class demo"),
151 wxMenu
*fileMenu
= new wxMenu
;
152 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
153 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
154 fileMenu
->Append( ID_TABULAR_TABLE
, _T("&Tabular table test\tCtrl-T"));
155 fileMenu
->AppendSeparator();
156 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
158 wxMenu
*viewMenu
= new wxMenu
;
159 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
160 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
161 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
162 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
163 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
164 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
165 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
166 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
167 viewMenu
->AppendCheckItem(ID_TOGGLENATIVEHEADER
, "&Native column headers");
168 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
169 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
170 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
171 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
172 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
173 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
175 wxMenu
*rowLabelMenu
= new wxMenu
;
177 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
179 _T("Change alignment of row labels") );
181 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
182 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
184 wxMenu
*colLabelMenu
= new wxMenu
;
186 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
188 _T("Change alignment of col labels") );
190 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
191 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
193 wxMenu
*colMenu
= new wxMenu
;
194 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
195 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
196 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
197 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
198 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
199 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
201 wxMenu
*editMenu
= new wxMenu
;
202 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
203 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
204 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
205 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
206 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
208 wxMenu
*selectMenu
= new wxMenu
;
209 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
210 _T("When off, old selection is deselected before ")
211 _T("selecting the new cells"), wxITEM_CHECK
);
212 selectMenu
->Append( ID_SHOW_SELECTION
,
213 _T("&Show current selection\tCtrl-Alt-S"));
214 selectMenu
->AppendSeparator();
215 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
216 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
217 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
218 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
219 selectMenu
->AppendSeparator();
220 selectMenu
->Append( ID_DESELECT_ALL
, _T("Deselect all"));
221 selectMenu
->Append( ID_DESELECT_ROW
, _T("Deselect row 2"));
222 selectMenu
->Append( ID_DESELECT_COL
, _T("Deselect col 2"));
223 selectMenu
->Append( ID_DESELECT_CELL
, _T("Deselect cell (3, 1)"));
224 wxMenu
*selectionMenu
= new wxMenu
;
225 selectMenu
->Append( ID_CHANGESEL
, _T("Change &selection mode"),
227 _T("Change selection mode") );
229 selectionMenu
->Append( ID_SELCELLS
, _T("Select &cells") );
230 selectionMenu
->Append( ID_SELROWS
, _T("Select &rows") );
231 selectionMenu
->Append( ID_SELCOLS
, _T("Select col&umns") );
232 selectionMenu
->Append( ID_SELROWSORCOLS
, _T("Select rows &or columns") );
234 wxMenu
*autosizeMenu
= new wxMenu
;
235 autosizeMenu
->Append( ID_SIZE_ROW
, _T("Selected &row data") );
236 autosizeMenu
->Append( ID_SIZE_COL
, _T("Selected &column data") );
237 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, _T("Selected row la&bel") );
238 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, _T("Selected column &label") );
239 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, _T("Column la&bels") );
240 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, _T("Row label&s") );
241 autosizeMenu
->Append( ID_SIZE_GRID
, _T("Entire &grid") );
243 wxMenu
*helpMenu
= new wxMenu
;
244 helpMenu
->Append( wxID_ABOUT
, _T("&About wxGrid demo") );
246 wxMenuBar
*menuBar
= new wxMenuBar
;
247 menuBar
->Append( fileMenu
, _T("&File") );
248 menuBar
->Append( viewMenu
, _T("&Grid") );
249 menuBar
->Append( colMenu
, _T("&Colours") );
250 menuBar
->Append( editMenu
, _T("&Edit") );
251 menuBar
->Append( selectMenu
, _T("&Select") );
252 menuBar
->Append( autosizeMenu
, _T("&Autosize") );
253 menuBar
->Append( helpMenu
, _T("&Help") );
255 SetMenuBar( menuBar
);
259 grid
= new wxGrid( this,
262 wxSize( 400, 300 ) );
265 int gridW
= 600, gridH
= 300;
266 int logW
= gridW
, logH
= 100;
268 logWin
= new wxTextCtrl( this,
271 wxPoint( 0, gridH
+ 20 ),
272 wxSize( logW
, logH
),
275 logger
= new wxLogTextCtrl( logWin
);
276 m_logOld
= wxLog::SetActiveTarget( logger
);
277 wxLog::DisableTimestamp();
280 // this will create a grid and, by default, an associated grid
282 grid
->CreateGrid( 0, 0 );
283 grid
->AppendRows(100);
284 grid
->AppendCols(100);
286 int ir
= grid
->GetNumberRows();
287 grid
->DeleteRows(0, ir
);
288 grid
->AppendRows(ir
);
290 grid
->SetRowSize( 0, 60 );
291 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
293 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
294 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
295 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
297 grid
->SetCellValue( 0, 2, _T("Blah") );
298 grid
->SetCellValue( 0, 3, _T("Read only") );
299 grid
->SetReadOnly( 0, 3 );
301 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
303 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
305 grid
->SetRowSize( 99, 60 );
306 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
307 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
309 grid
->SetCellTextColour(1, 2, *wxRED
);
310 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
312 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
314 grid
->SetCellValue(2, 2, _T("red"));
316 grid
->SetCellTextColour(2, 2, *wxRED
);
317 grid
->SetCellValue(3, 3, _T("green on grey"));
318 grid
->SetCellTextColour(3, 3, *wxGREEN
);
319 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
321 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
322 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
323 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
325 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
326 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
328 wxGridCellAttr
*attr
;
329 attr
= new wxGridCellAttr
;
330 attr
->SetTextColour(*wxBLUE
);
331 grid
->SetColAttr(5, attr
);
332 attr
= new wxGridCellAttr
;
333 attr
->SetBackgroundColour(*wxRED
);
334 grid
->SetRowAttr(5, attr
);
336 grid
->SetCellValue(2, 4, _T("a wider column"));
337 grid
->SetColSize(4, 120);
338 grid
->SetColMinimalWidth(4, 120);
340 grid
->SetCellTextColour(5, 8, *wxGREEN
);
341 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
342 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"));
344 grid
->SetColFormatFloat(6);
345 grid
->SetCellValue(0, 6, wxString::Format(wxT("%g"), 3.1415));
346 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 1415.0));
347 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 12345.67890));
349 grid
->SetColFormatFloat(7, 6, 2);
350 grid
->SetCellValue(0, 7, wxString::Format(wxT("%g"), 3.1415));
351 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 1415.0));
352 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 12345.67890));
354 grid
->SetColFormatNumber(8);
355 grid
->SetCellValue(0, 8, "17");
356 grid
->SetCellValue(1, 8, "0");
357 grid
->SetCellValue(2, 8, "-666");
359 const wxString choices
[] =
361 _T("Please select a choice"),
362 _T("This takes two cells"),
363 _T("Another choice"),
365 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
366 grid
->SetCellSize(4, 0, 1, 2);
367 grid
->SetCellValue(4, 0, choices
[0]);
368 grid
->SetCellOverflow(4, 0, false);
370 grid
->SetCellSize(7, 1, 3, 4);
371 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
372 grid
->SetCellValue(7, 1, _T("Big box!"));
374 // this does exactly nothing except testing that SetAttr() handles NULL
375 // attributes and does reference counting correctly
376 grid
->SetAttr(11, 11, NULL
);
377 grid
->SetAttr(11, 11, new wxGridCellAttr
);
378 grid
->SetAttr(11, 11, NULL
);
380 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
386 topSizer
->Add( logWin
,
391 SetSizerAndFit( topSizer
);
398 GridFrame::~GridFrame()
401 delete wxLog::SetActiveTarget(m_logOld
);
406 void GridFrame::SetDefaults()
408 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
409 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
410 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
411 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
412 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
413 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
414 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
415 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
416 GetMenuBar()->Check( ID_TOGGLENATIVEHEADER
, false );
417 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
418 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
422 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
424 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
426 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
430 grid
->SetRowLabelSize( 0 );
435 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
437 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
439 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
443 grid
->SetColLabelSize( 0 );
448 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
451 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
455 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
457 grid
->EnableDragRowSize(
458 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
462 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
464 grid
->EnableDragColSize(
465 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
468 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
470 grid
->EnableDragColMove(
471 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
474 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
476 grid
->EnableDragGridSize(
477 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
480 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
482 grid
->EnableDragCell(
483 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
486 void GridFrame::ToggleNativeHeader( wxCommandEvent
& WXUNUSED(ev
) )
488 grid
->SetUseNativeColLabels(
489 GetMenuBar()->IsChecked( ID_TOGGLENATIVEHEADER
) );
492 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
494 grid
->EnableGridLines(
495 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
498 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
500 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
502 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
503 _T("Pen Width"), 11, choices
);
505 int current
= grid
->GetCellHighlightPenWidth();
506 dlg
.SetSelection(current
);
507 if (dlg
.ShowModal() == wxID_OK
) {
508 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
512 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
514 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
516 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
517 _T("Pen Width"), 11, choices
);
519 int current
= grid
->GetCellHighlightROPenWidth();
520 dlg
.SetSelection(current
);
521 if (dlg
.ShowModal() == wxID_OK
) {
522 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
528 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
530 grid
->AutoSizeColumns();
534 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
536 grid
->SetDefaultCellOverflow(ev
.IsChecked());
540 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
543 grid
->SetCellSize( 7, 1, 5, 5 );
545 grid
->SetCellSize( 7, 1, 1, 5 );
549 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
551 wxColourDialog
dlg( NULL
);
552 if ( dlg
.ShowModal() == wxID_OK
)
554 wxColourData retData
;
555 retData
= dlg
.GetColourData();
556 wxColour colour
= retData
.GetColour();
558 grid
->SetLabelBackgroundColour( colour
);
563 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
565 wxColourDialog
dlg( NULL
);
566 if ( dlg
.ShowModal() == wxID_OK
)
568 wxColourData retData
;
569 retData
= dlg
.GetColourData();
570 wxColour colour
= retData
.GetColour();
572 grid
->SetLabelTextColour( colour
);
576 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
578 wxFont font
= wxGetFontFromUser(this);
581 grid
->SetLabelFont(font
);
585 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
588 grid
->GetRowLabelAlignment( &horiz
, &vert
);
593 horiz
= wxALIGN_CENTRE
;
597 horiz
= wxALIGN_RIGHT
;
601 horiz
= wxALIGN_LEFT
;
605 grid
->SetRowLabelAlignment( horiz
, vert
);
608 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
611 grid
->GetRowLabelAlignment( &horiz
, &vert
);
616 vert
= wxALIGN_CENTRE
;
620 vert
= wxALIGN_BOTTOM
;
628 grid
->SetRowLabelAlignment( horiz
, vert
);
632 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
635 grid
->GetColLabelAlignment( &horiz
, &vert
);
640 horiz
= wxALIGN_CENTRE
;
644 horiz
= wxALIGN_RIGHT
;
648 horiz
= wxALIGN_LEFT
;
652 grid
->SetColLabelAlignment( horiz
, vert
);
656 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
659 grid
->GetColLabelAlignment( &horiz
, &vert
);
664 vert
= wxALIGN_CENTRE
;
668 vert
= wxALIGN_BOTTOM
;
676 grid
->SetColLabelAlignment( horiz
, vert
);
680 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
682 wxColourDialog
dlg( NULL
);
683 if ( dlg
.ShowModal() == wxID_OK
)
685 wxColourData retData
;
686 retData
= dlg
.GetColourData();
687 wxColour colour
= retData
.GetColour();
689 grid
->SetGridLineColour( colour
);
694 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
696 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
700 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
702 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
706 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
708 if ( grid
->IsSelection() )
710 wxGridUpdateLocker
locker(grid
);
711 for ( int n
= 0; n
< grid
->GetNumberRows(); )
713 if ( grid
->IsInSelection( n
, 0 ) )
714 grid
->DeleteRows( n
, 1 );
722 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
724 wxGridUpdateLocker
locker(grid
);
725 const wxArrayInt sels
= grid
->GetSelectedRows();
726 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
728 grid
->AutoSizeRow( sels
[n
], false );
732 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
734 wxGridUpdateLocker
locker(grid
);
735 const wxArrayInt sels
= grid
->GetSelectedCols();
736 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
738 grid
->AutoSizeColumn( sels
[n
], false );
742 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
744 wxGridUpdateLocker
locker(grid
);
745 const wxArrayInt sels
= grid
->GetSelectedRows();
746 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
748 grid
->AutoSizeRowLabelSize( sels
[n
] );
752 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
754 wxGridUpdateLocker
locker(grid
);
755 const wxArrayInt sels
= grid
->GetSelectedCols();
756 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
758 grid
->AutoSizeColLabelSize( sels
[n
] );
762 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
764 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
767 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
769 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
772 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
778 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
780 if ( grid
->IsSelection() )
782 wxGridUpdateLocker
locker(grid
);
783 for ( int n
= 0; n
< grid
->GetNumberCols(); )
785 if ( grid
->IsInSelection( 0 , n
) )
786 grid
->DeleteCols( n
, 1 );
794 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
799 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
801 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
804 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
806 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
809 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
811 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
814 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
816 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
819 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
821 wxColour col
= wxGetColourFromUser(this);
824 grid
->SetDefaultCellTextColour(col
);
829 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
831 wxColour col
= wxGetColourFromUser(this);
834 // Check the new Refresh function by passing it a rectangle
835 // which exactly fits the grid.
837 wxRect
r(pt
, grid
->GetSize());
838 grid
->SetDefaultCellBackgroundColour(col
);
839 grid
->Refresh(true, &r
);
843 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
845 grid
->DeselectCell(3, 1);
848 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
850 grid
->DeselectCol(2);
853 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
855 grid
->DeselectRow(2);
858 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
860 grid
->ClearSelection();
863 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
865 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
868 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
870 grid
->SelectCol(2, m_addToSel
);
873 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
875 grid
->SelectRow(2, m_addToSel
);
878 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
883 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
885 m_addToSel
= event
.IsChecked();
888 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
891 if ( ev
.GetRow() != -1 )
893 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
895 else if ( ev
.GetCol() != -1 )
897 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
901 logBuf
<< _T("Left click on corner label");
904 if ( ev
.ShiftDown() )
905 logBuf
<< _T(" (shift down)");
906 if ( ev
.ControlDown() )
907 logBuf
<< _T(" (control down)");
908 wxLogMessage( wxT("%s"), logBuf
.c_str() );
910 // you must call event skip if you want default grid processing
916 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
918 wxLogMessage(_T("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
920 // you must call event skip if you want default grid processing
921 // (cell highlighting etc.)
927 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
929 wxLogMessage(_T("Resized row %d"), ev
.GetRowOrCol());
935 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
937 wxLogMessage(_T("Resized col %d"), ev
.GetRowOrCol());
943 void GridFrame::OnShowSelection(wxCommandEvent
& WXUNUSED(event
))
945 // max number of elements to dump -- otherwise it can take too much time
946 static const size_t countMax
= 100;
950 switch ( grid
->GetSelectionMode() )
952 case wxGrid::wxGridSelectCells
:
954 const wxGridCellCoordsArray
cells(grid
->GetSelectedCells());
955 size_t count
= cells
.size();
956 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count
);
957 if ( count
> countMax
)
959 wxLogMessage(_T("[too many selected cells, ")
960 _T("showing only the first %lu]"),
961 (unsigned long)countMax
);
965 for ( size_t n
= 0; n
< count
; n
++ )
967 const wxGridCellCoords
& c
= cells
[n
];
968 wxLogMessage(_T(" selected cell %lu: (%d, %d)"),
969 (unsigned long)n
, c
.GetCol(), c
.GetRow());
974 case wxGrid::wxGridSelectRows
:
978 case wxGrid::wxGridSelectColumns
:
980 const wxChar
*plural
, *single
;
988 plural
= _T("columns");
989 single
= _T("column");
992 const wxArrayInt
sels((const wxArrayInt
)(rows
? grid
->GetSelectedRows()
993 : grid
->GetSelectedCols()));
994 size_t count
= sels
.size();
995 wxLogMessage(_T("%lu %s selected:"),
996 (unsigned long)count
, plural
);
997 if ( count
> countMax
)
999 wxLogMessage(_T("[too many selected %s, ")
1000 _T("showing only the first %lu]"),
1001 plural
, (unsigned long)countMax
);
1005 for ( size_t n
= 0; n
< count
; n
++ )
1007 wxLogMessage(_T(" selected %s %lu: %d"),
1008 single
, (unsigned long)n
, sels
[n
]);
1014 wxFAIL_MSG( _T("unknown wxGrid selection mode") );
1019 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1022 if ( ev
.Selecting() )
1023 logBuf
<< _T("Selected ");
1025 logBuf
<< _T("Deselected ");
1026 logBuf
<< _T("cell at row ") << ev
.GetRow()
1027 << _T(" col ") << ev
.GetCol()
1028 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1029 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1030 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1031 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
1033 //Indicate whether this column was moved
1034 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1035 logBuf
<< _T(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1037 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1039 // you must call Skip() if you want the default processing
1040 // to occur in wxGrid
1044 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1047 if ( ev
.Selecting() )
1048 logBuf
<< _T("Selected ");
1050 logBuf
<< _T("Deselected ");
1051 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
1052 << _T(" col ") << ev
.GetLeftCol()
1053 << _T(" to row ") << ev
.GetBottomRow()
1054 << _T(" col ") << ev
.GetRightCol()
1055 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1056 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1057 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1058 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
1059 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1064 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1066 int row
= ev
.GetRow(),
1069 wxLogMessage("Value of cell at (%d, %d): about to change "
1070 "from \"%s\" to \"%s\"",
1072 grid
->GetCellValue(row
, col
), ev
.GetString());
1074 // test how vetoing works
1075 if ( ev
.GetString() == "42" )
1077 wxLogMessage("Vetoing the change.");
1085 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1087 int row
= ev
.GetRow(),
1090 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1093 grid
->GetCellValue(row
, col
), ev
.GetString());
1098 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1100 wxLogMessage(_T("Got request to drag cell at row %d, col %d"),
1101 ev
.GetRow(), ev
.GetCol());
1106 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1109 if ( (ev
.GetCol() == 4) &&
1110 (ev
.GetRow() == 0) &&
1111 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
1112 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1118 wxLogMessage( wxT("Cell editor shown.") );
1123 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1126 if ( (ev
.GetCol() == 4) &&
1127 (ev
.GetRow() == 0) &&
1128 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
1129 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1135 wxLogMessage( wxT("Cell editor hidden.") );
1140 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1142 wxAboutDialogInfo aboutInfo
;
1143 aboutInfo
.SetName(wxT("wxGrid demo"));
1144 aboutInfo
.SetDescription(_("wxGrid sample program"));
1145 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1146 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1147 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1149 wxAboutBox(aboutInfo
);
1153 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1158 void GridFrame::OnBugsTable(wxCommandEvent
& )
1160 BugsGridFrame
*frame
= new BugsGridFrame
;
1164 // ----------------------------------------------------------------------------
1165 // MyGridCellAttrProvider
1166 // ----------------------------------------------------------------------------
1168 MyGridCellAttrProvider::MyGridCellAttrProvider()
1170 m_attrForOddRows
= new wxGridCellAttr
;
1171 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1174 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1176 m_attrForOddRows
->DecRef();
1179 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1180 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1182 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1188 attr
= m_attrForOddRows
;
1193 if ( !attr
->HasBackgroundColour() )
1195 wxGridCellAttr
*attrNew
= attr
->Clone();
1198 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1206 void GridFrame::OnVTable(wxCommandEvent
& )
1208 static long s_sizeGrid
= 10000;
1210 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
1212 _T("wxGridDemo question"),
1216 if ( s_sizeGrid
!= -1 )
1218 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1223 // ----------------------------------------------------------------------------
1224 // MyGridCellRenderer
1225 // ----------------------------------------------------------------------------
1227 // do something that the default renderer doesn't here just to show that it is
1228 // possible to alter the appearance of the cell beyond what the attributes
1230 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1231 wxGridCellAttr
& attr
,
1237 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1239 dc
.SetPen(*wxGREEN_PEN
);
1240 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1241 dc
.DrawEllipse(rect
);
1244 // ============================================================================
1245 // BigGridFrame and BigGridTable: Sample of a non-standard table
1246 // ============================================================================
1248 BigGridFrame::BigGridFrame(long sizeGrid
)
1249 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1250 wxDefaultPosition
, wxSize(500, 450))
1252 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1253 m_table
= new BigGridTable(sizeGrid
);
1255 // VZ: I don't understand why this slows down the display that much,
1256 // must profile it...
1257 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1259 m_grid
->SetTable(m_table
, true);
1261 #if defined __WXMOTIF__
1262 // MB: the grid isn't getting a sensible default size under wxMotif
1264 GetClientSize( &cw
, &ch
);
1265 m_grid
->SetSize( cw
, ch
);
1269 // ============================================================================
1270 // BugsGridFrame: a "realistic" table
1271 // ============================================================================
1273 // ----------------------------------------------------------------------------
1275 // ----------------------------------------------------------------------------
1298 static const wxString severities
[] =
1307 static struct BugsGridData
1313 wxChar platform
[12];
1315 } gs_dataBugsGrid
[] =
1317 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1318 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1319 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1320 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1323 static const wxChar
*headers
[Col_Max
] =
1333 // ----------------------------------------------------------------------------
1335 // ----------------------------------------------------------------------------
1337 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1343 return wxGRID_VALUE_NUMBER
;;
1346 // fall thorugh (TODO should be a list)
1349 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1352 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1355 return wxGRID_VALUE_BOOL
;
1358 wxFAIL_MSG(_T("unknown column"));
1360 return wxEmptyString
;
1363 int BugsGridTable::GetNumberRows()
1365 return WXSIZEOF(gs_dataBugsGrid
);
1368 int BugsGridTable::GetNumberCols()
1373 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1378 wxString
BugsGridTable::GetValue( int row
, int col
)
1380 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1385 return wxString::Format(_T("%d"), gd
.id
);
1388 return wxString::Format(_T("%d"), gd
.prio
);
1391 return gd
.opened
? _T("1") : _T("0");
1394 return severities
[gd
.severity
];
1403 return wxEmptyString
;
1406 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1408 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1415 wxFAIL_MSG(_T("unexpected column"));
1421 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1423 if ( severities
[n
] == value
)
1425 gd
.severity
= (Severity
)n
;
1430 if ( n
== WXSIZEOF(severities
) )
1432 wxLogWarning(_T("Invalid severity value '%s'."),
1434 gd
.severity
= Sev_Normal
;
1440 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1444 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1450 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1452 const wxString
& typeName
)
1454 if ( typeName
== wxGRID_VALUE_STRING
)
1458 else if ( typeName
== wxGRID_VALUE_BOOL
)
1460 return col
== Col_Opened
;
1462 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1464 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1472 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1474 return CanGetValueAs(row
, col
, typeName
);
1477 long BugsGridTable::GetValueAsLong( int row
, int col
)
1479 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1493 wxFAIL_MSG(_T("unexpected column"));
1498 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1500 if ( col
== Col_Opened
)
1502 return gs_dataBugsGrid
[row
].opened
;
1506 wxFAIL_MSG(_T("unexpected column"));
1512 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1514 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1523 wxFAIL_MSG(_T("unexpected column"));
1527 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1529 if ( col
== Col_Opened
)
1531 gs_dataBugsGrid
[row
].opened
= value
;
1535 wxFAIL_MSG(_T("unexpected column"));
1539 wxString
BugsGridTable::GetColLabelValue( int col
)
1541 return headers
[col
];
1544 // ----------------------------------------------------------------------------
1546 // ----------------------------------------------------------------------------
1548 BugsGridFrame::BugsGridFrame()
1549 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"))
1551 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1552 wxGridTableBase
*table
= new BugsGridTable();
1553 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1554 grid
->SetTable(table
, true);
1556 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1557 *attrRangeEditor
= new wxGridCellAttr
,
1558 *attrCombo
= new wxGridCellAttr
;
1560 attrRO
->SetReadOnly();
1561 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1562 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1565 grid
->SetColAttr(Col_Id
, attrRO
);
1566 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1567 grid
->SetColAttr(Col_Severity
, attrCombo
);
1570 SetClientSize(grid
->GetSize());
1573 // ============================================================================
1574 // TabularGrid: grid used for display of tabular data
1575 // ============================================================================
1577 class TabularGridTable
: public wxGridTableBase
1594 TabularGridTable() { m_sortOrder
= NULL
; }
1596 virtual int GetNumberRows() { return ROW_MAX
; }
1597 virtual int GetNumberCols() { return COL_MAX
; }
1599 virtual wxString
GetValue(int row
, int col
)
1602 row
= m_sortOrder
[row
];
1608 return GetNameOrExt(row
, col
);
1611 return wxString::Format("%lu", GetSize(row
));
1614 return GetDate(row
).FormatDate();
1618 wxFAIL_MSG( "unknown column" );
1624 virtual void SetValue(int, int, const wxString
&)
1626 wxFAIL_MSG( "shouldn't be called" );
1629 virtual wxString
GetColLabelValue(int col
)
1631 // notice that column parameter here always refers to the internal
1632 // column index, independently of its position on the screen
1633 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1634 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1639 virtual void SetColLabelValue(int, const wxString
&)
1641 wxFAIL_MSG( "shouldn't be called" );
1644 void Sort(int col
, bool ascending
)
1646 // we hardcode all sorting orders for simplicity here
1647 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1649 // descending ascending
1650 { { 2, 1, 0 }, { 0, 1, 2 } },
1651 { { 2, 1, 0 }, { 0, 1, 2 } },
1652 { { 2, 1, 0 }, { 0, 1, 2 } },
1653 { { 1, 0, 2 }, { 2, 0, 1 } },
1656 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1660 wxString
GetNameOrExt(int row
, int col
) const
1663 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1664 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1666 const wxString
s(names
[row
]);
1667 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1670 unsigned long GetSize(int row
) const
1672 static const unsigned long
1673 sizes
[] = { 412, 604, 40774 };
1674 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1679 wxDateTime
GetDate(int row
) const
1682 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1683 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1686 dt
.ParseISODate(dates
[row
]);
1693 // specialized text control for column indexes entry
1694 class ColIndexEntry
: public wxTextCtrl
1697 ColIndexEntry(wxWindow
*parent
)
1698 : wxTextCtrl(parent
, wxID_ANY
, "")
1700 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1706 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1716 virtual wxSize
DoGetBestSize() const
1718 wxSize size
= wxTextCtrl::DoGetBestSize();
1719 size
.x
= 3*GetCharWidth();
1724 class TabularGridFrame
: public wxFrame
1732 Id_Check_UseNativeHeader
,
1733 Id_Check_DrawNativeLabels
,
1734 Id_Check_ShowRowLabels
,
1735 Id_Check_EnableColMove
1740 void OnToggleUseNativeHeader(wxCommandEvent
&)
1742 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1745 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1747 // we don't draw labels at all, native or otherwise, if we use the
1748 // native header control
1749 event
.Enable( !m_chkUseNative
->GetValue() );
1752 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1754 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1757 void OnToggleShowRowLabels(wxCommandEvent
&)
1759 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1764 void OnToggleColMove(wxCommandEvent
&)
1766 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1769 void OnShowHideColumn(wxCommandEvent
& event
)
1771 int col
= m_txtColShowHide
->GetCol();
1774 m_grid
->SetColSize(col
,
1775 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1777 UpdateOrderAndVisibility();
1781 void OnMoveColumn(wxCommandEvent
&)
1783 int col
= m_txtColIndex
->GetCol();
1784 int pos
= m_txtColPos
->GetCol();
1785 if ( col
== -1 || pos
== -1 )
1788 m_grid
->SetColPos(col
, pos
);
1790 UpdateOrderAndVisibility();
1793 void OnResetColumnOrder(wxCommandEvent
&)
1795 m_grid
->ResetColPos();
1797 UpdateOrderAndVisibility();
1800 void OnGridColSort(wxGridEvent
& event
)
1802 const int col
= event
.GetCol();
1803 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
1804 m_grid
->IsSortOrderAscending()));
1807 void OnGridColMove(wxGridEvent
& event
)
1809 // can't update it yet as the order hasn't been changed, so do it a bit
1811 m_shouldUpdateOrder
= true;
1816 void OnGridColSize(wxGridSizeEvent
& event
)
1818 // we only catch this event to react to the user showing or hiding this
1819 // column using the header control menu and not because we're
1820 // interested in column resizing
1821 UpdateOrderAndVisibility();
1826 void OnIdle(wxIdleEvent
& event
)
1828 if ( m_shouldUpdateOrder
)
1830 m_shouldUpdateOrder
= false;
1831 UpdateOrderAndVisibility();
1837 void UpdateOrderAndVisibility()
1840 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
1842 const int col
= m_grid
->GetColAt(pos
);
1843 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
1854 m_statOrder
->SetLabel(s
);
1859 TabularGridTable
*m_table
;
1860 wxCheckBox
*m_chkUseNative
,
1862 *m_chkShowRowLabels
,
1863 *m_chkEnableColMove
;
1865 ColIndexEntry
*m_txtColIndex
,
1869 wxStaticText
*m_statOrder
;
1871 // fla for EVT_IDLE handler
1872 bool m_shouldUpdateOrder
;
1874 DECLARE_NO_COPY_CLASS(TabularGridFrame
)
1875 DECLARE_EVENT_TABLE()
1878 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
1879 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
1880 TabularGridFrame::OnToggleUseNativeHeader
)
1881 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
1882 TabularGridFrame::OnToggleDrawNativeLabels
)
1883 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
1884 TabularGridFrame::OnToggleShowRowLabels
)
1885 EVT_CHECKBOX(Id_Check_EnableColMove
,
1886 TabularGridFrame::OnToggleColMove
)
1888 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
1889 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
1891 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
1892 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
1893 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
1894 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
1896 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
1897 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
1898 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
1900 EVT_IDLE(TabularGridFrame::OnIdle
)
1903 TabularGridFrame::TabularGridFrame()
1904 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
1906 m_shouldUpdateOrder
= false;
1908 wxPanel
* const panel
= new wxPanel(this);
1910 // create and initialize the grid with the specified data
1911 m_table
= new TabularGridTable
;
1912 m_grid
= new wxGrid(panel
, wxID_ANY
,
1913 wxDefaultPosition
, wxDefaultSize
,
1914 wxBORDER_STATIC
| wxWANTS_CHARS
);
1915 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
1917 m_grid
->EnableDragColMove();
1918 m_grid
->UseNativeColHeader();
1919 m_grid
->HideRowLabels();
1921 // add it and the other controls to the frame
1922 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
1923 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
1925 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
1927 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
1928 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
1929 "&Use native header");
1930 m_chkUseNative
->SetValue(true);
1931 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
1933 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
1934 "&Draw native column labels");
1935 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
1937 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
1938 "Show &row labels");
1939 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
1941 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
1942 "Allow column re&ordering");
1943 m_chkEnableColMove
->SetValue(true);
1944 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
1945 sizerControls
->Add(sizerStyles
);
1947 sizerControls
->AddSpacer(10);
1949 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
1950 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
1952 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
1953 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
1955 m_txtColIndex
= new ColIndexEntry(panel
);
1956 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
1957 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
1958 m_txtColPos
= new ColIndexEntry(panel
);
1959 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
1960 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
1962 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
1964 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
1965 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
1967 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
1968 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
1969 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
1970 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
1972 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
1973 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
1975 m_txtColShowHide
= new ColIndexEntry(panel
);
1976 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
1977 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
1978 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
1979 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
1981 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
1983 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
1985 panel
->SetSizer(sizerTop
);
1987 SetClientSize(panel
->GetBestSize());
1988 SetSizeHints(GetSize());
1993 void GridFrame::OnTabularTable(wxCommandEvent
&)
1995 new TabularGridFrame
;