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
)
90 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
91 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
92 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
93 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
94 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
95 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
96 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
= 80;
109 wxMenu
*fileMenu
= new wxMenu
;
110 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
112 wxMenu
*viewMenu
= new wxMenu
;
113 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
114 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
115 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
117 wxMenu
*rowLabelMenu
= new wxMenu
;
119 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
121 "Change alignment of row labels" );
123 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
124 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
126 wxMenu
*colLabelMenu
= new wxMenu
;
128 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
130 "Change alignment of col labels" );
132 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
133 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
135 wxMenu
*colMenu
= new wxMenu
;
136 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
137 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
138 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
139 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
140 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
142 wxMenu
*editMenu
= new wxMenu
;
143 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
144 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
145 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
146 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
147 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
149 wxMenu
*helpMenu
= new wxMenu
;
150 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
152 wxMenuBar
*menuBar
= new wxMenuBar
;
153 menuBar
->Append( fileMenu
, "&File" );
154 menuBar
->Append( viewMenu
, "&View" );
155 menuBar
->Append( colMenu
, "&Colours" );
156 menuBar
->Append( editMenu
, "&Edit" );
157 menuBar
->Append( helpMenu
, "&Help" );
159 SetMenuBar( menuBar
);
161 grid
= new wxGrid( this,
164 wxSize( 400, 300 ) );
166 logWin
= new wxTextCtrl( this,
169 wxPoint( 0, gridH
+ 20 ),
170 wxSize( logW
, logH
),
173 logger
= new wxLogTextCtrl( logWin
);
174 logger
->SetActiveTarget( logger
);
175 logger
->SetTimestamp( NULL
);
177 // this will create a grid and, by default, an associated grid
178 // table for string data
180 grid
->CreateGrid( 100, 100 );
182 grid
->SetRowSize( 0, 60 );
183 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
185 grid
->SetCellValue( 0, 1, "Blah" );
186 grid
->SetCellValue( 0, 2, "Blah" );
188 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
190 grid
->SetRowSize( 99, 60 );
191 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
193 grid
->SetCellValue(2, 2, "red");
195 grid
->SetCellTextColour(2, 2, *wxRED
);
196 grid
->SetCellValue(3, 3, "green on grey");
197 grid
->SetCellTextColour(3, 3, *wxGREEN
);
198 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
200 grid
->SetCellValue(4, 4, "a weird looking cell");
201 grid
->SetCellAlignment(4, 4, wxCENTRE
, wxCENTRE
);
202 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
204 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
209 topSizer
->Add( logWin
,
213 SetAutoLayout( TRUE
);
214 SetSizer( topSizer
);
216 topSizer
->Fit( this );
217 topSizer
->SetSizeHints( this );
224 GridFrame::~GridFrame()
229 void GridFrame::SetDefaults()
231 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
232 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
233 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
237 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
239 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
241 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
245 grid
->SetRowLabelSize( 0 );
250 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
252 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
254 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
258 grid
->SetColLabelSize( 0 );
263 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
266 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
270 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
272 wxColourDialog
dlg( NULL
);
273 if ( dlg
.ShowModal() == wxID_OK
)
275 wxColourData retData
;
276 retData
= dlg
.GetColourData();
277 wxColour colour
= retData
.GetColour();
279 grid
->SetLabelBackgroundColour( colour
);
284 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
286 wxColourDialog
dlg( NULL
);
287 if ( dlg
.ShowModal() == wxID_OK
)
289 wxColourData retData
;
290 retData
= dlg
.GetColourData();
291 wxColour colour
= retData
.GetColour();
293 grid
->SetLabelTextColour( colour
);
298 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
301 grid
->GetRowLabelAlignment( &horiz
, &vert
);
318 grid
->SetRowLabelAlignment( horiz
, -1 );
321 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
324 grid
->GetRowLabelAlignment( &horiz
, &vert
);
341 grid
->SetRowLabelAlignment( -1, vert
);
345 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
348 grid
->GetColLabelAlignment( &horiz
, &vert
);
365 grid
->SetColLabelAlignment( horiz
, -1 );
369 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
372 grid
->GetColLabelAlignment( &horiz
, &vert
);
389 grid
->SetColLabelAlignment( -1, vert
);
393 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
395 wxColourDialog
dlg( NULL
);
396 if ( dlg
.ShowModal() == wxID_OK
)
398 wxColourData retData
;
399 retData
= dlg
.GetColourData();
400 wxColour colour
= retData
.GetColour();
402 grid
->SetGridLineColour( colour
);
407 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
409 grid
->InsertRows( 0, 1 );
413 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
415 grid
->InsertCols( 0, 1 );
419 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
421 if ( grid
->IsSelection() )
423 int topRow
, bottomRow
, leftCol
, rightCol
;
424 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
425 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
430 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
432 if ( grid
->IsSelection() )
434 int topRow
, bottomRow
, leftCol
, rightCol
;
435 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
436 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
441 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
446 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
448 wxColour col
= wxGetColourFromUser(this);
450 grid
->SetDefaultCellTextColour(col
);
453 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
455 wxColour col
= wxGetColourFromUser(this);
457 grid
->SetDefaultCellBackgroundColour(col
);
460 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
463 if ( ev
.GetRow() != -1 )
465 logBuf
<< "Left click on row label " << ev
.GetRow();
467 else if ( ev
.GetCol() != -1 )
469 logBuf
<< "Left click on col label " << ev
.GetCol();
473 logBuf
<< "Left click on corner label";
476 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
477 wxLogMessage( "%s", logBuf
.c_str() );
479 // you must call event skip if you want default grid processing
485 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
488 logBuf
<< "Left click at row " << ev
.GetRow()
489 << " col " << ev
.GetCol();
490 wxLogMessage( "%s", logBuf
.c_str() );
492 // you must call event skip if you want default grid processing
493 // (cell highlighting etc.)
499 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
502 logBuf
<< "Resized row " << ev
.GetRowOrCol();
503 wxLogMessage( "%s", logBuf
.c_str() );
509 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
512 logBuf
<< "Resized col " << ev
.GetRowOrCol();
513 wxLogMessage( "%s", logBuf
.c_str() );
519 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
522 logBuf
<< "Selected cell at row " << ev
.GetRow()
523 << " col " << ev
.GetCol();
524 wxLogMessage( "%s", logBuf
.c_str() );
526 // you must call Skip() if you want the default processing
527 // to occur in wxGrid
531 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
534 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
535 << " col " << ev
.GetLeftCol()
536 << " to row " << ev
.GetBottomRow()
537 << " col " << ev
.GetRightCol();
539 wxLogMessage( "%s", logBuf
.c_str() );
544 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
547 logBuf
<< "Value changed for cell at"
548 << " row " << ev
.GetRow()
549 << " col " << ev
.GetCol();
551 wxLogMessage( "%s", logBuf
.c_str() );
557 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
559 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
561 "mbedward@ozemail.com.au \n\n",
567 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
572 // ----------------------------------------------------------------------------
573 // MyGridCellRenderer
574 // ----------------------------------------------------------------------------
576 void MyGridCellRenderer::Draw(wxGrid
& grid
,
582 wxGridCellStringRenderer::Draw(grid
, dc
, rect
, row
, col
, isSelected
);
584 dc
.SetPen(*wxGREEN_PEN
);
585 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
586 dc
.DrawEllipse(rect
);