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_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
79 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
80 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
81 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
82 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
83 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
84 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
85 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
86 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
87 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
88 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
89 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
90 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
91 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
92 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
93 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
95 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
96 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
98 EVT_MENU( ID_ABOUT
, GridFrame::About
)
99 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
100 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
101 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
102 EVT_MENU( ID_SMALL_GRID
, GridFrame::OnSmallGrid
)
104 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
105 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
106 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
107 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
108 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
109 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
110 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
111 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
112 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
114 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
115 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
117 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
118 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
119 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
120 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
121 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
122 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
123 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
125 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
126 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
130 GridFrame::GridFrame()
131 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
135 int gridW
= 600, gridH
= 300;
136 int logW
= gridW
, logH
= 100;
138 wxMenu
*fileMenu
= new wxMenu
;
139 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
140 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
141 fileMenu
->Append( ID_SMALL_GRID
, "&Small Grid test\tCtrl-S");
142 fileMenu
->AppendSeparator();
143 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
145 wxMenu
*viewMenu
= new wxMenu
;
146 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
147 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
148 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
149 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
150 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
151 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, "&Grid drag-resize", "", TRUE
);
152 viewMenu
->Append( ID_TOGGLEGRIDLINES
, "&Grid Lines", "", TRUE
);
153 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...", "" );
154 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...", "" );
155 viewMenu
->Append( ID_AUTOSIZECOLS
, "&Auto-size cols" );
157 wxMenu
*rowLabelMenu
= new wxMenu
;
159 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
161 "Change alignment of row labels" );
163 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
164 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
166 wxMenu
*colLabelMenu
= new wxMenu
;
168 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
170 "Change alignment of col labels" );
172 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
173 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
175 wxMenu
*colMenu
= new wxMenu
;
176 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour..." );
177 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour..." );
178 colMenu
->Append( ID_SETLABEL_FONT
, "Set label fo&nt..." );
179 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour..." );
180 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour..." );
181 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour..." );
183 wxMenu
*editMenu
= new wxMenu
;
184 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
185 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
186 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
187 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
188 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
190 wxMenu
*selectMenu
= new wxMenu
;
191 selectMenu
->Append( ID_SELECT_UNSELECT
, "Add new cells to the selection",
192 "When off, old selection is deselected before "
193 "selecting the new cells", TRUE
);
194 selectMenu
->Append( ID_SELECT_ALL
, "Select all");
195 selectMenu
->Append( ID_SELECT_ROW
, "Select row 2");
196 selectMenu
->Append( ID_SELECT_COL
, "Select col 2");
197 selectMenu
->Append( ID_SELECT_CELL
, "Select cell (3, 1)");
198 selectMenu
->Append( ID_DESELECT_ALL
, "Deselect all");
199 selectMenu
->Append( ID_DESELECT_ROW
, "Deselect row 2");
200 selectMenu
->Append( ID_DESELECT_COL
, "Deselect col 2");
201 selectMenu
->Append( ID_DESELECT_CELL
, "Deselect cell (3, 1)");
202 wxMenu
*selectionMenu
= new wxMenu
;
203 selectMenu
->Append( ID_CHANGESEL
, "Change &selection mode",
205 "Change selection mode" );
207 selectionMenu
->Append( ID_SELCELLS
, "Select &Cells" );
208 selectionMenu
->Append( ID_SELROWS
, "Select &Rows" );
209 selectionMenu
->Append( ID_SELCOLS
, "Select C&ols" );
212 wxMenu
*helpMenu
= new wxMenu
;
213 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
215 wxMenuBar
*menuBar
= new wxMenuBar
;
216 menuBar
->Append( fileMenu
, "&File" );
217 menuBar
->Append( viewMenu
, "&View" );
218 menuBar
->Append( colMenu
, "&Colours" );
219 menuBar
->Append( editMenu
, "&Edit" );
220 menuBar
->Append( selectMenu
, "&Select" );
221 menuBar
->Append( helpMenu
, "&Help" );
223 SetMenuBar( menuBar
);
227 grid
= new wxGrid( this,
230 wxSize( 400, 300 ) );
232 logWin
= new wxTextCtrl( this,
235 wxPoint( 0, gridH
+ 20 ),
236 wxSize( logW
, logH
),
239 logger
= new wxLogTextCtrl( logWin
);
240 m_logOld
= logger
->SetActiveTarget( logger
);
241 logger
->SetTimestamp( NULL
);
243 // this will create a grid and, by default, an associated grid
245 grid
->CreateGrid( 100, 100 );
247 grid
->SetRowSize( 0, 60 );
248 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
250 grid
->SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." );
251 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
252 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
254 grid
->SetCellValue( 0, 2, "Blah" );
255 grid
->SetCellValue( 0, 3, "Read only" );
256 grid
->SetReadOnly( 0, 3 );
258 grid
->SetCellValue( 0, 4, "Can veto edit this cell" );
260 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
262 grid
->SetRowSize( 99, 60 );
263 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
265 grid
->SetCellValue(2, 2, "red");
267 grid
->SetCellTextColour(2, 2, *wxRED
);
268 grid
->SetCellValue(3, 3, "green on grey");
269 grid
->SetCellTextColour(3, 3, *wxGREEN
);
270 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
272 grid
->SetCellValue(4, 4, "a weird looking cell");
273 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
274 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
276 grid
->SetCellValue(3, 0, "0");
277 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
278 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
280 wxGridCellAttr
*attr
;
281 attr
= new wxGridCellAttr
;
282 attr
->SetTextColour(*wxBLUE
);
283 grid
->SetColAttr(5, attr
);
284 attr
= new wxGridCellAttr
;
285 attr
->SetBackgroundColour(*wxRED
);
286 grid
->SetRowAttr(5, attr
);
288 grid
->SetCellValue(2, 4, "a wider column");
289 grid
->SetColSize(4, 120);
290 grid
->SetColMinimalWidth(4, 120);
292 grid
->SetCellTextColour(5, 8, *wxGREEN
);
293 grid
->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
294 grid
->SetCellValue(5, 5, "Bg from row attr\nText col from col attr");
296 grid
->SetColFormatFloat(6);
297 grid
->SetCellValue(0, 6, "3.1415");
298 grid
->SetCellValue(1, 6, "1415");
299 grid
->SetCellValue(2, 6, "12345.67890");
301 grid
->SetColFormatFloat(7, 6, 2);
302 grid
->SetCellValue(0, 7, "3.1415");
303 grid
->SetCellValue(1, 7, "1415");
304 grid
->SetCellValue(2, 7, "12345.67890");
306 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
311 topSizer
->Add( logWin
,
315 SetAutoLayout( TRUE
);
316 SetSizer( topSizer
);
318 topSizer
->Fit( this );
319 topSizer
->SetSizeHints( this );
326 GridFrame::~GridFrame()
328 delete wxLog::SetActiveTarget(m_logOld
);
332 void GridFrame::SetDefaults()
334 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
335 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
336 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
337 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
338 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
339 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
340 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
344 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
346 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
348 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
352 grid
->SetRowLabelSize( 0 );
357 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
359 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
361 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
365 grid
->SetColLabelSize( 0 );
370 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
373 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
377 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
379 grid
->EnableDragRowSize(
380 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
384 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
386 grid
->EnableDragColSize(
387 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
390 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
392 grid
->EnableDragGridSize(
393 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
397 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
399 grid
->EnableGridLines(
400 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
403 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
405 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
407 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
408 "Pen Width", 11, choices
);
410 int current
= grid
->GetCellHighlightPenWidth();
411 dlg
.SetSelection(current
);
412 if (dlg
.ShowModal() == wxID_OK
) {
413 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
417 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
419 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
421 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
422 "Pen Width", 11, choices
);
424 int current
= grid
->GetCellHighlightROPenWidth();
425 dlg
.SetSelection(current
);
426 if (dlg
.ShowModal() == wxID_OK
) {
427 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
433 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
435 grid
->AutoSizeColumns();
440 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
442 wxColourDialog
dlg( NULL
);
443 if ( dlg
.ShowModal() == wxID_OK
)
445 wxColourData retData
;
446 retData
= dlg
.GetColourData();
447 wxColour colour
= retData
.GetColour();
449 grid
->SetLabelBackgroundColour( colour
);
454 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
456 wxColourDialog
dlg( NULL
);
457 if ( dlg
.ShowModal() == wxID_OK
)
459 wxColourData retData
;
460 retData
= dlg
.GetColourData();
461 wxColour colour
= retData
.GetColour();
463 grid
->SetLabelTextColour( colour
);
467 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
469 wxFont font
= wxGetFontFromUser(this);
472 grid
->SetLabelFont(font
);
476 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
479 grid
->GetRowLabelAlignment( &horiz
, &vert
);
484 horiz
= wxALIGN_CENTRE
;
488 horiz
= wxALIGN_RIGHT
;
492 horiz
= wxALIGN_LEFT
;
496 grid
->SetRowLabelAlignment( horiz
, -1 );
499 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
502 grid
->GetRowLabelAlignment( &horiz
, &vert
);
507 vert
= wxALIGN_CENTRE
;
511 vert
= wxALIGN_BOTTOM
;
519 grid
->SetRowLabelAlignment( -1, vert
);
523 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
526 grid
->GetColLabelAlignment( &horiz
, &vert
);
531 horiz
= wxALIGN_CENTRE
;
535 horiz
= wxALIGN_RIGHT
;
539 horiz
= wxALIGN_LEFT
;
543 grid
->SetColLabelAlignment( horiz
, -1 );
547 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
550 grid
->GetColLabelAlignment( &horiz
, &vert
);
555 vert
= wxALIGN_CENTRE
;
559 vert
= wxALIGN_BOTTOM
;
567 grid
->SetColLabelAlignment( -1, vert
);
571 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
573 wxColourDialog
dlg( NULL
);
574 if ( dlg
.ShowModal() == wxID_OK
)
576 wxColourData retData
;
577 retData
= dlg
.GetColourData();
578 wxColour colour
= retData
.GetColour();
580 grid
->SetGridLineColour( colour
);
585 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
587 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
591 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
593 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
597 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
599 if ( grid
->IsSelection() )
602 for ( int n
= 0; n
< grid
->GetNumberRows(); )
603 if ( grid
->IsInSelection( n
, 0 ) )
604 grid
->DeleteRows( n
, 1 );
612 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
614 if ( grid
->IsSelection() )
617 for ( int n
= 0; n
< grid
->GetNumberCols(); )
618 if ( grid
->IsInSelection( 0 , n
) )
619 grid
->DeleteCols( n
, 1 );
627 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
632 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
634 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
637 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
639 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
642 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
644 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
647 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
649 wxColour col
= wxGetColourFromUser(this);
652 grid
->SetDefaultCellTextColour(col
);
657 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
659 wxColour col
= wxGetColourFromUser(this);
662 grid
->SetDefaultCellBackgroundColour(col
);
667 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
669 grid
->DeselectCell(3, 1);
672 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
674 grid
->DeselectCol(2);
677 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
679 grid
->DeselectRow(2);
682 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
684 grid
->ClearSelection();
687 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
689 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
692 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
694 grid
->SelectCol(2, m_addToSel
);
697 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
699 grid
->SelectRow(2, m_addToSel
);
702 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
707 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
709 m_addToSel
= event
.IsChecked();
712 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
715 if ( ev
.GetRow() != -1 )
717 logBuf
<< "Left click on row label " << ev
.GetRow();
719 else if ( ev
.GetCol() != -1 )
721 logBuf
<< "Left click on col label " << ev
.GetCol();
725 logBuf
<< "Left click on corner label";
728 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
729 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
730 wxLogMessage( wxT("%s"), logBuf
.c_str() );
732 // you must call event skip if you want default grid processing
738 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
741 logBuf
<< "Left click at row " << ev
.GetRow()
742 << " col " << ev
.GetCol();
743 wxLogMessage( wxT("%s"), logBuf
.c_str() );
745 // you must call event skip if you want default grid processing
746 // (cell highlighting etc.)
752 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
755 logBuf
<< "Resized row " << ev
.GetRowOrCol();
756 wxLogMessage( wxT("%s"), logBuf
.c_str() );
762 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
765 logBuf
<< "Resized col " << ev
.GetRowOrCol();
766 wxLogMessage( wxT("%s"), logBuf
.c_str() );
772 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
775 if ( ev
.Selecting() )
776 logBuf
<< "Selected ";
778 logBuf
<< "Deselected ";
779 logBuf
<< "cell at row " << ev
.GetRow()
780 << " col " << ev
.GetCol()
781 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
782 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
783 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
784 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
785 wxLogMessage( wxT("%s"), logBuf
.c_str() );
787 // you must call Skip() if you want the default processing
788 // to occur in wxGrid
792 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
795 if ( ev
.Selecting() )
796 logBuf
<< "Selected ";
798 logBuf
<< "Deselected ";
799 logBuf
<< "cells from row " << ev
.GetTopRow()
800 << " col " << ev
.GetLeftCol()
801 << " to row " << ev
.GetBottomRow()
802 << " col " << ev
.GetRightCol()
803 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
804 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
805 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
806 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
807 wxLogMessage( wxT("%s"), logBuf
.c_str() );
812 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
815 logBuf
<< "Value changed for cell at"
816 << " row " << ev
.GetRow()
817 << " col " << ev
.GetCol();
819 wxLogMessage( wxT("%s"), logBuf
.c_str() );
824 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
827 if ( (ev
.GetCol() == 4) &&
828 (ev
.GetRow() == 0) &&
829 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
830 _T("Checking"),wxYES_NO
) == wxNO
) ) {
836 wxLogMessage( wxT("Cell editor shown.") );
841 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
844 if ( (ev
.GetCol() == 4) &&
845 (ev
.GetRow() == 0) &&
846 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
847 _T("Checking"),wxYES_NO
) == wxNO
) ) {
853 wxLogMessage( wxT("Cell editor hidden.") );
858 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
860 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
862 "mbedward@ozemail.com.au \n\n",
868 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
873 void GridFrame::OnBugsTable(wxCommandEvent
& )
875 BugsGridFrame
*frame
= new BugsGridFrame
;
879 void GridFrame::OnSmallGrid(wxCommandEvent
& )
881 wxFrame
* frame
= new wxFrame(NULL
, -1, "A Small Grid",
882 wxDefaultPosition
, wxSize(640, 480));
883 wxPanel
* panel
= new wxPanel(frame
, -1);
884 wxGrid
* grid
= new wxGrid(panel
, -1, wxPoint(10,10), wxSize(400,400),
885 wxWANTS_CHARS
| wxSIMPLE_BORDER
);
886 grid
->CreateGrid(3,3);
890 void GridFrame::OnVTable(wxCommandEvent
& )
892 static long s_sizeGrid
= 10000;
895 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
898 s
= wxGetTextFromUser( "Size of the table to create",
902 s
.ToLong( &s_sizeGrid
);
905 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
907 "wxGridDemo question",
912 if ( s_sizeGrid
!= -1 )
914 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
919 // ----------------------------------------------------------------------------
920 // MyGridCellRenderer
921 // ----------------------------------------------------------------------------
923 // do something that the default renderer doesn't here just to show that it is
924 // possible to alter the appearance of the cell beyond what the attributes
926 void MyGridCellRenderer::Draw(wxGrid
& grid
,
927 wxGridCellAttr
& attr
,
933 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
935 dc
.SetPen(*wxGREEN_PEN
);
936 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
937 dc
.DrawEllipse(rect
);
940 // ----------------------------------------------------------------------------
941 // MyGridCellAttrProvider
942 // ----------------------------------------------------------------------------
944 MyGridCellAttrProvider::MyGridCellAttrProvider()
946 m_attrForOddRows
= new wxGridCellAttr
;
947 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
950 MyGridCellAttrProvider::~MyGridCellAttrProvider()
952 m_attrForOddRows
->DecRef();
955 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
956 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
958 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
964 attr
= m_attrForOddRows
;
969 if ( !attr
->HasBackgroundColour() )
971 wxGridCellAttr
*attrNew
= attr
->Clone();
974 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
982 // ============================================================================
983 // BigGridFrame and BigGridTable: Sample of a non-standard table
984 // ============================================================================
986 BigGridFrame::BigGridFrame(long sizeGrid
)
987 : wxFrame(NULL
, -1, "Plugin Virtual Table",
988 wxDefaultPosition
, wxSize(500, 450))
990 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
991 m_table
= new BigGridTable(sizeGrid
);
993 // VZ: I don't understand why this slows down the display that much,
994 // must profile it...
995 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
997 m_grid
->SetTable(m_table
, TRUE
);
999 #if defined __WXMOTIF__
1000 // MB: the grid isn't getting a sensible default size under wxMotif
1002 GetClientSize( &cw
, &ch
);
1003 m_grid
->SetSize( cw
, ch
);
1007 // ============================================================================
1008 // BugsGridFrame: a "realistic" table
1009 // ============================================================================
1011 // ----------------------------------------------------------------------------
1013 // ----------------------------------------------------------------------------
1036 static const wxString severities
[] =
1045 static struct BugsGridData
1051 wxChar platform
[12];
1053 } gs_dataBugsGrid
[] =
1055 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
1056 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
1057 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
1058 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
1061 static const wxChar
*headers
[Col_Max
] =
1071 // ----------------------------------------------------------------------------
1073 // ----------------------------------------------------------------------------
1075 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1081 return wxGRID_VALUE_NUMBER
;;
1084 // fall thorugh (TODO should be a list)
1087 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1090 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1093 return wxGRID_VALUE_BOOL
;
1096 wxFAIL_MSG(_T("unknown column"));
1098 return wxEmptyString
;
1101 int BugsGridTable::GetNumberRows()
1103 return WXSIZEOF(gs_dataBugsGrid
);
1106 int BugsGridTable::GetNumberCols()
1111 bool BugsGridTable::IsEmptyCell( int row
, int col
)
1116 wxString
BugsGridTable::GetValue( int row
, int col
)
1118 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1125 wxFAIL_MSG(_T("unexpected column"));
1129 return severities
[gd
.severity
];
1138 return wxEmptyString
;
1141 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1143 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1150 wxFAIL_MSG(_T("unexpected column"));
1156 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1158 if ( severities
[n
] == value
)
1160 gd
.severity
= (Severity
)n
;
1165 if ( n
== WXSIZEOF(severities
) )
1167 wxLogWarning(_T("Invalid severity value '%s'."),
1169 gd
.severity
= Sev_Normal
;
1175 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1179 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1184 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1186 if ( typeName
== wxGRID_VALUE_STRING
)
1190 else if ( typeName
== wxGRID_VALUE_BOOL
)
1192 return col
== Col_Opened
;
1194 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1196 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1204 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1206 return CanGetValueAs(row
, col
, typeName
);
1209 long BugsGridTable::GetValueAsLong( int row
, int col
)
1211 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1225 wxFAIL_MSG(_T("unexpected column"));
1230 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1232 if ( col
== Col_Opened
)
1234 return gs_dataBugsGrid
[row
].opened
;
1238 wxFAIL_MSG(_T("unexpected column"));
1244 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1246 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1255 wxFAIL_MSG(_T("unexpected column"));
1259 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1261 if ( col
== Col_Opened
)
1263 gs_dataBugsGrid
[row
].opened
= value
;
1267 wxFAIL_MSG(_T("unexpected column"));
1271 wxString
BugsGridTable::GetColLabelValue( int col
)
1273 return headers
[col
];
1276 BugsGridTable::BugsGridTable()
1280 // ----------------------------------------------------------------------------
1282 // ----------------------------------------------------------------------------
1284 BugsGridFrame::BugsGridFrame()
1285 : wxFrame(NULL
, -1, "Bugs table",
1286 wxDefaultPosition
, wxSize(500, 300))
1288 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1289 wxGridTableBase
*table
= new BugsGridTable();
1290 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1291 grid
->SetTable(table
, TRUE
);
1293 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1294 *attrRangeEditor
= new wxGridCellAttr
,
1295 *attrCombo
= new wxGridCellAttr
;
1297 attrRO
->SetReadOnly();
1298 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1299 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1302 grid
->SetColAttr(Col_Id
, attrRO
);
1303 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1304 grid
->SetColAttr(Col_Severity
, attrCombo
);
1306 grid
->SetMargins(0, 0);
1309 SetClientSize(grid
->GetSize());