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"
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_TOGGLECOLMOVING
, GridFrame::ToggleColMoving
)
72 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
73 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
74 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
75 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
76 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
77 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
78 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
79 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
80 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
81 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
82 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
83 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
84 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
85 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
86 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
87 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
88 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
89 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
90 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
91 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
92 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
93 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
95 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
96 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
98 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
99 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
100 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
101 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
102 EVT_MENU( ID_SMALL_GRID
, GridFrame::OnSmallGrid
)
104 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
105 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
106 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
107 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
108 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
109 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
110 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
111 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
112 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
113 EVT_MENU( ID_SHOW_SELECTION
, GridFrame::OnShowSelection
)
115 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
116 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
118 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
119 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
120 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
121 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
122 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
123 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
124 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
125 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
127 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
128 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
132 GridFrame::GridFrame()
133 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets grid class demo"),
137 wxMenu
*fileMenu
= new wxMenu
;
138 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
139 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
140 fileMenu
->Append( ID_SMALL_GRID
, _T("&Small Grid test\tCtrl-S"));
141 fileMenu
->AppendSeparator();
142 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
144 wxMenu
*viewMenu
= new wxMenu
;
145 viewMenu
->Append( ID_TOGGLEROWLABELS
, _T("&Row labels"), wxEmptyString
, wxITEM_CHECK
);
146 viewMenu
->Append( ID_TOGGLECOLLABELS
, _T("&Col labels"), wxEmptyString
, wxITEM_CHECK
);
147 viewMenu
->Append( ID_TOGGLEEDIT
, _T("&Editable"), wxEmptyString
, wxITEM_CHECK
);
148 viewMenu
->Append( ID_TOGGLEROWSIZING
, _T("Ro&w drag-resize"), wxEmptyString
, wxITEM_CHECK
);
149 viewMenu
->Append( ID_TOGGLECOLSIZING
, _T("C&ol drag-resize"), wxEmptyString
, wxITEM_CHECK
);
150 viewMenu
->Append( ID_TOGGLECOLMOVING
, _T("Col drag-&move"), wxEmptyString
, wxITEM_CHECK
);
151 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, _T("&Grid drag-resize"), wxEmptyString
, wxITEM_CHECK
);
152 viewMenu
->Append( ID_TOGGLEGRIDDRAGCELL
, _T("&Grid drag-cell"), wxEmptyString
, wxITEM_CHECK
);
153 viewMenu
->Append( ID_TOGGLEGRIDLINES
, _T("&Grid Lines"), wxEmptyString
, wxITEM_CHECK
);
154 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, _T("&Set Cell Highlight Width...") );
155 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, _T("&Set Cell RO Highlight Width...") );
156 viewMenu
->Append( ID_AUTOSIZECOLS
, _T("&Auto-size cols") );
157 viewMenu
->Append( ID_CELLOVERFLOW
, _T("&Overflow cells"), wxEmptyString
, wxITEM_CHECK
);
158 viewMenu
->Append( ID_RESIZECELL
, _T("&Resize cell (7,1)"), wxEmptyString
, wxITEM_CHECK
);
160 wxMenu
*rowLabelMenu
= new wxMenu
;
162 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
164 _T("Change alignment of row labels") );
166 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
167 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
169 wxMenu
*colLabelMenu
= new wxMenu
;
171 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
173 _T("Change alignment of col labels") );
175 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
176 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
178 wxMenu
*colMenu
= new wxMenu
;
179 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
180 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
181 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
182 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
183 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
184 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
186 wxMenu
*editMenu
= new wxMenu
;
187 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
188 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
189 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
190 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
191 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
193 wxMenu
*selectMenu
= new wxMenu
;
194 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
195 _T("When off, old selection is deselected before ")
196 _T("selecting the new cells"), wxITEM_CHECK
);
197 selectMenu
->Append( ID_SHOW_SELECTION
,
198 _T("&Show current selection\tCtrl-Alt-S"));
199 selectMenu
->AppendSeparator();
200 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
201 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
202 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
203 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
204 selectMenu
->AppendSeparator();
205 selectMenu
->Append( ID_DESELECT_ALL
, _T("Deselect all"));
206 selectMenu
->Append( ID_DESELECT_ROW
, _T("Deselect row 2"));
207 selectMenu
->Append( ID_DESELECT_COL
, _T("Deselect col 2"));
208 selectMenu
->Append( ID_DESELECT_CELL
, _T("Deselect cell (3, 1)"));
209 wxMenu
*selectionMenu
= new wxMenu
;
210 selectMenu
->Append( ID_CHANGESEL
, _T("Change &selection mode"),
212 _T("Change selection mode") );
214 selectionMenu
->Append( ID_SELCELLS
, _T("Select &Cells") );
215 selectionMenu
->Append( ID_SELROWS
, _T("Select &Rows") );
216 selectionMenu
->Append( ID_SELCOLS
, _T("Select C&ols") );
219 wxMenu
*helpMenu
= new wxMenu
;
220 helpMenu
->Append( wxID_ABOUT
, _T("&About wxGrid demo") );
222 wxMenuBar
*menuBar
= new wxMenuBar
;
223 menuBar
->Append( fileMenu
, _T("&File") );
224 menuBar
->Append( viewMenu
, _T("&View") );
225 menuBar
->Append( colMenu
, _T("&Colours") );
226 menuBar
->Append( editMenu
, _T("&Edit") );
227 menuBar
->Append( selectMenu
, _T("&Select") );
228 menuBar
->Append( helpMenu
, _T("&Help") );
230 SetMenuBar( menuBar
);
234 grid
= new wxGrid( this,
237 wxSize( 400, 300 ) );
240 int gridW
= 600, gridH
= 300;
241 int logW
= gridW
, logH
= 100;
243 logWin
= new wxTextCtrl( this,
246 wxPoint( 0, gridH
+ 20 ),
247 wxSize( logW
, logH
),
250 logger
= new wxLogTextCtrl( logWin
);
251 m_logOld
= wxLog::SetActiveTarget( logger
);
252 wxLog::SetTimestamp( NULL
);
255 // this will create a grid and, by default, an associated grid
257 grid
->CreateGrid( 0, 0 );
258 grid
->AppendRows(100);
259 grid
->AppendCols(100);
261 int ir
= grid
->GetNumberRows();
262 grid
->DeleteRows(0, ir
);
263 grid
->AppendRows(ir
);
265 grid
->SetRowSize( 0, 60 );
266 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
268 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
269 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
270 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
272 grid
->SetCellValue( 0, 2, _T("Blah") );
273 grid
->SetCellValue( 0, 3, _T("Read only") );
274 grid
->SetReadOnly( 0, 3 );
276 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
278 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
280 grid
->SetRowSize( 99, 60 );
281 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
282 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
284 grid
->SetCellTextColour(1, 2, *wxRED
);
285 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
287 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
289 grid
->SetCellValue(2, 2, _T("red"));
291 grid
->SetCellTextColour(2, 2, *wxRED
);
292 grid
->SetCellValue(3, 3, _T("green on grey"));
293 grid
->SetCellTextColour(3, 3, *wxGREEN
);
294 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
296 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
297 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
298 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
300 grid
->SetCellValue(3, 0, _T("0"));
301 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
302 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
304 wxGridCellAttr
*attr
;
305 attr
= new wxGridCellAttr
;
306 attr
->SetTextColour(*wxBLUE
);
307 grid
->SetColAttr(5, attr
);
308 attr
= new wxGridCellAttr
;
309 attr
->SetBackgroundColour(*wxRED
);
310 grid
->SetRowAttr(5, attr
);
312 grid
->SetCellValue(2, 4, _T("a wider column"));
313 grid
->SetColSize(4, 120);
314 grid
->SetColMinimalWidth(4, 120);
316 grid
->SetCellTextColour(5, 8, *wxGREEN
);
317 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
318 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"));
320 grid
->SetColFormatFloat(6);
321 grid
->SetCellValue(0, 6, _T("3.1415"));
322 grid
->SetCellValue(1, 6, _T("1415"));
323 grid
->SetCellValue(2, 6, _T("12345.67890"));
325 grid
->SetColFormatFloat(7, 6, 2);
326 grid
->SetCellValue(0, 7, _T("3.1415"));
327 grid
->SetCellValue(1, 7, _T("1415"));
328 grid
->SetCellValue(2, 7, _T("12345.67890"));
330 const wxString choices
[] =
332 _T("Please select a choice"),
333 _T("This takes two cells"),
334 _T("Another choice"),
336 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
337 grid
->SetCellSize(4, 0, 1, 2);
338 grid
->SetCellValue(4, 0, choices
[0]);
339 grid
->SetCellOverflow(4, 0, false);
341 grid
->SetCellSize(7, 1, 3, 4);
342 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
343 grid
->SetCellValue(7, 1, _T("Big box!"));
345 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
351 topSizer
->Add( logWin
,
357 SetSizer( topSizer
);
359 topSizer
->Fit( this );
366 GridFrame::~GridFrame()
369 delete wxLog::SetActiveTarget(m_logOld
);
374 void GridFrame::SetDefaults()
376 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
377 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
378 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
379 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
380 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
381 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
382 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
383 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
384 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
385 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
389 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
391 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
393 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
397 grid
->SetRowLabelSize( 0 );
402 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
404 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
406 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
410 grid
->SetColLabelSize( 0 );
415 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
418 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
422 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
424 grid
->EnableDragRowSize(
425 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
429 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
431 grid
->EnableDragColSize(
432 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
435 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
437 grid
->EnableDragColMove(
438 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
441 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
443 grid
->EnableDragGridSize(
444 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
447 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
449 grid
->EnableDragCell(
450 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
453 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
455 grid
->EnableGridLines(
456 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
459 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
461 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
463 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
464 _T("Pen Width"), 11, choices
);
466 int current
= grid
->GetCellHighlightPenWidth();
467 dlg
.SetSelection(current
);
468 if (dlg
.ShowModal() == wxID_OK
) {
469 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
473 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
475 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
477 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
478 _T("Pen Width"), 11, choices
);
480 int current
= grid
->GetCellHighlightROPenWidth();
481 dlg
.SetSelection(current
);
482 if (dlg
.ShowModal() == wxID_OK
) {
483 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
489 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
491 grid
->AutoSizeColumns();
495 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
497 grid
->SetDefaultCellOverflow(ev
.IsChecked());
501 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
504 grid
->SetCellSize( 7, 1, 5, 5 );
506 grid
->SetCellSize( 7, 1, 1, 5 );
510 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
512 wxColourDialog
dlg( NULL
);
513 if ( dlg
.ShowModal() == wxID_OK
)
515 wxColourData retData
;
516 retData
= dlg
.GetColourData();
517 wxColour colour
= retData
.GetColour();
519 grid
->SetLabelBackgroundColour( colour
);
524 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
526 wxColourDialog
dlg( NULL
);
527 if ( dlg
.ShowModal() == wxID_OK
)
529 wxColourData retData
;
530 retData
= dlg
.GetColourData();
531 wxColour colour
= retData
.GetColour();
533 grid
->SetLabelTextColour( colour
);
537 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
539 wxFont font
= wxGetFontFromUser(this);
542 grid
->SetLabelFont(font
);
546 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
549 grid
->GetRowLabelAlignment( &horiz
, &vert
);
554 horiz
= wxALIGN_CENTRE
;
558 horiz
= wxALIGN_RIGHT
;
562 horiz
= wxALIGN_LEFT
;
566 grid
->SetRowLabelAlignment( horiz
, vert
);
569 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
572 grid
->GetRowLabelAlignment( &horiz
, &vert
);
577 vert
= wxALIGN_CENTRE
;
581 vert
= wxALIGN_BOTTOM
;
589 grid
->SetRowLabelAlignment( horiz
, vert
);
593 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
596 grid
->GetColLabelAlignment( &horiz
, &vert
);
601 horiz
= wxALIGN_CENTRE
;
605 horiz
= wxALIGN_RIGHT
;
609 horiz
= wxALIGN_LEFT
;
613 grid
->SetColLabelAlignment( horiz
, vert
);
617 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
620 grid
->GetColLabelAlignment( &horiz
, &vert
);
625 vert
= wxALIGN_CENTRE
;
629 vert
= wxALIGN_BOTTOM
;
637 grid
->SetColLabelAlignment( horiz
, vert
);
641 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
643 wxColourDialog
dlg( NULL
);
644 if ( dlg
.ShowModal() == wxID_OK
)
646 wxColourData retData
;
647 retData
= dlg
.GetColourData();
648 wxColour colour
= retData
.GetColour();
650 grid
->SetGridLineColour( colour
);
655 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
657 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
661 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
663 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
667 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
669 if ( grid
->IsSelection() )
672 for ( int n
= 0; n
< grid
->GetNumberRows(); )
674 if ( grid
->IsInSelection( n
, 0 ) )
675 grid
->DeleteRows( n
, 1 );
684 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
686 if ( grid
->IsSelection() )
689 for ( int n
= 0; n
< grid
->GetNumberCols(); )
691 if ( grid
->IsInSelection( 0 , n
) )
692 grid
->DeleteCols( n
, 1 );
701 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
706 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
708 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
711 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
713 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
716 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
718 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
721 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
723 wxColour col
= wxGetColourFromUser(this);
726 grid
->SetDefaultCellTextColour(col
);
731 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
733 wxColour col
= wxGetColourFromUser(this);
736 // Check the new Refresh function by passing it a rectangle
737 // which exactly fits the grid.
739 wxRect
r(pt
, grid
->GetSize());
740 grid
->SetDefaultCellBackgroundColour(col
);
741 grid
->Refresh(true, &r
);
745 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
747 grid
->DeselectCell(3, 1);
750 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
752 grid
->DeselectCol(2);
755 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
757 grid
->DeselectRow(2);
760 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
762 grid
->ClearSelection();
765 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
767 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
770 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
772 grid
->SelectCol(2, m_addToSel
);
775 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
777 grid
->SelectRow(2, m_addToSel
);
780 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
785 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
787 m_addToSel
= event
.IsChecked();
790 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
792 logBuf
= wxEmptyString
;
793 if ( ev
.GetRow() != -1 )
795 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
797 else if ( ev
.GetCol() != -1 )
799 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
803 logBuf
<< _T("Left click on corner label");
806 if ( ev
.ShiftDown() ) logBuf
<< _T(" (shift down)");
807 if ( ev
.ControlDown() ) logBuf
<< _T(" (control down)");
808 wxLogMessage( wxT("%s"), logBuf
.c_str() );
810 // you must call event skip if you want default grid processing
816 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
818 logBuf
= wxEmptyString
;
819 logBuf
<< _T("Left click at row ") << ev
.GetRow()
820 << _T(" col ") << ev
.GetCol();
821 wxLogMessage( wxT("%s"), logBuf
.c_str() );
823 // you must call event skip if you want default grid processing
824 // (cell highlighting etc.)
830 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
832 logBuf
= wxEmptyString
;
833 logBuf
<< _T("Resized row ") << ev
.GetRowOrCol();
834 wxLogMessage( wxT("%s"), logBuf
.c_str() );
840 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
842 logBuf
= wxEmptyString
;
843 logBuf
<< _T("Resized col ") << ev
.GetRowOrCol();
844 wxLogMessage( wxT("%s"), logBuf
.c_str() );
850 void GridFrame::OnShowSelection(wxCommandEvent
& WXUNUSED(event
))
852 // max number of elements to dump -- otherwise it can take too much time
853 static const size_t countMax
= 100;
857 switch ( grid
->GetSelectionMode() )
859 case wxGrid::wxGridSelectCells
:
861 const wxGridCellCoordsArray
cells(grid
->GetSelectedCells());
862 size_t count
= cells
.size();
863 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count
);
864 if ( count
> countMax
)
866 wxLogMessage(_T("[too many selected cells, ")
867 _T("showing only the first %lu]"),
868 (unsigned long)countMax
);
872 for ( size_t n
= 0; n
< count
; n
++ )
874 const wxGridCellCoords
& c
= cells
[n
];
875 wxLogMessage(_T(" selected cell %lu: (%d, %d)"),
876 (unsigned long)n
, c
.GetCol(), c
.GetRow());
881 case wxGrid::wxGridSelectRows
:
885 case wxGrid::wxGridSelectColumns
:
887 const wxChar
*plural
, *single
;
895 plural
= _T("columns");
896 single
= _T("column");
899 const wxArrayInt
sels(rows
? grid
->GetSelectedRows()
900 : grid
->GetSelectedCols());
901 size_t count
= sels
.size();
902 wxLogMessage(_T("%lu %s selected:"),
903 (unsigned long)count
, plural
);
904 if ( count
> countMax
)
906 wxLogMessage(_T("[too many selected %s, ")
907 _T("showing only the first %lu]"),
908 plural
, (unsigned long)countMax
);
912 for ( size_t n
= 0; n
< count
; n
++ )
914 wxLogMessage(_T(" selected %s %lu: %d"),
915 single
, (unsigned long)n
, sels
[n
]);
921 wxFAIL_MSG( _T("unknown wxGrid selection mode") );
926 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
928 logBuf
= wxEmptyString
;
929 if ( ev
.Selecting() )
930 logBuf
<< _T("Selected ");
932 logBuf
<< _T("Deselected ");
933 logBuf
<< _T("cell at row ") << ev
.GetRow()
934 << _T(" col ") << ev
.GetCol()
935 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
936 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
937 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
938 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
940 //Indicate whether this column was moved
941 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
942 logBuf
<< _T(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
944 wxLogMessage( wxT("%s"), logBuf
.c_str() );
946 // you must call Skip() if you want the default processing
947 // to occur in wxGrid
951 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
953 logBuf
= wxEmptyString
;
954 if ( ev
.Selecting() )
955 logBuf
<< _T("Selected ");
957 logBuf
<< _T("Deselected ");
958 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
959 << _T(" col ") << ev
.GetLeftCol()
960 << _T(" to row ") << ev
.GetBottomRow()
961 << _T(" col ") << ev
.GetRightCol()
962 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
963 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
964 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
965 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
966 wxLogMessage( wxT("%s"), logBuf
.c_str() );
971 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
973 logBuf
= wxEmptyString
;
974 logBuf
<< _T("Value changed for cell at")
975 << _T(" row ") << ev
.GetRow()
976 << _T(" col ") << ev
.GetCol();
978 wxLogMessage( wxT("%s"), logBuf
.c_str() );
983 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
985 logBuf
= wxEmptyString
;
986 logBuf
<< _T("Got request to drag cell at")
987 << _T(" row ") << ev
.GetRow()
988 << _T(" col ") << ev
.GetCol();
990 wxLogMessage( wxT("%s"), logBuf
.c_str() );
995 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
998 if ( (ev
.GetCol() == 4) &&
999 (ev
.GetRow() == 0) &&
1000 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
1001 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1007 wxLogMessage( wxT("Cell editor shown.") );
1012 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1015 if ( (ev
.GetCol() == 4) &&
1016 (ev
.GetRow() == 0) &&
1017 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
1018 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1024 wxLogMessage( wxT("Cell editor hidden.") );
1029 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1031 (void)wxMessageBox( _T("\n\nwxGrid demo \n\n")
1032 _T("Michael Bedward, Julian Smart, Vadim Zeitlin"),
1038 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1043 void GridFrame::OnBugsTable(wxCommandEvent
& )
1045 BugsGridFrame
*frame
= new BugsGridFrame
;
1049 void GridFrame::OnSmallGrid(wxCommandEvent
& )
1051 wxFrame
* frame
= new wxFrame(NULL
, wxID_ANY
, _T("A Small Grid"),
1052 wxDefaultPosition
, wxSize(640, 480));
1053 wxPanel
* panel
= new wxPanel(frame
, wxID_ANY
);
1054 wxGrid
* grid
= new wxGrid(panel
, wxID_ANY
, wxPoint(10,10), wxSize(400,400),
1055 wxWANTS_CHARS
| wxSIMPLE_BORDER
);
1056 grid
->CreateGrid(3,3);
1060 void GridFrame::OnVTable(wxCommandEvent
& )
1062 static long s_sizeGrid
= 10000;
1065 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
1068 s
= wxGetTextFromUser( _T("Size of the table to create"),
1072 s
.ToLong( &s_sizeGrid
);
1075 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
1077 _T("wxGridDemo question"),
1082 if ( s_sizeGrid
!= -1 )
1084 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1089 // ----------------------------------------------------------------------------
1090 // MyGridCellRenderer
1091 // ----------------------------------------------------------------------------
1093 // do something that the default renderer doesn't here just to show that it is
1094 // possible to alter the appearance of the cell beyond what the attributes
1096 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1097 wxGridCellAttr
& attr
,
1103 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1105 dc
.SetPen(*wxGREEN_PEN
);
1106 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1107 dc
.DrawEllipse(rect
);
1110 // ----------------------------------------------------------------------------
1111 // MyGridCellAttrProvider
1112 // ----------------------------------------------------------------------------
1114 MyGridCellAttrProvider::MyGridCellAttrProvider()
1116 m_attrForOddRows
= new wxGridCellAttr
;
1117 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1120 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1122 m_attrForOddRows
->DecRef();
1125 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1126 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1128 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1134 attr
= m_attrForOddRows
;
1139 if ( !attr
->HasBackgroundColour() )
1141 wxGridCellAttr
*attrNew
= attr
->Clone();
1144 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1152 // ============================================================================
1153 // BigGridFrame and BigGridTable: Sample of a non-standard table
1154 // ============================================================================
1156 BigGridFrame::BigGridFrame(long sizeGrid
)
1157 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1158 wxDefaultPosition
, wxSize(500, 450))
1160 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1161 m_table
= new BigGridTable(sizeGrid
);
1163 // VZ: I don't understand why this slows down the display that much,
1164 // must profile it...
1165 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1167 m_grid
->SetTable(m_table
, true);
1169 #if defined __WXMOTIF__
1170 // MB: the grid isn't getting a sensible default size under wxMotif
1172 GetClientSize( &cw
, &ch
);
1173 m_grid
->SetSize( cw
, ch
);
1177 // ============================================================================
1178 // BugsGridFrame: a "realistic" table
1179 // ============================================================================
1181 // ----------------------------------------------------------------------------
1183 // ----------------------------------------------------------------------------
1206 static const wxString severities
[] =
1215 static struct BugsGridData
1221 wxChar platform
[12];
1223 } gs_dataBugsGrid
[] =
1225 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1226 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1227 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1228 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1231 static const wxChar
*headers
[Col_Max
] =
1241 // ----------------------------------------------------------------------------
1243 // ----------------------------------------------------------------------------
1245 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1251 return wxGRID_VALUE_NUMBER
;;
1254 // fall thorugh (TODO should be a list)
1257 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1260 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1263 return wxGRID_VALUE_BOOL
;
1266 wxFAIL_MSG(_T("unknown column"));
1268 return wxEmptyString
;
1271 int BugsGridTable::GetNumberRows()
1273 return WXSIZEOF(gs_dataBugsGrid
);
1276 int BugsGridTable::GetNumberCols()
1281 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1286 wxString
BugsGridTable::GetValue( int row
, int col
)
1288 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1293 return wxString::Format(_T("%d"), gd
.id
);
1296 return wxString::Format(_T("%d"), gd
.prio
);
1299 return gd
.opened
? _T("1") : _T("0");
1302 return severities
[gd
.severity
];
1311 return wxEmptyString
;
1314 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1316 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1323 wxFAIL_MSG(_T("unexpected column"));
1329 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1331 if ( severities
[n
] == value
)
1333 gd
.severity
= (Severity
)n
;
1338 if ( n
== WXSIZEOF(severities
) )
1340 wxLogWarning(_T("Invalid severity value '%s'."),
1342 gd
.severity
= Sev_Normal
;
1348 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1352 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1358 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1360 const wxString
& typeName
)
1362 if ( typeName
== wxGRID_VALUE_STRING
)
1366 else if ( typeName
== wxGRID_VALUE_BOOL
)
1368 return col
== Col_Opened
;
1370 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1372 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1380 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1382 return CanGetValueAs(row
, col
, typeName
);
1385 long BugsGridTable::GetValueAsLong( int row
, int col
)
1387 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1401 wxFAIL_MSG(_T("unexpected column"));
1406 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1408 if ( col
== Col_Opened
)
1410 return gs_dataBugsGrid
[row
].opened
;
1414 wxFAIL_MSG(_T("unexpected column"));
1420 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1422 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1431 wxFAIL_MSG(_T("unexpected column"));
1435 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1437 if ( col
== Col_Opened
)
1439 gs_dataBugsGrid
[row
].opened
= value
;
1443 wxFAIL_MSG(_T("unexpected column"));
1447 wxString
BugsGridTable::GetColLabelValue( int col
)
1449 return headers
[col
];
1452 // ----------------------------------------------------------------------------
1454 // ----------------------------------------------------------------------------
1456 BugsGridFrame::BugsGridFrame()
1457 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"),
1458 wxDefaultPosition
, wxSize(500, 300))
1460 wxGrid
*grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
);
1461 wxGridTableBase
*table
= new BugsGridTable();
1462 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1463 grid
->SetTable(table
, true);
1465 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1466 *attrRangeEditor
= new wxGridCellAttr
,
1467 *attrCombo
= new wxGridCellAttr
;
1469 attrRO
->SetReadOnly();
1470 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1471 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1474 grid
->SetColAttr(Col_Id
, attrRO
);
1475 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1476 grid
->SetColAttr(Col_Severity
, attrCombo
);
1478 grid
->SetMargins(0, 0);
1481 SetClientSize(grid
->GetSize());