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"
38 #include "wx/generic/gridctrl.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_APP( GridApp
)
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 bool GridApp::OnInit()
58 GridFrame
*frame
= new GridFrame
;
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
69 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
70 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
71 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
72 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
73 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
74 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
75 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
76 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
77 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
78 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
79 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
80 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
81 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
82 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
83 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
84 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
85 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
86 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
87 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
88 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
89 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
90 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
91 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
93 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
94 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
96 EVT_MENU( ID_ABOUT
, GridFrame::About
)
97 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
98 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
99 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
101 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
102 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
103 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
104 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
105 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
106 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
107 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
108 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
109 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
111 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
112 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
114 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
115 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
116 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
117 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
118 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
119 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
120 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
122 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
123 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
127 GridFrame::GridFrame()
128 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
132 int gridW
= 600, gridH
= 300;
133 int logW
= gridW
, logH
= 100;
135 wxMenu
*fileMenu
= new wxMenu
;
136 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
137 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
138 fileMenu
->AppendSeparator();
139 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
141 wxMenu
*viewMenu
= new wxMenu
;
142 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
143 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
144 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
145 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
146 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
147 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, "&Grid drag-resize", "", TRUE
);
148 viewMenu
->Append( ID_TOGGLEGRIDLINES
, "&Grid Lines", "", TRUE
);
149 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...", "" );
150 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...", "" );
151 viewMenu
->Append( ID_AUTOSIZECOLS
, "&Auto-size cols" );
153 wxMenu
*rowLabelMenu
= new wxMenu
;
155 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
157 "Change alignment of row labels" );
159 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
160 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
162 wxMenu
*colLabelMenu
= new wxMenu
;
164 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
166 "Change alignment of col labels" );
168 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
169 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
171 wxMenu
*colMenu
= new wxMenu
;
172 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
173 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
174 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
175 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
176 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
178 wxMenu
*editMenu
= new wxMenu
;
179 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
180 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
181 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
182 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
183 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
185 wxMenu
*selectMenu
= new wxMenu
;
186 selectMenu
->Append( ID_SELECT_UNSELECT
, "Add new cells to the selection",
187 "When off, old selection is deselected before "
188 "selecting the new cells", TRUE
);
189 selectMenu
->Append( ID_SELECT_ALL
, "Select all");
190 selectMenu
->Append( ID_SELECT_ROW
, "Select row 2");
191 selectMenu
->Append( ID_SELECT_COL
, "Select col 2");
192 selectMenu
->Append( ID_SELECT_CELL
, "Select cell (3, 1)");
193 selectMenu
->Append( ID_DESELECT_ALL
, "Deselect all");
194 selectMenu
->Append( ID_DESELECT_ROW
, "Deselect row 2");
195 selectMenu
->Append( ID_DESELECT_COL
, "Deselect col 2");
196 selectMenu
->Append( ID_DESELECT_CELL
, "Deselect cell (3, 1)");
197 wxMenu
*selectionMenu
= new wxMenu
;
198 selectMenu
->Append( ID_CHANGESEL
, "Change &selection mode",
200 "Change selection mode" );
202 selectionMenu
->Append( ID_SELCELLS
, "Select &Cells" );
203 selectionMenu
->Append( ID_SELROWS
, "Select &Rows" );
204 selectionMenu
->Append( ID_SELCOLS
, "Select C&ols" );
207 wxMenu
*helpMenu
= new wxMenu
;
208 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
210 wxMenuBar
*menuBar
= new wxMenuBar
;
211 menuBar
->Append( fileMenu
, "&File" );
212 menuBar
->Append( viewMenu
, "&View" );
213 menuBar
->Append( colMenu
, "&Colours" );
214 menuBar
->Append( editMenu
, "&Edit" );
215 menuBar
->Append( selectMenu
, "&Select" );
216 menuBar
->Append( helpMenu
, "&Help" );
218 SetMenuBar( menuBar
);
222 grid
= new wxGrid( this,
225 wxSize( 400, 300 ) );
227 logWin
= new wxTextCtrl( this,
230 wxPoint( 0, gridH
+ 20 ),
231 wxSize( logW
, logH
),
234 logger
= new wxLogTextCtrl( logWin
);
235 m_logOld
= logger
->SetActiveTarget( logger
);
236 logger
->SetTimestamp( NULL
);
238 // this will create a grid and, by default, an associated grid
240 grid
->CreateGrid( 100, 100 );
242 grid
->SetRowSize( 0, 60 );
243 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
245 grid
->SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." );
246 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
247 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
249 grid
->SetCellValue( 0, 2, "Blah" );
250 grid
->SetCellValue( 0, 3, "Read only" );
251 grid
->SetReadOnly( 0, 3 );
253 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
255 grid
->SetRowSize( 99, 60 );
256 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
258 grid
->SetCellValue(2, 2, "red");
260 grid
->SetCellTextColour(2, 2, *wxRED
);
261 grid
->SetCellValue(3, 3, "green on grey");
262 grid
->SetCellTextColour(3, 3, *wxGREEN
);
263 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
265 grid
->SetCellValue(4, 4, "a weird looking cell");
266 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
267 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
269 grid
->SetCellValue(3, 0, "0");
270 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
271 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
273 wxGridCellAttr
*attr
;
274 attr
= new wxGridCellAttr
;
275 attr
->SetTextColour(*wxBLUE
);
276 grid
->SetColAttr(5, attr
);
277 attr
= new wxGridCellAttr
;
278 attr
->SetBackgroundColour(*wxRED
);
279 grid
->SetRowAttr(5, attr
);
281 grid
->SetCellValue(2, 4, "a wider column");
282 grid
->SetColSize(4, 120);
283 grid
->SetColMinimalWidth(4, 120);
285 grid
->SetCellTextColour(5, 8, *wxGREEN
);
286 grid
->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
287 grid
->SetCellValue(5, 5, "Bg from row attr\nText col from col attr");
289 grid
->SetColFormatFloat(6);
290 grid
->SetCellValue(0, 6, "3.1415");
291 grid
->SetCellValue(1, 6, "1415");
292 grid
->SetCellValue(2, 6, "12345.67890");
294 grid
->SetColFormatFloat(7, 6, 2);
295 grid
->SetCellValue(0, 7, "3.1415");
296 grid
->SetCellValue(1, 7, "1415");
297 grid
->SetCellValue(2, 7, "12345.67890");
299 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
304 topSizer
->Add( logWin
,
308 SetAutoLayout( TRUE
);
309 SetSizer( topSizer
);
311 topSizer
->Fit( this );
312 topSizer
->SetSizeHints( this );
319 GridFrame::~GridFrame()
321 delete wxLog::SetActiveTarget(m_logOld
);
325 void GridFrame::SetDefaults()
327 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
328 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
329 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
330 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
331 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
332 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
333 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
337 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
339 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
341 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
345 grid
->SetRowLabelSize( 0 );
350 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
352 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
354 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
358 grid
->SetColLabelSize( 0 );
363 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
366 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
370 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
372 grid
->EnableDragRowSize(
373 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
377 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
379 grid
->EnableDragColSize(
380 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
383 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
385 grid
->EnableDragGridSize(
386 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
390 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
392 grid
->EnableGridLines(
393 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
396 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
398 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
400 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
401 "Pen Width", 11, choices
);
403 int current
= grid
->GetCellHighlightPenWidth();
404 dlg
.SetSelection(current
);
405 if (dlg
.ShowModal() == wxID_OK
) {
406 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
410 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
412 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
414 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
415 "Pen Width", 11, choices
);
417 int current
= grid
->GetCellHighlightROPenWidth();
418 dlg
.SetSelection(current
);
419 if (dlg
.ShowModal() == wxID_OK
) {
420 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
426 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
428 grid
->AutoSizeColumns();
433 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
435 wxColourDialog
dlg( NULL
);
436 if ( dlg
.ShowModal() == wxID_OK
)
438 wxColourData retData
;
439 retData
= dlg
.GetColourData();
440 wxColour colour
= retData
.GetColour();
442 grid
->SetLabelBackgroundColour( colour
);
447 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
449 wxColourDialog
dlg( NULL
);
450 if ( dlg
.ShowModal() == wxID_OK
)
452 wxColourData retData
;
453 retData
= dlg
.GetColourData();
454 wxColour colour
= retData
.GetColour();
456 grid
->SetLabelTextColour( colour
);
461 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
464 grid
->GetRowLabelAlignment( &horiz
, &vert
);
469 horiz
= wxALIGN_CENTRE
;
473 horiz
= wxALIGN_RIGHT
;
477 horiz
= wxALIGN_LEFT
;
481 grid
->SetRowLabelAlignment( horiz
, -1 );
484 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
487 grid
->GetRowLabelAlignment( &horiz
, &vert
);
492 vert
= wxALIGN_CENTRE
;
496 vert
= wxALIGN_BOTTOM
;
504 grid
->SetRowLabelAlignment( -1, vert
);
508 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
511 grid
->GetColLabelAlignment( &horiz
, &vert
);
516 horiz
= wxALIGN_CENTRE
;
520 horiz
= wxALIGN_RIGHT
;
524 horiz
= wxALIGN_LEFT
;
528 grid
->SetColLabelAlignment( horiz
, -1 );
532 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
535 grid
->GetColLabelAlignment( &horiz
, &vert
);
540 vert
= wxALIGN_CENTRE
;
544 vert
= wxALIGN_BOTTOM
;
552 grid
->SetColLabelAlignment( -1, vert
);
556 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
558 wxColourDialog
dlg( NULL
);
559 if ( dlg
.ShowModal() == wxID_OK
)
561 wxColourData retData
;
562 retData
= dlg
.GetColourData();
563 wxColour colour
= retData
.GetColour();
565 grid
->SetGridLineColour( colour
);
570 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
572 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
576 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
578 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
582 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
584 if ( grid
->IsSelection() )
587 for ( int n
= 0; n
< grid
->GetNumberRows(); )
588 if ( grid
->IsInSelection( n
, 0 ) )
589 grid
->DeleteRows( n
, 1 );
597 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
599 if ( grid
->IsSelection() )
602 for ( int n
= 0; n
< grid
->GetNumberCols(); )
603 if ( grid
->IsInSelection( 0 , n
) )
604 grid
->DeleteCols( n
, 1 );
612 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
617 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
619 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
622 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
624 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
627 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
629 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
632 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
634 wxColour col
= wxGetColourFromUser(this);
637 grid
->SetDefaultCellTextColour(col
);
642 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
644 wxColour col
= wxGetColourFromUser(this);
647 grid
->SetDefaultCellBackgroundColour(col
);
652 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
654 grid
->DeselectCell(3, 1);
657 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
659 grid
->DeselectCol(2);
662 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
664 grid
->DeselectRow(2);
667 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
669 grid
->ClearSelection();
672 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
674 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
677 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
679 grid
->SelectCol(2, m_addToSel
);
682 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
684 grid
->SelectRow(2, m_addToSel
);
687 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
692 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
694 m_addToSel
= event
.IsChecked();
697 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
700 if ( ev
.GetRow() != -1 )
702 logBuf
<< "Left click on row label " << ev
.GetRow();
704 else if ( ev
.GetCol() != -1 )
706 logBuf
<< "Left click on col label " << ev
.GetCol();
710 logBuf
<< "Left click on corner label";
713 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
714 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
715 wxLogMessage( wxT("%s"), logBuf
.c_str() );
717 // you must call event skip if you want default grid processing
723 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
726 logBuf
<< "Left click at row " << ev
.GetRow()
727 << " col " << ev
.GetCol();
728 wxLogMessage( wxT("%s"), logBuf
.c_str() );
730 // you must call event skip if you want default grid processing
731 // (cell highlighting etc.)
737 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
740 logBuf
<< "Resized row " << ev
.GetRowOrCol();
741 wxLogMessage( wxT("%s"), logBuf
.c_str() );
747 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
750 logBuf
<< "Resized col " << ev
.GetRowOrCol();
751 wxLogMessage( wxT("%s"), logBuf
.c_str() );
757 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
760 if ( ev
.Selecting() )
761 logBuf
<< "Selected ";
763 logBuf
<< "Deselected ";
764 logBuf
<< "cell at row " << ev
.GetRow()
765 << " col " << ev
.GetCol()
766 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
767 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
768 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
769 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
770 wxLogMessage( wxT("%s"), logBuf
.c_str() );
772 // you must call Skip() if you want the default processing
773 // to occur in wxGrid
777 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
780 if ( ev
.Selecting() )
781 logBuf
<< "Selected ";
783 logBuf
<< "Deselected ";
784 logBuf
<< "cells from row " << ev
.GetTopRow()
785 << " col " << ev
.GetLeftCol()
786 << " to row " << ev
.GetBottomRow()
787 << " col " << ev
.GetRightCol()
788 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
789 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
790 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
791 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
792 wxLogMessage( wxT("%s"), logBuf
.c_str() );
797 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
800 logBuf
<< "Value changed for cell at"
801 << " row " << ev
.GetRow()
802 << " col " << ev
.GetCol();
804 wxLogMessage( wxT("%s"), logBuf
.c_str() );
809 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
811 wxLogMessage( wxT("Cell editor shown.") );
816 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
818 wxLogMessage( wxT("Cell editor hidden.") );
823 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
825 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
827 "mbedward@ozemail.com.au \n\n",
833 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
838 void GridFrame::OnBugsTable(wxCommandEvent
& )
840 BugsGridFrame
*frame
= new BugsGridFrame
;
845 void GridFrame::OnVTable(wxCommandEvent
& )
847 static long s_sizeGrid
= 10000;
850 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
853 s
= wxGetTextFromUser( "Size of the table to create",
857 s
.ToLong( &s_sizeGrid
);
860 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
862 "wxGridDemo question",
867 if ( s_sizeGrid
!= -1 )
869 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
874 // ----------------------------------------------------------------------------
875 // MyGridCellRenderer
876 // ----------------------------------------------------------------------------
878 // do something that the default renderer doesn't here just to show that it is
879 // possible to alter the appearance of the cell beyond what the attributes
881 void MyGridCellRenderer::Draw(wxGrid
& grid
,
882 wxGridCellAttr
& attr
,
888 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
890 dc
.SetPen(*wxGREEN_PEN
);
891 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
892 dc
.DrawEllipse(rect
);
895 // ----------------------------------------------------------------------------
896 // MyGridCellAttrProvider
897 // ----------------------------------------------------------------------------
899 MyGridCellAttrProvider::MyGridCellAttrProvider()
901 m_attrForOddRows
= new wxGridCellAttr
;
902 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
905 MyGridCellAttrProvider::~MyGridCellAttrProvider()
907 m_attrForOddRows
->DecRef();
910 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
911 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
913 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
919 attr
= m_attrForOddRows
;
924 if ( !attr
->HasBackgroundColour() )
926 wxGridCellAttr
*attrNew
= attr
->Clone();
929 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
937 // ============================================================================
938 // BigGridFrame and BigGridTable: Sample of a non-standard table
939 // ============================================================================
941 BigGridFrame::BigGridFrame(long sizeGrid
)
942 : wxFrame(NULL
, -1, "Plugin Virtual Table",
943 wxDefaultPosition
, wxSize(500, 450))
945 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
946 m_table
= new BigGridTable(sizeGrid
);
948 // VZ: I don't understand why this slows down the display that much,
949 // must profile it...
950 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
952 m_grid
->SetTable(m_table
, TRUE
);
954 #if defined __WXMOTIF__
955 // MB: the grid isn't getting a sensible default size under wxMotif
957 GetClientSize( &cw
, &ch
);
958 m_grid
->SetSize( cw
, ch
);
962 // ============================================================================
963 // BugsGridFrame: a "realistic" table
964 // ============================================================================
966 // ----------------------------------------------------------------------------
968 // ----------------------------------------------------------------------------
991 static const wxString severities
[] =
1000 static struct BugsGridData
1006 wxChar platform
[12];
1008 } gs_dataBugsGrid
[] =
1010 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
1011 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
1012 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
1013 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
1016 static const wxChar
*headers
[Col_Max
] =
1026 // ----------------------------------------------------------------------------
1028 // ----------------------------------------------------------------------------
1030 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1036 return wxGRID_VALUE_NUMBER
;;
1039 // fall thorugh (TODO should be a list)
1042 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1045 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1048 return wxGRID_VALUE_BOOL
;
1051 wxFAIL_MSG(_T("unknown column"));
1053 return wxEmptyString
;
1056 int BugsGridTable::GetNumberRows()
1058 return WXSIZEOF(gs_dataBugsGrid
);
1061 int BugsGridTable::GetNumberCols()
1066 bool BugsGridTable::IsEmptyCell( int row
, int col
)
1071 wxString
BugsGridTable::GetValue( int row
, int col
)
1073 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1080 wxFAIL_MSG(_T("unexpected column"));
1084 return severities
[gd
.severity
];
1093 return wxEmptyString
;
1096 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1098 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1105 wxFAIL_MSG(_T("unexpected column"));
1111 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1113 if ( severities
[n
] == value
)
1115 gd
.severity
= (Severity
)n
;
1120 if ( n
== WXSIZEOF(severities
) )
1122 wxLogWarning(_T("Invalid severity value '%s'."),
1124 gd
.severity
= Sev_Normal
;
1130 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1134 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1139 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1141 if ( typeName
== wxGRID_VALUE_STRING
)
1145 else if ( typeName
== wxGRID_VALUE_BOOL
)
1147 return col
== Col_Opened
;
1149 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1151 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1159 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1161 return CanGetValueAs(row
, col
, typeName
);
1164 long BugsGridTable::GetValueAsLong( int row
, int col
)
1166 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1180 wxFAIL_MSG(_T("unexpected column"));
1185 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1187 if ( col
== Col_Opened
)
1189 return gs_dataBugsGrid
[row
].opened
;
1193 wxFAIL_MSG(_T("unexpected column"));
1199 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1201 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1210 wxFAIL_MSG(_T("unexpected column"));
1214 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1216 if ( col
== Col_Opened
)
1218 gs_dataBugsGrid
[row
].opened
= value
;
1222 wxFAIL_MSG(_T("unexpected column"));
1226 wxString
BugsGridTable::GetColLabelValue( int col
)
1228 return headers
[col
];
1231 BugsGridTable::BugsGridTable()
1235 // ----------------------------------------------------------------------------
1237 // ----------------------------------------------------------------------------
1239 BugsGridFrame::BugsGridFrame()
1240 : wxFrame(NULL
, -1, "Bugs table",
1241 wxDefaultPosition
, wxSize(500, 300))
1243 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1244 wxGridTableBase
*table
= new BugsGridTable();
1245 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1246 grid
->SetTable(table
, TRUE
);
1248 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1249 *attrRangeEditor
= new wxGridCellAttr
,
1250 *attrCombo
= new wxGridCellAttr
;
1252 attrRO
->SetReadOnly();
1253 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1254 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1257 grid
->SetColAttr(Col_Id
, attrRO
);
1258 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1259 grid
->SetColAttr(Col_Severity
, attrCombo
);
1261 grid
->SetMargins(0, 0);
1264 SetClientSize(grid
->GetSize());