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 int gridW
= 600, gridH
= 300;
139 int logW
= gridW
, logH
= 100;
141 wxMenu
*fileMenu
= new wxMenu
;
142 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
143 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
144 fileMenu
->Append( ID_SMALL_GRID
, _T("&Small Grid test\tCtrl-S"));
145 fileMenu
->AppendSeparator();
146 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
148 wxMenu
*viewMenu
= new wxMenu
;
149 viewMenu
->Append( ID_TOGGLEROWLABELS
, _T("&Row labels"), wxEmptyString
, wxITEM_CHECK
);
150 viewMenu
->Append( ID_TOGGLECOLLABELS
, _T("&Col labels"), wxEmptyString
, wxITEM_CHECK
);
151 viewMenu
->Append( ID_TOGGLEEDIT
, _T("&Editable"), wxEmptyString
, wxITEM_CHECK
);
152 viewMenu
->Append( ID_TOGGLEROWSIZING
, _T("Ro&w drag-resize"), wxEmptyString
, wxITEM_CHECK
);
153 viewMenu
->Append( ID_TOGGLECOLSIZING
, _T("C&ol drag-resize"), wxEmptyString
, wxITEM_CHECK
);
154 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, _T("&Grid drag-resize"), wxEmptyString
, wxITEM_CHECK
);
155 viewMenu
->Append( ID_TOGGLEGRIDLINES
, _T("&Grid Lines"), wxEmptyString
, wxITEM_CHECK
);
156 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, _T("&Set Cell Highlight Width...") );
157 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, _T("&Set Cell RO Highlight Width...") );
158 viewMenu
->Append( ID_AUTOSIZECOLS
, _T("&Auto-size cols") );
159 viewMenu
->Append( ID_CELLOVERFLOW
, _T("&Overflow cells"), wxEmptyString
, wxITEM_CHECK
);
160 viewMenu
->Append( ID_RESIZECELL
, _T("&Resize cell (7,1)"), wxEmptyString
, wxITEM_CHECK
);
162 wxMenu
*rowLabelMenu
= new wxMenu
;
164 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
166 _T("Change alignment of row labels") );
168 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
169 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
171 wxMenu
*colLabelMenu
= new wxMenu
;
173 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
175 _T("Change alignment of col labels") );
177 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
178 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
180 wxMenu
*colMenu
= new wxMenu
;
181 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
182 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
183 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
184 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
185 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
186 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
188 wxMenu
*editMenu
= new wxMenu
;
189 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
190 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
191 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
192 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
193 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
195 wxMenu
*selectMenu
= new wxMenu
;
196 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
197 _T("When off, old selection is deselected before ")
198 _T("selecting the new cells"), wxITEM_CHECK
);
199 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
200 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
201 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
202 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
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( ID_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 ) );
237 logWin
= new wxTextCtrl( this,
240 wxPoint( 0, gridH
+ 20 ),
241 wxSize( logW
, logH
),
244 logger
= new wxLogTextCtrl( logWin
);
245 m_logOld
= logger
->SetActiveTarget( logger
);
246 logger
->SetTimestamp( NULL
);
248 // this will create a grid and, by default, an associated grid
250 grid
->CreateGrid( 0, 0 );
251 grid
->AppendRows(100);
252 grid
->AppendCols(100);
254 int ir
= grid
->GetNumberRows();
255 grid
->DeleteRows(0, ir
);
256 grid
->AppendRows(ir
);
258 grid
->SetRowSize( 0, 60 );
259 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
261 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
262 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
263 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
265 grid
->SetCellValue( 0, 2, _T("Blah") );
266 grid
->SetCellValue( 0, 3, _T("Read only") );
267 grid
->SetReadOnly( 0, 3 );
269 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
271 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
273 grid
->SetRowSize( 99, 60 );
274 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
275 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
277 grid
->SetCellTextColour(1, 2, *wxRED
);
278 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
280 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
282 grid
->SetCellValue(2, 2, _T("red"));
284 grid
->SetCellTextColour(2, 2, *wxRED
);
285 grid
->SetCellValue(3, 3, _T("green on grey"));
286 grid
->SetCellTextColour(3, 3, *wxGREEN
);
287 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
289 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
290 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
291 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
293 grid
->SetCellValue(3, 0, _T("0"));
294 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
295 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
297 wxGridCellAttr
*attr
;
298 attr
= new wxGridCellAttr
;
299 attr
->SetTextColour(*wxBLUE
);
300 grid
->SetColAttr(5, attr
);
301 attr
= new wxGridCellAttr
;
302 attr
->SetBackgroundColour(*wxRED
);
303 grid
->SetRowAttr(5, attr
);
305 grid
->SetCellValue(2, 4, _T("a wider column"));
306 grid
->SetColSize(4, 120);
307 grid
->SetColMinimalWidth(4, 120);
309 grid
->SetCellTextColour(5, 8, *wxGREEN
);
310 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
311 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"));
313 grid
->SetColFormatFloat(6);
314 grid
->SetCellValue(0, 6, _T("3.1415"));
315 grid
->SetCellValue(1, 6, _T("1415"));
316 grid
->SetCellValue(2, 6, _T("12345.67890"));
318 grid
->SetColFormatFloat(7, 6, 2);
319 grid
->SetCellValue(0, 7, _T("3.1415"));
320 grid
->SetCellValue(1, 7, _T("1415"));
321 grid
->SetCellValue(2, 7, _T("12345.67890"));
323 const wxString choices
[] =
325 _T("Please select a choice"),
326 _T("This takes two cells"),
327 _T("Another choice"),
329 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
330 grid
->SetCellSize(4, 0, 1, 2);
331 grid
->SetCellValue(4, 0, choices
[0]);
332 grid
->SetCellOverflow(4, 0, false);
334 grid
->SetCellSize(7, 1, 3, 4);
335 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
336 grid
->SetCellValue(7, 1, _T("Big box!"));
338 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
343 topSizer
->Add( logWin
,
347 SetSizer( topSizer
);
349 topSizer
->Fit( this );
350 topSizer
->SetSizeHints( this );
357 GridFrame::~GridFrame()
359 delete wxLog::SetActiveTarget(m_logOld
);
363 void GridFrame::SetDefaults()
365 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
366 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
367 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
368 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
369 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
370 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
371 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
372 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
376 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
378 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
380 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
384 grid
->SetRowLabelSize( 0 );
389 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
391 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
393 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
397 grid
->SetColLabelSize( 0 );
402 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
405 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
409 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
411 grid
->EnableDragRowSize(
412 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
416 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
418 grid
->EnableDragColSize(
419 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
422 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
424 grid
->EnableDragGridSize(
425 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
429 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
431 grid
->EnableGridLines(
432 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
435 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
437 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
439 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
440 _T("Pen Width"), 11, choices
);
442 int current
= grid
->GetCellHighlightPenWidth();
443 dlg
.SetSelection(current
);
444 if (dlg
.ShowModal() == wxID_OK
) {
445 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
449 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
451 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
453 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
454 _T("Pen Width"), 11, choices
);
456 int current
= grid
->GetCellHighlightROPenWidth();
457 dlg
.SetSelection(current
);
458 if (dlg
.ShowModal() == wxID_OK
) {
459 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
465 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
467 grid
->AutoSizeColumns();
471 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
473 grid
->SetDefaultCellOverflow(ev
.IsChecked());
477 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
480 grid
->SetCellSize( 7, 1, 5, 5 );
482 grid
->SetCellSize( 7, 1, 1, 5 );
486 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
488 wxColourDialog
dlg( NULL
);
489 if ( dlg
.ShowModal() == wxID_OK
)
491 wxColourData retData
;
492 retData
= dlg
.GetColourData();
493 wxColour colour
= retData
.GetColour();
495 grid
->SetLabelBackgroundColour( colour
);
500 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
502 wxColourDialog
dlg( NULL
);
503 if ( dlg
.ShowModal() == wxID_OK
)
505 wxColourData retData
;
506 retData
= dlg
.GetColourData();
507 wxColour colour
= retData
.GetColour();
509 grid
->SetLabelTextColour( colour
);
513 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
515 wxFont font
= wxGetFontFromUser(this);
518 grid
->SetLabelFont(font
);
522 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
525 grid
->GetRowLabelAlignment( &horiz
, &vert
);
530 horiz
= wxALIGN_CENTRE
;
534 horiz
= wxALIGN_RIGHT
;
538 horiz
= wxALIGN_LEFT
;
542 grid
->SetRowLabelAlignment( horiz
, vert
);
545 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
548 grid
->GetRowLabelAlignment( &horiz
, &vert
);
553 vert
= wxALIGN_CENTRE
;
557 vert
= wxALIGN_BOTTOM
;
565 grid
->SetRowLabelAlignment( horiz
, vert
);
569 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
572 grid
->GetColLabelAlignment( &horiz
, &vert
);
577 horiz
= wxALIGN_CENTRE
;
581 horiz
= wxALIGN_RIGHT
;
585 horiz
= wxALIGN_LEFT
;
589 grid
->SetColLabelAlignment( horiz
, vert
);
593 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
596 grid
->GetColLabelAlignment( &horiz
, &vert
);
601 vert
= wxALIGN_CENTRE
;
605 vert
= wxALIGN_BOTTOM
;
613 grid
->SetColLabelAlignment( horiz
, vert
);
617 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
619 wxColourDialog
dlg( NULL
);
620 if ( dlg
.ShowModal() == wxID_OK
)
622 wxColourData retData
;
623 retData
= dlg
.GetColourData();
624 wxColour colour
= retData
.GetColour();
626 grid
->SetGridLineColour( colour
);
631 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
633 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
637 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
639 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
643 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
645 if ( grid
->IsSelection() )
648 for ( int n
= 0; n
< grid
->GetNumberRows(); )
649 if ( grid
->IsInSelection( n
, 0 ) )
650 grid
->DeleteRows( n
, 1 );
658 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
660 if ( grid
->IsSelection() )
663 for ( int n
= 0; n
< grid
->GetNumberCols(); )
664 if ( grid
->IsInSelection( 0 , n
) )
665 grid
->DeleteCols( n
, 1 );
673 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
678 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
680 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
683 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
685 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
688 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
690 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
693 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
695 wxColour col
= wxGetColourFromUser(this);
698 grid
->SetDefaultCellTextColour(col
);
703 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
705 wxColour col
= wxGetColourFromUser(this);
708 // Check the new Refresh function by passing it a rectangle
709 // which exactly fits the grid.
710 wxRect
r(wxPoint(0, 0), grid
->GetSize());
711 grid
->SetDefaultCellBackgroundColour(col
);
712 grid
->Refresh(true, &r
);
716 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
718 grid
->DeselectCell(3, 1);
721 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
723 grid
->DeselectCol(2);
726 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
728 grid
->DeselectRow(2);
731 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
733 grid
->ClearSelection();
736 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
738 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
741 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
743 grid
->SelectCol(2, m_addToSel
);
746 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
748 grid
->SelectRow(2, m_addToSel
);
751 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
756 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
758 m_addToSel
= event
.IsChecked();
761 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
763 logBuf
= wxEmptyString
;
764 if ( ev
.GetRow() != -1 )
766 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
768 else if ( ev
.GetCol() != -1 )
770 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
774 logBuf
<< _T("Left click on corner label");
777 if ( ev
.ShiftDown() ) logBuf
<< _T(" (shift down)");
778 if ( ev
.ControlDown() ) logBuf
<< _T(" (control down)");
779 wxLogMessage( wxT("%s"), logBuf
.c_str() );
781 // you must call event skip if you want default grid processing
787 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
789 logBuf
= wxEmptyString
;
790 logBuf
<< _T("Left click at row ") << ev
.GetRow()
791 << _T(" col ") << ev
.GetCol();
792 wxLogMessage( wxT("%s"), logBuf
.c_str() );
794 // you must call event skip if you want default grid processing
795 // (cell highlighting etc.)
801 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
803 logBuf
= wxEmptyString
;
804 logBuf
<< _T("Resized row ") << ev
.GetRowOrCol();
805 wxLogMessage( wxT("%s"), logBuf
.c_str() );
811 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
813 logBuf
= wxEmptyString
;
814 logBuf
<< _T("Resized col ") << ev
.GetRowOrCol();
815 wxLogMessage( wxT("%s"), logBuf
.c_str() );
821 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
823 logBuf
= wxEmptyString
;
824 if ( ev
.Selecting() )
825 logBuf
<< _T("Selected ");
827 logBuf
<< _T("Deselected ");
828 logBuf
<< _T("cell at row ") << ev
.GetRow()
829 << _T(" col ") << ev
.GetCol()
830 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
831 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
832 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
833 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
834 wxLogMessage( wxT("%s"), logBuf
.c_str() );
836 // you must call Skip() if you want the default processing
837 // to occur in wxGrid
841 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
843 logBuf
= wxEmptyString
;
844 if ( ev
.Selecting() )
845 logBuf
<< _T("Selected ");
847 logBuf
<< _T("Deselected ");
848 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
849 << _T(" col ") << ev
.GetLeftCol()
850 << _T(" to row ") << ev
.GetBottomRow()
851 << _T(" col ") << ev
.GetRightCol()
852 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
853 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
854 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
855 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
856 wxLogMessage( wxT("%s"), logBuf
.c_str() );
861 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
863 logBuf
= wxEmptyString
;
864 logBuf
<< _T("Value changed for cell at")
865 << _T(" row ") << ev
.GetRow()
866 << _T(" col ") << ev
.GetCol();
868 wxLogMessage( wxT("%s"), logBuf
.c_str() );
873 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
876 if ( (ev
.GetCol() == 4) &&
877 (ev
.GetRow() == 0) &&
878 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
879 _T("Checking"),wxYES_NO
) == wxNO
) ) {
885 wxLogMessage( wxT("Cell editor shown.") );
890 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
893 if ( (ev
.GetCol() == 4) &&
894 (ev
.GetRow() == 0) &&
895 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
896 _T("Checking"),wxYES_NO
) == wxNO
) ) {
902 wxLogMessage( wxT("Cell editor hidden.") );
907 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
909 (void)wxMessageBox( _T("\n\nwxGrid demo \n\n")
910 _T("Michael Bedward \n")
911 _T("mbedward@ozemail.com.au \n\n"),
917 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
922 void GridFrame::OnBugsTable(wxCommandEvent
& )
924 BugsGridFrame
*frame
= new BugsGridFrame
;
928 void GridFrame::OnSmallGrid(wxCommandEvent
& )
930 wxFrame
* frame
= new wxFrame(NULL
, wxID_ANY
, _T("A Small Grid"),
931 wxDefaultPosition
, wxSize(640, 480));
932 wxPanel
* panel
= new wxPanel(frame
, wxID_ANY
);
933 wxGrid
* grid
= new wxGrid(panel
, wxID_ANY
, wxPoint(10,10), wxSize(400,400),
934 wxWANTS_CHARS
| wxSIMPLE_BORDER
);
935 grid
->CreateGrid(3,3);
939 void GridFrame::OnVTable(wxCommandEvent
& )
941 static long s_sizeGrid
= 10000;
944 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
947 s
= wxGetTextFromUser( _T("Size of the table to create"),
951 s
.ToLong( &s_sizeGrid
);
954 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
956 _T("wxGridDemo question"),
961 if ( s_sizeGrid
!= -1 )
963 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
968 // ----------------------------------------------------------------------------
969 // MyGridCellRenderer
970 // ----------------------------------------------------------------------------
972 // do something that the default renderer doesn't here just to show that it is
973 // possible to alter the appearance of the cell beyond what the attributes
975 void MyGridCellRenderer::Draw(wxGrid
& grid
,
976 wxGridCellAttr
& attr
,
982 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
984 dc
.SetPen(*wxGREEN_PEN
);
985 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
986 dc
.DrawEllipse(rect
);
989 // ----------------------------------------------------------------------------
990 // MyGridCellAttrProvider
991 // ----------------------------------------------------------------------------
993 MyGridCellAttrProvider::MyGridCellAttrProvider()
995 m_attrForOddRows
= new wxGridCellAttr
;
996 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
999 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1001 m_attrForOddRows
->DecRef();
1004 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1005 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1007 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1013 attr
= m_attrForOddRows
;
1018 if ( !attr
->HasBackgroundColour() )
1020 wxGridCellAttr
*attrNew
= attr
->Clone();
1023 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1031 // ============================================================================
1032 // BigGridFrame and BigGridTable: Sample of a non-standard table
1033 // ============================================================================
1035 BigGridFrame::BigGridFrame(long sizeGrid
)
1036 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1037 wxDefaultPosition
, wxSize(500, 450))
1039 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1040 m_table
= new BigGridTable(sizeGrid
);
1042 // VZ: I don't understand why this slows down the display that much,
1043 // must profile it...
1044 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1046 m_grid
->SetTable(m_table
, true);
1048 #if defined __WXMOTIF__
1049 // MB: the grid isn't getting a sensible default size under wxMotif
1051 GetClientSize( &cw
, &ch
);
1052 m_grid
->SetSize( cw
, ch
);
1056 // ============================================================================
1057 // BugsGridFrame: a "realistic" table
1058 // ============================================================================
1060 // ----------------------------------------------------------------------------
1062 // ----------------------------------------------------------------------------
1085 static const wxString severities
[] =
1094 static struct BugsGridData
1100 wxChar platform
[12];
1102 } gs_dataBugsGrid
[] =
1104 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1105 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1106 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1107 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1110 static const wxChar
*headers
[Col_Max
] =
1120 // ----------------------------------------------------------------------------
1122 // ----------------------------------------------------------------------------
1124 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1130 return wxGRID_VALUE_NUMBER
;;
1133 // fall thorugh (TODO should be a list)
1136 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1139 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1142 return wxGRID_VALUE_BOOL
;
1145 wxFAIL_MSG(_T("unknown column"));
1147 return wxEmptyString
;
1150 int BugsGridTable::GetNumberRows()
1152 return WXSIZEOF(gs_dataBugsGrid
);
1155 int BugsGridTable::GetNumberCols()
1160 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1165 wxString
BugsGridTable::GetValue( int row
, int col
)
1167 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1174 wxFAIL_MSG(_T("unexpected column"));
1178 return severities
[gd
.severity
];
1187 return wxEmptyString
;
1190 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1192 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1199 wxFAIL_MSG(_T("unexpected column"));
1205 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1207 if ( severities
[n
] == value
)
1209 gd
.severity
= (Severity
)n
;
1214 if ( n
== WXSIZEOF(severities
) )
1216 wxLogWarning(_T("Invalid severity value '%s'."),
1218 gd
.severity
= Sev_Normal
;
1224 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1228 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1233 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1235 if ( typeName
== wxGRID_VALUE_STRING
)
1239 else if ( typeName
== wxGRID_VALUE_BOOL
)
1241 return col
== Col_Opened
;
1243 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1245 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1253 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1255 return CanGetValueAs(row
, col
, typeName
);
1258 long BugsGridTable::GetValueAsLong( int row
, int col
)
1260 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1274 wxFAIL_MSG(_T("unexpected column"));
1279 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1281 if ( col
== Col_Opened
)
1283 return gs_dataBugsGrid
[row
].opened
;
1287 wxFAIL_MSG(_T("unexpected column"));
1293 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1295 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1304 wxFAIL_MSG(_T("unexpected column"));
1308 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1310 if ( col
== Col_Opened
)
1312 gs_dataBugsGrid
[row
].opened
= value
;
1316 wxFAIL_MSG(_T("unexpected column"));
1320 wxString
BugsGridTable::GetColLabelValue( int col
)
1322 return headers
[col
];
1325 BugsGridTable::BugsGridTable()
1329 // ----------------------------------------------------------------------------
1331 // ----------------------------------------------------------------------------
1333 BugsGridFrame::BugsGridFrame()
1334 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"),
1335 wxDefaultPosition
, wxSize(500, 300))
1337 wxGrid
*grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
);
1338 wxGridTableBase
*table
= new BugsGridTable();
1339 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1340 grid
->SetTable(table
, true);
1342 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1343 *attrRangeEditor
= new wxGridCellAttr
,
1344 *attrCombo
= new wxGridCellAttr
;
1346 attrRO
->SetReadOnly();
1347 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1348 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1351 grid
->SetColAttr(Col_Id
, attrRO
);
1352 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1353 grid
->SetColAttr(Col_Severity
, attrCombo
);
1355 grid
->SetMargins(0, 0);
1358 SetClientSize(grid
->GetSize());