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
)
102 GridFrame::GridFrame()
103 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
107 int gridW
= 600, gridH
= 300;
108 int logW
= gridW
, logH
= 80;
110 wxMenu
*fileMenu
= new wxMenu
;
111 fileMenu
->Append( ID_VTABLE
, "&Virtual table test");
112 fileMenu
->AppendSeparator();
113 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
115 wxMenu
*viewMenu
= new wxMenu
;
116 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
117 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
118 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
120 wxMenu
*rowLabelMenu
= new wxMenu
;
122 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
124 "Change alignment of row labels" );
126 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
127 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
129 wxMenu
*colLabelMenu
= new wxMenu
;
131 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
133 "Change alignment of col labels" );
135 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
136 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
138 wxMenu
*colMenu
= new wxMenu
;
139 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
140 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
141 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
142 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
143 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
145 wxMenu
*editMenu
= new wxMenu
;
146 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
147 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
148 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
149 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
150 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
152 wxMenu
*helpMenu
= new wxMenu
;
153 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
155 wxMenuBar
*menuBar
= new wxMenuBar
;
156 menuBar
->Append( fileMenu
, "&File" );
157 menuBar
->Append( viewMenu
, "&View" );
158 menuBar
->Append( colMenu
, "&Colours" );
159 menuBar
->Append( editMenu
, "&Edit" );
160 menuBar
->Append( helpMenu
, "&Help" );
162 SetMenuBar( menuBar
);
164 grid
= new wxGrid( this,
167 wxSize( 400, 300 ) );
169 logWin
= new wxTextCtrl( this,
172 wxPoint( 0, gridH
+ 20 ),
173 wxSize( logW
, logH
),
176 logger
= new wxLogTextCtrl( logWin
);
177 logger
->SetActiveTarget( logger
);
178 logger
->SetTimestamp( NULL
);
180 // this will create a grid and, by default, an associated grid
181 // table for string data
183 grid
->CreateGrid( 100, 100 );
185 grid
->SetRowSize( 0, 60 );
186 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
188 grid
->SetCellValue( 0, 1, "Blah" );
189 grid
->SetCellValue( 0, 2, "Blah" );
191 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
193 grid
->SetRowSize( 99, 60 );
194 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
196 grid
->SetCellValue(2, 2, "red");
198 grid
->SetCellTextColour(2, 2, *wxRED
);
199 grid
->SetCellValue(3, 3, "green on grey");
200 grid
->SetCellTextColour(3, 3, *wxGREEN
);
201 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
203 grid
->SetCellValue(4, 4, "a weird looking cell");
204 grid
->SetCellAlignment(4, 4, wxCENTRE
, wxCENTRE
);
205 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
207 wxGridCellAttr
*attr
;
208 attr
= new wxGridCellAttr
;
209 attr
->SetTextColour(*wxBLUE
);
210 grid
->SetColAttr(5, attr
);
211 attr
= new wxGridCellAttr
;
212 attr
->SetBackgroundColour(*wxBLUE
);
213 grid
->SetRowAttr(5, attr
);
215 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
220 topSizer
->Add( logWin
,
224 SetAutoLayout( TRUE
);
225 SetSizer( topSizer
);
227 topSizer
->Fit( this );
228 topSizer
->SetSizeHints( this );
235 GridFrame::~GridFrame()
240 void GridFrame::SetDefaults()
242 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
243 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
244 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
248 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
250 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
252 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
256 grid
->SetRowLabelSize( 0 );
261 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
263 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
265 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
269 grid
->SetColLabelSize( 0 );
274 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
277 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
281 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
283 wxColourDialog
dlg( NULL
);
284 if ( dlg
.ShowModal() == wxID_OK
)
286 wxColourData retData
;
287 retData
= dlg
.GetColourData();
288 wxColour colour
= retData
.GetColour();
290 grid
->SetLabelBackgroundColour( colour
);
295 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
297 wxColourDialog
dlg( NULL
);
298 if ( dlg
.ShowModal() == wxID_OK
)
300 wxColourData retData
;
301 retData
= dlg
.GetColourData();
302 wxColour colour
= retData
.GetColour();
304 grid
->SetLabelTextColour( colour
);
309 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
312 grid
->GetRowLabelAlignment( &horiz
, &vert
);
329 grid
->SetRowLabelAlignment( horiz
, -1 );
332 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
335 grid
->GetRowLabelAlignment( &horiz
, &vert
);
352 grid
->SetRowLabelAlignment( -1, vert
);
356 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
359 grid
->GetColLabelAlignment( &horiz
, &vert
);
376 grid
->SetColLabelAlignment( horiz
, -1 );
380 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
383 grid
->GetColLabelAlignment( &horiz
, &vert
);
400 grid
->SetColLabelAlignment( -1, vert
);
404 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
406 wxColourDialog
dlg( NULL
);
407 if ( dlg
.ShowModal() == wxID_OK
)
409 wxColourData retData
;
410 retData
= dlg
.GetColourData();
411 wxColour colour
= retData
.GetColour();
413 grid
->SetGridLineColour( colour
);
418 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
420 grid
->InsertRows( 0, 1 );
424 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
426 grid
->InsertCols( 0, 1 );
430 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
432 if ( grid
->IsSelection() )
434 int topRow
, bottomRow
, leftCol
, rightCol
;
435 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
436 grid
->DeleteRows( topRow
, bottomRow
- topRow
+ 1 );
441 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
443 if ( grid
->IsSelection() )
445 int topRow
, bottomRow
, leftCol
, rightCol
;
446 grid
->GetSelection( &topRow
, &leftCol
, &bottomRow
, &rightCol
);
447 grid
->DeleteCols( leftCol
, rightCol
- leftCol
+ 1 );
452 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
457 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
459 wxColour col
= wxGetColourFromUser(this);
462 grid
->SetDefaultCellTextColour(col
);
467 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
469 wxColour col
= wxGetColourFromUser(this);
472 grid
->SetDefaultCellBackgroundColour(col
);
477 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
480 if ( ev
.GetRow() != -1 )
482 logBuf
<< "Left click on row label " << ev
.GetRow();
484 else if ( ev
.GetCol() != -1 )
486 logBuf
<< "Left click on col label " << ev
.GetCol();
490 logBuf
<< "Left click on corner label";
493 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
494 wxLogMessage( "%s", logBuf
.c_str() );
496 // you must call event skip if you want default grid processing
502 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
505 logBuf
<< "Left click at row " << ev
.GetRow()
506 << " col " << ev
.GetCol();
507 wxLogMessage( "%s", logBuf
.c_str() );
509 // you must call event skip if you want default grid processing
510 // (cell highlighting etc.)
516 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
519 logBuf
<< "Resized row " << ev
.GetRowOrCol();
520 wxLogMessage( "%s", logBuf
.c_str() );
526 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
529 logBuf
<< "Resized col " << ev
.GetRowOrCol();
530 wxLogMessage( "%s", logBuf
.c_str() );
536 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
539 logBuf
<< "Selected cell at row " << ev
.GetRow()
540 << " col " << ev
.GetCol();
541 wxLogMessage( "%s", logBuf
.c_str() );
543 // you must call Skip() if you want the default processing
544 // to occur in wxGrid
548 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
551 logBuf
<< "Selected cells from row " << ev
.GetTopRow()
552 << " col " << ev
.GetLeftCol()
553 << " to row " << ev
.GetBottomRow()
554 << " col " << ev
.GetRightCol();
556 wxLogMessage( "%s", logBuf
.c_str() );
561 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
564 logBuf
<< "Value changed for cell at"
565 << " row " << ev
.GetRow()
566 << " col " << ev
.GetCol();
568 wxLogMessage( "%s", logBuf
.c_str() );
574 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
576 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
578 "mbedward@ozemail.com.au \n\n",
584 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
589 void GridFrame::OnVTable(wxCommandEvent
& )
591 BigGridFrame
* win
= new BigGridFrame();
595 // ----------------------------------------------------------------------------
596 // MyGridCellRenderer
597 // ----------------------------------------------------------------------------
599 // do something that the default renderer doesn't here just to show that it is
600 // possible to alter the appearance of the cell beyond what the attributes
602 void MyGridCellRenderer::Draw(wxGrid
& grid
,
603 wxGridCellAttr
& attr
,
609 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
611 dc
.SetPen(*wxGREEN_PEN
);
612 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
613 dc
.DrawEllipse(rect
);
617 // ----------------------------------------------------------------------------
618 // BigGridFrame and BigGridTable: Sample of a non-standard table
619 // ----------------------------------------------------------------------------
621 BigGridFrame::BigGridFrame()
622 : wxFrame(NULL
, -1, "Plugin Virtual Table", wxDefaultPosition
,
625 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
626 m_table
= new BigGridTable
;
627 m_grid
->SetTable(m_table
, TRUE
);