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"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 IMPLEMENT_APP( GridApp
)
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 bool GridApp::OnInit()
57 GridFrame
*frame
= new GridFrame
;
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
68 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
69 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
70 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
71 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
72 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
73 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
74 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
75 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
76 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
77 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
78 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
79 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
80 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
81 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
82 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
83 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
84 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
85 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
86 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
87 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
88 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
89 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
90 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
92 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
93 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
95 EVT_MENU( ID_ABOUT
, GridFrame::About
)
96 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
97 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
98 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
100 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
101 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
102 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
103 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
104 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
105 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
106 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
107 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
108 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
110 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
111 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
113 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
114 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
115 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
116 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
117 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
118 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
119 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
121 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
122 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
126 GridFrame::GridFrame()
127 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
131 int gridW
= 600, gridH
= 300;
132 int logW
= gridW
, logH
= 100;
134 wxMenu
*fileMenu
= new wxMenu
;
135 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
136 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
137 fileMenu
->AppendSeparator();
138 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
140 wxMenu
*viewMenu
= new wxMenu
;
141 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
142 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
143 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
144 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
145 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
146 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, "&Grid drag-resize", "", TRUE
);
147 viewMenu
->Append( ID_TOGGLEGRIDLINES
, "&Grid Lines", "", TRUE
);
148 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...", "" );
149 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...", "" );
150 viewMenu
->Append( ID_AUTOSIZECOLS
, "&Auto-size cols" );
152 wxMenu
*rowLabelMenu
= new wxMenu
;
154 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
156 "Change alignment of row labels" );
158 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
159 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
161 wxMenu
*colLabelMenu
= new wxMenu
;
163 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
165 "Change alignment of col labels" );
167 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
168 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
170 wxMenu
*colMenu
= new wxMenu
;
171 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
172 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
173 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
174 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
175 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
177 wxMenu
*editMenu
= new wxMenu
;
178 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
179 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
180 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
181 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
182 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
184 wxMenu
*selectMenu
= new wxMenu
;
185 selectMenu
->Append( ID_SELECT_UNSELECT
, "Add new cells to the selection",
186 "When off, old selection is deselected before "
187 "selecting the new cells", TRUE
);
188 selectMenu
->Append( ID_SELECT_ALL
, "Select all");
189 selectMenu
->Append( ID_SELECT_ROW
, "Select row 2");
190 selectMenu
->Append( ID_SELECT_COL
, "Select col 2");
191 selectMenu
->Append( ID_SELECT_CELL
, "Select cell (3, 1)");
192 selectMenu
->Append( ID_DESELECT_ALL
, "Deselect all");
193 selectMenu
->Append( ID_DESELECT_ROW
, "Deselect row 2");
194 selectMenu
->Append( ID_DESELECT_COL
, "Deselect col 2");
195 selectMenu
->Append( ID_DESELECT_CELL
, "Deselect cell (3, 1)");
196 wxMenu
*selectionMenu
= new wxMenu
;
197 selectMenu
->Append( ID_CHANGESEL
, "Change &selection mode",
199 "Change selection mode" );
201 selectionMenu
->Append( ID_SELCELLS
, "Select &Cells" );
202 selectionMenu
->Append( ID_SELROWS
, "Select &Rows" );
203 selectionMenu
->Append( ID_SELCOLS
, "Select C&ols" );
206 wxMenu
*helpMenu
= new wxMenu
;
207 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
209 wxMenuBar
*menuBar
= new wxMenuBar
;
210 menuBar
->Append( fileMenu
, "&File" );
211 menuBar
->Append( viewMenu
, "&View" );
212 menuBar
->Append( colMenu
, "&Colours" );
213 menuBar
->Append( editMenu
, "&Edit" );
214 menuBar
->Append( selectMenu
, "&Select" );
215 menuBar
->Append( helpMenu
, "&Help" );
217 SetMenuBar( menuBar
);
221 grid
= new wxGrid( this,
224 wxSize( 400, 300 ) );
226 logWin
= new wxTextCtrl( this,
229 wxPoint( 0, gridH
+ 20 ),
230 wxSize( logW
, logH
),
233 logger
= new wxLogTextCtrl( logWin
);
234 m_logOld
= logger
->SetActiveTarget( logger
);
235 logger
->SetTimestamp( NULL
);
237 // this will create a grid and, by default, an associated grid
239 grid
->CreateGrid( 100, 100 );
241 grid
->SetRowSize( 0, 60 );
242 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
244 grid
->SetCellValue( 0, 1, "Blah" );
245 grid
->SetCellValue( 0, 2, "Blah" );
246 grid
->SetCellValue( 0, 3, "Read only" );
247 grid
->SetReadOnly( 0, 3 );
249 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
251 grid
->SetRowSize( 99, 60 );
252 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
254 grid
->SetCellValue(2, 2, "red");
256 grid
->SetCellTextColour(2, 2, *wxRED
);
257 grid
->SetCellValue(3, 3, "green on grey");
258 grid
->SetCellTextColour(3, 3, *wxGREEN
);
259 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
261 grid
->SetCellValue(4, 4, "a weird looking cell");
262 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
263 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
265 grid
->SetCellValue(3, 0, "0");
266 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
267 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
269 wxGridCellAttr
*attr
;
270 attr
= new wxGridCellAttr
;
271 attr
->SetTextColour(*wxBLUE
);
272 grid
->SetColAttr(5, attr
);
273 attr
= new wxGridCellAttr
;
274 attr
->SetBackgroundColour(*wxRED
);
275 grid
->SetRowAttr(5, attr
);
277 grid
->SetCellValue(2, 4, "a wider column");
278 grid
->SetColSize(4, 120);
279 grid
->SetColMinimalWidth(4, 120);
281 grid
->SetCellTextColour(5, 8, *wxGREEN
);
282 grid
->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
283 grid
->SetCellValue(5, 5, "Bg from row attr\nText col from col attr");
285 grid
->SetColFormatFloat(6);
286 grid
->SetCellValue(0, 6, "3.1415");
287 grid
->SetCellValue(1, 6, "1415");
288 grid
->SetCellValue(2, 6, "12345.67890");
290 grid
->SetColFormatFloat(7, 6, 2);
291 grid
->SetCellValue(0, 7, "3.1415");
292 grid
->SetCellValue(1, 7, "1415");
293 grid
->SetCellValue(2, 7, "12345.67890");
295 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
300 topSizer
->Add( logWin
,
304 SetAutoLayout( TRUE
);
305 SetSizer( topSizer
);
307 topSizer
->Fit( this );
308 topSizer
->SetSizeHints( this );
315 GridFrame::~GridFrame()
317 delete wxLog::SetActiveTarget(m_logOld
);
321 void GridFrame::SetDefaults()
323 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
324 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
325 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
326 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
327 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
328 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
329 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
333 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
335 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
337 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
341 grid
->SetRowLabelSize( 0 );
346 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
348 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
350 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
354 grid
->SetColLabelSize( 0 );
359 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
362 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
366 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
368 grid
->EnableDragRowSize(
369 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
373 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
375 grid
->EnableDragColSize(
376 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
379 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
381 grid
->EnableDragGridSize(
382 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
386 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
388 grid
->EnableGridLines(
389 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
392 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
394 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
396 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
397 "Pen Width", 11, choices
);
399 int current
= grid
->GetCellHighlightPenWidth();
400 dlg
.SetSelection(current
);
401 if (dlg
.ShowModal() == wxID_OK
) {
402 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
406 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
408 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
410 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
411 "Pen Width", 11, choices
);
413 int current
= grid
->GetCellHighlightROPenWidth();
414 dlg
.SetSelection(current
);
415 if (dlg
.ShowModal() == wxID_OK
) {
416 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
422 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
424 grid
->AutoSizeColumns();
429 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
431 wxColourDialog
dlg( NULL
);
432 if ( dlg
.ShowModal() == wxID_OK
)
434 wxColourData retData
;
435 retData
= dlg
.GetColourData();
436 wxColour colour
= retData
.GetColour();
438 grid
->SetLabelBackgroundColour( colour
);
443 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
445 wxColourDialog
dlg( NULL
);
446 if ( dlg
.ShowModal() == wxID_OK
)
448 wxColourData retData
;
449 retData
= dlg
.GetColourData();
450 wxColour colour
= retData
.GetColour();
452 grid
->SetLabelTextColour( colour
);
457 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
460 grid
->GetRowLabelAlignment( &horiz
, &vert
);
465 horiz
= wxALIGN_CENTRE
;
469 horiz
= wxALIGN_RIGHT
;
473 horiz
= wxALIGN_LEFT
;
477 grid
->SetRowLabelAlignment( horiz
, -1 );
480 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
483 grid
->GetRowLabelAlignment( &horiz
, &vert
);
488 vert
= wxALIGN_CENTRE
;
492 vert
= wxALIGN_BOTTOM
;
500 grid
->SetRowLabelAlignment( -1, vert
);
504 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
507 grid
->GetColLabelAlignment( &horiz
, &vert
);
512 horiz
= wxALIGN_CENTRE
;
516 horiz
= wxALIGN_RIGHT
;
520 horiz
= wxALIGN_LEFT
;
524 grid
->SetColLabelAlignment( horiz
, -1 );
528 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
531 grid
->GetColLabelAlignment( &horiz
, &vert
);
536 vert
= wxALIGN_CENTRE
;
540 vert
= wxALIGN_BOTTOM
;
548 grid
->SetColLabelAlignment( -1, vert
);
552 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
554 wxColourDialog
dlg( NULL
);
555 if ( dlg
.ShowModal() == wxID_OK
)
557 wxColourData retData
;
558 retData
= dlg
.GetColourData();
559 wxColour colour
= retData
.GetColour();
561 grid
->SetGridLineColour( colour
);
566 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
568 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
572 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
574 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
578 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
580 if ( grid
->IsSelection() )
583 for ( int n
= 0; n
< grid
->GetNumberRows(); )
584 if ( grid
->IsInSelection( n
, 0 ) )
585 grid
->DeleteRows( n
, 1 );
593 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
595 if ( grid
->IsSelection() )
598 for ( int n
= 0; n
< grid
->GetNumberCols(); )
599 if ( grid
->IsInSelection( 0 , n
) )
600 grid
->DeleteCols( n
, 1 );
608 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
613 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
615 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
618 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
620 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
623 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
625 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
628 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
630 wxColour col
= wxGetColourFromUser(this);
633 grid
->SetDefaultCellTextColour(col
);
638 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
640 wxColour col
= wxGetColourFromUser(this);
643 grid
->SetDefaultCellBackgroundColour(col
);
648 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
650 grid
->DeselectCell(3, 1);
653 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
655 grid
->DeselectCol(2);
658 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
660 grid
->DeselectRow(2);
663 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
665 grid
->ClearSelection();
668 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
670 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
673 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
675 grid
->SelectCol(2, m_addToSel
);
678 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
680 grid
->SelectRow(2, m_addToSel
);
683 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
688 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
690 m_addToSel
= event
.IsChecked();
693 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
696 if ( ev
.GetRow() != -1 )
698 logBuf
<< "Left click on row label " << ev
.GetRow();
700 else if ( ev
.GetCol() != -1 )
702 logBuf
<< "Left click on col label " << ev
.GetCol();
706 logBuf
<< "Left click on corner label";
709 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
710 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
711 wxLogMessage( "%s", logBuf
.c_str() );
713 // you must call event skip if you want default grid processing
719 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
722 logBuf
<< "Left click at row " << ev
.GetRow()
723 << " col " << ev
.GetCol();
724 wxLogMessage( "%s", logBuf
.c_str() );
726 // you must call event skip if you want default grid processing
727 // (cell highlighting etc.)
733 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
736 logBuf
<< "Resized row " << ev
.GetRowOrCol();
737 wxLogMessage( "%s", logBuf
.c_str() );
743 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
746 logBuf
<< "Resized col " << ev
.GetRowOrCol();
747 wxLogMessage( "%s", logBuf
.c_str() );
753 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
756 if ( ev
.Selecting() )
757 logBuf
<< "Selected ";
759 logBuf
<< "Deselected ";
760 logBuf
<< "cell at row " << ev
.GetRow()
761 << " col " << ev
.GetCol()
762 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
763 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
764 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
765 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
766 wxLogMessage( "%s", logBuf
.c_str() );
768 // you must call Skip() if you want the default processing
769 // to occur in wxGrid
773 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
776 if ( ev
.Selecting() )
777 logBuf
<< "Selected ";
779 logBuf
<< "Deselected ";
780 logBuf
<< "cells from row " << ev
.GetTopRow()
781 << " col " << ev
.GetLeftCol()
782 << " to row " << ev
.GetBottomRow()
783 << " col " << ev
.GetRightCol()
784 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
785 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
786 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
787 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
788 wxLogMessage( "%s", logBuf
.c_str() );
793 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
796 logBuf
<< "Value changed for cell at"
797 << " row " << ev
.GetRow()
798 << " col " << ev
.GetCol();
800 wxLogMessage( "%s", logBuf
.c_str() );
805 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
807 wxLogMessage( "Cell editor shown." );
812 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
814 wxLogMessage( "Cell editor hidden." );
819 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
821 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
823 "mbedward@ozemail.com.au \n\n",
829 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
834 void GridFrame::OnBugsTable(wxCommandEvent
& )
836 BugsGridFrame
*frame
= new BugsGridFrame
;
841 void GridFrame::OnVTable(wxCommandEvent
& )
843 static long s_sizeGrid
= 10000;
846 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
849 s
= wxGetTextFromUser( "Size of the table to create",
853 s
.ToLong( &s_sizeGrid
);
856 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
858 "wxGridDemo question",
863 if ( s_sizeGrid
!= -1 )
865 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
870 // ----------------------------------------------------------------------------
871 // MyGridCellRenderer
872 // ----------------------------------------------------------------------------
874 // do something that the default renderer doesn't here just to show that it is
875 // possible to alter the appearance of the cell beyond what the attributes
877 void MyGridCellRenderer::Draw(wxGrid
& grid
,
878 wxGridCellAttr
& attr
,
884 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
886 dc
.SetPen(*wxGREEN_PEN
);
887 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
888 dc
.DrawEllipse(rect
);
891 // ----------------------------------------------------------------------------
892 // MyGridCellAttrProvider
893 // ----------------------------------------------------------------------------
895 MyGridCellAttrProvider::MyGridCellAttrProvider()
897 m_attrForOddRows
= new wxGridCellAttr
;
898 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
901 MyGridCellAttrProvider::~MyGridCellAttrProvider()
903 m_attrForOddRows
->DecRef();
906 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
907 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
909 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
915 attr
= m_attrForOddRows
;
920 if ( !attr
->HasBackgroundColour() )
922 wxGridCellAttr
*attrNew
= attr
->Clone();
925 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
933 // ============================================================================
934 // BigGridFrame and BigGridTable: Sample of a non-standard table
935 // ============================================================================
937 BigGridFrame::BigGridFrame(long sizeGrid
)
938 : wxFrame(NULL
, -1, "Plugin Virtual Table",
939 wxDefaultPosition
, wxSize(500, 450))
941 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
942 m_table
= new BigGridTable(sizeGrid
);
944 // VZ: I don't understand why this slows down the display that much,
945 // must profile it...
946 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
948 m_grid
->SetTable(m_table
, TRUE
);
950 #if defined __WXMOTIF__
951 // MB: the grid isn't getting a sensible default size under wxMotif
953 GetClientSize( &cw
, &ch
);
954 m_grid
->SetSize( cw
, ch
);
958 // ============================================================================
959 // BugsGridFrame: a "realistic" table
960 // ============================================================================
962 // ----------------------------------------------------------------------------
964 // ----------------------------------------------------------------------------
987 static const wxString severities
[] =
996 static struct BugsGridData
1002 wxChar platform
[12];
1004 } gs_dataBugsGrid
[] =
1006 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
1007 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
1008 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
1009 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
1012 static const wxChar
*headers
[Col_Max
] =
1022 // ----------------------------------------------------------------------------
1024 // ----------------------------------------------------------------------------
1026 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1032 return wxGRID_VALUE_NUMBER
;;
1035 // fall thorugh (TODO should be a list)
1038 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1041 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1044 return wxGRID_VALUE_BOOL
;
1047 wxFAIL_MSG(_T("unknown column"));
1049 return wxEmptyString
;
1052 int BugsGridTable::GetNumberRows()
1054 return WXSIZEOF(gs_dataBugsGrid
);
1057 int BugsGridTable::GetNumberCols()
1062 bool BugsGridTable::IsEmptyCell( int row
, int col
)
1067 wxString
BugsGridTable::GetValue( int row
, int col
)
1069 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1076 wxFAIL_MSG(_T("unexpected column"));
1080 return severities
[gd
.severity
];
1089 return wxEmptyString
;
1092 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1094 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1101 wxFAIL_MSG(_T("unexpected column"));
1107 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1109 if ( severities
[n
] == value
)
1111 gd
.severity
= (Severity
)n
;
1116 if ( n
== WXSIZEOF(severities
) )
1118 wxLogWarning(_T("Invalid severity value '%s'."),
1120 gd
.severity
= Sev_Normal
;
1126 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1130 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1135 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1137 if ( typeName
== wxGRID_VALUE_STRING
)
1141 else if ( typeName
== wxGRID_VALUE_BOOL
)
1143 return col
== Col_Opened
;
1145 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1147 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1155 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1157 return CanGetValueAs(row
, col
, typeName
);
1160 long BugsGridTable::GetValueAsLong( int row
, int col
)
1162 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1176 wxFAIL_MSG(_T("unexpected column"));
1181 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1183 if ( col
== Col_Opened
)
1185 return gs_dataBugsGrid
[row
].opened
;
1189 wxFAIL_MSG(_T("unexpected column"));
1195 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1197 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1206 wxFAIL_MSG(_T("unexpected column"));
1210 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1212 if ( col
== Col_Opened
)
1214 gs_dataBugsGrid
[row
].opened
= value
;
1218 wxFAIL_MSG(_T("unexpected column"));
1222 wxString
BugsGridTable::GetColLabelValue( int col
)
1224 return headers
[col
];
1227 BugsGridTable::BugsGridTable()
1231 // ----------------------------------------------------------------------------
1233 // ----------------------------------------------------------------------------
1235 BugsGridFrame::BugsGridFrame()
1236 : wxFrame(NULL
, -1, "Bugs table",
1237 wxDefaultPosition
, wxSize(500, 300))
1239 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1240 wxGridTableBase
*table
= new BugsGridTable();
1241 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1242 grid
->SetTable(table
, TRUE
);
1244 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1245 *attrRangeEditor
= new wxGridCellAttr
,
1246 *attrCombo
= new wxGridCellAttr
;
1248 attrRO
->SetReadOnly();
1249 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1250 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1253 grid
->SetColAttr(Col_Id
, attrRO
);
1254 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1255 grid
->SetColAttr(Col_Severity
, attrCombo
);
1257 grid
->SetMargins(0, 0);
1260 wxSize size
= grid
->GetSize();
1263 SetClientSize(size
);