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
)
91 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
92 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
93 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
94 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
95 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
96 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
97 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
99 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
100 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
104 GridFrame::GridFrame()
105 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
109 int gridW
= 600, gridH
= 300;
110 int logW
= gridW
, logH
= 100;
112 wxMenu
*fileMenu
= new wxMenu
;
113 fileMenu
->Append( ID_VTABLE
, "&Virtual table test");
114 fileMenu
->AppendSeparator();
115 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
117 wxMenu
*viewMenu
= new wxMenu
;
118 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
119 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
120 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
122 wxMenu
*rowLabelMenu
= new wxMenu
;
124 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
126 "Change alignment of row labels" );
128 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
129 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
131 wxMenu
*colLabelMenu
= new wxMenu
;
133 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
135 "Change alignment of col labels" );
137 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
138 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
140 wxMenu
*colMenu
= new wxMenu
;
141 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
142 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
143 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
144 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
145 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
147 wxMenu
*editMenu
= new wxMenu
;
148 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
149 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
150 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
151 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
152 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
154 wxMenu
*helpMenu
= new wxMenu
;
155 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
157 wxMenuBar
*menuBar
= new wxMenuBar
;
158 menuBar
->Append( fileMenu
, "&File" );
159 menuBar
->Append( viewMenu
, "&View" );
160 menuBar
->Append( colMenu
, "&Colours" );
161 menuBar
->Append( editMenu
, "&Edit" );
162 menuBar
->Append( helpMenu
, "&Help" );
164 SetMenuBar( menuBar
);
166 grid
= new wxGrid( this,
169 wxSize( 400, 300 ) );
171 logWin
= new wxTextCtrl( this,
174 wxPoint( 0, gridH
+ 20 ),
175 wxSize( logW
, logH
),
178 logger
= new wxLogTextCtrl( logWin
);
179 logger
->SetActiveTarget( logger
);
180 logger
->SetTimestamp( NULL
);
182 // this will create a grid and, by default, an associated grid
183 // table for string data
185 grid
->CreateGrid( 100, 100 );
187 grid
->SetRowSize( 0, 60 );
188 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
190 grid
->SetCellValue( 0, 1, "Blah" );
191 grid
->SetCellValue( 0, 2, "Blah" );
192 grid
->SetCellValue( 0, 3, "Read only" );
193 grid
->SetReadOnly( 0, 3 );
195 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
197 grid
->SetRowSize( 99, 60 );
198 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
200 grid
->SetCellValue(2, 2, "red");
202 grid
->SetCellTextColour(2, 2, *wxRED
);
203 grid
->SetCellValue(3, 3, "green on grey");
204 grid
->SetCellTextColour(3, 3, *wxGREEN
);
205 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
207 grid
->SetCellValue(4, 4, "a weird looking cell");
208 grid
->SetCellAlignment(4, 4, wxCENTRE
, wxCENTRE
);
209 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
211 grid
->SetCellValue(3, 0, "1");
212 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
213 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
215 wxGridCellAttr
*attr
;
216 attr
= new wxGridCellAttr
;
217 attr
->SetTextColour(*wxBLUE
);
218 grid
->SetColAttr(5, attr
);
219 attr
= new wxGridCellAttr
;
220 attr
->SetBackgroundColour(*wxBLUE
);
221 grid
->SetRowAttr(5, attr
);
223 // VZ: cell borders don't look nice otherwise :-) (for now...)
224 grid
->SetDefaultCellBackgroundColour(wxColour(200, 200, 180));
226 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
231 topSizer
->Add( logWin
,
235 SetAutoLayout( TRUE
);
236 SetSizer( topSizer
);
238 topSizer
->Fit( this );
239 topSizer
->SetSizeHints( this );
246 GridFrame::~GridFrame()
251 void GridFrame::SetDefaults()
253 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
254 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
255 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
259 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
261 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
263 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
267 grid
->SetRowLabelSize( 0 );
272 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
274 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
276 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
280 grid
->SetColLabelSize( 0 );
285 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
288 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
292 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
294 wxColourDialog
dlg( NULL
);
295 if ( dlg
.ShowModal() == wxID_OK
)
297 wxColourData retData
;
298 retData
= dlg
.GetColourData();
299 wxColour colour
= retData
.GetColour();
301 grid
->SetLabelBackgroundColour( colour
);
306 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
308 wxColourDialog
dlg( NULL
);
309 if ( dlg
.ShowModal() == wxID_OK
)
311 wxColourData retData
;
312 retData
= dlg
.GetColourData();
313 wxColour colour
= retData
.GetColour();
315 grid
->SetLabelTextColour( colour
);
320 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
323 grid
->GetRowLabelAlignment( &horiz
, &vert
);
340 grid
->SetRowLabelAlignment( horiz
, -1 );
343 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
346 grid
->GetRowLabelAlignment( &horiz
, &vert
);
363 grid
->SetRowLabelAlignment( -1, vert
);
367 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
370 grid
->GetColLabelAlignment( &horiz
, &vert
);
387 grid
->SetColLabelAlignment( horiz
, -1 );
391 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
394 grid
->GetColLabelAlignment( &horiz
, &vert
);
411 grid
->SetColLabelAlignment( -1, vert
);
415 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
417 wxColourDialog
dlg( NULL
);
418 if ( dlg
.ShowModal() == wxID_OK
)
420 wxColourData retData
;
421 retData
= dlg
.GetColourData();
422 wxColour colour
= retData
.GetColour();
424 grid
->SetGridLineColour( colour
);
429 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
431 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
435 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
437 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
441 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
443 if ( grid
->IsSelection() )
445 int topRow
, bottomRow
, leftCol
, rightCol
;
446 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
447 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
452 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
454 if ( grid
->IsSelection() )
456 int topRow
, bottomRow
, leftCol
, rightCol
;
457 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
458 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
463 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
468 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
470 wxColour col
= wxGetColourFromUser(this);
473 grid
->SetDefaultCellTextColour(col
);
478 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
480 wxColour col
= wxGetColourFromUser(this);
483 grid
->SetDefaultCellBackgroundColour(col
);
488 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
491 if ( ev
.GetRow() != -1 )
493 logBuf
<< "Left click on row label " << ev
.GetRow();
495 else if ( ev
.GetCol() != -1 )
497 logBuf
<< "Left click on col label " << ev
.GetCol();
501 logBuf
<< "Left click on corner label";
504 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
505 wxLogMessage( "%s", logBuf
.c_str() );
507 // you must call event skip if you want default grid processing
513 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
516 logBuf
<< "Left click at row " << ev
.GetRow()
517 << " col " << ev
.GetCol();
518 wxLogMessage( "%s", logBuf
.c_str() );
520 // you must call event skip if you want default grid processing
521 // (cell highlighting etc.)
527 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
530 logBuf
<< "Resized row " << ev
.GetRowOrCol();
531 wxLogMessage( "%s", logBuf
.c_str() );
537 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
540 logBuf
<< "Resized col " << ev
.GetRowOrCol();
541 wxLogMessage( "%s", logBuf
.c_str() );
547 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
550 logBuf
<< "Selected cell at row " << ev
.GetRow()
551 << " col " << ev
.GetCol();
552 wxLogMessage( "%s", logBuf
.c_str() );
554 // you must call Skip() if you want the default processing
555 // to occur in wxGrid
559 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
562 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
563 << " col " << ev
.GetLeftCol()
564 << " to row " << ev
.GetBottomRow()
565 << " col " << ev
.GetRightCol();
567 wxLogMessage( "%s", logBuf
.c_str() );
572 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
575 logBuf
<< "Value changed for cell at"
576 << " row " << ev
.GetRow()
577 << " col " << ev
.GetCol();
579 wxLogMessage( "%s", logBuf
.c_str() );
584 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
586 wxLogMessage( "Cell editor shown." );
591 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
593 wxLogMessage( "Cell editor hidden." );
598 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
600 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
602 "mbedward@ozemail.com.au \n\n",
608 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
613 void GridFrame::OnVTable(wxCommandEvent
& )
615 BigGridFrame
* win
= new BigGridFrame();
619 // ----------------------------------------------------------------------------
620 // MyGridCellRenderer
621 // ----------------------------------------------------------------------------
623 // do something that the default renderer doesn't here just to show that it is
624 // possible to alter the appearance of the cell beyond what the attributes
626 void MyGridCellRenderer::Draw(wxGrid
& grid
,
627 wxGridCellAttr
& attr
,
633 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
635 dc
.SetPen(*wxGREEN_PEN
);
636 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
637 dc
.DrawEllipse(rect
);
641 // ----------------------------------------------------------------------------
642 // BigGridFrame and BigGridTable: Sample of a non-standard table
643 // ----------------------------------------------------------------------------
645 BigGridFrame::BigGridFrame()
646 : wxFrame(NULL
, -1, "Plugin Virtual Table", wxDefaultPosition
,
649 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
650 m_table
= new BigGridTable
;
651 m_grid
->SetTable(m_table
, TRUE
);