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"
36 #include "wx/fontdlg.h"
39 #include "wx/generic/gridctrl.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 IMPLEMENT_APP( GridApp
)
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 bool GridApp::OnInit()
59 GridFrame
*frame
= new GridFrame
;
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
70 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
71 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
72 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
73 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
74 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
75 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
76 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
77 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
78 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
79 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
80 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
81 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
82 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
83 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
84 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
85 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
86 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
87 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
88 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
89 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
90 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
91 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
92 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
93 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
95 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
96 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
98 EVT_MENU( ID_ABOUT
, GridFrame::About
)
99 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
100 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
101 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
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
)
124 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
125 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
129 GridFrame::GridFrame()
130 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
134 int gridW
= 600, gridH
= 300;
135 int logW
= gridW
, logH
= 100;
137 wxMenu
*fileMenu
= new wxMenu
;
138 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
139 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
140 fileMenu
->AppendSeparator();
141 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
143 wxMenu
*viewMenu
= new wxMenu
;
144 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
145 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
146 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
147 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
148 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
149 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, "&Grid drag-resize", "", TRUE
);
150 viewMenu
->Append( ID_TOGGLEGRIDLINES
, "&Grid Lines", "", TRUE
);
151 viewMenu
->Append( ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...", "" );
152 viewMenu
->Append( ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...", "" );
153 viewMenu
->Append( ID_AUTOSIZECOLS
, "&Auto-size cols" );
155 wxMenu
*rowLabelMenu
= new wxMenu
;
157 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
159 "Change alignment of row labels" );
161 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
162 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
164 wxMenu
*colLabelMenu
= new wxMenu
;
166 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
168 "Change alignment of col labels" );
170 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
171 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
173 wxMenu
*colMenu
= new wxMenu
;
174 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour..." );
175 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour..." );
176 colMenu
->Append( ID_SETLABEL_FONT
, "Set label fo&nt..." );
177 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour..." );
178 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour..." );
179 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour..." );
181 wxMenu
*editMenu
= new wxMenu
;
182 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
183 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
184 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
185 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
186 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
188 wxMenu
*selectMenu
= new wxMenu
;
189 selectMenu
->Append( ID_SELECT_UNSELECT
, "Add new cells to the selection",
190 "When off, old selection is deselected before "
191 "selecting the new cells", TRUE
);
192 selectMenu
->Append( ID_SELECT_ALL
, "Select all");
193 selectMenu
->Append( ID_SELECT_ROW
, "Select row 2");
194 selectMenu
->Append( ID_SELECT_COL
, "Select col 2");
195 selectMenu
->Append( ID_SELECT_CELL
, "Select cell (3, 1)");
196 selectMenu
->Append( ID_DESELECT_ALL
, "Deselect all");
197 selectMenu
->Append( ID_DESELECT_ROW
, "Deselect row 2");
198 selectMenu
->Append( ID_DESELECT_COL
, "Deselect col 2");
199 selectMenu
->Append( ID_DESELECT_CELL
, "Deselect cell (3, 1)");
200 wxMenu
*selectionMenu
= new wxMenu
;
201 selectMenu
->Append( ID_CHANGESEL
, "Change &selection mode",
203 "Change selection mode" );
205 selectionMenu
->Append( ID_SELCELLS
, "Select &Cells" );
206 selectionMenu
->Append( ID_SELROWS
, "Select &Rows" );
207 selectionMenu
->Append( ID_SELCOLS
, "Select C&ols" );
210 wxMenu
*helpMenu
= new wxMenu
;
211 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
213 wxMenuBar
*menuBar
= new wxMenuBar
;
214 menuBar
->Append( fileMenu
, "&File" );
215 menuBar
->Append( viewMenu
, "&View" );
216 menuBar
->Append( colMenu
, "&Colours" );
217 menuBar
->Append( editMenu
, "&Edit" );
218 menuBar
->Append( selectMenu
, "&Select" );
219 menuBar
->Append( helpMenu
, "&Help" );
221 SetMenuBar( menuBar
);
225 grid
= new wxGrid( this,
228 wxSize( 400, 300 ) );
230 logWin
= new wxTextCtrl( this,
233 wxPoint( 0, gridH
+ 20 ),
234 wxSize( logW
, logH
),
237 logger
= new wxLogTextCtrl( logWin
);
238 m_logOld
= logger
->SetActiveTarget( logger
);
239 logger
->SetTimestamp( NULL
);
241 // this will create a grid and, by default, an associated grid
243 grid
->CreateGrid( 100, 100 );
245 grid
->SetRowSize( 0, 60 );
246 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
248 grid
->SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." );
249 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
250 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
252 grid
->SetCellValue( 0, 2, "Blah" );
253 grid
->SetCellValue( 0, 3, "Read only" );
254 grid
->SetReadOnly( 0, 3 );
256 grid
->SetCellValue( 0, 4, "Can veto edit this cell" );
258 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
260 grid
->SetRowSize( 99, 60 );
261 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
263 grid
->SetCellValue(2, 2, "red");
265 grid
->SetCellTextColour(2, 2, *wxRED
);
266 grid
->SetCellValue(3, 3, "green on grey");
267 grid
->SetCellTextColour(3, 3, *wxGREEN
);
268 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
270 grid
->SetCellValue(4, 4, "a weird looking cell");
271 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
272 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
274 grid
->SetCellValue(3, 0, "0");
275 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
276 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
278 wxGridCellAttr
*attr
;
279 attr
= new wxGridCellAttr
;
280 attr
->SetTextColour(*wxBLUE
);
281 grid
->SetColAttr(5, attr
);
282 attr
= new wxGridCellAttr
;
283 attr
->SetBackgroundColour(*wxRED
);
284 grid
->SetRowAttr(5, attr
);
286 grid
->SetCellValue(2, 4, "a wider column");
287 grid
->SetColSize(4, 120);
288 grid
->SetColMinimalWidth(4, 120);
290 grid
->SetCellTextColour(5, 8, *wxGREEN
);
291 grid
->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
292 grid
->SetCellValue(5, 5, "Bg from row attr\nText col from col attr");
294 grid
->SetColFormatFloat(6);
295 grid
->SetCellValue(0, 6, "3.1415");
296 grid
->SetCellValue(1, 6, "1415");
297 grid
->SetCellValue(2, 6, "12345.67890");
299 grid
->SetColFormatFloat(7, 6, 2);
300 grid
->SetCellValue(0, 7, "3.1415");
301 grid
->SetCellValue(1, 7, "1415");
302 grid
->SetCellValue(2, 7, "12345.67890");
304 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
309 topSizer
->Add( logWin
,
313 SetAutoLayout( TRUE
);
314 SetSizer( topSizer
);
316 topSizer
->Fit( this );
317 topSizer
->SetSizeHints( this );
324 GridFrame::~GridFrame()
326 delete wxLog::SetActiveTarget(m_logOld
);
330 void GridFrame::SetDefaults()
332 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
333 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
334 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
335 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
336 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
337 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
338 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
342 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
344 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
346 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
350 grid
->SetRowLabelSize( 0 );
355 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
357 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
359 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
363 grid
->SetColLabelSize( 0 );
368 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
371 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
375 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
377 grid
->EnableDragRowSize(
378 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
382 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
384 grid
->EnableDragColSize(
385 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
388 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
390 grid
->EnableDragGridSize(
391 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
395 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
397 grid
->EnableGridLines(
398 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
401 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
403 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
405 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
406 "Pen Width", 11, choices
);
408 int current
= grid
->GetCellHighlightPenWidth();
409 dlg
.SetSelection(current
);
410 if (dlg
.ShowModal() == wxID_OK
) {
411 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
415 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
417 wxString choices
[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
419 wxSingleChoiceDialog
dlg(this, "Choose the thickness of the highlight pen:",
420 "Pen Width", 11, choices
);
422 int current
= grid
->GetCellHighlightROPenWidth();
423 dlg
.SetSelection(current
);
424 if (dlg
.ShowModal() == wxID_OK
) {
425 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
431 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
433 grid
->AutoSizeColumns();
438 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
440 wxColourDialog
dlg( NULL
);
441 if ( dlg
.ShowModal() == wxID_OK
)
443 wxColourData retData
;
444 retData
= dlg
.GetColourData();
445 wxColour colour
= retData
.GetColour();
447 grid
->SetLabelBackgroundColour( colour
);
452 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
454 wxColourDialog
dlg( NULL
);
455 if ( dlg
.ShowModal() == wxID_OK
)
457 wxColourData retData
;
458 retData
= dlg
.GetColourData();
459 wxColour colour
= retData
.GetColour();
461 grid
->SetLabelTextColour( colour
);
465 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
467 wxFont font
= wxGetFontFromUser(this);
470 grid
->SetLabelFont(font
);
474 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
477 grid
->GetRowLabelAlignment( &horiz
, &vert
);
482 horiz
= wxALIGN_CENTRE
;
486 horiz
= wxALIGN_RIGHT
;
490 horiz
= wxALIGN_LEFT
;
494 grid
->SetRowLabelAlignment( horiz
, -1 );
497 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
500 grid
->GetRowLabelAlignment( &horiz
, &vert
);
505 vert
= wxALIGN_CENTRE
;
509 vert
= wxALIGN_BOTTOM
;
517 grid
->SetRowLabelAlignment( -1, vert
);
521 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
524 grid
->GetColLabelAlignment( &horiz
, &vert
);
529 horiz
= wxALIGN_CENTRE
;
533 horiz
= wxALIGN_RIGHT
;
537 horiz
= wxALIGN_LEFT
;
541 grid
->SetColLabelAlignment( horiz
, -1 );
545 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
548 grid
->GetColLabelAlignment( &horiz
, &vert
);
553 vert
= wxALIGN_CENTRE
;
557 vert
= wxALIGN_BOTTOM
;
565 grid
->SetColLabelAlignment( -1, vert
);
569 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
571 wxColourDialog
dlg( NULL
);
572 if ( dlg
.ShowModal() == wxID_OK
)
574 wxColourData retData
;
575 retData
= dlg
.GetColourData();
576 wxColour colour
= retData
.GetColour();
578 grid
->SetGridLineColour( colour
);
583 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
585 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
589 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
591 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
595 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
597 if ( grid
->IsSelection() )
600 for ( int n
= 0; n
< grid
->GetNumberRows(); )
601 if ( grid
->IsInSelection( n
, 0 ) )
602 grid
->DeleteRows( n
, 1 );
610 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
612 if ( grid
->IsSelection() )
615 for ( int n
= 0; n
< grid
->GetNumberCols(); )
616 if ( grid
->IsInSelection( 0 , n
) )
617 grid
->DeleteCols( n
, 1 );
625 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
630 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
632 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
635 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
637 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
640 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
642 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
645 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
647 wxColour col
= wxGetColourFromUser(this);
650 grid
->SetDefaultCellTextColour(col
);
655 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
657 wxColour col
= wxGetColourFromUser(this);
660 grid
->SetDefaultCellBackgroundColour(col
);
665 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
667 grid
->DeselectCell(3, 1);
670 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
672 grid
->DeselectCol(2);
675 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
677 grid
->DeselectRow(2);
680 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
682 grid
->ClearSelection();
685 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
687 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
690 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
692 grid
->SelectCol(2, m_addToSel
);
695 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
697 grid
->SelectRow(2, m_addToSel
);
700 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
705 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
707 m_addToSel
= event
.IsChecked();
710 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
713 if ( ev
.GetRow() != -1 )
715 logBuf
<< "Left click on row label " << ev
.GetRow();
717 else if ( ev
.GetCol() != -1 )
719 logBuf
<< "Left click on col label " << ev
.GetCol();
723 logBuf
<< "Left click on corner label";
726 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
727 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
728 wxLogMessage( wxT("%s"), logBuf
.c_str() );
730 // you must call event skip if you want default grid processing
736 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
739 logBuf
<< "Left click at row " << ev
.GetRow()
740 << " col " << ev
.GetCol();
741 wxLogMessage( wxT("%s"), logBuf
.c_str() );
743 // you must call event skip if you want default grid processing
744 // (cell highlighting etc.)
750 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
753 logBuf
<< "Resized row " << ev
.GetRowOrCol();
754 wxLogMessage( wxT("%s"), logBuf
.c_str() );
760 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
763 logBuf
<< "Resized col " << ev
.GetRowOrCol();
764 wxLogMessage( wxT("%s"), logBuf
.c_str() );
770 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
773 if ( ev
.Selecting() )
774 logBuf
<< "Selected ";
776 logBuf
<< "Deselected ";
777 logBuf
<< "cell at row " << ev
.GetRow()
778 << " col " << ev
.GetCol()
779 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
780 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
781 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
782 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
783 wxLogMessage( wxT("%s"), logBuf
.c_str() );
785 // you must call Skip() if you want the default processing
786 // to occur in wxGrid
790 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
793 if ( ev
.Selecting() )
794 logBuf
<< "Selected ";
796 logBuf
<< "Deselected ";
797 logBuf
<< "cells from row " << ev
.GetTopRow()
798 << " col " << ev
.GetLeftCol()
799 << " to row " << ev
.GetBottomRow()
800 << " col " << ev
.GetRightCol()
801 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
802 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
803 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
804 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
805 wxLogMessage( wxT("%s"), logBuf
.c_str() );
810 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
813 logBuf
<< "Value changed for cell at"
814 << " row " << ev
.GetRow()
815 << " col " << ev
.GetCol();
817 wxLogMessage( wxT("%s"), logBuf
.c_str() );
822 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
825 if ( (ev
.GetCol() == 4) &&
826 (ev
.GetRow() == 0) &&
827 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
828 _T("Checking"),wxYES_NO
) == wxNO
) ) {
834 wxLogMessage( wxT("Cell editor shown.") );
839 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
842 if ( (ev
.GetCol() == 4) &&
843 (ev
.GetRow() == 0) &&
844 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
845 _T("Checking"),wxYES_NO
) == wxNO
) ) {
851 wxLogMessage( wxT("Cell editor hidden.") );
856 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
858 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
860 "mbedward@ozemail.com.au \n\n",
866 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
871 void GridFrame::OnBugsTable(wxCommandEvent
& )
873 BugsGridFrame
*frame
= new BugsGridFrame
;
878 void GridFrame::OnVTable(wxCommandEvent
& )
880 static long s_sizeGrid
= 10000;
883 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
886 s
= wxGetTextFromUser( "Size of the table to create",
890 s
.ToLong( &s_sizeGrid
);
893 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
895 "wxGridDemo question",
900 if ( s_sizeGrid
!= -1 )
902 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
907 // ----------------------------------------------------------------------------
908 // MyGridCellRenderer
909 // ----------------------------------------------------------------------------
911 // do something that the default renderer doesn't here just to show that it is
912 // possible to alter the appearance of the cell beyond what the attributes
914 void MyGridCellRenderer::Draw(wxGrid
& grid
,
915 wxGridCellAttr
& attr
,
921 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
923 dc
.SetPen(*wxGREEN_PEN
);
924 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
925 dc
.DrawEllipse(rect
);
928 // ----------------------------------------------------------------------------
929 // MyGridCellAttrProvider
930 // ----------------------------------------------------------------------------
932 MyGridCellAttrProvider::MyGridCellAttrProvider()
934 m_attrForOddRows
= new wxGridCellAttr
;
935 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
938 MyGridCellAttrProvider::~MyGridCellAttrProvider()
940 m_attrForOddRows
->DecRef();
943 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
944 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
946 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
952 attr
= m_attrForOddRows
;
957 if ( !attr
->HasBackgroundColour() )
959 wxGridCellAttr
*attrNew
= attr
->Clone();
962 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
970 // ============================================================================
971 // BigGridFrame and BigGridTable: Sample of a non-standard table
972 // ============================================================================
974 BigGridFrame::BigGridFrame(long sizeGrid
)
975 : wxFrame(NULL
, -1, "Plugin Virtual Table",
976 wxDefaultPosition
, wxSize(500, 450))
978 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
979 m_table
= new BigGridTable(sizeGrid
);
981 // VZ: I don't understand why this slows down the display that much,
982 // must profile it...
983 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
985 m_grid
->SetTable(m_table
, TRUE
);
987 #if defined __WXMOTIF__
988 // MB: the grid isn't getting a sensible default size under wxMotif
990 GetClientSize( &cw
, &ch
);
991 m_grid
->SetSize( cw
, ch
);
995 // ============================================================================
996 // BugsGridFrame: a "realistic" table
997 // ============================================================================
999 // ----------------------------------------------------------------------------
1001 // ----------------------------------------------------------------------------
1024 static const wxString severities
[] =
1033 static struct BugsGridData
1039 wxChar platform
[12];
1041 } gs_dataBugsGrid
[] =
1043 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
1044 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
1045 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
1046 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
1049 static const wxChar
*headers
[Col_Max
] =
1059 // ----------------------------------------------------------------------------
1061 // ----------------------------------------------------------------------------
1063 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1069 return wxGRID_VALUE_NUMBER
;;
1072 // fall thorugh (TODO should be a list)
1075 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1078 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1081 return wxGRID_VALUE_BOOL
;
1084 wxFAIL_MSG(_T("unknown column"));
1086 return wxEmptyString
;
1089 int BugsGridTable::GetNumberRows()
1091 return WXSIZEOF(gs_dataBugsGrid
);
1094 int BugsGridTable::GetNumberCols()
1099 bool BugsGridTable::IsEmptyCell( int row
, int col
)
1104 wxString
BugsGridTable::GetValue( int row
, int col
)
1106 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1113 wxFAIL_MSG(_T("unexpected column"));
1117 return severities
[gd
.severity
];
1126 return wxEmptyString
;
1129 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1131 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1138 wxFAIL_MSG(_T("unexpected column"));
1144 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1146 if ( severities
[n
] == value
)
1148 gd
.severity
= (Severity
)n
;
1153 if ( n
== WXSIZEOF(severities
) )
1155 wxLogWarning(_T("Invalid severity value '%s'."),
1157 gd
.severity
= Sev_Normal
;
1163 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1167 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1172 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1174 if ( typeName
== wxGRID_VALUE_STRING
)
1178 else if ( typeName
== wxGRID_VALUE_BOOL
)
1180 return col
== Col_Opened
;
1182 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1184 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1192 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1194 return CanGetValueAs(row
, col
, typeName
);
1197 long BugsGridTable::GetValueAsLong( int row
, int col
)
1199 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1213 wxFAIL_MSG(_T("unexpected column"));
1218 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1220 if ( col
== Col_Opened
)
1222 return gs_dataBugsGrid
[row
].opened
;
1226 wxFAIL_MSG(_T("unexpected column"));
1232 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1234 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1243 wxFAIL_MSG(_T("unexpected column"));
1247 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1249 if ( col
== Col_Opened
)
1251 gs_dataBugsGrid
[row
].opened
= value
;
1255 wxFAIL_MSG(_T("unexpected column"));
1259 wxString
BugsGridTable::GetColLabelValue( int col
)
1261 return headers
[col
];
1264 BugsGridTable::BugsGridTable()
1268 // ----------------------------------------------------------------------------
1270 // ----------------------------------------------------------------------------
1272 BugsGridFrame::BugsGridFrame()
1273 : wxFrame(NULL
, -1, "Bugs table",
1274 wxDefaultPosition
, wxSize(500, 300))
1276 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1277 wxGridTableBase
*table
= new BugsGridTable();
1278 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1279 grid
->SetTable(table
, TRUE
);
1281 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1282 *attrRangeEditor
= new wxGridCellAttr
,
1283 *attrCombo
= new wxGridCellAttr
;
1285 attrRO
->SetReadOnly();
1286 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1287 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1290 grid
->SetColAttr(Col_Id
, attrRO
);
1291 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1292 grid
->SetColAttr(Col_Severity
, attrCombo
);
1294 grid
->SetMargins(0, 0);
1297 SetClientSize(grid
->GetSize());