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 );
188 grid
->SetTable(new SimpleTable(100, 100), TRUE
);
190 // VZ: cell borders don't look nice otherwise :-) (for now...)
191 grid
->SetDefaultCellBackgroundColour(wxColour(200, 200, 180));
193 grid
->SetRowSize( 0, 60 );
194 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
196 grid
->SetCellValue( 0, 1, "Blah" );
197 grid
->SetCellValue( 0, 2, "Blah" );
198 grid
->SetCellValue( 0, 3, "Read only" );
199 grid
->SetReadOnly( 0, 3 );
201 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
203 grid
->SetRowSize( 99, 60 );
204 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
206 grid
->SetCellValue(2, 2, "red");
208 grid
->SetCellTextColour(2, 2, *wxRED
);
209 grid
->SetCellValue(3, 3, "green on grey");
210 grid
->SetCellTextColour(3, 3, *wxGREEN
);
211 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
213 grid
->SetCellValue(4, 4, "a weird looking cell");
214 grid
->SetCellAlignment(4, 4, wxCENTRE
, wxCENTRE
);
215 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
217 grid
->SetCellValue(3, 0, "1");
218 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
219 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
221 wxGridCellAttr
*attr
;
222 attr
= new wxGridCellAttr
;
223 attr
->SetTextColour(*wxBLUE
);
224 grid
->SetColAttr(5, attr
);
225 attr
= new wxGridCellAttr
;
226 attr
->SetBackgroundColour(*wxBLUE
);
227 grid
->SetRowAttr(5, attr
);
229 grid
->SetCellValue(2, 4, "a wider column");
230 grid
->SetColSize(4, 120);
231 grid
->SetColMinimalWidth(4, 120);
233 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
238 topSizer
->Add( logWin
,
242 SetAutoLayout( TRUE
);
243 SetSizer( topSizer
);
245 topSizer
->Fit( this );
246 topSizer
->SetSizeHints( this );
253 GridFrame::~GridFrame()
258 void GridFrame::SetDefaults()
260 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
261 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
262 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
266 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
268 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
270 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
274 grid
->SetRowLabelSize( 0 );
279 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
281 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
283 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
287 grid
->SetColLabelSize( 0 );
292 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
295 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
299 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
301 wxColourDialog
dlg( NULL
);
302 if ( dlg
.ShowModal() == wxID_OK
)
304 wxColourData retData
;
305 retData
= dlg
.GetColourData();
306 wxColour colour
= retData
.GetColour();
308 grid
->SetLabelBackgroundColour( colour
);
313 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
315 wxColourDialog
dlg( NULL
);
316 if ( dlg
.ShowModal() == wxID_OK
)
318 wxColourData retData
;
319 retData
= dlg
.GetColourData();
320 wxColour colour
= retData
.GetColour();
322 grid
->SetLabelTextColour( colour
);
327 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
330 grid
->GetRowLabelAlignment( &horiz
, &vert
);
347 grid
->SetRowLabelAlignment( horiz
, -1 );
350 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
353 grid
->GetRowLabelAlignment( &horiz
, &vert
);
370 grid
->SetRowLabelAlignment( -1, vert
);
374 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
377 grid
->GetColLabelAlignment( &horiz
, &vert
);
394 grid
->SetColLabelAlignment( horiz
, -1 );
398 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
401 grid
->GetColLabelAlignment( &horiz
, &vert
);
418 grid
->SetColLabelAlignment( -1, vert
);
422 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
424 wxColourDialog
dlg( NULL
);
425 if ( dlg
.ShowModal() == wxID_OK
)
427 wxColourData retData
;
428 retData
= dlg
.GetColourData();
429 wxColour colour
= retData
.GetColour();
431 grid
->SetGridLineColour( colour
);
436 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
438 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
442 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
444 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
448 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
450 if ( grid
->IsSelection() )
452 int topRow
, bottomRow
, leftCol
, rightCol
;
453 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
454 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
459 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
461 if ( grid
->IsSelection() )
463 int topRow
, bottomRow
, leftCol
, rightCol
;
464 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
465 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
470 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
475 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
477 wxColour col
= wxGetColourFromUser(this);
480 grid
->SetDefaultCellTextColour(col
);
485 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
487 wxColour col
= wxGetColourFromUser(this);
490 grid
->SetDefaultCellBackgroundColour(col
);
495 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
498 if ( ev
.GetRow() != -1 )
500 logBuf
<< "Left click on row label " << ev
.GetRow();
502 else if ( ev
.GetCol() != -1 )
504 logBuf
<< "Left click on col label " << ev
.GetCol();
508 logBuf
<< "Left click on corner label";
511 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
512 wxLogMessage( "%s", logBuf
.c_str() );
514 // you must call event skip if you want default grid processing
520 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
523 logBuf
<< "Left click at row " << ev
.GetRow()
524 << " col " << ev
.GetCol();
525 wxLogMessage( "%s", logBuf
.c_str() );
527 // you must call event skip if you want default grid processing
528 // (cell highlighting etc.)
534 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
537 logBuf
<< "Resized row " << ev
.GetRowOrCol();
538 wxLogMessage( "%s", logBuf
.c_str() );
544 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
547 logBuf
<< "Resized col " << ev
.GetRowOrCol();
548 wxLogMessage( "%s", logBuf
.c_str() );
554 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
557 logBuf
<< "Selected cell at row " << ev
.GetRow()
558 << " col " << ev
.GetCol();
559 wxLogMessage( "%s", logBuf
.c_str() );
561 // you must call Skip() if you want the default processing
562 // to occur in wxGrid
566 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
569 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
570 << " col " << ev
.GetLeftCol()
571 << " to row " << ev
.GetBottomRow()
572 << " col " << ev
.GetRightCol();
574 wxLogMessage( "%s", logBuf
.c_str() );
579 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
582 logBuf
<< "Value changed for cell at"
583 << " row " << ev
.GetRow()
584 << " col " << ev
.GetCol();
586 wxLogMessage( "%s", logBuf
.c_str() );
591 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
593 wxLogMessage( "Cell editor shown." );
598 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
600 wxLogMessage( "Cell editor hidden." );
605 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
607 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
609 "mbedward@ozemail.com.au \n\n",
615 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
620 void GridFrame::OnBugsTable(wxCommandEvent
& )
622 BugsGridFrame
*frame
= new BugsGridFrame
;
626 void GridFrame::OnVTable(wxCommandEvent
& )
628 static long s_sizeGrid
= 10000;
630 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
632 "wxGridDemo question",
635 if ( s_sizeGrid
!= -1 )
637 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
642 // ----------------------------------------------------------------------------
643 // MyGridCellRenderer
644 // ----------------------------------------------------------------------------
646 // do something that the default renderer doesn't here just to show that it is
647 // possible to alter the appearance of the cell beyond what the attributes
649 void MyGridCellRenderer::Draw(wxGrid
& grid
,
650 wxGridCellAttr
& attr
,
656 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
658 dc
.SetPen(*wxGREEN_PEN
);
659 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
660 dc
.DrawEllipse(rect
);
664 // ----------------------------------------------------------------------------
665 // BigGridFrame and BigGridTable: Sample of a non-standard table
666 // ----------------------------------------------------------------------------
668 BigGridFrame::BigGridFrame(long sizeGrid
)
669 : wxFrame(NULL
, -1, "Plugin Virtual Table",
670 wxDefaultPosition
, wxSize(500, 450))
672 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
673 m_table
= new BigGridTable(sizeGrid
);
674 m_grid
->SetTable(m_table
, TRUE
);
677 // ----------------------------------------------------------------------------
678 // BugsGridFrame: a "realistic" table
679 // ----------------------------------------------------------------------------
681 BugsGridFrame::BugsGridFrame()
682 : wxFrame(NULL
, -1, "Bugs table",
683 wxDefaultPosition
, wxSize(500, 300))
694 static const wxChar
* severities
[] =
703 static const struct GridData
706 const wxChar
*summary
;
709 const wxChar
*platform
;
713 { 18, _T("foo doesn't work"), Major
, 1, _T("wxMSW"), TRUE
},
714 { 27, _T("bar crashes"), Critical
, 1, _T("all"), FALSE
},
715 { 45, _T("printing is slow"), Minor
, 3, _T("wxMSW"), TRUE
},
716 { 68, _T("Rectangle() fails"), Normal
, 1, _T("wxMSW"), FALSE
},
719 static const wxChar
*headers
[] =
729 // TODO the correct data type must be used for each column
731 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
732 wxGridTableBase
*table
=
733 new wxGridStringTable(WXSIZEOF(data
), WXSIZEOF(headers
));
734 for ( size_t row
= 0; row
< WXSIZEOF(data
); row
++ )
736 const GridData
& gd
= data
[row
];
737 table
->SetValue(row
, 0, wxString::Format("%d", gd
.id
));
738 table
->SetValue(row
, 1, gd
.summary
);
739 table
->SetValue(row
, 2, severities
[gd
.severity
]);
740 table
->SetValue(row
, 3, wxString::Format("%d", gd
.prio
));
741 table
->SetValue(row
, 4, gd
.platform
);
742 table
->SetValue(row
, 5, gd
.opened
? _T("True") : wxEmptyString
);
745 for ( size_t col
= 0; col
< WXSIZEOF(headers
); col
++ )
747 table
->SetColLabelValue(col
, headers
[col
]);
750 grid
->SetTable(table
, TRUE
);