1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
7 // Copyright: (c) Michael Bedward, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/colordlg.h"
31 #include "wx/fontdlg.h"
32 #include "wx/numdlg.h"
35 #include "wx/generic/gridctrl.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 IMPLEMENT_APP( GridApp
)
45 // ============================================================================
47 // ============================================================================
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 bool GridApp::OnInit()
55 GridFrame
*frame
= new GridFrame
;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
66 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
67 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
68 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
69 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
70 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
71 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
72 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
73 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
74 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
75 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
76 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
77 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
78 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
79 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
80 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
81 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
82 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
83 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
84 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
85 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
86 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
87 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
88 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
89 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
90 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
91 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
92 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
94 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
95 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
97 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
98 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
99 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
100 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
101 EVT_MENU( ID_SMALL_GRID
, GridFrame::OnSmallGrid
)
103 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
104 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
105 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
106 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
107 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
108 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
109 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
110 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
111 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
113 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
114 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
116 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
117 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
118 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
119 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
120 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
121 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
122 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
123 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
125 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
126 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
130 GridFrame::GridFrame()
131 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets grid class demo"),
135 wxMenu
*fileMenu
= new wxMenu
;
136 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
137 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
138 fileMenu
->Append( ID_SMALL_GRID
, _T("&Small Grid test\tCtrl-S"));
139 fileMenu
->AppendSeparator();
140 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
142 wxMenu
*viewMenu
= new wxMenu
;
143 viewMenu
->Append( ID_TOGGLEROWLABELS
, _T("&Row labels"), wxEmptyString
, wxITEM_CHECK
);
144 viewMenu
->Append( ID_TOGGLECOLLABELS
, _T("&Col labels"), wxEmptyString
, wxITEM_CHECK
);
145 viewMenu
->Append( ID_TOGGLEEDIT
, _T("&Editable"), wxEmptyString
, wxITEM_CHECK
);
146 viewMenu
->Append( ID_TOGGLEROWSIZING
, _T("Ro&w drag-resize"), wxEmptyString
, wxITEM_CHECK
);
147 viewMenu
->Append( ID_TOGGLECOLSIZING
, _T("C&ol drag-resize"), wxEmptyString
, wxITEM_CHECK
);
148 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, _T("&Grid drag-resize"), wxEmptyString
, wxITEM_CHECK
);
149 viewMenu
->Append( ID_TOGGLEGRIDDRAGCELL
, _T("&Grid drag-cell"), wxEmptyString
, wxITEM_CHECK
);
150 viewMenu
->Append( ID_TOGGLEGRIDLINES
, _T("&Grid Lines"), wxEmptyString
, wxITEM_CHECK
);
151 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, _T("&Set Cell Highlight Width...") );
152 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, _T("&Set Cell RO Highlight Width...") );
153 viewMenu
->Append( ID_AUTOSIZECOLS
, _T("&Auto-size cols") );
154 viewMenu
->Append( ID_CELLOVERFLOW
, _T("&Overflow cells"), wxEmptyString
, wxITEM_CHECK
);
155 viewMenu
->Append( ID_RESIZECELL
, _T("&Resize cell (7,1)"), wxEmptyString
, wxITEM_CHECK
);
157 wxMenu
*rowLabelMenu
= new wxMenu
;
159 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
161 _T("Change alignment of row labels") );
163 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
164 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
166 wxMenu
*colLabelMenu
= new wxMenu
;
168 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
170 _T("Change alignment of col labels") );
172 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
173 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
175 wxMenu
*colMenu
= new wxMenu
;
176 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
177 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
178 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
179 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
180 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
181 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
183 wxMenu
*editMenu
= new wxMenu
;
184 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
185 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
186 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
187 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
188 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
190 wxMenu
*selectMenu
= new wxMenu
;
191 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
192 _T("When off, old selection is deselected before ")
193 _T("selecting the new cells"), wxITEM_CHECK
);
194 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
195 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
196 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
197 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
198 selectMenu
->Append( ID_DESELECT_ALL
, _T("Deselect all"));
199 selectMenu
->Append( ID_DESELECT_ROW
, _T("Deselect row 2"));
200 selectMenu
->Append( ID_DESELECT_COL
, _T("Deselect col 2"));
201 selectMenu
->Append( ID_DESELECT_CELL
, _T("Deselect cell (3, 1)"));
202 wxMenu
*selectionMenu
= new wxMenu
;
203 selectMenu
->Append( ID_CHANGESEL
, _T("Change &selection mode"),
205 _T("Change selection mode") );
207 selectionMenu
->Append( ID_SELCELLS
, _T("Select &Cells") );
208 selectionMenu
->Append( ID_SELROWS
, _T("Select &Rows") );
209 selectionMenu
->Append( ID_SELCOLS
, _T("Select C&ols") );
212 wxMenu
*helpMenu
= new wxMenu
;
213 helpMenu
->Append( wxID_ABOUT
, _T("&About wxGrid demo") );
215 wxMenuBar
*menuBar
= new wxMenuBar
;
216 menuBar
->Append( fileMenu
, _T("&File") );
217 menuBar
->Append( viewMenu
, _T("&View") );
218 menuBar
->Append( colMenu
, _T("&Colours") );
219 menuBar
->Append( editMenu
, _T("&Edit") );
220 menuBar
->Append( selectMenu
, _T("&Select") );
221 menuBar
->Append( helpMenu
, _T("&Help") );
223 SetMenuBar( menuBar
);
227 grid
= new wxGrid( this,
230 wxSize( 400, 300 ) );
233 int gridW
= 600, gridH
= 300;
234 int logW
= gridW
, logH
= 100;
236 logWin
= new wxTextCtrl( this,
239 wxPoint( 0, gridH
+ 20 ),
240 wxSize( logW
, logH
),
243 logger
= new wxLogTextCtrl( logWin
);
244 m_logOld
= wxLog::SetActiveTarget( logger
);
245 wxLog::SetTimestamp( NULL
);
248 // this will create a grid and, by default, an associated grid
250 grid
->CreateGrid( 0, 0 );
251 grid
->AppendRows(100);
252 grid
->AppendCols(100);
254 int ir
= grid
->GetNumberRows();
255 grid
->DeleteRows(0, ir
);
256 grid
->AppendRows(ir
);
258 grid
->SetRowSize( 0, 60 );
259 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
261 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
262 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
263 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
265 grid
->SetCellValue( 0, 2, _T("Blah") );
266 grid
->SetCellValue( 0, 3, _T("Read only") );
267 grid
->SetReadOnly( 0, 3 );
269 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
271 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
273 grid
->SetRowSize( 99, 60 );
274 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
275 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
277 grid
->SetCellTextColour(1, 2, *wxRED
);
278 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
280 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
282 grid
->SetCellValue(2, 2, _T("red"));
284 grid
->SetCellTextColour(2, 2, *wxRED
);
285 grid
->SetCellValue(3, 3, _T("green on grey"));
286 grid
->SetCellTextColour(3, 3, *wxGREEN
);
287 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
289 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
290 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
291 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
293 grid
->SetCellValue(3, 0, _T("0"));
294 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
295 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
297 wxGridCellAttr
*attr
;
298 attr
= new wxGridCellAttr
;
299 attr
->SetTextColour(*wxBLUE
);
300 grid
->SetColAttr(5, attr
);
301 attr
= new wxGridCellAttr
;
302 attr
->SetBackgroundColour(*wxRED
);
303 grid
->SetRowAttr(5, attr
);
305 grid
->SetCellValue(2, 4, _T("a wider column"));
306 grid
->SetColSize(4, 120);
307 grid
->SetColMinimalWidth(4, 120);
309 grid
->SetCellTextColour(5, 8, *wxGREEN
);
310 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
311 grid
->SetCellValue(5, 5, _T("Bg from row attr Text col from col attr and this text is so long that it covers over many many empty cells but is broken by one that isn't"));
313 grid
->SetColFormatFloat(6);
314 grid
->SetCellValue(0, 6, _T("3.1415"));
315 grid
->SetCellValue(1, 6, _T("1415"));
316 grid
->SetCellValue(2, 6, _T("12345.67890"));
318 grid
->SetColFormatFloat(7, 6, 2);
319 grid
->SetCellValue(0, 7, _T("3.1415"));
320 grid
->SetCellValue(1, 7, _T("1415"));
321 grid
->SetCellValue(2, 7, _T("12345.67890"));
323 const wxString choices
[] =
325 _T("Please select a choice"),
326 _T("This takes two cells"),
327 _T("Another choice"),
329 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
330 grid
->SetCellSize(4, 0, 1, 2);
331 grid
->SetCellValue(4, 0, choices
[0]);
332 grid
->SetCellOverflow(4, 0, false);
334 grid
->SetCellSize(7, 1, 3, 4);
335 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
336 grid
->SetCellValue(7, 1, _T("Big box!"));
338 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
344 topSizer
->Add( logWin
,
350 SetSizer( topSizer
);
352 topSizer
->Fit( this );
359 GridFrame::~GridFrame()
362 delete wxLog::SetActiveTarget(m_logOld
);
367 void GridFrame::SetDefaults()
369 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
370 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
371 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
372 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
373 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
374 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
375 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
376 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
377 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
381 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
383 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
385 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
389 grid
->SetRowLabelSize( 0 );
394 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
396 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
398 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
402 grid
->SetColLabelSize( 0 );
407 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
410 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
414 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
416 grid
->EnableDragRowSize(
417 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
421 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
423 grid
->EnableDragColSize(
424 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
427 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
429 grid
->EnableDragGridSize(
430 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
433 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
435 grid
->EnableDragCell(
436 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
439 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
441 grid
->EnableGridLines(
442 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
445 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
447 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
449 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
450 _T("Pen Width"), 11, choices
);
452 int current
= grid
->GetCellHighlightPenWidth();
453 dlg
.SetSelection(current
);
454 if (dlg
.ShowModal() == wxID_OK
) {
455 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
459 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
461 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
463 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
464 _T("Pen Width"), 11, choices
);
466 int current
= grid
->GetCellHighlightROPenWidth();
467 dlg
.SetSelection(current
);
468 if (dlg
.ShowModal() == wxID_OK
) {
469 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
475 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
477 grid
->AutoSizeColumns();
481 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
483 grid
->SetDefaultCellOverflow(ev
.IsChecked());
487 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
490 grid
->SetCellSize( 7, 1, 5, 5 );
492 grid
->SetCellSize( 7, 1, 1, 5 );
496 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
498 wxColourDialog
dlg( NULL
);
499 if ( dlg
.ShowModal() == wxID_OK
)
501 wxColourData retData
;
502 retData
= dlg
.GetColourData();
503 wxColour colour
= retData
.GetColour();
505 grid
->SetLabelBackgroundColour( colour
);
510 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
512 wxColourDialog
dlg( NULL
);
513 if ( dlg
.ShowModal() == wxID_OK
)
515 wxColourData retData
;
516 retData
= dlg
.GetColourData();
517 wxColour colour
= retData
.GetColour();
519 grid
->SetLabelTextColour( colour
);
523 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
525 wxFont font
= wxGetFontFromUser(this);
528 grid
->SetLabelFont(font
);
532 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
535 grid
->GetRowLabelAlignment( &horiz
, &vert
);
540 horiz
= wxALIGN_CENTRE
;
544 horiz
= wxALIGN_RIGHT
;
548 horiz
= wxALIGN_LEFT
;
552 grid
->SetRowLabelAlignment( horiz
, vert
);
555 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
558 grid
->GetRowLabelAlignment( &horiz
, &vert
);
563 vert
= wxALIGN_CENTRE
;
567 vert
= wxALIGN_BOTTOM
;
575 grid
->SetRowLabelAlignment( horiz
, vert
);
579 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
582 grid
->GetColLabelAlignment( &horiz
, &vert
);
587 horiz
= wxALIGN_CENTRE
;
591 horiz
= wxALIGN_RIGHT
;
595 horiz
= wxALIGN_LEFT
;
599 grid
->SetColLabelAlignment( horiz
, vert
);
603 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
606 grid
->GetColLabelAlignment( &horiz
, &vert
);
611 vert
= wxALIGN_CENTRE
;
615 vert
= wxALIGN_BOTTOM
;
623 grid
->SetColLabelAlignment( horiz
, vert
);
627 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
629 wxColourDialog
dlg( NULL
);
630 if ( dlg
.ShowModal() == wxID_OK
)
632 wxColourData retData
;
633 retData
= dlg
.GetColourData();
634 wxColour colour
= retData
.GetColour();
636 grid
->SetGridLineColour( colour
);
641 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
643 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
647 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
649 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
653 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
655 if ( grid
->IsSelection() )
658 for ( int n
= 0; n
< grid
->GetNumberRows(); )
660 if ( grid
->IsInSelection( n
, 0 ) )
661 grid
->DeleteRows( n
, 1 );
670 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
672 if ( grid
->IsSelection() )
675 for ( int n
= 0; n
< grid
->GetNumberCols(); )
677 if ( grid
->IsInSelection( 0 , n
) )
678 grid
->DeleteCols( n
, 1 );
687 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
692 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
694 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
697 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
699 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
702 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
704 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
707 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
709 wxColour col
= wxGetColourFromUser(this);
712 grid
->SetDefaultCellTextColour(col
);
717 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
719 wxColour col
= wxGetColourFromUser(this);
722 // Check the new Refresh function by passing it a rectangle
723 // which exactly fits the grid.
725 wxRect
r(pt
, grid
->GetSize());
726 grid
->SetDefaultCellBackgroundColour(col
);
727 grid
->Refresh(true, &r
);
731 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
733 grid
->DeselectCell(3, 1);
736 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
738 grid
->DeselectCol(2);
741 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
743 grid
->DeselectRow(2);
746 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
748 grid
->ClearSelection();
751 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
753 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
756 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
758 grid
->SelectCol(2, m_addToSel
);
761 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
763 grid
->SelectRow(2, m_addToSel
);
766 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
771 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
773 m_addToSel
= event
.IsChecked();
776 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
778 logBuf
= wxEmptyString
;
779 if ( ev
.GetRow() != -1 )
781 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
783 else if ( ev
.GetCol() != -1 )
785 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
789 logBuf
<< _T("Left click on corner label");
792 if ( ev
.ShiftDown() ) logBuf
<< _T(" (shift down)");
793 if ( ev
.ControlDown() ) logBuf
<< _T(" (control down)");
794 wxLogMessage( wxT("%s"), logBuf
.c_str() );
796 // you must call event skip if you want default grid processing
802 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
804 logBuf
= wxEmptyString
;
805 logBuf
<< _T("Left click at row ") << ev
.GetRow()
806 << _T(" col ") << ev
.GetCol();
807 wxLogMessage( wxT("%s"), logBuf
.c_str() );
809 // you must call event skip if you want default grid processing
810 // (cell highlighting etc.)
816 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
818 logBuf
= wxEmptyString
;
819 logBuf
<< _T("Resized row ") << ev
.GetRowOrCol();
820 wxLogMessage( wxT("%s"), logBuf
.c_str() );
826 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
828 logBuf
= wxEmptyString
;
829 logBuf
<< _T("Resized col ") << ev
.GetRowOrCol();
830 wxLogMessage( wxT("%s"), logBuf
.c_str() );
836 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
838 logBuf
= wxEmptyString
;
839 if ( ev
.Selecting() )
840 logBuf
<< _T("Selected ");
842 logBuf
<< _T("Deselected ");
843 logBuf
<< _T("cell at row ") << ev
.GetRow()
844 << _T(" col ") << ev
.GetCol()
845 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
846 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
847 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
848 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
849 wxLogMessage( wxT("%s"), logBuf
.c_str() );
851 // you must call Skip() if you want the default processing
852 // to occur in wxGrid
856 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
858 logBuf
= wxEmptyString
;
859 if ( ev
.Selecting() )
860 logBuf
<< _T("Selected ");
862 logBuf
<< _T("Deselected ");
863 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
864 << _T(" col ") << ev
.GetLeftCol()
865 << _T(" to row ") << ev
.GetBottomRow()
866 << _T(" col ") << ev
.GetRightCol()
867 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
868 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
869 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
870 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
871 wxLogMessage( wxT("%s"), logBuf
.c_str() );
876 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
878 logBuf
= wxEmptyString
;
879 logBuf
<< _T("Value changed for cell at")
880 << _T(" row ") << ev
.GetRow()
881 << _T(" col ") << ev
.GetCol();
883 wxLogMessage( wxT("%s"), logBuf
.c_str() );
888 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
890 logBuf
= wxEmptyString
;
891 logBuf
<< _T("Got request to drag cell at")
892 << _T(" row ") << ev
.GetRow()
893 << _T(" col ") << ev
.GetCol();
895 wxLogMessage( wxT("%s"), logBuf
.c_str() );
900 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
903 if ( (ev
.GetCol() == 4) &&
904 (ev
.GetRow() == 0) &&
905 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
906 _T("Checking"),wxYES_NO
) == wxNO
) ) {
912 wxLogMessage( wxT("Cell editor shown.") );
917 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
920 if ( (ev
.GetCol() == 4) &&
921 (ev
.GetRow() == 0) &&
922 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
923 _T("Checking"),wxYES_NO
) == wxNO
) ) {
929 wxLogMessage( wxT("Cell editor hidden.") );
934 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
936 (void)wxMessageBox( _T("\n\nwxGrid demo \n\n")
937 _T("Michael Bedward, Julian Smart, Vadim Zeitlin"),
943 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
948 void GridFrame::OnBugsTable(wxCommandEvent
& )
950 BugsGridFrame
*frame
= new BugsGridFrame
;
954 void GridFrame::OnSmallGrid(wxCommandEvent
& )
956 wxFrame
* frame
= new wxFrame(NULL
, wxID_ANY
, _T("A Small Grid"),
957 wxDefaultPosition
, wxSize(640, 480));
958 wxPanel
* panel
= new wxPanel(frame
, wxID_ANY
);
959 wxGrid
* grid
= new wxGrid(panel
, wxID_ANY
, wxPoint(10,10), wxSize(400,400),
960 wxWANTS_CHARS
| wxSIMPLE_BORDER
);
961 grid
->CreateGrid(3,3);
965 void GridFrame::OnVTable(wxCommandEvent
& )
967 static long s_sizeGrid
= 10000;
970 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
973 s
= wxGetTextFromUser( _T("Size of the table to create"),
977 s
.ToLong( &s_sizeGrid
);
980 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
982 _T("wxGridDemo question"),
987 if ( s_sizeGrid
!= -1 )
989 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
994 // ----------------------------------------------------------------------------
995 // MyGridCellRenderer
996 // ----------------------------------------------------------------------------
998 // do something that the default renderer doesn't here just to show that it is
999 // possible to alter the appearance of the cell beyond what the attributes
1001 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1002 wxGridCellAttr
& attr
,
1008 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1010 dc
.SetPen(*wxGREEN_PEN
);
1011 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1012 dc
.DrawEllipse(rect
);
1015 // ----------------------------------------------------------------------------
1016 // MyGridCellAttrProvider
1017 // ----------------------------------------------------------------------------
1019 MyGridCellAttrProvider::MyGridCellAttrProvider()
1021 m_attrForOddRows
= new wxGridCellAttr
;
1022 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1025 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1027 m_attrForOddRows
->DecRef();
1030 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1031 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1033 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1039 attr
= m_attrForOddRows
;
1044 if ( !attr
->HasBackgroundColour() )
1046 wxGridCellAttr
*attrNew
= attr
->Clone();
1049 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1057 // ============================================================================
1058 // BigGridFrame and BigGridTable: Sample of a non-standard table
1059 // ============================================================================
1061 BigGridFrame::BigGridFrame(long sizeGrid
)
1062 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1063 wxDefaultPosition
, wxSize(500, 450))
1065 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1066 m_table
= new BigGridTable(sizeGrid
);
1068 // VZ: I don't understand why this slows down the display that much,
1069 // must profile it...
1070 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1072 m_grid
->SetTable(m_table
, true);
1074 #if defined __WXMOTIF__
1075 // MB: the grid isn't getting a sensible default size under wxMotif
1077 GetClientSize( &cw
, &ch
);
1078 m_grid
->SetSize( cw
, ch
);
1082 // ============================================================================
1083 // BugsGridFrame: a "realistic" table
1084 // ============================================================================
1086 // ----------------------------------------------------------------------------
1088 // ----------------------------------------------------------------------------
1111 static const wxString severities
[] =
1120 static struct BugsGridData
1126 wxChar platform
[12];
1128 } gs_dataBugsGrid
[] =
1130 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1131 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1132 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1133 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1136 static const wxChar
*headers
[Col_Max
] =
1146 // ----------------------------------------------------------------------------
1148 // ----------------------------------------------------------------------------
1150 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1156 return wxGRID_VALUE_NUMBER
;;
1159 // fall thorugh (TODO should be a list)
1162 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1165 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1168 return wxGRID_VALUE_BOOL
;
1171 wxFAIL_MSG(_T("unknown column"));
1173 return wxEmptyString
;
1176 int BugsGridTable::GetNumberRows()
1178 return WXSIZEOF(gs_dataBugsGrid
);
1181 int BugsGridTable::GetNumberCols()
1186 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1191 wxString
BugsGridTable::GetValue( int row
, int col
)
1193 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1198 return wxString::Format(_T("%d"), gd
.id
);
1201 return wxString::Format(_T("%d"), gd
.prio
);
1204 return gd
.opened
? _T("1") : _T("0");
1207 return severities
[gd
.severity
];
1216 return wxEmptyString
;
1219 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1221 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1228 wxFAIL_MSG(_T("unexpected column"));
1234 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1236 if ( severities
[n
] == value
)
1238 gd
.severity
= (Severity
)n
;
1243 if ( n
== WXSIZEOF(severities
) )
1245 wxLogWarning(_T("Invalid severity value '%s'."),
1247 gd
.severity
= Sev_Normal
;
1253 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1257 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1263 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1265 const wxString
& typeName
)
1267 if ( typeName
== wxGRID_VALUE_STRING
)
1271 else if ( typeName
== wxGRID_VALUE_BOOL
)
1273 return col
== Col_Opened
;
1275 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1277 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1285 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1287 return CanGetValueAs(row
, col
, typeName
);
1290 long BugsGridTable::GetValueAsLong( int row
, int col
)
1292 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1306 wxFAIL_MSG(_T("unexpected column"));
1311 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1313 if ( col
== Col_Opened
)
1315 return gs_dataBugsGrid
[row
].opened
;
1319 wxFAIL_MSG(_T("unexpected column"));
1325 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1327 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1336 wxFAIL_MSG(_T("unexpected column"));
1340 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1342 if ( col
== Col_Opened
)
1344 gs_dataBugsGrid
[row
].opened
= value
;
1348 wxFAIL_MSG(_T("unexpected column"));
1352 wxString
BugsGridTable::GetColLabelValue( int col
)
1354 return headers
[col
];
1357 // ----------------------------------------------------------------------------
1359 // ----------------------------------------------------------------------------
1361 BugsGridFrame::BugsGridFrame()
1362 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"),
1363 wxDefaultPosition
, wxSize(500, 300))
1365 wxGrid
*grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
);
1366 wxGridTableBase
*table
= new BugsGridTable();
1367 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1368 grid
->SetTable(table
, true);
1370 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1371 *attrRangeEditor
= new wxGridCellAttr
,
1372 *attrCombo
= new wxGridCellAttr
;
1374 attrRO
->SetReadOnly();
1375 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1376 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1379 grid
->SetColAttr(Col_Id
, attrRO
);
1380 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1381 grid
->SetColAttr(Col_Severity
, attrCombo
);
1383 grid
->SetMargins(0, 0);
1386 SetClientSize(grid
->GetSize());