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_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
74 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
75 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
76 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
77 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
78 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
79 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
80 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
81 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
82 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
83 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
84 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
86 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
87 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
89 EVT_MENU( ID_ABOUT
, GridFrame::About
)
90 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
91 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
92 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
94 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
95 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
96 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
97 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
98 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
99 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
100 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
102 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
103 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
107 GridFrame::GridFrame()
108 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
112 int gridW
= 600, gridH
= 300;
113 int logW
= gridW
, logH
= 100;
115 wxMenu
*fileMenu
= new wxMenu
;
116 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
117 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
118 fileMenu
->AppendSeparator();
119 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
121 wxMenu
*viewMenu
= new wxMenu
;
122 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
123 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
124 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
125 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
126 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
128 wxMenu
*rowLabelMenu
= new wxMenu
;
130 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
132 "Change alignment of row labels" );
134 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
135 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
137 wxMenu
*colLabelMenu
= new wxMenu
;
139 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
141 "Change alignment of col labels" );
143 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
144 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
146 wxMenu
*colMenu
= new wxMenu
;
147 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
148 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
149 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
150 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
151 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
153 wxMenu
*editMenu
= new wxMenu
;
154 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
155 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
156 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
157 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
158 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
160 wxMenu
*helpMenu
= new wxMenu
;
161 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
163 wxMenuBar
*menuBar
= new wxMenuBar
;
164 menuBar
->Append( fileMenu
, "&File" );
165 menuBar
->Append( viewMenu
, "&View" );
166 menuBar
->Append( colMenu
, "&Colours" );
167 menuBar
->Append( editMenu
, "&Edit" );
168 menuBar
->Append( helpMenu
, "&Help" );
170 SetMenuBar( menuBar
);
172 grid
= new wxGrid( this,
175 wxSize( 400, 300 ) );
177 logWin
= new wxTextCtrl( this,
180 wxPoint( 0, gridH
+ 20 ),
181 wxSize( logW
, logH
),
184 logger
= new wxLogTextCtrl( logWin
);
185 logger
->SetActiveTarget( logger
);
186 logger
->SetTimestamp( NULL
);
188 // this will create a grid and, by default, an associated grid
189 // table for string data
191 //grid->CreateGrid( 100, 100 );
192 grid
->SetTable(new SimpleTable(100, 100), TRUE
);
194 grid
->SetRowSize( 0, 60 );
195 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
197 grid
->SetCellValue( 0, 1, "Blah" );
198 grid
->SetCellValue( 0, 2, "Blah" );
199 grid
->SetCellValue( 0, 3, "Read only" );
200 grid
->SetReadOnly( 0, 3 );
202 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
204 grid
->SetRowSize( 99, 60 );
205 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
207 grid
->SetCellValue(2, 2, "red");
209 grid
->SetCellTextColour(2, 2, *wxRED
);
210 grid
->SetCellValue(3, 3, "green on grey");
211 grid
->SetCellTextColour(3, 3, *wxGREEN
);
212 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
214 grid
->SetCellValue(4, 4, "a weird looking cell");
215 grid
->SetCellAlignment(4, 4, wxCENTRE
, wxCENTRE
);
216 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
218 grid
->SetCellValue(3, 0, "1");
219 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
220 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
222 wxGridCellAttr
*attr
;
223 attr
= new wxGridCellAttr
;
224 attr
->SetTextColour(*wxBLUE
);
225 grid
->SetColAttr(5, attr
);
226 attr
= new wxGridCellAttr
;
227 attr
->SetBackgroundColour(*wxBLUE
);
228 grid
->SetRowAttr(5, attr
);
230 grid
->SetCellValue(2, 4, "a wider column");
231 grid
->SetColSize(4, 120);
232 grid
->SetColMinimalWidth(4, 120);
234 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
239 topSizer
->Add( logWin
,
243 SetAutoLayout( TRUE
);
244 SetSizer( topSizer
);
246 topSizer
->Fit( this );
247 topSizer
->SetSizeHints( this );
254 GridFrame::~GridFrame()
259 void GridFrame::SetDefaults()
261 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
262 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
263 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
264 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
265 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
269 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
271 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
273 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
277 grid
->SetRowLabelSize( 0 );
282 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
284 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
286 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
290 grid
->SetColLabelSize( 0 );
295 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
298 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
302 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
304 grid
->EnableDragRowSize(
305 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
309 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
311 grid
->EnableDragColSize(
312 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
316 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
318 wxColourDialog
dlg( NULL
);
319 if ( dlg
.ShowModal() == wxID_OK
)
321 wxColourData retData
;
322 retData
= dlg
.GetColourData();
323 wxColour colour
= retData
.GetColour();
325 grid
->SetLabelBackgroundColour( colour
);
330 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
332 wxColourDialog
dlg( NULL
);
333 if ( dlg
.ShowModal() == wxID_OK
)
335 wxColourData retData
;
336 retData
= dlg
.GetColourData();
337 wxColour colour
= retData
.GetColour();
339 grid
->SetLabelTextColour( colour
);
344 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
347 grid
->GetRowLabelAlignment( &horiz
, &vert
);
364 grid
->SetRowLabelAlignment( horiz
, -1 );
367 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
370 grid
->GetRowLabelAlignment( &horiz
, &vert
);
387 grid
->SetRowLabelAlignment( -1, vert
);
391 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
394 grid
->GetColLabelAlignment( &horiz
, &vert
);
411 grid
->SetColLabelAlignment( horiz
, -1 );
415 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
418 grid
->GetColLabelAlignment( &horiz
, &vert
);
435 grid
->SetColLabelAlignment( -1, vert
);
439 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
441 wxColourDialog
dlg( NULL
);
442 if ( dlg
.ShowModal() == wxID_OK
)
444 wxColourData retData
;
445 retData
= dlg
.GetColourData();
446 wxColour colour
= retData
.GetColour();
448 grid
->SetGridLineColour( colour
);
453 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
455 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
459 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
461 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
465 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
467 if ( grid
->IsSelection() )
469 int topRow
, bottomRow
, leftCol
, rightCol
;
470 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
471 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
476 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
478 if ( grid
->IsSelection() )
480 int topRow
, bottomRow
, leftCol
, rightCol
;
481 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
482 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
487 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
492 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
494 wxColour col
= wxGetColourFromUser(this);
497 grid
->SetDefaultCellTextColour(col
);
502 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
504 wxColour col
= wxGetColourFromUser(this);
507 grid
->SetDefaultCellBackgroundColour(col
);
512 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
515 if ( ev
.GetRow() != -1 )
517 logBuf
<< "Left click on row label " << ev
.GetRow();
519 else if ( ev
.GetCol() != -1 )
521 logBuf
<< "Left click on col label " << ev
.GetCol();
525 logBuf
<< "Left click on corner label";
528 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
529 wxLogMessage( "%s", logBuf
.c_str() );
531 // you must call event skip if you want default grid processing
537 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
540 logBuf
<< "Left click at row " << ev
.GetRow()
541 << " col " << ev
.GetCol();
542 wxLogMessage( "%s", logBuf
.c_str() );
544 // you must call event skip if you want default grid processing
545 // (cell highlighting etc.)
551 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
554 logBuf
<< "Resized row " << ev
.GetRowOrCol();
555 wxLogMessage( "%s", logBuf
.c_str() );
561 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
564 logBuf
<< "Resized col " << ev
.GetRowOrCol();
565 wxLogMessage( "%s", logBuf
.c_str() );
571 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
574 logBuf
<< "Selected cell at row " << ev
.GetRow()
575 << " col " << ev
.GetCol();
576 wxLogMessage( "%s", logBuf
.c_str() );
578 // you must call Skip() if you want the default processing
579 // to occur in wxGrid
583 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
586 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
587 << " col " << ev
.GetLeftCol()
588 << " to row " << ev
.GetBottomRow()
589 << " col " << ev
.GetRightCol();
591 wxLogMessage( "%s", logBuf
.c_str() );
596 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
599 logBuf
<< "Value changed for cell at"
600 << " row " << ev
.GetRow()
601 << " col " << ev
.GetCol();
603 wxLogMessage( "%s", logBuf
.c_str() );
608 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
610 wxLogMessage( "Cell editor shown." );
615 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
617 wxLogMessage( "Cell editor hidden." );
622 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
624 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
626 "mbedward@ozemail.com.au \n\n",
632 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
637 void GridFrame::OnBugsTable(wxCommandEvent
& )
639 BugsGridFrame
*frame
= new BugsGridFrame
;
643 void GridFrame::OnVTable(wxCommandEvent
& )
645 static long s_sizeGrid
= 10000;
647 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
649 "wxGridDemo question",
652 if ( s_sizeGrid
!= -1 )
654 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
659 // ----------------------------------------------------------------------------
660 // MyGridCellRenderer
661 // ----------------------------------------------------------------------------
663 // do something that the default renderer doesn't here just to show that it is
664 // possible to alter the appearance of the cell beyond what the attributes
666 void MyGridCellRenderer::Draw(wxGrid
& grid
,
667 wxGridCellAttr
& attr
,
673 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
675 dc
.SetPen(*wxGREEN_PEN
);
676 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
677 dc
.DrawEllipse(rect
);
681 // ----------------------------------------------------------------------------
682 // BigGridFrame and BigGridTable: Sample of a non-standard table
683 // ----------------------------------------------------------------------------
685 BigGridFrame::BigGridFrame(long sizeGrid
)
686 : wxFrame(NULL
, -1, "Plugin Virtual Table",
687 wxDefaultPosition
, wxSize(500, 450))
689 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
690 m_table
= new BigGridTable(sizeGrid
);
691 m_grid
->SetTable(m_table
, TRUE
);
694 // ----------------------------------------------------------------------------
695 // BugsGridFrame: a "realistic" table
696 // ----------------------------------------------------------------------------
698 BugsGridFrame::BugsGridFrame()
699 : wxFrame(NULL
, -1, "Bugs table",
700 wxDefaultPosition
, wxSize(500, 300))
711 static const wxChar
* severities
[] =
720 static const struct GridData
723 const wxChar
*summary
;
726 const wxChar
*platform
;
730 { 18, _T("foo doesn't work"), Major
, 1, _T("wxMSW"), TRUE
},
731 { 27, _T("bar crashes"), Critical
, 1, _T("all"), FALSE
},
732 { 45, _T("printing is slow"), Minor
, 3, _T("wxMSW"), TRUE
},
733 { 68, _T("Rectangle() fails"), Normal
, 1, _T("wxMSW"), FALSE
},
736 static const wxChar
*headers
[] =
746 // TODO the correct data type must be used for each column
748 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
749 wxGridTableBase
*table
=
750 new wxGridStringTable(WXSIZEOF(data
), WXSIZEOF(headers
));
751 for ( size_t row
= 0; row
< WXSIZEOF(data
); row
++ )
753 const GridData
& gd
= data
[row
];
754 table
->SetValue(row
, 0, wxString::Format("%d", gd
.id
));
755 table
->SetValue(row
, 1, gd
.summary
);
756 table
->SetValue(row
, 2, severities
[gd
.severity
]);
757 table
->SetValue(row
, 3, wxString::Format("%d", gd
.prio
));
758 table
->SetValue(row
, 4, gd
.platform
);
759 table
->SetValue(row
, 5, gd
.opened
? _T("True") : wxEmptyString
);
762 for ( size_t col
= 0; col
< WXSIZEOF(headers
); col
++ )
764 table
->SetColLabelValue(col
, headers
[col
]);
767 grid
->SetTable(table
, TRUE
);