1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWindows sample
4 // Author: Michael Bedward
7 // Copyright: (c) Michael Bedward, Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
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"
39 #include "wx/generic/gridctrl.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 IMPLEMENT_APP( GridApp
)
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 bool GridApp::OnInit()
59 GridFrame
*frame
= new GridFrame
;
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
70 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
71 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
72 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
73 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
74 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
75 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
76 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
77 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
78 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
79 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
80 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
81 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
82 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
83 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
84 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
85 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
86 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
87 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
88 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
89 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
90 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
91 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
92 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
93 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
94 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
95 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
97 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
98 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
100 EVT_MENU( ID_ABOUT
, GridFrame::About
)
101 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
102 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
103 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
104 EVT_MENU( ID_SMALL_GRID
, GridFrame::OnSmallGrid
)
106 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
107 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
108 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
109 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
110 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
111 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
112 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
113 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
114 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
116 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
117 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
119 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
120 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
121 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
122 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
123 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
124 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
125 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
127 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
128 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
132 GridFrame::GridFrame()
133 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
137 int gridW
= 600, gridH
= 300;
138 int logW
= gridW
, logH
= 100;
140 wxMenu
*fileMenu
= new wxMenu
;
141 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
142 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
143 fileMenu
->Append( ID_SMALL_GRID
, "&Small Grid test\tCtrl-S");
144 fileMenu
->AppendSeparator();
145 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
147 wxMenu
*viewMenu
= new wxMenu
;
148 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
149 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
150 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
151 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
152 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
153 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, "&Grid drag-resize", "", TRUE
);
154 viewMenu
->Append( ID_TOGGLEGRIDLINES
, "&Grid Lines", "", TRUE
);
155 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...", "" );
156 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...", "" );
157 viewMenu
->Append( ID_AUTOSIZECOLS
, "&Auto-size cols" );
158 viewMenu
->Append( ID_CELLOVERFLOW
, "&Overflow cells", "", TRUE
);
159 viewMenu
->Append( ID_RESIZECELL
, "&Resize cell (7,1)", "", TRUE
);
161 wxMenu
*rowLabelMenu
= new wxMenu
;
163 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
165 "Change alignment of row labels" );
167 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
168 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
170 wxMenu
*colLabelMenu
= new wxMenu
;
172 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
174 "Change alignment of col labels" );
176 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
177 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
179 wxMenu
*colMenu
= new wxMenu
;
180 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour..." );
181 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour..." );
182 colMenu
->Append( ID_SETLABEL_FONT
, "Set label fo&nt..." );
183 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour..." );
184 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour..." );
185 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour..." );
187 wxMenu
*editMenu
= new wxMenu
;
188 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
189 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
190 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
191 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
192 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
194 wxMenu
*selectMenu
= new wxMenu
;
195 selectMenu
->Append( ID_SELECT_UNSELECT
, "Add new cells to the selection",
196 "When off, old selection is deselected before "
197 "selecting the new cells", TRUE
);
198 selectMenu
->Append( ID_SELECT_ALL
, "Select all");
199 selectMenu
->Append( ID_SELECT_ROW
, "Select row 2");
200 selectMenu
->Append( ID_SELECT_COL
, "Select col 2");
201 selectMenu
->Append( ID_SELECT_CELL
, "Select cell (3, 1)");
202 selectMenu
->Append( ID_DESELECT_ALL
, "Deselect all");
203 selectMenu
->Append( ID_DESELECT_ROW
, "Deselect row 2");
204 selectMenu
->Append( ID_DESELECT_COL
, "Deselect col 2");
205 selectMenu
->Append( ID_DESELECT_CELL
, "Deselect cell (3, 1)");
206 wxMenu
*selectionMenu
= new wxMenu
;
207 selectMenu
->Append( ID_CHANGESEL
, "Change &selection mode",
209 "Change selection mode" );
211 selectionMenu
->Append( ID_SELCELLS
, "Select &Cells" );
212 selectionMenu
->Append( ID_SELROWS
, "Select &Rows" );
213 selectionMenu
->Append( ID_SELCOLS
, "Select C&ols" );
216 wxMenu
*helpMenu
= new wxMenu
;
217 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
219 wxMenuBar
*menuBar
= new wxMenuBar
;
220 menuBar
->Append( fileMenu
, "&File" );
221 menuBar
->Append( viewMenu
, "&View" );
222 menuBar
->Append( colMenu
, "&Colours" );
223 menuBar
->Append( editMenu
, "&Edit" );
224 menuBar
->Append( selectMenu
, "&Select" );
225 menuBar
->Append( helpMenu
, "&Help" );
227 SetMenuBar( menuBar
);
231 grid
= new wxGrid( this,
234 wxSize( 400, 300 ) );
236 logWin
= new wxTextCtrl( this,
239 wxPoint( 0, gridH
+ 20 ),
240 wxSize( logW
, logH
),
243 logger
= new wxLogTextCtrl( logWin
);
244 m_logOld
= logger
->SetActiveTarget( logger
);
245 logger
->SetTimestamp( NULL
);
247 // this will create a grid and, by default, an associated grid
249 grid
->CreateGrid( 100, 100 );
251 grid
->SetRowSize( 0, 60 );
252 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
254 grid
->SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." );
255 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
256 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
258 grid
->SetCellValue( 0, 2, "Blah" );
259 grid
->SetCellValue( 0, 3, "Read only" );
260 grid
->SetReadOnly( 0, 3 );
262 grid
->SetCellValue( 0, 4, "Can veto edit this cell" );
264 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
266 grid
->SetRowSize( 99, 60 );
267 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
268 grid
->SetCellValue( 1, 0, "This default cell will overflow into neighboring cells, but not if you turn overflow off.");
270 grid
->SetCellTextColour(1, 2, *wxRED
);
271 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
273 grid
->SetCellValue( 1, 4, "I'm in the middle");
275 grid
->SetCellValue(2, 2, "red");
277 grid
->SetCellTextColour(2, 2, *wxRED
);
278 grid
->SetCellValue(3, 3, "green on grey");
279 grid
->SetCellTextColour(3, 3, *wxGREEN
);
280 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
282 grid
->SetCellValue(4, 4, "a weird looking cell");
283 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
284 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
286 grid
->SetCellValue(3, 0, "0");
287 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
288 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
290 wxGridCellAttr
*attr
;
291 attr
= new wxGridCellAttr
;
292 attr
->SetTextColour(*wxBLUE
);
293 grid
->SetColAttr(5, attr
);
294 attr
= new wxGridCellAttr
;
295 attr
->SetBackgroundColour(*wxRED
);
296 grid
->SetRowAttr(5, attr
);
298 grid
->SetCellValue(2, 4, "a wider column");
299 grid
->SetColSize(4, 120);
300 grid
->SetColMinimalWidth(4, 120);
302 grid
->SetCellTextColour(5, 8, *wxGREEN
);
303 grid
->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
304 grid
->SetCellValue(5, 5, "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");
306 grid
->SetColFormatFloat(6);
307 grid
->SetCellValue(0, 6, "3.1415");
308 grid
->SetCellValue(1, 6, "1415");
309 grid
->SetCellValue(2, 6, "12345.67890");
311 grid
->SetColFormatFloat(7, 6, 2);
312 grid
->SetCellValue(0, 7, "3.1415");
313 grid
->SetCellValue(1, 7, "1415");
314 grid
->SetCellValue(2, 7, "12345.67890");
316 const wxString choices
[] =
318 _T("Please select a choice"),
319 _T("This takes two cells"),
320 _T("Another choice"),
322 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
323 grid
->SetCellSize(4, 0, 1, 2);
324 grid
->SetCellValue(4, 0, choices
[0]);
325 grid
->SetCellOverflow(4, 0, FALSE
);
327 grid
->SetCellSize(7, 1, 3, 4);
328 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
329 grid
->SetCellValue(7, 1, "Big box!");
331 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
336 topSizer
->Add( logWin
,
340 SetAutoLayout( TRUE
);
341 SetSizer( topSizer
);
343 topSizer
->Fit( this );
344 topSizer
->SetSizeHints( this );
351 GridFrame::~GridFrame()
353 delete wxLog::SetActiveTarget(m_logOld
);
357 void GridFrame::SetDefaults()
359 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
360 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
361 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
362 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
363 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
364 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
365 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
366 GetMenuBar()->Check( ID_CELLOVERFLOW
, TRUE
);
370 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
372 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
374 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
378 grid
->SetRowLabelSize( 0 );
383 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
385 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
387 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
391 grid
->SetColLabelSize( 0 );
396 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
399 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
403 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
405 grid
->EnableDragRowSize(
406 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
410 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
412 grid
->EnableDragColSize(
413 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
416 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
418 grid
->EnableDragGridSize(
419 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
423 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
425 grid
->EnableGridLines(
426 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
429 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
431 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
433 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
434 "Pen Width", 11, choices
);
436 int current
= grid
->GetCellHighlightPenWidth();
437 dlg
.SetSelection(current
);
438 if (dlg
.ShowModal() == wxID_OK
) {
439 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
443 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
445 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
447 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
448 "Pen Width", 11, choices
);
450 int current
= grid
->GetCellHighlightROPenWidth();
451 dlg
.SetSelection(current
);
452 if (dlg
.ShowModal() == wxID_OK
) {
453 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
459 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
461 grid
->AutoSizeColumns();
465 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
467 grid
->SetDefaultCellOverflow(ev
.IsChecked());
471 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
474 grid
->SetCellSize( 7, 1, 5, 5 );
476 grid
->SetCellSize( 7, 1, 1, 5 );
480 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
482 wxColourDialog
dlg( NULL
);
483 if ( dlg
.ShowModal() == wxID_OK
)
485 wxColourData retData
;
486 retData
= dlg
.GetColourData();
487 wxColour colour
= retData
.GetColour();
489 grid
->SetLabelBackgroundColour( colour
);
494 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
496 wxColourDialog
dlg( NULL
);
497 if ( dlg
.ShowModal() == wxID_OK
)
499 wxColourData retData
;
500 retData
= dlg
.GetColourData();
501 wxColour colour
= retData
.GetColour();
503 grid
->SetLabelTextColour( colour
);
507 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
509 wxFont font
= wxGetFontFromUser(this);
512 grid
->SetLabelFont(font
);
516 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
519 grid
->GetRowLabelAlignment( &horiz
, &vert
);
524 horiz
= wxALIGN_CENTRE
;
528 horiz
= wxALIGN_RIGHT
;
532 horiz
= wxALIGN_LEFT
;
536 grid
->SetRowLabelAlignment( horiz
, -1 );
539 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
542 grid
->GetRowLabelAlignment( &horiz
, &vert
);
547 vert
= wxALIGN_CENTRE
;
551 vert
= wxALIGN_BOTTOM
;
559 grid
->SetRowLabelAlignment( -1, vert
);
563 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
566 grid
->GetColLabelAlignment( &horiz
, &vert
);
571 horiz
= wxALIGN_CENTRE
;
575 horiz
= wxALIGN_RIGHT
;
579 horiz
= wxALIGN_LEFT
;
583 grid
->SetColLabelAlignment( horiz
, -1 );
587 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
590 grid
->GetColLabelAlignment( &horiz
, &vert
);
595 vert
= wxALIGN_CENTRE
;
599 vert
= wxALIGN_BOTTOM
;
607 grid
->SetColLabelAlignment( -1, vert
);
611 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
613 wxColourDialog
dlg( NULL
);
614 if ( dlg
.ShowModal() == wxID_OK
)
616 wxColourData retData
;
617 retData
= dlg
.GetColourData();
618 wxColour colour
= retData
.GetColour();
620 grid
->SetGridLineColour( colour
);
625 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
627 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
631 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
633 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
637 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
639 if ( grid
->IsSelection() )
642 for ( int n
= 0; n
< grid
->GetNumberRows(); )
643 if ( grid
->IsInSelection( n
, 0 ) )
644 grid
->DeleteRows( n
, 1 );
652 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
654 if ( grid
->IsSelection() )
657 for ( int n
= 0; n
< grid
->GetNumberCols(); )
658 if ( grid
->IsInSelection( 0 , n
) )
659 grid
->DeleteCols( n
, 1 );
667 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
672 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
674 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
677 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
679 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
682 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
684 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
687 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
689 wxColour col
= wxGetColourFromUser(this);
692 grid
->SetDefaultCellTextColour(col
);
697 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
699 wxColour col
= wxGetColourFromUser(this);
702 // Check the new Refresh function by passing it a rectangle
703 // which exactly fits the grid.
704 wxRect
r(wxPoint(0, 0), grid
->GetSize());
705 grid
->SetDefaultCellBackgroundColour(col
);
706 grid
->Refresh(TRUE
, &r
);
710 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
712 grid
->DeselectCell(3, 1);
715 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
717 grid
->DeselectCol(2);
720 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
722 grid
->DeselectRow(2);
725 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
727 grid
->ClearSelection();
730 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
732 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
735 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
737 grid
->SelectCol(2, m_addToSel
);
740 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
742 grid
->SelectRow(2, m_addToSel
);
745 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
750 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
752 m_addToSel
= event
.IsChecked();
755 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
758 if ( ev
.GetRow() != -1 )
760 logBuf
<< "Left click on row label " << ev
.GetRow();
762 else if ( ev
.GetCol() != -1 )
764 logBuf
<< "Left click on col label " << ev
.GetCol();
768 logBuf
<< "Left click on corner label";
771 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
772 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
773 wxLogMessage( wxT("%s"), logBuf
.c_str() );
775 // you must call event skip if you want default grid processing
781 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
784 logBuf
<< "Left click at row " << ev
.GetRow()
785 << " col " << ev
.GetCol();
786 wxLogMessage( wxT("%s"), logBuf
.c_str() );
788 // you must call event skip if you want default grid processing
789 // (cell highlighting etc.)
795 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
798 logBuf
<< "Resized row " << ev
.GetRowOrCol();
799 wxLogMessage( wxT("%s"), logBuf
.c_str() );
805 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
808 logBuf
<< "Resized col " << ev
.GetRowOrCol();
809 wxLogMessage( wxT("%s"), logBuf
.c_str() );
815 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
818 if ( ev
.Selecting() )
819 logBuf
<< "Selected ";
821 logBuf
<< "Deselected ";
822 logBuf
<< "cell at row " << ev
.GetRow()
823 << " col " << ev
.GetCol()
824 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
825 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
826 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
827 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
828 wxLogMessage( wxT("%s"), logBuf
.c_str() );
830 // you must call Skip() if you want the default processing
831 // to occur in wxGrid
835 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
838 if ( ev
.Selecting() )
839 logBuf
<< "Selected ";
841 logBuf
<< "Deselected ";
842 logBuf
<< "cells from row " << ev
.GetTopRow()
843 << " col " << ev
.GetLeftCol()
844 << " to row " << ev
.GetBottomRow()
845 << " col " << ev
.GetRightCol()
846 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
847 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
848 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
849 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
850 wxLogMessage( wxT("%s"), logBuf
.c_str() );
855 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
858 logBuf
<< "Value changed for cell at"
859 << " row " << ev
.GetRow()
860 << " col " << ev
.GetCol();
862 wxLogMessage( wxT("%s"), logBuf
.c_str() );
867 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
870 if ( (ev
.GetCol() == 4) &&
871 (ev
.GetRow() == 0) &&
872 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
873 _T("Checking"),wxYES_NO
) == wxNO
) ) {
879 wxLogMessage( wxT("Cell editor shown.") );
884 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
887 if ( (ev
.GetCol() == 4) &&
888 (ev
.GetRow() == 0) &&
889 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
890 _T("Checking"),wxYES_NO
) == wxNO
) ) {
896 wxLogMessage( wxT("Cell editor hidden.") );
901 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
903 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
905 "mbedward@ozemail.com.au \n\n",
911 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
916 void GridFrame::OnBugsTable(wxCommandEvent
& )
918 BugsGridFrame
*frame
= new BugsGridFrame
;
922 void GridFrame::OnSmallGrid(wxCommandEvent
& )
924 wxFrame
* frame
= new wxFrame(NULL
, -1, "A Small Grid",
925 wxDefaultPosition
, wxSize(640, 480));
926 wxPanel
* panel
= new wxPanel(frame
, -1);
927 wxGrid
* grid
= new wxGrid(panel
, -1, wxPoint(10,10), wxSize(400,400),
928 wxWANTS_CHARS
| wxSIMPLE_BORDER
);
929 grid
->CreateGrid(3,3);
933 void GridFrame::OnVTable(wxCommandEvent
& )
935 static long s_sizeGrid
= 10000;
938 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
941 s
= wxGetTextFromUser( "Size of the table to create",
945 s
.ToLong( &s_sizeGrid
);
948 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
950 "wxGridDemo question",
955 if ( s_sizeGrid
!= -1 )
957 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
962 // ----------------------------------------------------------------------------
963 // MyGridCellRenderer
964 // ----------------------------------------------------------------------------
966 // do something that the default renderer doesn't here just to show that it is
967 // possible to alter the appearance of the cell beyond what the attributes
969 void MyGridCellRenderer::Draw(wxGrid
& grid
,
970 wxGridCellAttr
& attr
,
976 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
978 dc
.SetPen(*wxGREEN_PEN
);
979 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
980 dc
.DrawEllipse(rect
);
983 // ----------------------------------------------------------------------------
984 // MyGridCellAttrProvider
985 // ----------------------------------------------------------------------------
987 MyGridCellAttrProvider::MyGridCellAttrProvider()
989 m_attrForOddRows
= new wxGridCellAttr
;
990 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
993 MyGridCellAttrProvider::~MyGridCellAttrProvider()
995 m_attrForOddRows
->DecRef();
998 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
999 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1001 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1007 attr
= m_attrForOddRows
;
1012 if ( !attr
->HasBackgroundColour() )
1014 wxGridCellAttr
*attrNew
= attr
->Clone();
1017 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1025 // ============================================================================
1026 // BigGridFrame and BigGridTable: Sample of a non-standard table
1027 // ============================================================================
1029 BigGridFrame::BigGridFrame(long sizeGrid
)
1030 : wxFrame(NULL
, -1, "Plugin Virtual Table",
1031 wxDefaultPosition
, wxSize(500, 450))
1033 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
1034 m_table
= new BigGridTable(sizeGrid
);
1036 // VZ: I don't understand why this slows down the display that much,
1037 // must profile it...
1038 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1040 m_grid
->SetTable(m_table
, TRUE
);
1042 #if defined __WXMOTIF__
1043 // MB: the grid isn't getting a sensible default size under wxMotif
1045 GetClientSize( &cw
, &ch
);
1046 m_grid
->SetSize( cw
, ch
);
1050 // ============================================================================
1051 // BugsGridFrame: a "realistic" table
1052 // ============================================================================
1054 // ----------------------------------------------------------------------------
1056 // ----------------------------------------------------------------------------
1079 static const wxString severities
[] =
1088 static struct BugsGridData
1094 wxChar platform
[12];
1096 } gs_dataBugsGrid
[] =
1098 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
1099 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
1100 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
1101 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
1104 static const wxChar
*headers
[Col_Max
] =
1114 // ----------------------------------------------------------------------------
1116 // ----------------------------------------------------------------------------
1118 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1124 return wxGRID_VALUE_NUMBER
;;
1127 // fall thorugh (TODO should be a list)
1130 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1133 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1136 return wxGRID_VALUE_BOOL
;
1139 wxFAIL_MSG(_T("unknown column"));
1141 return wxEmptyString
;
1144 int BugsGridTable::GetNumberRows()
1146 return WXSIZEOF(gs_dataBugsGrid
);
1149 int BugsGridTable::GetNumberCols()
1154 bool BugsGridTable::IsEmptyCell( int row
, int col
)
1159 wxString
BugsGridTable::GetValue( int row
, int col
)
1161 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1168 wxFAIL_MSG(_T("unexpected column"));
1172 return severities
[gd
.severity
];
1181 return wxEmptyString
;
1184 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1186 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1193 wxFAIL_MSG(_T("unexpected column"));
1199 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1201 if ( severities
[n
] == value
)
1203 gd
.severity
= (Severity
)n
;
1208 if ( n
== WXSIZEOF(severities
) )
1210 wxLogWarning(_T("Invalid severity value '%s'."),
1212 gd
.severity
= Sev_Normal
;
1218 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1222 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1227 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1229 if ( typeName
== wxGRID_VALUE_STRING
)
1233 else if ( typeName
== wxGRID_VALUE_BOOL
)
1235 return col
== Col_Opened
;
1237 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1239 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1247 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1249 return CanGetValueAs(row
, col
, typeName
);
1252 long BugsGridTable::GetValueAsLong( int row
, int col
)
1254 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1268 wxFAIL_MSG(_T("unexpected column"));
1273 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1275 if ( col
== Col_Opened
)
1277 return gs_dataBugsGrid
[row
].opened
;
1281 wxFAIL_MSG(_T("unexpected column"));
1287 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1289 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1298 wxFAIL_MSG(_T("unexpected column"));
1302 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1304 if ( col
== Col_Opened
)
1306 gs_dataBugsGrid
[row
].opened
= value
;
1310 wxFAIL_MSG(_T("unexpected column"));
1314 wxString
BugsGridTable::GetColLabelValue( int col
)
1316 return headers
[col
];
1319 BugsGridTable::BugsGridTable()
1323 // ----------------------------------------------------------------------------
1325 // ----------------------------------------------------------------------------
1327 BugsGridFrame::BugsGridFrame()
1328 : wxFrame(NULL
, -1, "Bugs table",
1329 wxDefaultPosition
, wxSize(500, 300))
1331 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1332 wxGridTableBase
*table
= new BugsGridTable();
1333 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1334 grid
->SetTable(table
, TRUE
);
1336 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1337 *attrRangeEditor
= new wxGridCellAttr
,
1338 *attrCombo
= new wxGridCellAttr
;
1340 attrRO
->SetReadOnly();
1341 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1342 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1345 grid
->SetColAttr(Col_Id
, attrRO
);
1346 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1347 grid
->SetColAttr(Col_Severity
, attrCombo
);
1349 grid
->SetMargins(0, 0);
1352 SetClientSize(grid
->GetSize());