1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
7 // Copyright: (c) Michael Bedward, Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(__APPLE__)
20 #pragma implementation
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/colordlg.h"
36 #include "wx/fontdlg.h"
37 #include "wx/numdlg.h"
40 #include "wx/generic/gridctrl.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 IMPLEMENT_APP( GridApp
)
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 bool GridApp::OnInit()
60 GridFrame
*frame
= new GridFrame
;
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
71 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
72 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
73 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
74 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
75 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
76 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
77 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
78 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
79 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
80 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
81 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
82 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
83 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
84 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
85 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
86 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
87 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
88 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
89 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
90 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
91 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
92 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
93 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
94 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
95 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
96 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
98 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
99 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
101 EVT_MENU( ID_ABOUT
, GridFrame::About
)
102 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
103 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
104 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
105 EVT_MENU( ID_SMALL_GRID
, GridFrame::OnSmallGrid
)
107 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
108 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
109 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
110 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
111 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
112 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
113 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
114 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
115 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
117 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
118 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
120 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
121 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
122 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
123 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
124 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
125 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
126 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
128 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
129 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
133 GridFrame::GridFrame()
134 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets grid class demo"),
138 wxMenu
*fileMenu
= new wxMenu
;
139 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
140 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
141 fileMenu
->Append( ID_SMALL_GRID
, _T("&Small Grid test\tCtrl-S"));
142 fileMenu
->AppendSeparator();
143 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
145 wxMenu
*viewMenu
= new wxMenu
;
146 viewMenu
->Append( ID_TOGGLEROWLABELS
, _T("&Row labels"), wxEmptyString
, wxITEM_CHECK
);
147 viewMenu
->Append( ID_TOGGLECOLLABELS
, _T("&Col labels"), wxEmptyString
, wxITEM_CHECK
);
148 viewMenu
->Append( ID_TOGGLEEDIT
, _T("&Editable"), wxEmptyString
, wxITEM_CHECK
);
149 viewMenu
->Append( ID_TOGGLEROWSIZING
, _T("Ro&w drag-resize"), wxEmptyString
, wxITEM_CHECK
);
150 viewMenu
->Append( ID_TOGGLECOLSIZING
, _T("C&ol drag-resize"), wxEmptyString
, wxITEM_CHECK
);
151 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, _T("&Grid drag-resize"), wxEmptyString
, wxITEM_CHECK
);
152 viewMenu
->Append( ID_TOGGLEGRIDLINES
, _T("&Grid Lines"), wxEmptyString
, wxITEM_CHECK
);
153 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, _T("&Set Cell Highlight Width...") );
154 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, _T("&Set Cell RO Highlight Width...") );
155 viewMenu
->Append( ID_AUTOSIZECOLS
, _T("&Auto-size cols") );
156 viewMenu
->Append( ID_CELLOVERFLOW
, _T("&Overflow cells"), wxEmptyString
, wxITEM_CHECK
);
157 viewMenu
->Append( ID_RESIZECELL
, _T("&Resize cell (7,1)"), wxEmptyString
, wxITEM_CHECK
);
159 wxMenu
*rowLabelMenu
= new wxMenu
;
161 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
163 _T("Change alignment of row labels") );
165 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
166 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
168 wxMenu
*colLabelMenu
= new wxMenu
;
170 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
172 _T("Change alignment of col labels") );
174 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
175 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
177 wxMenu
*colMenu
= new wxMenu
;
178 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
179 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
180 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
181 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
182 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
183 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
185 wxMenu
*editMenu
= new wxMenu
;
186 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
187 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
188 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
189 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
190 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
192 wxMenu
*selectMenu
= new wxMenu
;
193 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
194 _T("When off, old selection is deselected before ")
195 _T("selecting the new cells"), wxITEM_CHECK
);
196 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
197 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
198 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
199 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
200 selectMenu
->Append( ID_DESELECT_ALL
, _T("Deselect all"));
201 selectMenu
->Append( ID_DESELECT_ROW
, _T("Deselect row 2"));
202 selectMenu
->Append( ID_DESELECT_COL
, _T("Deselect col 2"));
203 selectMenu
->Append( ID_DESELECT_CELL
, _T("Deselect cell (3, 1)"));
204 wxMenu
*selectionMenu
= new wxMenu
;
205 selectMenu
->Append( ID_CHANGESEL
, _T("Change &selection mode"),
207 _T("Change selection mode") );
209 selectionMenu
->Append( ID_SELCELLS
, _T("Select &Cells") );
210 selectionMenu
->Append( ID_SELROWS
, _T("Select &Rows") );
211 selectionMenu
->Append( ID_SELCOLS
, _T("Select C&ols") );
214 wxMenu
*helpMenu
= new wxMenu
;
215 helpMenu
->Append( ID_ABOUT
, _T("&About wxGrid demo") );
217 wxMenuBar
*menuBar
= new wxMenuBar
;
218 menuBar
->Append( fileMenu
, _T("&File") );
219 menuBar
->Append( viewMenu
, _T("&View") );
220 menuBar
->Append( colMenu
, _T("&Colours") );
221 menuBar
->Append( editMenu
, _T("&Edit") );
222 menuBar
->Append( selectMenu
, _T("&Select") );
223 menuBar
->Append( helpMenu
, _T("&Help") );
225 SetMenuBar( menuBar
);
229 grid
= new wxGrid( this,
232 wxSize( 400, 300 ) );
235 int gridW
= 600, gridH
= 300;
236 int logW
= gridW
, logH
= 100;
238 logWin
= new wxTextCtrl( this,
241 wxPoint( 0, gridH
+ 20 ),
242 wxSize( logW
, logH
),
245 logger
= new wxLogTextCtrl( logWin
);
246 m_logOld
= logger
->SetActiveTarget( logger
);
247 logger
->SetTimestamp( NULL
);
250 // this will create a grid and, by default, an associated grid
252 grid
->CreateGrid( 0, 0 );
253 grid
->AppendRows(100);
254 grid
->AppendCols(100);
256 int ir
= grid
->GetNumberRows();
257 grid
->DeleteRows(0, ir
);
258 grid
->AppendRows(ir
);
260 grid
->SetRowSize( 0, 60 );
261 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
263 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
264 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
265 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
267 grid
->SetCellValue( 0, 2, _T("Blah") );
268 grid
->SetCellValue( 0, 3, _T("Read only") );
269 grid
->SetReadOnly( 0, 3 );
271 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
273 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
275 grid
->SetRowSize( 99, 60 );
276 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
277 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
279 grid
->SetCellTextColour(1, 2, *wxRED
);
280 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
282 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
284 grid
->SetCellValue(2, 2, _T("red"));
286 grid
->SetCellTextColour(2, 2, *wxRED
);
287 grid
->SetCellValue(3, 3, _T("green on grey"));
288 grid
->SetCellTextColour(3, 3, *wxGREEN
);
289 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
291 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
292 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
293 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
295 grid
->SetCellValue(3, 0, _T("0"));
296 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
297 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
299 wxGridCellAttr
*attr
;
300 attr
= new wxGridCellAttr
;
301 attr
->SetTextColour(*wxBLUE
);
302 grid
->SetColAttr(5, attr
);
303 attr
= new wxGridCellAttr
;
304 attr
->SetBackgroundColour(*wxRED
);
305 grid
->SetRowAttr(5, attr
);
307 grid
->SetCellValue(2, 4, _T("a wider column"));
308 grid
->SetColSize(4, 120);
309 grid
->SetColMinimalWidth(4, 120);
311 grid
->SetCellTextColour(5, 8, *wxGREEN
);
312 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
313 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"));
315 grid
->SetColFormatFloat(6);
316 grid
->SetCellValue(0, 6, _T("3.1415"));
317 grid
->SetCellValue(1, 6, _T("1415"));
318 grid
->SetCellValue(2, 6, _T("12345.67890"));
320 grid
->SetColFormatFloat(7, 6, 2);
321 grid
->SetCellValue(0, 7, _T("3.1415"));
322 grid
->SetCellValue(1, 7, _T("1415"));
323 grid
->SetCellValue(2, 7, _T("12345.67890"));
325 const wxString choices
[] =
327 _T("Please select a choice"),
328 _T("This takes two cells"),
329 _T("Another choice"),
331 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
332 grid
->SetCellSize(4, 0, 1, 2);
333 grid
->SetCellValue(4, 0, choices
[0]);
334 grid
->SetCellOverflow(4, 0, false);
336 grid
->SetCellSize(7, 1, 3, 4);
337 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
338 grid
->SetCellValue(7, 1, _T("Big box!"));
340 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
346 topSizer
->Add( logWin
,
351 SetSizer( topSizer
);
353 topSizer
->Fit( this );
360 GridFrame::~GridFrame()
363 delete wxLog::SetActiveTarget(m_logOld
);
368 void GridFrame::SetDefaults()
370 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
371 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
372 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
373 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
374 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
375 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
376 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
377 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
381 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
383 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
385 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
389 grid
->SetRowLabelSize( 0 );
394 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
396 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
398 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
402 grid
->SetColLabelSize( 0 );
407 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
410 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
414 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
416 grid
->EnableDragRowSize(
417 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
421 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
423 grid
->EnableDragColSize(
424 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
427 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
429 grid
->EnableDragGridSize(
430 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
434 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
436 grid
->EnableGridLines(
437 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
440 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
442 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
444 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
445 _T("Pen Width"), 11, choices
);
447 int current
= grid
->GetCellHighlightPenWidth();
448 dlg
.SetSelection(current
);
449 if (dlg
.ShowModal() == wxID_OK
) {
450 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
454 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
456 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
458 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
459 _T("Pen Width"), 11, choices
);
461 int current
= grid
->GetCellHighlightROPenWidth();
462 dlg
.SetSelection(current
);
463 if (dlg
.ShowModal() == wxID_OK
) {
464 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
470 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
472 grid
->AutoSizeColumns();
476 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
478 grid
->SetDefaultCellOverflow(ev
.IsChecked());
482 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
485 grid
->SetCellSize( 7, 1, 5, 5 );
487 grid
->SetCellSize( 7, 1, 1, 5 );
491 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
493 wxColourDialog
dlg( NULL
);
494 if ( dlg
.ShowModal() == wxID_OK
)
496 wxColourData retData
;
497 retData
= dlg
.GetColourData();
498 wxColour colour
= retData
.GetColour();
500 grid
->SetLabelBackgroundColour( colour
);
505 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
507 wxColourDialog
dlg( NULL
);
508 if ( dlg
.ShowModal() == wxID_OK
)
510 wxColourData retData
;
511 retData
= dlg
.GetColourData();
512 wxColour colour
= retData
.GetColour();
514 grid
->SetLabelTextColour( colour
);
518 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
520 wxFont font
= wxGetFontFromUser(this);
523 grid
->SetLabelFont(font
);
527 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
530 grid
->GetRowLabelAlignment( &horiz
, &vert
);
535 horiz
= wxALIGN_CENTRE
;
539 horiz
= wxALIGN_RIGHT
;
543 horiz
= wxALIGN_LEFT
;
547 grid
->SetRowLabelAlignment( horiz
, vert
);
550 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
553 grid
->GetRowLabelAlignment( &horiz
, &vert
);
558 vert
= wxALIGN_CENTRE
;
562 vert
= wxALIGN_BOTTOM
;
570 grid
->SetRowLabelAlignment( horiz
, vert
);
574 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
577 grid
->GetColLabelAlignment( &horiz
, &vert
);
582 horiz
= wxALIGN_CENTRE
;
586 horiz
= wxALIGN_RIGHT
;
590 horiz
= wxALIGN_LEFT
;
594 grid
->SetColLabelAlignment( horiz
, vert
);
598 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
601 grid
->GetColLabelAlignment( &horiz
, &vert
);
606 vert
= wxALIGN_CENTRE
;
610 vert
= wxALIGN_BOTTOM
;
618 grid
->SetColLabelAlignment( horiz
, vert
);
622 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
624 wxColourDialog
dlg( NULL
);
625 if ( dlg
.ShowModal() == wxID_OK
)
627 wxColourData retData
;
628 retData
= dlg
.GetColourData();
629 wxColour colour
= retData
.GetColour();
631 grid
->SetGridLineColour( colour
);
636 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
638 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
642 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
644 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
648 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
650 if ( grid
->IsSelection() )
653 for ( int n
= 0; n
< grid
->GetNumberRows(); )
654 if ( grid
->IsInSelection( n
, 0 ) )
655 grid
->DeleteRows( n
, 1 );
663 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
665 if ( grid
->IsSelection() )
668 for ( int n
= 0; n
< grid
->GetNumberCols(); )
669 if ( grid
->IsInSelection( 0 , n
) )
670 grid
->DeleteCols( n
, 1 );
678 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
683 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
685 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
688 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
690 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
693 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
695 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
698 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
700 wxColour col
= wxGetColourFromUser(this);
703 grid
->SetDefaultCellTextColour(col
);
708 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
710 wxColour col
= wxGetColourFromUser(this);
713 // Check the new Refresh function by passing it a rectangle
714 // which exactly fits the grid.
715 wxRect
r(wxPoint(0, 0), grid
->GetSize());
716 grid
->SetDefaultCellBackgroundColour(col
);
717 grid
->Refresh(true, &r
);
721 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
723 grid
->DeselectCell(3, 1);
726 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
728 grid
->DeselectCol(2);
731 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
733 grid
->DeselectRow(2);
736 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
738 grid
->ClearSelection();
741 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
743 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
746 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
748 grid
->SelectCol(2, m_addToSel
);
751 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
753 grid
->SelectRow(2, m_addToSel
);
756 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
761 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
763 m_addToSel
= event
.IsChecked();
766 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
768 logBuf
= wxEmptyString
;
769 if ( ev
.GetRow() != -1 )
771 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
773 else if ( ev
.GetCol() != -1 )
775 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
779 logBuf
<< _T("Left click on corner label");
782 if ( ev
.ShiftDown() ) logBuf
<< _T(" (shift down)");
783 if ( ev
.ControlDown() ) logBuf
<< _T(" (control down)");
784 wxLogMessage( wxT("%s"), logBuf
.c_str() );
786 // you must call event skip if you want default grid processing
792 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
794 logBuf
= wxEmptyString
;
795 logBuf
<< _T("Left click at row ") << ev
.GetRow()
796 << _T(" col ") << ev
.GetCol();
797 wxLogMessage( wxT("%s"), logBuf
.c_str() );
799 // you must call event skip if you want default grid processing
800 // (cell highlighting etc.)
806 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
808 logBuf
= wxEmptyString
;
809 logBuf
<< _T("Resized row ") << ev
.GetRowOrCol();
810 wxLogMessage( wxT("%s"), logBuf
.c_str() );
816 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
818 logBuf
= wxEmptyString
;
819 logBuf
<< _T("Resized col ") << ev
.GetRowOrCol();
820 wxLogMessage( wxT("%s"), logBuf
.c_str() );
826 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
828 logBuf
= wxEmptyString
;
829 if ( ev
.Selecting() )
830 logBuf
<< _T("Selected ");
832 logBuf
<< _T("Deselected ");
833 logBuf
<< _T("cell at row ") << ev
.GetRow()
834 << _T(" col ") << ev
.GetCol()
835 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
836 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
837 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
838 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
839 wxLogMessage( wxT("%s"), logBuf
.c_str() );
841 // you must call Skip() if you want the default processing
842 // to occur in wxGrid
846 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
848 logBuf
= wxEmptyString
;
849 if ( ev
.Selecting() )
850 logBuf
<< _T("Selected ");
852 logBuf
<< _T("Deselected ");
853 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
854 << _T(" col ") << ev
.GetLeftCol()
855 << _T(" to row ") << ev
.GetBottomRow()
856 << _T(" col ") << ev
.GetRightCol()
857 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
858 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
859 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
860 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
861 wxLogMessage( wxT("%s"), logBuf
.c_str() );
866 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
868 logBuf
= wxEmptyString
;
869 logBuf
<< _T("Value changed for cell at")
870 << _T(" row ") << ev
.GetRow()
871 << _T(" col ") << ev
.GetCol();
873 wxLogMessage( wxT("%s"), logBuf
.c_str() );
878 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
881 if ( (ev
.GetCol() == 4) &&
882 (ev
.GetRow() == 0) &&
883 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
884 _T("Checking"),wxYES_NO
) == wxNO
) ) {
890 wxLogMessage( wxT("Cell editor shown.") );
895 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
898 if ( (ev
.GetCol() == 4) &&
899 (ev
.GetRow() == 0) &&
900 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
901 _T("Checking"),wxYES_NO
) == wxNO
) ) {
907 wxLogMessage( wxT("Cell editor hidden.") );
912 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
914 (void)wxMessageBox( _T("\n\nwxGrid demo \n\n")
915 _T("Michael Bedward \n")
916 _T("mbedward@ozemail.com.au \n\n"),
922 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
927 void GridFrame::OnBugsTable(wxCommandEvent
& )
929 BugsGridFrame
*frame
= new BugsGridFrame
;
933 void GridFrame::OnSmallGrid(wxCommandEvent
& )
935 wxFrame
* frame
= new wxFrame(NULL
, wxID_ANY
, _T("A Small Grid"),
936 wxDefaultPosition
, wxSize(640, 480));
937 wxPanel
* panel
= new wxPanel(frame
, wxID_ANY
);
938 wxGrid
* grid
= new wxGrid(panel
, wxID_ANY
, wxPoint(10,10), wxSize(400,400),
939 wxWANTS_CHARS
| wxSIMPLE_BORDER
);
940 grid
->CreateGrid(3,3);
944 void GridFrame::OnVTable(wxCommandEvent
& )
946 static long s_sizeGrid
= 10000;
949 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
952 s
= wxGetTextFromUser( _T("Size of the table to create"),
956 s
.ToLong( &s_sizeGrid
);
959 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
961 _T("wxGridDemo question"),
966 if ( s_sizeGrid
!= -1 )
968 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
973 // ----------------------------------------------------------------------------
974 // MyGridCellRenderer
975 // ----------------------------------------------------------------------------
977 // do something that the default renderer doesn't here just to show that it is
978 // possible to alter the appearance of the cell beyond what the attributes
980 void MyGridCellRenderer::Draw(wxGrid
& grid
,
981 wxGridCellAttr
& attr
,
987 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
989 dc
.SetPen(*wxGREEN_PEN
);
990 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
991 dc
.DrawEllipse(rect
);
994 // ----------------------------------------------------------------------------
995 // MyGridCellAttrProvider
996 // ----------------------------------------------------------------------------
998 MyGridCellAttrProvider::MyGridCellAttrProvider()
1000 m_attrForOddRows
= new wxGridCellAttr
;
1001 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1004 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1006 m_attrForOddRows
->DecRef();
1009 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1010 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1012 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1018 attr
= m_attrForOddRows
;
1023 if ( !attr
->HasBackgroundColour() )
1025 wxGridCellAttr
*attrNew
= attr
->Clone();
1028 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1036 // ============================================================================
1037 // BigGridFrame and BigGridTable: Sample of a non-standard table
1038 // ============================================================================
1040 BigGridFrame::BigGridFrame(long sizeGrid
)
1041 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1042 wxDefaultPosition
, wxSize(500, 450))
1044 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1045 m_table
= new BigGridTable(sizeGrid
);
1047 // VZ: I don't understand why this slows down the display that much,
1048 // must profile it...
1049 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1051 m_grid
->SetTable(m_table
, true);
1053 #if defined __WXMOTIF__
1054 // MB: the grid isn't getting a sensible default size under wxMotif
1056 GetClientSize( &cw
, &ch
);
1057 m_grid
->SetSize( cw
, ch
);
1061 // ============================================================================
1062 // BugsGridFrame: a "realistic" table
1063 // ============================================================================
1065 // ----------------------------------------------------------------------------
1067 // ----------------------------------------------------------------------------
1090 static const wxString severities
[] =
1099 static struct BugsGridData
1105 wxChar platform
[12];
1107 } gs_dataBugsGrid
[] =
1109 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1110 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1111 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1112 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1115 static const wxChar
*headers
[Col_Max
] =
1125 // ----------------------------------------------------------------------------
1127 // ----------------------------------------------------------------------------
1129 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1135 return wxGRID_VALUE_NUMBER
;;
1138 // fall thorugh (TODO should be a list)
1141 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1144 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1147 return wxGRID_VALUE_BOOL
;
1150 wxFAIL_MSG(_T("unknown column"));
1152 return wxEmptyString
;
1155 int BugsGridTable::GetNumberRows()
1157 return WXSIZEOF(gs_dataBugsGrid
);
1160 int BugsGridTable::GetNumberCols()
1165 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1170 wxString
BugsGridTable::GetValue( int row
, int col
)
1172 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1179 wxFAIL_MSG(_T("unexpected column"));
1183 return severities
[gd
.severity
];
1192 return wxEmptyString
;
1195 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1197 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1204 wxFAIL_MSG(_T("unexpected column"));
1210 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1212 if ( severities
[n
] == value
)
1214 gd
.severity
= (Severity
)n
;
1219 if ( n
== WXSIZEOF(severities
) )
1221 wxLogWarning(_T("Invalid severity value '%s'."),
1223 gd
.severity
= Sev_Normal
;
1229 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1233 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1238 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1240 if ( typeName
== wxGRID_VALUE_STRING
)
1244 else if ( typeName
== wxGRID_VALUE_BOOL
)
1246 return col
== Col_Opened
;
1248 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1250 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1258 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1260 return CanGetValueAs(row
, col
, typeName
);
1263 long BugsGridTable::GetValueAsLong( int row
, int col
)
1265 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1279 wxFAIL_MSG(_T("unexpected column"));
1284 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1286 if ( col
== Col_Opened
)
1288 return gs_dataBugsGrid
[row
].opened
;
1292 wxFAIL_MSG(_T("unexpected column"));
1298 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1300 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1309 wxFAIL_MSG(_T("unexpected column"));
1313 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1315 if ( col
== Col_Opened
)
1317 gs_dataBugsGrid
[row
].opened
= value
;
1321 wxFAIL_MSG(_T("unexpected column"));
1325 wxString
BugsGridTable::GetColLabelValue( int col
)
1327 return headers
[col
];
1330 BugsGridTable::BugsGridTable()
1334 // ----------------------------------------------------------------------------
1336 // ----------------------------------------------------------------------------
1338 BugsGridFrame::BugsGridFrame()
1339 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"),
1340 wxDefaultPosition
, wxSize(500, 300))
1342 wxGrid
*grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
);
1343 wxGridTableBase
*table
= new BugsGridTable();
1344 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1345 grid
->SetTable(table
, true);
1347 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1348 *attrRangeEditor
= new wxGridCellAttr
,
1349 *attrCombo
= new wxGridCellAttr
;
1351 attrRO
->SetReadOnly();
1352 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1353 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1356 grid
->SetColAttr(Col_Id
, attrRO
);
1357 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1358 grid
->SetColAttr(Col_Severity
, attrCombo
);
1360 grid
->SetMargins(0, 0);
1363 SetClientSize(grid
->GetSize());