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, 4, "Can veto edit this cell" );
255 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
257 grid
->SetRowSize( 99, 60 );
258 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
260 grid
->SetCellValue(2, 2, "red");
262 grid
->SetCellTextColour(2, 2, *wxRED
);
263 grid
->SetCellValue(3, 3, "green on grey");
264 grid
->SetCellTextColour(3, 3, *wxGREEN
);
265 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
267 grid
->SetCellValue(4, 4, "a weird looking cell");
268 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
269 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
271 grid
->SetCellValue(3, 0, "0");
272 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
273 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
275 wxGridCellAttr
*attr
;
276 attr
= new wxGridCellAttr
;
277 attr
->SetTextColour(*wxBLUE
);
278 grid
->SetColAttr(5, attr
);
279 attr
= new wxGridCellAttr
;
280 attr
->SetBackgroundColour(*wxRED
);
281 grid
->SetRowAttr(5, attr
);
283 grid
->SetCellValue(2, 4, "a wider column");
284 grid
->SetColSize(4, 120);
285 grid
->SetColMinimalWidth(4, 120);
287 grid
->SetCellTextColour(5, 8, *wxGREEN
);
288 grid
->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
289 grid
->SetCellValue(5, 5, "Bg from row attr\nText col from col attr");
291 grid
->SetColFormatFloat(6);
292 grid
->SetCellValue(0, 6, "3.1415");
293 grid
->SetCellValue(1, 6, "1415");
294 grid
->SetCellValue(2, 6, "12345.67890");
296 grid
->SetColFormatFloat(7, 6, 2);
297 grid
->SetCellValue(0, 7, "3.1415");
298 grid
->SetCellValue(1, 7, "1415");
299 grid
->SetCellValue(2, 7, "12345.67890");
301 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
306 topSizer
->Add( logWin
,
310 SetAutoLayout( TRUE
);
311 SetSizer( topSizer
);
313 topSizer
->Fit( this );
314 topSizer
->SetSizeHints( this );
321 GridFrame::~GridFrame()
323 delete wxLog::SetActiveTarget(m_logOld
);
327 void GridFrame::SetDefaults()
329 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
330 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
331 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
332 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
333 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
334 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
335 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
339 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
341 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
343 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
347 grid
->SetRowLabelSize( 0 );
352 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
354 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
356 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
360 grid
->SetColLabelSize( 0 );
365 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
368 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
372 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
374 grid
->EnableDragRowSize(
375 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
379 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
381 grid
->EnableDragColSize(
382 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
385 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
387 grid
->EnableDragGridSize(
388 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
392 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
394 grid
->EnableGridLines(
395 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
398 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
400 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
402 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
403 "Pen Width", 11, choices
);
405 int current
= grid
->GetCellHighlightPenWidth();
406 dlg
.SetSelection(current
);
407 if (dlg
.ShowModal() == wxID_OK
) {
408 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
412 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
414 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
416 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
417 "Pen Width", 11, choices
);
419 int current
= grid
->GetCellHighlightROPenWidth();
420 dlg
.SetSelection(current
);
421 if (dlg
.ShowModal() == wxID_OK
) {
422 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
428 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
430 grid
->AutoSizeColumns();
435 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
437 wxColourDialog
dlg( NULL
);
438 if ( dlg
.ShowModal() == wxID_OK
)
440 wxColourData retData
;
441 retData
= dlg
.GetColourData();
442 wxColour colour
= retData
.GetColour();
444 grid
->SetLabelBackgroundColour( colour
);
449 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
451 wxColourDialog
dlg( NULL
);
452 if ( dlg
.ShowModal() == wxID_OK
)
454 wxColourData retData
;
455 retData
= dlg
.GetColourData();
456 wxColour colour
= retData
.GetColour();
458 grid
->SetLabelTextColour( colour
);
463 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
466 grid
->GetRowLabelAlignment( &horiz
, &vert
);
471 horiz
= wxALIGN_CENTRE
;
475 horiz
= wxALIGN_RIGHT
;
479 horiz
= wxALIGN_LEFT
;
483 grid
->SetRowLabelAlignment( horiz
, -1 );
486 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
489 grid
->GetRowLabelAlignment( &horiz
, &vert
);
494 vert
= wxALIGN_CENTRE
;
498 vert
= wxALIGN_BOTTOM
;
506 grid
->SetRowLabelAlignment( -1, vert
);
510 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
513 grid
->GetColLabelAlignment( &horiz
, &vert
);
518 horiz
= wxALIGN_CENTRE
;
522 horiz
= wxALIGN_RIGHT
;
526 horiz
= wxALIGN_LEFT
;
530 grid
->SetColLabelAlignment( horiz
, -1 );
534 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
537 grid
->GetColLabelAlignment( &horiz
, &vert
);
542 vert
= wxALIGN_CENTRE
;
546 vert
= wxALIGN_BOTTOM
;
554 grid
->SetColLabelAlignment( -1, vert
);
558 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
560 wxColourDialog
dlg( NULL
);
561 if ( dlg
.ShowModal() == wxID_OK
)
563 wxColourData retData
;
564 retData
= dlg
.GetColourData();
565 wxColour colour
= retData
.GetColour();
567 grid
->SetGridLineColour( colour
);
572 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
574 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
578 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
580 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
584 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
586 if ( grid
->IsSelection() )
589 for ( int n
= 0; n
< grid
->GetNumberRows(); )
590 if ( grid
->IsInSelection( n
, 0 ) )
591 grid
->DeleteRows( n
, 1 );
599 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
601 if ( grid
->IsSelection() )
604 for ( int n
= 0; n
< grid
->GetNumberCols(); )
605 if ( grid
->IsInSelection( 0 , n
) )
606 grid
->DeleteCols( n
, 1 );
614 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
619 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
621 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
624 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
626 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
629 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
631 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
634 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
636 wxColour col
= wxGetColourFromUser(this);
639 grid
->SetDefaultCellTextColour(col
);
644 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
646 wxColour col
= wxGetColourFromUser(this);
649 grid
->SetDefaultCellBackgroundColour(col
);
654 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
656 grid
->DeselectCell(3, 1);
659 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
661 grid
->DeselectCol(2);
664 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
666 grid
->DeselectRow(2);
669 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
671 grid
->ClearSelection();
674 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
676 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
679 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
681 grid
->SelectCol(2, m_addToSel
);
684 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
686 grid
->SelectRow(2, m_addToSel
);
689 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
694 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
696 m_addToSel
= event
.IsChecked();
699 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
702 if ( ev
.GetRow() != -1 )
704 logBuf
<< "Left click on row label " << ev
.GetRow();
706 else if ( ev
.GetCol() != -1 )
708 logBuf
<< "Left click on col label " << ev
.GetCol();
712 logBuf
<< "Left click on corner label";
715 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
716 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
717 wxLogMessage( wxT("%s"), logBuf
.c_str() );
719 // you must call event skip if you want default grid processing
725 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
728 logBuf
<< "Left click at row " << ev
.GetRow()
729 << " col " << ev
.GetCol();
730 wxLogMessage( wxT("%s"), logBuf
.c_str() );
732 // you must call event skip if you want default grid processing
733 // (cell highlighting etc.)
739 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
742 logBuf
<< "Resized row " << ev
.GetRowOrCol();
743 wxLogMessage( wxT("%s"), logBuf
.c_str() );
749 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
752 logBuf
<< "Resized col " << ev
.GetRowOrCol();
753 wxLogMessage( wxT("%s"), logBuf
.c_str() );
759 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
762 if ( ev
.Selecting() )
763 logBuf
<< "Selected ";
765 logBuf
<< "Deselected ";
766 logBuf
<< "cell at row " << ev
.GetRow()
767 << " col " << ev
.GetCol()
768 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
769 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
770 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
771 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
772 wxLogMessage( wxT("%s"), logBuf
.c_str() );
774 // you must call Skip() if you want the default processing
775 // to occur in wxGrid
779 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
782 if ( ev
.Selecting() )
783 logBuf
<< "Selected ";
785 logBuf
<< "Deselected ";
786 logBuf
<< "cells from row " << ev
.GetTopRow()
787 << " col " << ev
.GetLeftCol()
788 << " to row " << ev
.GetBottomRow()
789 << " col " << ev
.GetRightCol()
790 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
791 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
792 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
793 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
794 wxLogMessage( wxT("%s"), logBuf
.c_str() );
799 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
802 logBuf
<< "Value changed for cell at"
803 << " row " << ev
.GetRow()
804 << " col " << ev
.GetCol();
806 wxLogMessage( wxT("%s"), logBuf
.c_str() );
811 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
814 if ( (ev
.GetCol() == 4) &&
815 (ev
.GetRow() == 0) &&
816 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
817 _T("Checking"),wxYES_NO
) == wxNO
) ) {
824 wxLogMessage( wxT("Cell editor shown.") );
829 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
832 if ( (ev
.GetCol() == 4) &&
833 (ev
.GetRow() == 0) &&
834 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
835 _T("Checking"),wxYES_NO
) == wxNO
) ) {
841 wxLogMessage( wxT("Cell editor hidden.") );
846 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
848 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
850 "mbedward@ozemail.com.au \n\n",
856 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
861 void GridFrame::OnBugsTable(wxCommandEvent
& )
863 BugsGridFrame
*frame
= new BugsGridFrame
;
868 void GridFrame::OnVTable(wxCommandEvent
& )
870 static long s_sizeGrid
= 10000;
873 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
876 s
= wxGetTextFromUser( "Size of the table to create",
880 s
.ToLong( &s_sizeGrid
);
883 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
885 "wxGridDemo question",
890 if ( s_sizeGrid
!= -1 )
892 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
897 // ----------------------------------------------------------------------------
898 // MyGridCellRenderer
899 // ----------------------------------------------------------------------------
901 // do something that the default renderer doesn't here just to show that it is
902 // possible to alter the appearance of the cell beyond what the attributes
904 void MyGridCellRenderer::Draw(wxGrid
& grid
,
905 wxGridCellAttr
& attr
,
911 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
913 dc
.SetPen(*wxGREEN_PEN
);
914 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
915 dc
.DrawEllipse(rect
);
918 // ----------------------------------------------------------------------------
919 // MyGridCellAttrProvider
920 // ----------------------------------------------------------------------------
922 MyGridCellAttrProvider::MyGridCellAttrProvider()
924 m_attrForOddRows
= new wxGridCellAttr
;
925 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
928 MyGridCellAttrProvider::~MyGridCellAttrProvider()
930 m_attrForOddRows
->DecRef();
933 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
934 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
936 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
942 attr
= m_attrForOddRows
;
947 if ( !attr
->HasBackgroundColour() )
949 wxGridCellAttr
*attrNew
= attr
->Clone();
952 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
960 // ============================================================================
961 // BigGridFrame and BigGridTable: Sample of a non-standard table
962 // ============================================================================
964 BigGridFrame::BigGridFrame(long sizeGrid
)
965 : wxFrame(NULL
, -1, "Plugin Virtual Table",
966 wxDefaultPosition
, wxSize(500, 450))
968 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
969 m_table
= new BigGridTable(sizeGrid
);
971 // VZ: I don't understand why this slows down the display that much,
972 // must profile it...
973 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
975 m_grid
->SetTable(m_table
, TRUE
);
977 #if defined __WXMOTIF__
978 // MB: the grid isn't getting a sensible default size under wxMotif
980 GetClientSize( &cw
, &ch
);
981 m_grid
->SetSize( cw
, ch
);
985 // ============================================================================
986 // BugsGridFrame: a "realistic" table
987 // ============================================================================
989 // ----------------------------------------------------------------------------
991 // ----------------------------------------------------------------------------
1014 static const wxString severities
[] =
1023 static struct BugsGridData
1029 wxChar platform
[12];
1031 } gs_dataBugsGrid
[] =
1033 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
1034 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
1035 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
1036 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
1039 static const wxChar
*headers
[Col_Max
] =
1049 // ----------------------------------------------------------------------------
1051 // ----------------------------------------------------------------------------
1053 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1059 return wxGRID_VALUE_NUMBER
;;
1062 // fall thorugh (TODO should be a list)
1065 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1068 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1071 return wxGRID_VALUE_BOOL
;
1074 wxFAIL_MSG(_T("unknown column"));
1076 return wxEmptyString
;
1079 int BugsGridTable::GetNumberRows()
1081 return WXSIZEOF(gs_dataBugsGrid
);
1084 int BugsGridTable::GetNumberCols()
1089 bool BugsGridTable::IsEmptyCell( int row
, int col
)
1094 wxString
BugsGridTable::GetValue( int row
, int col
)
1096 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1103 wxFAIL_MSG(_T("unexpected column"));
1107 return severities
[gd
.severity
];
1116 return wxEmptyString
;
1119 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1121 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1128 wxFAIL_MSG(_T("unexpected column"));
1134 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1136 if ( severities
[n
] == value
)
1138 gd
.severity
= (Severity
)n
;
1143 if ( n
== WXSIZEOF(severities
) )
1145 wxLogWarning(_T("Invalid severity value '%s'."),
1147 gd
.severity
= Sev_Normal
;
1153 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1157 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1162 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1164 if ( typeName
== wxGRID_VALUE_STRING
)
1168 else if ( typeName
== wxGRID_VALUE_BOOL
)
1170 return col
== Col_Opened
;
1172 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1174 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1182 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1184 return CanGetValueAs(row
, col
, typeName
);
1187 long BugsGridTable::GetValueAsLong( int row
, int col
)
1189 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1203 wxFAIL_MSG(_T("unexpected column"));
1208 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1210 if ( col
== Col_Opened
)
1212 return gs_dataBugsGrid
[row
].opened
;
1216 wxFAIL_MSG(_T("unexpected column"));
1222 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1224 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1233 wxFAIL_MSG(_T("unexpected column"));
1237 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1239 if ( col
== Col_Opened
)
1241 gs_dataBugsGrid
[row
].opened
= value
;
1245 wxFAIL_MSG(_T("unexpected column"));
1249 wxString
BugsGridTable::GetColLabelValue( int col
)
1251 return headers
[col
];
1254 BugsGridTable::BugsGridTable()
1258 // ----------------------------------------------------------------------------
1260 // ----------------------------------------------------------------------------
1262 BugsGridFrame::BugsGridFrame()
1263 : wxFrame(NULL
, -1, "Bugs table",
1264 wxDefaultPosition
, wxSize(500, 300))
1266 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1267 wxGridTableBase
*table
= new BugsGridTable();
1268 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1269 grid
->SetTable(table
, TRUE
);
1271 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1272 *attrRangeEditor
= new wxGridCellAttr
,
1273 *attrCombo
= new wxGridCellAttr
;
1275 attrRO
->SetReadOnly();
1276 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1277 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1280 grid
->SetColAttr(Col_Id
, attrRO
);
1281 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1282 grid
->SetColAttr(Col_Severity
, attrCombo
);
1284 grid
->SetMargins(0, 0);
1287 SetClientSize(grid
->GetSize());