1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
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"
35 #include "wx/generic/gridctrl.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 IMPLEMENT_APP( GridApp
)
45 // ============================================================================
47 // ============================================================================
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 bool GridApp::OnInit()
55 GridFrame
*frame
= new GridFrame
;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
66 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
67 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
68 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
69 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
70 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
71 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
72 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
73 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
74 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
75 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
76 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
77 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
78 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
79 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
80 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
81 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
82 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
83 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
84 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
85 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
86 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
87 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
88 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
89 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
90 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
91 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
92 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
94 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
95 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
97 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
98 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
99 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
100 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
101 EVT_MENU( ID_SMALL_GRID
, GridFrame::OnSmallGrid
)
103 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
104 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
105 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
106 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
107 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
108 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
109 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
110 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
111 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
112 EVT_MENU( ID_SHOW_SELECTION
, GridFrame::OnShowSelection
)
114 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
115 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
117 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
118 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
119 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
120 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
121 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
122 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
123 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
124 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
126 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
127 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
131 GridFrame::GridFrame()
132 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets grid class demo"),
136 wxMenu
*fileMenu
= new wxMenu
;
137 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
138 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
139 fileMenu
->Append( ID_SMALL_GRID
, _T("&Small Grid test\tCtrl-S"));
140 fileMenu
->AppendSeparator();
141 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
143 wxMenu
*viewMenu
= new wxMenu
;
144 viewMenu
->Append( ID_TOGGLEROWLABELS
, _T("&Row labels"), wxEmptyString
, wxITEM_CHECK
);
145 viewMenu
->Append( ID_TOGGLECOLLABELS
, _T("&Col labels"), wxEmptyString
, wxITEM_CHECK
);
146 viewMenu
->Append( ID_TOGGLEEDIT
, _T("&Editable"), wxEmptyString
, wxITEM_CHECK
);
147 viewMenu
->Append( ID_TOGGLEROWSIZING
, _T("Ro&w drag-resize"), wxEmptyString
, wxITEM_CHECK
);
148 viewMenu
->Append( ID_TOGGLECOLSIZING
, _T("C&ol drag-resize"), wxEmptyString
, wxITEM_CHECK
);
149 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, _T("&Grid drag-resize"), wxEmptyString
, wxITEM_CHECK
);
150 viewMenu
->Append( ID_TOGGLEGRIDDRAGCELL
, _T("&Grid drag-cell"), wxEmptyString
, wxITEM_CHECK
);
151 viewMenu
->Append( ID_TOGGLEGRIDLINES
, _T("&Grid Lines"), wxEmptyString
, wxITEM_CHECK
);
152 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, _T("&Set Cell Highlight Width...") );
153 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, _T("&Set Cell RO Highlight Width...") );
154 viewMenu
->Append( ID_AUTOSIZECOLS
, _T("&Auto-size cols") );
155 viewMenu
->Append( ID_CELLOVERFLOW
, _T("&Overflow cells"), wxEmptyString
, wxITEM_CHECK
);
156 viewMenu
->Append( ID_RESIZECELL
, _T("&Resize cell (7,1)"), wxEmptyString
, wxITEM_CHECK
);
158 wxMenu
*rowLabelMenu
= new wxMenu
;
160 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
162 _T("Change alignment of row labels") );
164 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
165 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
167 wxMenu
*colLabelMenu
= new wxMenu
;
169 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
171 _T("Change alignment of col labels") );
173 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
174 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
176 wxMenu
*colMenu
= new wxMenu
;
177 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
178 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
179 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
180 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
181 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
182 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
184 wxMenu
*editMenu
= new wxMenu
;
185 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
186 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
187 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
188 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
189 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
191 wxMenu
*selectMenu
= new wxMenu
;
192 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
193 _T("When off, old selection is deselected before ")
194 _T("selecting the new cells"), wxITEM_CHECK
);
195 selectMenu
->Append( ID_SHOW_SELECTION
,
196 _T("&Show current selection\tCtrl-Alt-S"));
197 selectMenu
->AppendSeparator();
198 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
199 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
200 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
201 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
202 selectMenu
->AppendSeparator();
203 selectMenu
->Append( ID_DESELECT_ALL
, _T("Deselect all"));
204 selectMenu
->Append( ID_DESELECT_ROW
, _T("Deselect row 2"));
205 selectMenu
->Append( ID_DESELECT_COL
, _T("Deselect col 2"));
206 selectMenu
->Append( ID_DESELECT_CELL
, _T("Deselect cell (3, 1)"));
207 wxMenu
*selectionMenu
= new wxMenu
;
208 selectMenu
->Append( ID_CHANGESEL
, _T("Change &selection mode"),
210 _T("Change selection mode") );
212 selectionMenu
->Append( ID_SELCELLS
, _T("Select &Cells") );
213 selectionMenu
->Append( ID_SELROWS
, _T("Select &Rows") );
214 selectionMenu
->Append( ID_SELCOLS
, _T("Select C&ols") );
217 wxMenu
*helpMenu
= new wxMenu
;
218 helpMenu
->Append( wxID_ABOUT
, _T("&About wxGrid demo") );
220 wxMenuBar
*menuBar
= new wxMenuBar
;
221 menuBar
->Append( fileMenu
, _T("&File") );
222 menuBar
->Append( viewMenu
, _T("&View") );
223 menuBar
->Append( colMenu
, _T("&Colours") );
224 menuBar
->Append( editMenu
, _T("&Edit") );
225 menuBar
->Append( selectMenu
, _T("&Select") );
226 menuBar
->Append( helpMenu
, _T("&Help") );
228 SetMenuBar( menuBar
);
232 grid
= new wxGrid( this,
235 wxSize( 400, 300 ) );
238 int gridW
= 600, gridH
= 300;
239 int logW
= gridW
, logH
= 100;
241 logWin
= new wxTextCtrl( this,
244 wxPoint( 0, gridH
+ 20 ),
245 wxSize( logW
, logH
),
248 logger
= new wxLogTextCtrl( logWin
);
249 m_logOld
= wxLog::SetActiveTarget( logger
);
250 wxLog::SetTimestamp( NULL
);
253 // this will create a grid and, by default, an associated grid
255 grid
->CreateGrid( 0, 0 );
256 grid
->AppendRows(100);
257 grid
->AppendCols(100);
259 int ir
= grid
->GetNumberRows();
260 grid
->DeleteRows(0, ir
);
261 grid
->AppendRows(ir
);
263 grid
->SetRowSize( 0, 60 );
264 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
266 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
267 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
268 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
270 grid
->SetCellValue( 0, 2, _T("Blah") );
271 grid
->SetCellValue( 0, 3, _T("Read only") );
272 grid
->SetReadOnly( 0, 3 );
274 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
276 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
278 grid
->SetRowSize( 99, 60 );
279 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
280 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
282 grid
->SetCellTextColour(1, 2, *wxRED
);
283 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
285 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
287 grid
->SetCellValue(2, 2, _T("red"));
289 grid
->SetCellTextColour(2, 2, *wxRED
);
290 grid
->SetCellValue(3, 3, _T("green on grey"));
291 grid
->SetCellTextColour(3, 3, *wxGREEN
);
292 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
294 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
295 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
296 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
298 grid
->SetCellValue(3, 0, _T("0"));
299 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
300 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
302 wxGridCellAttr
*attr
;
303 attr
= new wxGridCellAttr
;
304 attr
->SetTextColour(*wxBLUE
);
305 grid
->SetColAttr(5, attr
);
306 attr
= new wxGridCellAttr
;
307 attr
->SetBackgroundColour(*wxRED
);
308 grid
->SetRowAttr(5, attr
);
310 grid
->SetCellValue(2, 4, _T("a wider column"));
311 grid
->SetColSize(4, 120);
312 grid
->SetColMinimalWidth(4, 120);
314 grid
->SetCellTextColour(5, 8, *wxGREEN
);
315 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
316 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"));
318 grid
->SetColFormatFloat(6);
319 grid
->SetCellValue(0, 6, _T("3.1415"));
320 grid
->SetCellValue(1, 6, _T("1415"));
321 grid
->SetCellValue(2, 6, _T("12345.67890"));
323 grid
->SetColFormatFloat(7, 6, 2);
324 grid
->SetCellValue(0, 7, _T("3.1415"));
325 grid
->SetCellValue(1, 7, _T("1415"));
326 grid
->SetCellValue(2, 7, _T("12345.67890"));
328 const wxString choices
[] =
330 _T("Please select a choice"),
331 _T("This takes two cells"),
332 _T("Another choice"),
334 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
335 grid
->SetCellSize(4, 0, 1, 2);
336 grid
->SetCellValue(4, 0, choices
[0]);
337 grid
->SetCellOverflow(4, 0, false);
339 grid
->SetCellSize(7, 1, 3, 4);
340 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
341 grid
->SetCellValue(7, 1, _T("Big box!"));
343 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
349 topSizer
->Add( logWin
,
355 SetSizer( topSizer
);
357 topSizer
->Fit( this );
364 GridFrame::~GridFrame()
367 delete wxLog::SetActiveTarget(m_logOld
);
372 void GridFrame::SetDefaults()
374 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
375 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
376 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
377 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
378 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
379 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
380 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
381 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
382 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
386 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
388 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
390 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
394 grid
->SetRowLabelSize( 0 );
399 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
401 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
403 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
407 grid
->SetColLabelSize( 0 );
412 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
415 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
419 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
421 grid
->EnableDragRowSize(
422 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
426 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
428 grid
->EnableDragColSize(
429 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
432 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
434 grid
->EnableDragGridSize(
435 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
438 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
440 grid
->EnableDragCell(
441 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
444 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
446 grid
->EnableGridLines(
447 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
450 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
452 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
454 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
455 _T("Pen Width"), 11, choices
);
457 int current
= grid
->GetCellHighlightPenWidth();
458 dlg
.SetSelection(current
);
459 if (dlg
.ShowModal() == wxID_OK
) {
460 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
464 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
466 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
468 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
469 _T("Pen Width"), 11, choices
);
471 int current
= grid
->GetCellHighlightROPenWidth();
472 dlg
.SetSelection(current
);
473 if (dlg
.ShowModal() == wxID_OK
) {
474 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
480 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
482 grid
->AutoSizeColumns();
486 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
488 grid
->SetDefaultCellOverflow(ev
.IsChecked());
492 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
495 grid
->SetCellSize( 7, 1, 5, 5 );
497 grid
->SetCellSize( 7, 1, 1, 5 );
501 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
503 wxColourDialog
dlg( NULL
);
504 if ( dlg
.ShowModal() == wxID_OK
)
506 wxColourData retData
;
507 retData
= dlg
.GetColourData();
508 wxColour colour
= retData
.GetColour();
510 grid
->SetLabelBackgroundColour( colour
);
515 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
517 wxColourDialog
dlg( NULL
);
518 if ( dlg
.ShowModal() == wxID_OK
)
520 wxColourData retData
;
521 retData
= dlg
.GetColourData();
522 wxColour colour
= retData
.GetColour();
524 grid
->SetLabelTextColour( colour
);
528 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
530 wxFont font
= wxGetFontFromUser(this);
533 grid
->SetLabelFont(font
);
537 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
540 grid
->GetRowLabelAlignment( &horiz
, &vert
);
545 horiz
= wxALIGN_CENTRE
;
549 horiz
= wxALIGN_RIGHT
;
553 horiz
= wxALIGN_LEFT
;
557 grid
->SetRowLabelAlignment( horiz
, vert
);
560 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
563 grid
->GetRowLabelAlignment( &horiz
, &vert
);
568 vert
= wxALIGN_CENTRE
;
572 vert
= wxALIGN_BOTTOM
;
580 grid
->SetRowLabelAlignment( horiz
, vert
);
584 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
587 grid
->GetColLabelAlignment( &horiz
, &vert
);
592 horiz
= wxALIGN_CENTRE
;
596 horiz
= wxALIGN_RIGHT
;
600 horiz
= wxALIGN_LEFT
;
604 grid
->SetColLabelAlignment( horiz
, vert
);
608 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
611 grid
->GetColLabelAlignment( &horiz
, &vert
);
616 vert
= wxALIGN_CENTRE
;
620 vert
= wxALIGN_BOTTOM
;
628 grid
->SetColLabelAlignment( horiz
, vert
);
632 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
634 wxColourDialog
dlg( NULL
);
635 if ( dlg
.ShowModal() == wxID_OK
)
637 wxColourData retData
;
638 retData
= dlg
.GetColourData();
639 wxColour colour
= retData
.GetColour();
641 grid
->SetGridLineColour( colour
);
646 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
648 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
652 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
654 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
658 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
660 if ( grid
->IsSelection() )
663 for ( int n
= 0; n
< grid
->GetNumberRows(); )
665 if ( grid
->IsInSelection( n
, 0 ) )
666 grid
->DeleteRows( n
, 1 );
675 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
677 if ( grid
->IsSelection() )
680 for ( int n
= 0; n
< grid
->GetNumberCols(); )
682 if ( grid
->IsInSelection( 0 , n
) )
683 grid
->DeleteCols( n
, 1 );
692 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
697 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
699 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
702 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
704 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
707 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
709 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
712 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
714 wxColour col
= wxGetColourFromUser(this);
717 grid
->SetDefaultCellTextColour(col
);
722 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
724 wxColour col
= wxGetColourFromUser(this);
727 // Check the new Refresh function by passing it a rectangle
728 // which exactly fits the grid.
730 wxRect
r(pt
, grid
->GetSize());
731 grid
->SetDefaultCellBackgroundColour(col
);
732 grid
->Refresh(true, &r
);
736 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
738 grid
->DeselectCell(3, 1);
741 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
743 grid
->DeselectCol(2);
746 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
748 grid
->DeselectRow(2);
751 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
753 grid
->ClearSelection();
756 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
758 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
761 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
763 grid
->SelectCol(2, m_addToSel
);
766 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
768 grid
->SelectRow(2, m_addToSel
);
771 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
776 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
778 m_addToSel
= event
.IsChecked();
781 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
783 logBuf
= wxEmptyString
;
784 if ( ev
.GetRow() != -1 )
786 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
788 else if ( ev
.GetCol() != -1 )
790 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
794 logBuf
<< _T("Left click on corner label");
797 if ( ev
.ShiftDown() ) logBuf
<< _T(" (shift down)");
798 if ( ev
.ControlDown() ) logBuf
<< _T(" (control down)");
799 wxLogMessage( wxT("%s"), logBuf
.c_str() );
801 // you must call event skip if you want default grid processing
807 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
809 logBuf
= wxEmptyString
;
810 logBuf
<< _T("Left click at row ") << ev
.GetRow()
811 << _T(" col ") << ev
.GetCol();
812 wxLogMessage( wxT("%s"), logBuf
.c_str() );
814 // you must call event skip if you want default grid processing
815 // (cell highlighting etc.)
821 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
823 logBuf
= wxEmptyString
;
824 logBuf
<< _T("Resized row ") << ev
.GetRowOrCol();
825 wxLogMessage( wxT("%s"), logBuf
.c_str() );
831 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
833 logBuf
= wxEmptyString
;
834 logBuf
<< _T("Resized col ") << ev
.GetRowOrCol();
835 wxLogMessage( wxT("%s"), logBuf
.c_str() );
841 void GridFrame::OnShowSelection(wxCommandEvent
& WXUNUSED(event
))
843 // max number of elements to dump -- otherwise it can take too much time
844 static const size_t countMax
= 100;
848 switch ( grid
->GetSelectionMode() )
850 case wxGrid::wxGridSelectCells
:
852 const wxGridCellCoordsArray
cells(grid
->GetSelectedCells());
853 size_t count
= cells
.size();
854 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count
);
855 if ( count
> countMax
)
857 wxLogMessage(_T("[too many selected cells, ")
858 _T("showing only the first %lu]"),
859 (unsigned long)countMax
);
863 for ( size_t n
= 0; n
< count
; n
++ )
865 const wxGridCellCoords
& c
= cells
[n
];
866 wxLogMessage(_T(" selected cell %lu: (%d, %d)"),
867 (unsigned long)n
, c
.GetCol(), c
.GetRow());
872 case wxGrid::wxGridSelectRows
:
876 case wxGrid::wxGridSelectColumns
:
878 const wxChar
*plural
, *single
;
886 plural
= _T("columns");
887 single
= _T("column");
890 const wxArrayInt
sels(rows
? grid
->GetSelectedRows()
891 : grid
->GetSelectedCols());
892 size_t count
= sels
.size();
893 wxLogMessage(_T("%lu %s selected:"),
894 (unsigned long)count
, plural
);
895 if ( count
> countMax
)
897 wxLogMessage(_T("[too many selected %s, ")
898 _T("showing only the first %lu]"),
899 plural
, (unsigned long)countMax
);
903 for ( size_t n
= 0; n
< count
; n
++ )
905 wxLogMessage(_T(" selected %s %lu: %d"),
906 single
, (unsigned long)n
, sels
[n
]);
912 wxFAIL_MSG( _T("unknown wxGrid selection mode") );
917 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
919 logBuf
= wxEmptyString
;
920 if ( ev
.Selecting() )
921 logBuf
<< _T("Selected ");
923 logBuf
<< _T("Deselected ");
924 logBuf
<< _T("cell at row ") << ev
.GetRow()
925 << _T(" col ") << ev
.GetCol()
926 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
927 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
928 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
929 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
930 wxLogMessage( wxT("%s"), logBuf
.c_str() );
932 // you must call Skip() if you want the default processing
933 // to occur in wxGrid
937 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
939 logBuf
= wxEmptyString
;
940 if ( ev
.Selecting() )
941 logBuf
<< _T("Selected ");
943 logBuf
<< _T("Deselected ");
944 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
945 << _T(" col ") << ev
.GetLeftCol()
946 << _T(" to row ") << ev
.GetBottomRow()
947 << _T(" col ") << ev
.GetRightCol()
948 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
949 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
950 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
951 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
952 wxLogMessage( wxT("%s"), logBuf
.c_str() );
957 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
959 logBuf
= wxEmptyString
;
960 logBuf
<< _T("Value changed for cell at")
961 << _T(" row ") << ev
.GetRow()
962 << _T(" col ") << ev
.GetCol();
964 wxLogMessage( wxT("%s"), logBuf
.c_str() );
969 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
971 logBuf
= wxEmptyString
;
972 logBuf
<< _T("Got request to drag cell at")
973 << _T(" row ") << ev
.GetRow()
974 << _T(" col ") << ev
.GetCol();
976 wxLogMessage( wxT("%s"), logBuf
.c_str() );
981 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
984 if ( (ev
.GetCol() == 4) &&
985 (ev
.GetRow() == 0) &&
986 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
987 _T("Checking"),wxYES_NO
) == wxNO
) ) {
993 wxLogMessage( wxT("Cell editor shown.") );
998 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1001 if ( (ev
.GetCol() == 4) &&
1002 (ev
.GetRow() == 0) &&
1003 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
1004 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1010 wxLogMessage( wxT("Cell editor hidden.") );
1015 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1017 (void)wxMessageBox( _T("\n\nwxGrid demo \n\n")
1018 _T("Michael Bedward, Julian Smart, Vadim Zeitlin"),
1024 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1029 void GridFrame::OnBugsTable(wxCommandEvent
& )
1031 BugsGridFrame
*frame
= new BugsGridFrame
;
1035 void GridFrame::OnSmallGrid(wxCommandEvent
& )
1037 wxFrame
* frame
= new wxFrame(NULL
, wxID_ANY
, _T("A Small Grid"),
1038 wxDefaultPosition
, wxSize(640, 480));
1039 wxPanel
* panel
= new wxPanel(frame
, wxID_ANY
);
1040 wxGrid
* grid
= new wxGrid(panel
, wxID_ANY
, wxPoint(10,10), wxSize(400,400),
1041 wxWANTS_CHARS
| wxSIMPLE_BORDER
);
1042 grid
->CreateGrid(3,3);
1046 void GridFrame::OnVTable(wxCommandEvent
& )
1048 static long s_sizeGrid
= 10000;
1051 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
1054 s
= wxGetTextFromUser( _T("Size of the table to create"),
1058 s
.ToLong( &s_sizeGrid
);
1061 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
1063 _T("wxGridDemo question"),
1068 if ( s_sizeGrid
!= -1 )
1070 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1075 // ----------------------------------------------------------------------------
1076 // MyGridCellRenderer
1077 // ----------------------------------------------------------------------------
1079 // do something that the default renderer doesn't here just to show that it is
1080 // possible to alter the appearance of the cell beyond what the attributes
1082 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1083 wxGridCellAttr
& attr
,
1089 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1091 dc
.SetPen(*wxGREEN_PEN
);
1092 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1093 dc
.DrawEllipse(rect
);
1096 // ----------------------------------------------------------------------------
1097 // MyGridCellAttrProvider
1098 // ----------------------------------------------------------------------------
1100 MyGridCellAttrProvider::MyGridCellAttrProvider()
1102 m_attrForOddRows
= new wxGridCellAttr
;
1103 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1106 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1108 m_attrForOddRows
->DecRef();
1111 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1112 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1114 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1120 attr
= m_attrForOddRows
;
1125 if ( !attr
->HasBackgroundColour() )
1127 wxGridCellAttr
*attrNew
= attr
->Clone();
1130 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1138 // ============================================================================
1139 // BigGridFrame and BigGridTable: Sample of a non-standard table
1140 // ============================================================================
1142 BigGridFrame::BigGridFrame(long sizeGrid
)
1143 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1144 wxDefaultPosition
, wxSize(500, 450))
1146 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1147 m_table
= new BigGridTable(sizeGrid
);
1149 // VZ: I don't understand why this slows down the display that much,
1150 // must profile it...
1151 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1153 m_grid
->SetTable(m_table
, true);
1155 #if defined __WXMOTIF__
1156 // MB: the grid isn't getting a sensible default size under wxMotif
1158 GetClientSize( &cw
, &ch
);
1159 m_grid
->SetSize( cw
, ch
);
1163 // ============================================================================
1164 // BugsGridFrame: a "realistic" table
1165 // ============================================================================
1167 // ----------------------------------------------------------------------------
1169 // ----------------------------------------------------------------------------
1192 static const wxString severities
[] =
1201 static struct BugsGridData
1207 wxChar platform
[12];
1209 } gs_dataBugsGrid
[] =
1211 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1212 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1213 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1214 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1217 static const wxChar
*headers
[Col_Max
] =
1227 // ----------------------------------------------------------------------------
1229 // ----------------------------------------------------------------------------
1231 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1237 return wxGRID_VALUE_NUMBER
;;
1240 // fall thorugh (TODO should be a list)
1243 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1246 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1249 return wxGRID_VALUE_BOOL
;
1252 wxFAIL_MSG(_T("unknown column"));
1254 return wxEmptyString
;
1257 int BugsGridTable::GetNumberRows()
1259 return WXSIZEOF(gs_dataBugsGrid
);
1262 int BugsGridTable::GetNumberCols()
1267 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1272 wxString
BugsGridTable::GetValue( int row
, int col
)
1274 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1279 return wxString::Format(_T("%d"), gd
.id
);
1282 return wxString::Format(_T("%d"), gd
.prio
);
1285 return gd
.opened
? _T("1") : _T("0");
1288 return severities
[gd
.severity
];
1297 return wxEmptyString
;
1300 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1302 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1309 wxFAIL_MSG(_T("unexpected column"));
1315 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1317 if ( severities
[n
] == value
)
1319 gd
.severity
= (Severity
)n
;
1324 if ( n
== WXSIZEOF(severities
) )
1326 wxLogWarning(_T("Invalid severity value '%s'."),
1328 gd
.severity
= Sev_Normal
;
1334 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1338 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1344 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1346 const wxString
& typeName
)
1348 if ( typeName
== wxGRID_VALUE_STRING
)
1352 else if ( typeName
== wxGRID_VALUE_BOOL
)
1354 return col
== Col_Opened
;
1356 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1358 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1366 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1368 return CanGetValueAs(row
, col
, typeName
);
1371 long BugsGridTable::GetValueAsLong( int row
, int col
)
1373 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1387 wxFAIL_MSG(_T("unexpected column"));
1392 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1394 if ( col
== Col_Opened
)
1396 return gs_dataBugsGrid
[row
].opened
;
1400 wxFAIL_MSG(_T("unexpected column"));
1406 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1408 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1417 wxFAIL_MSG(_T("unexpected column"));
1421 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1423 if ( col
== Col_Opened
)
1425 gs_dataBugsGrid
[row
].opened
= value
;
1429 wxFAIL_MSG(_T("unexpected column"));
1433 wxString
BugsGridTable::GetColLabelValue( int col
)
1435 return headers
[col
];
1438 // ----------------------------------------------------------------------------
1440 // ----------------------------------------------------------------------------
1442 BugsGridFrame::BugsGridFrame()
1443 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"),
1444 wxDefaultPosition
, wxSize(500, 300))
1446 wxGrid
*grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
);
1447 wxGridTableBase
*table
= new BugsGridTable();
1448 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1449 grid
->SetTable(table
, true);
1451 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1452 *attrRangeEditor
= new wxGridCellAttr
,
1453 *attrCombo
= new wxGridCellAttr
;
1455 attrRO
->SetReadOnly();
1456 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1457 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1460 grid
->SetColAttr(Col_Id
, attrRO
);
1461 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1462 grid
->SetColAttr(Col_Severity
, attrCombo
);
1464 grid
->SetMargins(0, 0);
1467 SetClientSize(grid
->GetSize());