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
)
109 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
110 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
111 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
112 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
113 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
114 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
115 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
117 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
118 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
122 GridFrame::GridFrame()
123 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
127 int gridW
= 600, gridH
= 300;
128 int logW
= gridW
, logH
= 100;
130 wxMenu
*fileMenu
= new wxMenu
;
131 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
132 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
133 fileMenu
->AppendSeparator();
134 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
136 wxMenu
*viewMenu
= new wxMenu
;
137 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
138 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
139 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
140 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
141 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
142 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, "&Grid drag-resize", "", TRUE
);
143 viewMenu
->Append( ID_TOGGLEGRIDLINES
, "&Grid Lines", "", TRUE
);
144 viewMenu
->Append( ID_AUTOSIZECOLS
, "&Auto-size cols" );
146 wxMenu
*rowLabelMenu
= new wxMenu
;
148 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
150 "Change alignment of row labels" );
152 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
153 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
155 wxMenu
*colLabelMenu
= new wxMenu
;
157 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
159 "Change alignment of col labels" );
161 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
162 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
164 wxMenu
*colMenu
= new wxMenu
;
165 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
166 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
167 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
168 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
169 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
171 wxMenu
*editMenu
= new wxMenu
;
172 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
173 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
174 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
175 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
176 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
178 wxMenu
*selectMenu
= new wxMenu
;
179 selectMenu
->Append( ID_SELECT_ALL
, "Select all");
180 selectMenu
->Append( ID_SELECT_ROW
, "Select row 2");
181 selectMenu
->Append( ID_SELECT_COL
, "Select col 2");
182 selectMenu
->Append( ID_SELECT_CELL
, "Select cell (3, 1)");
183 selectMenu
->Append( ID_DESELECT_ALL
, "Deselect all");
184 selectMenu
->Append( ID_DESELECT_ROW
, "Deselect row 2");
185 selectMenu
->Append( ID_DESELECT_COL
, "Deselect col 2");
186 selectMenu
->Append( ID_DESELECT_CELL
, "Deselect cell (3, 1)");
187 wxMenu
*selectionMenu
= new wxMenu
;
188 selectMenu
->Append( ID_CHANGESEL
, "Change &selection mode",
190 "Change selection mode" );
192 selectionMenu
->Append( ID_SELCELLS
, "Select &Cells" );
193 selectionMenu
->Append( ID_SELROWS
, "Select &Rows" );
194 selectionMenu
->Append( ID_SELCOLS
, "Select C&ols" );
197 wxMenu
*helpMenu
= new wxMenu
;
198 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
200 wxMenuBar
*menuBar
= new wxMenuBar
;
201 menuBar
->Append( fileMenu
, "&File" );
202 menuBar
->Append( viewMenu
, "&View" );
203 menuBar
->Append( colMenu
, "&Colours" );
204 menuBar
->Append( editMenu
, "&Edit" );
205 menuBar
->Append( selectMenu
, "&Select" );
206 menuBar
->Append( helpMenu
, "&Help" );
208 SetMenuBar( menuBar
);
210 grid
= new wxGrid( this,
213 wxSize( 400, 300 ) );
215 logWin
= new wxTextCtrl( this,
218 wxPoint( 0, gridH
+ 20 ),
219 wxSize( logW
, logH
),
222 logger
= new wxLogTextCtrl( logWin
);
223 m_logOld
= logger
->SetActiveTarget( logger
);
224 logger
->SetTimestamp( NULL
);
226 // this will create a grid and, by default, an associated grid
228 grid
->CreateGrid( 100, 100 );
230 grid
->SetRowSize( 0, 60 );
231 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
233 grid
->SetCellValue( 0, 1, "Blah" );
234 grid
->SetCellValue( 0, 2, "Blah" );
235 grid
->SetCellValue( 0, 3, "Read only" );
236 grid
->SetReadOnly( 0, 3 );
238 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
240 grid
->SetRowSize( 99, 60 );
241 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
243 grid
->SetCellValue(2, 2, "red");
245 grid
->SetCellTextColour(2, 2, *wxRED
);
246 grid
->SetCellValue(3, 3, "green on grey");
247 grid
->SetCellTextColour(3, 3, *wxGREEN
);
248 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
250 grid
->SetCellValue(4, 4, "a weird looking cell");
251 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
252 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
254 grid
->SetCellValue(3, 0, "1");
255 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
256 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
258 wxGridCellAttr
*attr
;
259 attr
= new wxGridCellAttr
;
260 attr
->SetTextColour(*wxBLUE
);
261 grid
->SetColAttr(5, attr
);
262 attr
= new wxGridCellAttr
;
263 attr
->SetBackgroundColour(*wxBLUE
);
264 grid
->SetRowAttr(5, attr
);
266 grid
->SetCellValue(2, 4, "a wider column");
267 grid
->SetColSize(4, 120);
268 grid
->SetColMinimalWidth(4, 120);
270 grid
->SetColFormatFloat(5);
271 grid
->SetCellValue(0, 5, "3.1415");
272 grid
->SetCellValue(1, 5, "1415");
273 grid
->SetCellValue(2, 5, "12345.67890");
275 grid
->SetColFormatFloat(6, 6, 2);
276 grid
->SetCellValue(0, 6, "3.1415");
277 grid
->SetCellValue(1, 6, "1415");
278 grid
->SetCellValue(2, 6, "12345.67890");
280 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
285 topSizer
->Add( logWin
,
289 SetAutoLayout( TRUE
);
290 SetSizer( topSizer
);
292 topSizer
->Fit( this );
293 topSizer
->SetSizeHints( this );
300 GridFrame::~GridFrame()
302 delete wxLog::SetActiveTarget(m_logOld
);
306 void GridFrame::SetDefaults()
308 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
309 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
310 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
311 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
312 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
313 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
314 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
318 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
320 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
322 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
326 grid
->SetRowLabelSize( 0 );
331 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
333 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
335 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
339 grid
->SetColLabelSize( 0 );
344 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
347 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
351 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
353 grid
->EnableDragRowSize(
354 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
358 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
360 grid
->EnableDragColSize(
361 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
364 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
366 grid
->EnableDragGridSize(
367 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
371 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
373 grid
->EnableGridLines(
374 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
378 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
380 grid
->AutoSizeColumns();
385 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
387 wxColourDialog
dlg( NULL
);
388 if ( dlg
.ShowModal() == wxID_OK
)
390 wxColourData retData
;
391 retData
= dlg
.GetColourData();
392 wxColour colour
= retData
.GetColour();
394 grid
->SetLabelBackgroundColour( colour
);
399 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
401 wxColourDialog
dlg( NULL
);
402 if ( dlg
.ShowModal() == wxID_OK
)
404 wxColourData retData
;
405 retData
= dlg
.GetColourData();
406 wxColour colour
= retData
.GetColour();
408 grid
->SetLabelTextColour( colour
);
413 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
416 grid
->GetRowLabelAlignment( &horiz
, &vert
);
421 horiz
= wxALIGN_CENTRE
;
425 horiz
= wxALIGN_RIGHT
;
429 horiz
= wxALIGN_LEFT
;
433 grid
->SetRowLabelAlignment( horiz
, -1 );
436 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
439 grid
->GetRowLabelAlignment( &horiz
, &vert
);
444 vert
= wxALIGN_CENTRE
;
448 vert
= wxALIGN_BOTTOM
;
456 grid
->SetRowLabelAlignment( -1, vert
);
460 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
463 grid
->GetColLabelAlignment( &horiz
, &vert
);
468 horiz
= wxALIGN_CENTRE
;
472 horiz
= wxALIGN_RIGHT
;
476 horiz
= wxALIGN_LEFT
;
480 grid
->SetColLabelAlignment( horiz
, -1 );
484 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
487 grid
->GetColLabelAlignment( &horiz
, &vert
);
492 vert
= wxALIGN_CENTRE
;
496 vert
= wxALIGN_BOTTOM
;
504 grid
->SetColLabelAlignment( -1, vert
);
508 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
510 wxColourDialog
dlg( NULL
);
511 if ( dlg
.ShowModal() == wxID_OK
)
513 wxColourData retData
;
514 retData
= dlg
.GetColourData();
515 wxColour colour
= retData
.GetColour();
517 grid
->SetGridLineColour( colour
);
522 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
524 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
528 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
530 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
534 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
536 if ( grid
->IsSelection() )
539 for ( int n
= 0; n
< grid
->GetNumberRows(); )
540 if ( grid
->IsInSelection( n
, 0 ) )
541 grid
->DeleteRows( n
, 1 );
549 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
551 if ( grid
->IsSelection() )
554 for ( int n
= 0; n
< grid
->GetNumberCols(); )
555 if ( grid
->IsInSelection( 0 , n
) )
556 grid
->DeleteCols( n
, 1 );
564 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
569 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
571 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
574 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
576 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
579 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
581 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
584 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
586 wxColour col
= wxGetColourFromUser(this);
589 grid
->SetDefaultCellTextColour(col
);
594 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
596 wxColour col
= wxGetColourFromUser(this);
599 grid
->SetDefaultCellBackgroundColour(col
);
604 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
606 grid
->DeselectCell(3, 1);
609 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
611 grid
->DeselectCol(2);
614 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
616 grid
->DeselectRow(2);
619 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
621 grid
->ClearSelection();
624 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
626 grid
->SelectBlock(3, 1, 3, 1);
629 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
631 grid
->SelectCol(2, TRUE
);
634 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
636 grid
->SelectRow(2, TRUE
);
639 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
644 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
647 if ( ev
.GetRow() != -1 )
649 logBuf
<< "Left click on row label " << ev
.GetRow();
651 else if ( ev
.GetCol() != -1 )
653 logBuf
<< "Left click on col label " << ev
.GetCol();
657 logBuf
<< "Left click on corner label";
660 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
661 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
662 wxLogMessage( "%s", logBuf
.c_str() );
664 // you must call event skip if you want default grid processing
670 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
673 logBuf
<< "Left click at row " << ev
.GetRow()
674 << " col " << ev
.GetCol();
675 wxLogMessage( "%s", logBuf
.c_str() );
677 // you must call event skip if you want default grid processing
678 // (cell highlighting etc.)
684 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
687 logBuf
<< "Resized row " << ev
.GetRowOrCol();
688 wxLogMessage( "%s", logBuf
.c_str() );
694 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
697 logBuf
<< "Resized col " << ev
.GetRowOrCol();
698 wxLogMessage( "%s", logBuf
.c_str() );
704 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
707 if ( ev
.Selecting() )
708 logBuf
<< "Selected ";
710 logBuf
<< "Deselected ";
711 logBuf
<< "cell at row " << ev
.GetRow()
712 << " col " << ev
.GetCol()
713 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
714 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
715 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
716 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
717 wxLogMessage( "%s", logBuf
.c_str() );
719 // you must call Skip() if you want the default processing
720 // to occur in wxGrid
724 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
727 if ( ev
.Selecting() )
728 logBuf
<< "Selected ";
730 logBuf
<< "Deselected ";
731 logBuf
<< "cells from row " << ev
.GetTopRow()
732 << " col " << ev
.GetLeftCol()
733 << " to row " << ev
.GetBottomRow()
734 << " col " << ev
.GetRightCol()
735 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
736 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
737 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
738 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
739 wxLogMessage( "%s", logBuf
.c_str() );
744 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
747 logBuf
<< "Value changed for cell at"
748 << " row " << ev
.GetRow()
749 << " col " << ev
.GetCol();
751 wxLogMessage( "%s", logBuf
.c_str() );
756 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
758 wxLogMessage( "Cell editor shown." );
763 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
765 wxLogMessage( "Cell editor hidden." );
770 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
772 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
774 "mbedward@ozemail.com.au \n\n",
780 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
785 void GridFrame::OnBugsTable(wxCommandEvent
& )
787 BugsGridFrame
*frame
= new BugsGridFrame
;
791 void GridFrame::OnVTable(wxCommandEvent
& )
793 static long s_sizeGrid
= 10000;
796 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
799 s
= wxGetTextFromUser( "Size of the table to create",
803 s
.ToLong( &s_sizeGrid
);
806 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
808 "wxGridDemo question",
813 if ( s_sizeGrid
!= -1 )
815 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
820 // ----------------------------------------------------------------------------
821 // MyGridCellRenderer
822 // ----------------------------------------------------------------------------
824 // do something that the default renderer doesn't here just to show that it is
825 // possible to alter the appearance of the cell beyond what the attributes
827 void MyGridCellRenderer::Draw(wxGrid
& grid
,
828 wxGridCellAttr
& attr
,
834 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
836 dc
.SetPen(*wxGREEN_PEN
);
837 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
838 dc
.DrawEllipse(rect
);
841 // ----------------------------------------------------------------------------
842 // MyGridCellAttrProvider
843 // ----------------------------------------------------------------------------
845 MyGridCellAttrProvider::MyGridCellAttrProvider()
847 m_attrForOddRows
= new wxGridCellAttr
;
848 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
851 MyGridCellAttrProvider::~MyGridCellAttrProvider()
853 m_attrForOddRows
->DecRef();
856 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
) const
858 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
);
864 attr
= m_attrForOddRows
;
869 if ( !attr
->HasBackgroundColour() )
871 wxGridCellAttr
*attrNew
= attr
->Clone();
874 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
882 // ============================================================================
883 // BigGridFrame and BigGridTable: Sample of a non-standard table
884 // ============================================================================
886 BigGridFrame::BigGridFrame(long sizeGrid
)
887 : wxFrame(NULL
, -1, "Plugin Virtual Table",
888 wxDefaultPosition
, wxSize(500, 450))
890 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
891 m_table
= new BigGridTable(sizeGrid
);
893 // VZ: I don't understand why this slows down the display that much,
894 // must profile it...
895 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
897 m_grid
->SetTable(m_table
, TRUE
);
899 #if defined __WXMOTIF__
900 // MB: the grid isn't getting a sensible default size under wxMotif
902 GetClientSize( &cw
, &ch
);
903 m_grid
->SetSize( cw
, ch
);
907 // ============================================================================
908 // BugsGridFrame: a "realistic" table
909 // ============================================================================
911 // ----------------------------------------------------------------------------
913 // ----------------------------------------------------------------------------
936 static const wxString severities
[] =
945 static struct BugsGridData
953 } gs_dataBugsGrid
[] =
955 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
956 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
957 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
958 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
961 static const wxChar
*headers
[Col_Max
] =
971 // ----------------------------------------------------------------------------
973 // ----------------------------------------------------------------------------
975 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
981 return wxGRID_VALUE_NUMBER
;;
984 // fall thorugh (TODO should be a list)
987 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
990 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
993 return wxGRID_VALUE_BOOL
;
996 wxFAIL_MSG(_T("unknown column"));
998 return wxEmptyString
;
1001 int BugsGridTable::GetNumberRows()
1003 return WXSIZEOF(gs_dataBugsGrid
);
1006 int BugsGridTable::GetNumberCols()
1011 bool BugsGridTable::IsEmptyCell( int row
, int col
)
1016 wxString
BugsGridTable::GetValue( int row
, int col
)
1018 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1025 wxFAIL_MSG(_T("unexpected column"));
1029 return severities
[gd
.severity
];
1038 return wxEmptyString
;
1041 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1043 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1050 wxFAIL_MSG(_T("unexpected column"));
1056 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1058 if ( severities
[n
] == value
)
1060 gd
.severity
= (Severity
)n
;
1065 if ( n
== WXSIZEOF(severities
) )
1067 wxLogWarning(_T("Invalid severity value '%s'."),
1069 gd
.severity
= Sev_Normal
;
1075 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1079 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1084 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1086 if ( typeName
== wxGRID_VALUE_STRING
)
1090 else if ( typeName
== wxGRID_VALUE_BOOL
)
1092 return col
== Col_Opened
;
1094 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1096 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1104 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1106 return CanGetValueAs(row
, col
, typeName
);
1109 long BugsGridTable::GetValueAsLong( int row
, int col
)
1111 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1125 wxFAIL_MSG(_T("unexpected column"));
1130 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1132 if ( col
== Col_Opened
)
1134 return gs_dataBugsGrid
[row
].opened
;
1138 wxFAIL_MSG(_T("unexpected column"));
1144 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1146 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1155 wxFAIL_MSG(_T("unexpected column"));
1159 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1161 if ( col
== Col_Opened
)
1163 gs_dataBugsGrid
[row
].opened
= value
;
1167 wxFAIL_MSG(_T("unexpected column"));
1171 wxString
BugsGridTable::GetColLabelValue( int col
)
1173 return headers
[col
];
1176 BugsGridTable::BugsGridTable()
1180 // ----------------------------------------------------------------------------
1182 // ----------------------------------------------------------------------------
1184 BugsGridFrame::BugsGridFrame()
1185 : wxFrame(NULL
, -1, "Bugs table",
1186 wxDefaultPosition
, wxSize(500, 300))
1188 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1189 wxGridTableBase
*table
= new BugsGridTable();
1190 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1191 grid
->SetTable(table
, TRUE
);
1193 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1194 *attrRangeEditor
= new wxGridCellAttr
,
1195 *attrCombo
= new wxGridCellAttr
;
1197 attrRO
->SetReadOnly();
1198 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1199 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1202 grid
->SetColAttr(Col_Id
, attrRO
);
1203 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1204 grid
->SetColAttr(Col_Severity
, attrCombo
);
1206 grid
->SetMargins(0, 0);
1209 wxSize size
= grid
->GetSize();
1212 SetClientSize(size
);