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 wxGridCellAttr
*attr
;
205 attr
= new wxGridCellAttr
;
206 attr
->SetTextColour(*wxBLUE
);
207 grid
->SetColAttr(5, attr
);
208 attr
= new wxGridCellAttr
;
209 attr
->SetBackgroundColour(*wxBLUE
);
210 grid
->SetRowAttr(5, attr
);
212 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
217 topSizer
->Add( logWin
,
221 SetAutoLayout( TRUE
);
222 SetSizer( topSizer
);
224 topSizer
->Fit( this );
225 topSizer
->SetSizeHints( this );
232 GridFrame::~GridFrame()
237 void GridFrame::SetDefaults()
239 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
240 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
241 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
245 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
247 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
249 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
253 grid
->SetRowLabelSize( 0 );
258 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
260 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
262 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
266 grid
->SetColLabelSize( 0 );
271 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
274 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
278 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
280 wxColourDialog
dlg( NULL
);
281 if ( dlg
.ShowModal() == wxID_OK
)
283 wxColourData retData
;
284 retData
= dlg
.GetColourData();
285 wxColour colour
= retData
.GetColour();
287 grid
->SetLabelBackgroundColour( colour
);
292 void GridFrame::SetLabelTextColour( 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
->SetLabelTextColour( colour
);
306 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
309 grid
->GetRowLabelAlignment( &horiz
, &vert
);
326 grid
->SetRowLabelAlignment( horiz
, -1 );
329 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
332 grid
->GetRowLabelAlignment( &horiz
, &vert
);
349 grid
->SetRowLabelAlignment( -1, vert
);
353 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
356 grid
->GetColLabelAlignment( &horiz
, &vert
);
373 grid
->SetColLabelAlignment( horiz
, -1 );
377 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
380 grid
->GetColLabelAlignment( &horiz
, &vert
);
397 grid
->SetColLabelAlignment( -1, vert
);
401 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
403 wxColourDialog
dlg( NULL
);
404 if ( dlg
.ShowModal() == wxID_OK
)
406 wxColourData retData
;
407 retData
= dlg
.GetColourData();
408 wxColour colour
= retData
.GetColour();
410 grid
->SetGridLineColour( colour
);
415 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
417 grid
->InsertRows( 0, 1 );
421 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
423 grid
->InsertCols( 0, 1 );
427 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
429 if ( grid
->IsSelection() )
431 int topRow
, bottomRow
, leftCol
, rightCol
;
432 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
433 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
438 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
440 if ( grid
->IsSelection() )
442 int topRow
, bottomRow
, leftCol
, rightCol
;
443 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
444 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
449 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
454 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
456 wxColour col
= wxGetColourFromUser(this);
459 grid
->SetDefaultCellTextColour(col
);
464 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
466 wxColour col
= wxGetColourFromUser(this);
469 grid
->SetDefaultCellBackgroundColour(col
);
474 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
477 if ( ev
.GetRow() != -1 )
479 logBuf
<< "Left click on row label " << ev
.GetRow();
481 else if ( ev
.GetCol() != -1 )
483 logBuf
<< "Left click on col label " << ev
.GetCol();
487 logBuf
<< "Left click on corner label";
490 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
491 wxLogMessage( "%s", logBuf
.c_str() );
493 // you must call event skip if you want default grid processing
499 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
502 logBuf
<< "Left click at row " << ev
.GetRow()
503 << " col " << ev
.GetCol();
504 wxLogMessage( "%s", logBuf
.c_str() );
506 // you must call event skip if you want default grid processing
507 // (cell highlighting etc.)
513 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
516 logBuf
<< "Resized row " << ev
.GetRowOrCol();
517 wxLogMessage( "%s", logBuf
.c_str() );
523 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
526 logBuf
<< "Resized col " << ev
.GetRowOrCol();
527 wxLogMessage( "%s", logBuf
.c_str() );
533 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
536 logBuf
<< "Selected cell at row " << ev
.GetRow()
537 << " col " << ev
.GetCol();
538 wxLogMessage( "%s", logBuf
.c_str() );
540 // you must call Skip() if you want the default processing
541 // to occur in wxGrid
545 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
548 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
549 << " col " << ev
.GetLeftCol()
550 << " to row " << ev
.GetBottomRow()
551 << " col " << ev
.GetRightCol();
553 wxLogMessage( "%s", logBuf
.c_str() );
558 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
561 logBuf
<< "Value changed for cell at"
562 << " row " << ev
.GetRow()
563 << " col " << ev
.GetCol();
565 wxLogMessage( "%s", logBuf
.c_str() );
571 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
573 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
575 "mbedward@ozemail.com.au \n\n",
581 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
586 // ----------------------------------------------------------------------------
587 // MyGridCellRenderer
588 // ----------------------------------------------------------------------------
590 // do something that the default renderer doesn't here just to show that it is
591 // possible to alter the appearance of the cell beyond what the attributes
593 void MyGridCellRenderer::Draw(wxGrid
& grid
,
599 wxGridCellStringRenderer::Draw(grid
, dc
, rect
, row
, col
, isSelected
);
601 dc
.SetPen(*wxGREEN_PEN
);
602 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
603 dc
.DrawEllipse(rect
);