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_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
72 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
73 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
74 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
75 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
76 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
77 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
78 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
79 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
80 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
81 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
82 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
84 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
85 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
87 EVT_MENU( ID_ABOUT
, GridFrame::About
)
88 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
89 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
90 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
92 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
93 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
94 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
95 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
96 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
97 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
98 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
100 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
101 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
105 GridFrame::GridFrame()
106 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
110 int gridW
= 600, gridH
= 300;
111 int logW
= gridW
, logH
= 100;
113 wxMenu
*fileMenu
= new wxMenu
;
114 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
115 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
116 fileMenu
->AppendSeparator();
117 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
119 wxMenu
*viewMenu
= new wxMenu
;
120 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
121 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
122 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
124 wxMenu
*rowLabelMenu
= new wxMenu
;
126 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
128 "Change alignment of row labels" );
130 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
131 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
133 wxMenu
*colLabelMenu
= new wxMenu
;
135 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
137 "Change alignment of col labels" );
139 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
140 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
142 wxMenu
*colMenu
= new wxMenu
;
143 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
144 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
145 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
146 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
147 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
149 wxMenu
*editMenu
= new wxMenu
;
150 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
151 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
152 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
153 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
154 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
156 wxMenu
*helpMenu
= new wxMenu
;
157 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
159 wxMenuBar
*menuBar
= new wxMenuBar
;
160 menuBar
->Append( fileMenu
, "&File" );
161 menuBar
->Append( viewMenu
, "&View" );
162 menuBar
->Append( colMenu
, "&Colours" );
163 menuBar
->Append( editMenu
, "&Edit" );
164 menuBar
->Append( helpMenu
, "&Help" );
166 SetMenuBar( menuBar
);
168 grid
= new wxGrid( this,
171 wxSize( 400, 300 ) );
173 logWin
= new wxTextCtrl( this,
176 wxPoint( 0, gridH
+ 20 ),
177 wxSize( logW
, logH
),
180 logger
= new wxLogTextCtrl( logWin
);
181 logger
->SetActiveTarget( logger
);
182 logger
->SetTimestamp( NULL
);
184 // this will create a grid and, by default, an associated grid
185 // table for string data
187 grid
->CreateGrid( 100, 100 );
189 grid
->SetRowSize( 0, 60 );
190 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
192 grid
->SetCellValue( 0, 1, "Blah" );
193 grid
->SetCellValue( 0, 2, "Blah" );
194 grid
->SetCellValue( 0, 3, "Read only" );
195 grid
->SetReadOnly( 0, 3 );
197 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
199 grid
->SetRowSize( 99, 60 );
200 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
202 grid
->SetCellValue(2, 2, "red");
204 grid
->SetCellTextColour(2, 2, *wxRED
);
205 grid
->SetCellValue(3, 3, "green on grey");
206 grid
->SetCellTextColour(3, 3, *wxGREEN
);
207 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
209 grid
->SetCellValue(4, 4, "a weird looking cell");
210 grid
->SetCellAlignment(4, 4, wxCENTRE
, wxCENTRE
);
211 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
213 grid
->SetCellValue(3, 0, "1");
214 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
215 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
217 wxGridCellAttr
*attr
;
218 attr
= new wxGridCellAttr
;
219 attr
->SetTextColour(*wxBLUE
);
220 grid
->SetColAttr(5, attr
);
221 attr
= new wxGridCellAttr
;
222 attr
->SetBackgroundColour(*wxBLUE
);
223 grid
->SetRowAttr(5, attr
);
225 grid
->SetCellValue(2, 4, "a wider column");
226 grid
->SetColSize(4, 120);
227 grid
->SetColMinimalWidth(4, 120);
229 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
234 topSizer
->Add( logWin
,
238 SetAutoLayout( TRUE
);
239 SetSizer( topSizer
);
241 topSizer
->Fit( this );
242 topSizer
->SetSizeHints( this );
249 GridFrame::~GridFrame()
254 void GridFrame::SetDefaults()
256 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
257 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
258 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
262 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
264 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
266 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
270 grid
->SetRowLabelSize( 0 );
275 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
277 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
279 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
283 grid
->SetColLabelSize( 0 );
288 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
291 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
295 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
297 wxColourDialog
dlg( NULL
);
298 if ( dlg
.ShowModal() == wxID_OK
)
300 wxColourData retData
;
301 retData
= dlg
.GetColourData();
302 wxColour colour
= retData
.GetColour();
304 grid
->SetLabelBackgroundColour( colour
);
309 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
311 wxColourDialog
dlg( NULL
);
312 if ( dlg
.ShowModal() == wxID_OK
)
314 wxColourData retData
;
315 retData
= dlg
.GetColourData();
316 wxColour colour
= retData
.GetColour();
318 grid
->SetLabelTextColour( colour
);
323 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
326 grid
->GetRowLabelAlignment( &horiz
, &vert
);
343 grid
->SetRowLabelAlignment( horiz
, -1 );
346 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
349 grid
->GetRowLabelAlignment( &horiz
, &vert
);
366 grid
->SetRowLabelAlignment( -1, vert
);
370 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
373 grid
->GetColLabelAlignment( &horiz
, &vert
);
390 grid
->SetColLabelAlignment( horiz
, -1 );
394 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
397 grid
->GetColLabelAlignment( &horiz
, &vert
);
414 grid
->SetColLabelAlignment( -1, vert
);
418 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
420 wxColourDialog
dlg( NULL
);
421 if ( dlg
.ShowModal() == wxID_OK
)
423 wxColourData retData
;
424 retData
= dlg
.GetColourData();
425 wxColour colour
= retData
.GetColour();
427 grid
->SetGridLineColour( colour
);
432 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
434 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
438 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
440 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
444 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
446 if ( grid
->IsSelection() )
448 int topRow
, bottomRow
, leftCol
, rightCol
;
449 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
450 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
455 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
457 if ( grid
->IsSelection() )
459 int topRow
, bottomRow
, leftCol
, rightCol
;
460 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
461 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
466 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
471 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
473 wxColour col
= wxGetColourFromUser(this);
476 grid
->SetDefaultCellTextColour(col
);
481 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
483 wxColour col
= wxGetColourFromUser(this);
486 grid
->SetDefaultCellBackgroundColour(col
);
491 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
494 if ( ev
.GetRow() != -1 )
496 logBuf
<< "Left click on row label " << ev
.GetRow();
498 else if ( ev
.GetCol() != -1 )
500 logBuf
<< "Left click on col label " << ev
.GetCol();
504 logBuf
<< "Left click on corner label";
507 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
508 wxLogMessage( "%s", logBuf
.c_str() );
510 // you must call event skip if you want default grid processing
516 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
519 logBuf
<< "Left click at row " << ev
.GetRow()
520 << " col " << ev
.GetCol();
521 wxLogMessage( "%s", logBuf
.c_str() );
523 // you must call event skip if you want default grid processing
524 // (cell highlighting etc.)
530 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
533 logBuf
<< "Resized row " << ev
.GetRowOrCol();
534 wxLogMessage( "%s", logBuf
.c_str() );
540 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
543 logBuf
<< "Resized col " << ev
.GetRowOrCol();
544 wxLogMessage( "%s", logBuf
.c_str() );
550 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
553 logBuf
<< "Selected cell at row " << ev
.GetRow()
554 << " col " << ev
.GetCol();
555 wxLogMessage( "%s", logBuf
.c_str() );
557 // you must call Skip() if you want the default processing
558 // to occur in wxGrid
562 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
565 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
566 << " col " << ev
.GetLeftCol()
567 << " to row " << ev
.GetBottomRow()
568 << " col " << ev
.GetRightCol();
570 wxLogMessage( "%s", logBuf
.c_str() );
575 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
578 logBuf
<< "Value changed for cell at"
579 << " row " << ev
.GetRow()
580 << " col " << ev
.GetCol();
582 wxLogMessage( "%s", logBuf
.c_str() );
587 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
589 wxLogMessage( "Cell editor shown." );
594 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
596 wxLogMessage( "Cell editor hidden." );
601 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
603 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
605 "mbedward@ozemail.com.au \n\n",
611 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
616 void GridFrame::OnBugsTable(wxCommandEvent
& )
618 BugsGridFrame
*frame
= new BugsGridFrame
;
622 void GridFrame::OnVTable(wxCommandEvent
& )
624 static long s_sizeGrid
= 10000;
626 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
628 "wxGridDemo question",
631 if ( s_sizeGrid
!= -1 )
633 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
638 // ----------------------------------------------------------------------------
639 // MyGridCellRenderer
640 // ----------------------------------------------------------------------------
642 // do something that the default renderer doesn't here just to show that it is
643 // possible to alter the appearance of the cell beyond what the attributes
645 void MyGridCellRenderer::Draw(wxGrid
& grid
,
646 wxGridCellAttr
& attr
,
652 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
654 dc
.SetPen(*wxGREEN_PEN
);
655 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
656 dc
.DrawEllipse(rect
);
660 // ----------------------------------------------------------------------------
661 // BigGridFrame and BigGridTable: Sample of a non-standard table
662 // ----------------------------------------------------------------------------
664 BigGridFrame::BigGridFrame(long sizeGrid
)
665 : wxFrame(NULL
, -1, "Plugin Virtual Table",
666 wxDefaultPosition
, wxSize(500, 450))
668 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
669 m_table
= new BigGridTable(sizeGrid
);
670 m_grid
->SetTable(m_table
, TRUE
);
673 // ----------------------------------------------------------------------------
674 // BugsGridFrame: a "realistic" table
675 // ----------------------------------------------------------------------------
677 BugsGridFrame::BugsGridFrame()
678 : wxFrame(NULL
, -1, "Bugs table",
679 wxDefaultPosition
, wxSize(500, 300))
690 static const wxChar
* severities
[] =
699 static const struct GridData
702 const wxChar
*summary
;
705 const wxChar
*platform
;
709 { 18, _T("foo doesn't work"), Major
, 1, _T("wxMSW"), TRUE
},
710 { 27, _T("bar crashes"), Critical
, 1, _T("all"), FALSE
},
711 { 45, _T("printing is slow"), Minor
, 3, _T("wxMSW"), TRUE
},
712 { 68, _T("Rectangle() fails"), Normal
, 1, _T("wxMSW"), FALSE
},
715 static const wxChar
*headers
[] =
725 // TODO the correct data type must be used for each column
727 wxGrid
*grid
= new wxGrid(this, -1);
728 wxGridTableBase
*table
=
729 new wxGridStringTable(WXSIZEOF(data
), WXSIZEOF(headers
));
730 for ( size_t row
= 0; row
< WXSIZEOF(data
); row
++ )
732 const GridData
& gd
= data
[row
];
733 table
->SetValue(row
, 0, wxString::Format("%d", gd
.id
));
734 table
->SetValue(row
, 1, gd
.summary
);
735 table
->SetValue(row
, 2, severities
[gd
.severity
]);
736 table
->SetValue(row
, 3, wxString::Format("%d", gd
.prio
));
737 table
->SetValue(row
, 4, gd
.platform
);
738 table
->SetValue(row
, 5, gd
.opened
? _T("True") : wxEmptyString
);
741 for ( size_t col
= 0; col
< WXSIZEOF(headers
); col
++ )
743 table
->SetColLabelValue(col
, headers
[col
]);
746 grid
->SetTable(table
, TRUE
);