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
)
101 GridFrame::GridFrame()
102 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
106 int gridW
= 600, gridH
= 300;
107 int logW
= gridW
, logH
= 100;
109 wxMenu
*fileMenu
= new wxMenu
;
110 fileMenu
->Append( ID_VTABLE
, "&Virtual table test");
111 fileMenu
->AppendSeparator();
112 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
114 wxMenu
*viewMenu
= new wxMenu
;
115 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
116 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
117 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
119 wxMenu
*rowLabelMenu
= new wxMenu
;
121 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
123 "Change alignment of row labels" );
125 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
126 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
128 wxMenu
*colLabelMenu
= new wxMenu
;
130 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
132 "Change alignment of col labels" );
134 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
135 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
137 wxMenu
*colMenu
= new wxMenu
;
138 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
139 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
140 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
141 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
142 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
144 wxMenu
*editMenu
= new wxMenu
;
145 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
146 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
147 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
148 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
149 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
151 wxMenu
*helpMenu
= new wxMenu
;
152 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
154 wxMenuBar
*menuBar
= new wxMenuBar
;
155 menuBar
->Append( fileMenu
, "&File" );
156 menuBar
->Append( viewMenu
, "&View" );
157 menuBar
->Append( colMenu
, "&Colours" );
158 menuBar
->Append( editMenu
, "&Edit" );
159 menuBar
->Append( helpMenu
, "&Help" );
161 SetMenuBar( menuBar
);
163 grid
= new wxGrid( this,
166 wxSize( 400, 300 ) );
168 logWin
= new wxTextCtrl( this,
171 wxPoint( 0, gridH
+ 20 ),
172 wxSize( logW
, logH
),
175 logger
= new wxLogTextCtrl( logWin
);
176 logger
->SetActiveTarget( logger
);
177 logger
->SetTimestamp( NULL
);
179 // this will create a grid and, by default, an associated grid
180 // table for string data
182 grid
->CreateGrid( 100, 100 );
184 grid
->SetRowSize( 0, 60 );
185 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
187 grid
->SetCellValue( 0, 1, "Blah" );
188 grid
->SetCellValue( 0, 2, "Blah" );
189 grid
->SetCellValue( 0, 3, "Read only" );
190 grid
->SetReadOnly( 0, 3 );
192 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
194 grid
->SetRowSize( 99, 60 );
195 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
197 grid
->SetCellValue(2, 2, "red");
199 grid
->SetCellTextColour(2, 2, *wxRED
);
200 grid
->SetCellValue(3, 3, "green on grey");
201 grid
->SetCellTextColour(3, 3, *wxGREEN
);
202 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
204 grid
->SetCellValue(4, 4, "a weird looking cell");
205 grid
->SetCellAlignment(4, 4, wxCENTRE
, wxCENTRE
);
206 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
208 wxGridCellAttr
*attr
;
209 attr
= new wxGridCellAttr
;
210 attr
->SetTextColour(*wxBLUE
);
211 grid
->SetColAttr(5, attr
);
212 attr
= new wxGridCellAttr
;
213 attr
->SetBackgroundColour(*wxBLUE
);
214 grid
->SetRowAttr(5, attr
);
216 // VZ: cell borders don't look nice otherwise :-) (for now...)
217 grid
->SetDefaultCellBackgroundColour(wxColour(200, 200, 180));
219 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
224 topSizer
->Add( logWin
,
228 SetAutoLayout( TRUE
);
229 SetSizer( topSizer
);
231 topSizer
->Fit( this );
232 topSizer
->SetSizeHints( this );
239 GridFrame::~GridFrame()
244 void GridFrame::SetDefaults()
246 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
247 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
248 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
252 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
254 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
256 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
260 grid
->SetRowLabelSize( 0 );
265 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
267 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
269 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
273 grid
->SetColLabelSize( 0 );
278 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
281 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
285 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
287 wxColourDialog
dlg( NULL
);
288 if ( dlg
.ShowModal() == wxID_OK
)
290 wxColourData retData
;
291 retData
= dlg
.GetColourData();
292 wxColour colour
= retData
.GetColour();
294 grid
->SetLabelBackgroundColour( colour
);
299 void GridFrame::SetLabelTextColour( 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
->SetLabelTextColour( colour
);
313 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
316 grid
->GetRowLabelAlignment( &horiz
, &vert
);
333 grid
->SetRowLabelAlignment( horiz
, -1 );
336 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
339 grid
->GetRowLabelAlignment( &horiz
, &vert
);
356 grid
->SetRowLabelAlignment( -1, vert
);
360 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
363 grid
->GetColLabelAlignment( &horiz
, &vert
);
380 grid
->SetColLabelAlignment( horiz
, -1 );
384 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
387 grid
->GetColLabelAlignment( &horiz
, &vert
);
404 grid
->SetColLabelAlignment( -1, vert
);
408 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
410 wxColourDialog
dlg( NULL
);
411 if ( dlg
.ShowModal() == wxID_OK
)
413 wxColourData retData
;
414 retData
= dlg
.GetColourData();
415 wxColour colour
= retData
.GetColour();
417 grid
->SetGridLineColour( colour
);
422 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
424 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
428 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
430 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
434 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
436 if ( grid
->IsSelection() )
438 int topRow
, bottomRow
, leftCol
, rightCol
;
439 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
440 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
445 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
447 if ( grid
->IsSelection() )
449 int topRow
, bottomRow
, leftCol
, rightCol
;
450 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
451 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
456 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
461 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
463 wxColour col
= wxGetColourFromUser(this);
466 grid
->SetDefaultCellTextColour(col
);
471 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
473 wxColour col
= wxGetColourFromUser(this);
476 grid
->SetDefaultCellBackgroundColour(col
);
481 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
484 if ( ev
.GetRow() != -1 )
486 logBuf
<< "Left click on row label " << ev
.GetRow();
488 else if ( ev
.GetCol() != -1 )
490 logBuf
<< "Left click on col label " << ev
.GetCol();
494 logBuf
<< "Left click on corner label";
497 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
498 wxLogMessage( "%s", logBuf
.c_str() );
500 // you must call event skip if you want default grid processing
506 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
509 logBuf
<< "Left click at row " << ev
.GetRow()
510 << " col " << ev
.GetCol();
511 wxLogMessage( "%s", logBuf
.c_str() );
513 // you must call event skip if you want default grid processing
514 // (cell highlighting etc.)
520 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
523 logBuf
<< "Resized row " << ev
.GetRowOrCol();
524 wxLogMessage( "%s", logBuf
.c_str() );
530 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
533 logBuf
<< "Resized col " << ev
.GetRowOrCol();
534 wxLogMessage( "%s", logBuf
.c_str() );
540 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
543 logBuf
<< "Selected cell at row " << ev
.GetRow()
544 << " col " << ev
.GetCol();
545 wxLogMessage( "%s", logBuf
.c_str() );
547 // you must call Skip() if you want the default processing
548 // to occur in wxGrid
552 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
555 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
556 << " col " << ev
.GetLeftCol()
557 << " to row " << ev
.GetBottomRow()
558 << " col " << ev
.GetRightCol();
560 wxLogMessage( "%s", logBuf
.c_str() );
565 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
568 logBuf
<< "Value changed for cell at"
569 << " row " << ev
.GetRow()
570 << " col " << ev
.GetCol();
572 wxLogMessage( "%s", logBuf
.c_str() );
578 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
580 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
582 "mbedward@ozemail.com.au \n\n",
588 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
593 void GridFrame::OnVTable(wxCommandEvent
& )
595 BigGridFrame
* win
= new BigGridFrame();
599 // ----------------------------------------------------------------------------
600 // MyGridCellRenderer
601 // ----------------------------------------------------------------------------
603 // do something that the default renderer doesn't here just to show that it is
604 // possible to alter the appearance of the cell beyond what the attributes
606 void MyGridCellRenderer::Draw(wxGrid
& grid
,
607 wxGridCellAttr
& attr
,
613 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
615 dc
.SetPen(*wxGREEN_PEN
);
616 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
617 dc
.DrawEllipse(rect
);
621 // ----------------------------------------------------------------------------
622 // BigGridFrame and BigGridTable: Sample of a non-standard table
623 // ----------------------------------------------------------------------------
625 BigGridFrame::BigGridFrame()
626 : wxFrame(NULL
, -1, "Plugin Virtual Table", wxDefaultPosition
,
629 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
630 m_table
= new BigGridTable
;
631 m_grid
->SetTable(m_table
, TRUE
);