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_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
72 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
73 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
74 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
75 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
76 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
77 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
78 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
79 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
80 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
81 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
82 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
83 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
84 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
85 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
86 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
87 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
88 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
89 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
90 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
92 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
93 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
95 EVT_MENU( ID_ABOUT
, GridFrame::About
)
96 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
97 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
98 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
100 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
101 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
102 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
103 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
104 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
105 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
106 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
107 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
108 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
110 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
111 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
112 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
113 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
114 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
115 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
116 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
118 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
119 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
123 GridFrame::GridFrame()
124 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
128 int gridW
= 600, gridH
= 300;
129 int logW
= gridW
, logH
= 100;
131 wxMenu
*fileMenu
= new wxMenu
;
132 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
133 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
134 fileMenu
->AppendSeparator();
135 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
137 wxMenu
*viewMenu
= new wxMenu
;
138 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
139 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
140 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
141 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
142 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
143 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, "&Grid drag-resize", "", TRUE
);
144 viewMenu
->Append( ID_TOGGLEGRIDLINES
, "&Grid Lines", "", TRUE
);
145 viewMenu
->Append( ID_AUTOSIZECOLS
, "&Auto-size cols" );
147 wxMenu
*rowLabelMenu
= new wxMenu
;
149 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
151 "Change alignment of row labels" );
153 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
154 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
156 wxMenu
*colLabelMenu
= new wxMenu
;
158 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
160 "Change alignment of col labels" );
162 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
163 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
165 wxMenu
*colMenu
= new wxMenu
;
166 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
167 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
168 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
169 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
170 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
172 wxMenu
*editMenu
= new wxMenu
;
173 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
174 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
175 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
176 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
177 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
179 wxMenu
*selectMenu
= new wxMenu
;
180 selectMenu
->Append( ID_SELECT_UNSELECT
, "Add new cells to the selection",
181 "When off, old selection is deselected before "
182 "selecting the new cells", TRUE
);
183 selectMenu
->Append( ID_SELECT_ALL
, "Select all");
184 selectMenu
->Append( ID_SELECT_ROW
, "Select row 2");
185 selectMenu
->Append( ID_SELECT_COL
, "Select col 2");
186 selectMenu
->Append( ID_SELECT_CELL
, "Select cell (3, 1)");
187 selectMenu
->Append( ID_DESELECT_ALL
, "Deselect all");
188 selectMenu
->Append( ID_DESELECT_ROW
, "Deselect row 2");
189 selectMenu
->Append( ID_DESELECT_COL
, "Deselect col 2");
190 selectMenu
->Append( ID_DESELECT_CELL
, "Deselect cell (3, 1)");
191 wxMenu
*selectionMenu
= new wxMenu
;
192 selectMenu
->Append( ID_CHANGESEL
, "Change &selection mode",
194 "Change selection mode" );
196 selectionMenu
->Append( ID_SELCELLS
, "Select &Cells" );
197 selectionMenu
->Append( ID_SELROWS
, "Select &Rows" );
198 selectionMenu
->Append( ID_SELCOLS
, "Select C&ols" );
201 wxMenu
*helpMenu
= new wxMenu
;
202 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
204 wxMenuBar
*menuBar
= new wxMenuBar
;
205 menuBar
->Append( fileMenu
, "&File" );
206 menuBar
->Append( viewMenu
, "&View" );
207 menuBar
->Append( colMenu
, "&Colours" );
208 menuBar
->Append( editMenu
, "&Edit" );
209 menuBar
->Append( selectMenu
, "&Select" );
210 menuBar
->Append( helpMenu
, "&Help" );
212 SetMenuBar( menuBar
);
216 grid
= new wxGrid( this,
219 wxSize( 400, 300 ) );
221 logWin
= new wxTextCtrl( this,
224 wxPoint( 0, gridH
+ 20 ),
225 wxSize( logW
, logH
),
228 logger
= new wxLogTextCtrl( logWin
);
229 m_logOld
= logger
->SetActiveTarget( logger
);
230 logger
->SetTimestamp( NULL
);
232 // this will create a grid and, by default, an associated grid
234 grid
->CreateGrid( 100, 100 );
236 grid
->SetRowSize( 0, 60 );
237 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
239 grid
->SetCellValue( 0, 1, "Blah" );
240 grid
->SetCellValue( 0, 2, "Blah" );
241 grid
->SetCellValue( 0, 3, "Read only" );
242 grid
->SetReadOnly( 0, 3 );
244 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
246 grid
->SetRowSize( 99, 60 );
247 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
249 grid
->SetCellValue(2, 2, "red");
251 grid
->SetCellTextColour(2, 2, *wxRED
);
252 grid
->SetCellValue(3, 3, "green on grey");
253 grid
->SetCellTextColour(3, 3, *wxGREEN
);
254 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
256 grid
->SetCellValue(4, 4, "a weird looking cell");
257 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
258 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
260 grid
->SetCellValue(3, 0, "1");
261 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
262 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
264 wxGridCellAttr
*attr
;
265 attr
= new wxGridCellAttr
;
266 attr
->SetTextColour(*wxBLUE
);
267 grid
->SetColAttr(5, attr
);
268 attr
= new wxGridCellAttr
;
269 attr
->SetBackgroundColour(*wxBLUE
);
270 grid
->SetRowAttr(5, attr
);
272 grid
->SetCellValue(2, 4, "a wider column");
273 grid
->SetColSize(4, 120);
274 grid
->SetColMinimalWidth(4, 120);
276 grid
->SetColFormatFloat(5);
277 grid
->SetCellValue(0, 5, "3.1415");
278 grid
->SetCellValue(1, 5, "1415");
279 grid
->SetCellValue(2, 5, "12345.67890");
281 grid
->SetColFormatFloat(6, 6, 2);
282 grid
->SetCellValue(0, 6, "3.1415");
283 grid
->SetCellValue(1, 6, "1415");
284 grid
->SetCellValue(2, 6, "12345.67890");
286 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
291 topSizer
->Add( logWin
,
295 SetAutoLayout( TRUE
);
296 SetSizer( topSizer
);
298 topSizer
->Fit( this );
299 topSizer
->SetSizeHints( this );
306 GridFrame::~GridFrame()
308 delete wxLog::SetActiveTarget(m_logOld
);
312 void GridFrame::SetDefaults()
314 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
315 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
316 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
317 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
318 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
319 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
320 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
324 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
326 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
328 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
332 grid
->SetRowLabelSize( 0 );
337 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
339 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
341 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
345 grid
->SetColLabelSize( 0 );
350 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
353 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
357 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
359 grid
->EnableDragRowSize(
360 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
364 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
366 grid
->EnableDragColSize(
367 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
370 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
372 grid
->EnableDragGridSize(
373 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
377 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
379 grid
->EnableGridLines(
380 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
384 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
386 grid
->AutoSizeColumns();
391 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
393 wxColourDialog
dlg( NULL
);
394 if ( dlg
.ShowModal() == wxID_OK
)
396 wxColourData retData
;
397 retData
= dlg
.GetColourData();
398 wxColour colour
= retData
.GetColour();
400 grid
->SetLabelBackgroundColour( colour
);
405 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
407 wxColourDialog
dlg( NULL
);
408 if ( dlg
.ShowModal() == wxID_OK
)
410 wxColourData retData
;
411 retData
= dlg
.GetColourData();
412 wxColour colour
= retData
.GetColour();
414 grid
->SetLabelTextColour( colour
);
419 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
422 grid
->GetRowLabelAlignment( &horiz
, &vert
);
427 horiz
= wxALIGN_CENTRE
;
431 horiz
= wxALIGN_RIGHT
;
435 horiz
= wxALIGN_LEFT
;
439 grid
->SetRowLabelAlignment( horiz
, -1 );
442 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
445 grid
->GetRowLabelAlignment( &horiz
, &vert
);
450 vert
= wxALIGN_CENTRE
;
454 vert
= wxALIGN_BOTTOM
;
462 grid
->SetRowLabelAlignment( -1, vert
);
466 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
469 grid
->GetColLabelAlignment( &horiz
, &vert
);
474 horiz
= wxALIGN_CENTRE
;
478 horiz
= wxALIGN_RIGHT
;
482 horiz
= wxALIGN_LEFT
;
486 grid
->SetColLabelAlignment( horiz
, -1 );
490 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
493 grid
->GetColLabelAlignment( &horiz
, &vert
);
498 vert
= wxALIGN_CENTRE
;
502 vert
= wxALIGN_BOTTOM
;
510 grid
->SetColLabelAlignment( -1, vert
);
514 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
516 wxColourDialog
dlg( NULL
);
517 if ( dlg
.ShowModal() == wxID_OK
)
519 wxColourData retData
;
520 retData
= dlg
.GetColourData();
521 wxColour colour
= retData
.GetColour();
523 grid
->SetGridLineColour( colour
);
528 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
530 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
534 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
536 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
540 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
542 if ( grid
->IsSelection() )
545 for ( int n
= 0; n
< grid
->GetNumberRows(); )
546 if ( grid
->IsInSelection( n
, 0 ) )
547 grid
->DeleteRows( n
, 1 );
555 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
557 if ( grid
->IsSelection() )
560 for ( int n
= 0; n
< grid
->GetNumberCols(); )
561 if ( grid
->IsInSelection( 0 , n
) )
562 grid
->DeleteCols( n
, 1 );
570 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
575 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
577 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
580 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
582 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
585 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
587 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
590 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
592 wxColour col
= wxGetColourFromUser(this);
595 grid
->SetDefaultCellTextColour(col
);
600 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
602 wxColour col
= wxGetColourFromUser(this);
605 grid
->SetDefaultCellBackgroundColour(col
);
610 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
612 grid
->DeselectCell(3, 1);
615 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
617 grid
->DeselectCol(2);
620 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
622 grid
->DeselectRow(2);
625 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
627 grid
->ClearSelection();
630 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
632 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
635 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
637 grid
->SelectCol(2, m_addToSel
);
640 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
642 grid
->SelectRow(2, m_addToSel
);
645 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
650 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
652 m_addToSel
= event
.IsChecked();
655 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
658 if ( ev
.GetRow() != -1 )
660 logBuf
<< "Left click on row label " << ev
.GetRow();
662 else if ( ev
.GetCol() != -1 )
664 logBuf
<< "Left click on col label " << ev
.GetCol();
668 logBuf
<< "Left click on corner label";
671 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
672 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
673 wxLogMessage( "%s", logBuf
.c_str() );
675 // you must call event skip if you want default grid processing
681 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
684 logBuf
<< "Left click at row " << ev
.GetRow()
685 << " col " << ev
.GetCol();
686 wxLogMessage( "%s", logBuf
.c_str() );
688 // you must call event skip if you want default grid processing
689 // (cell highlighting etc.)
695 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
698 logBuf
<< "Resized row " << ev
.GetRowOrCol();
699 wxLogMessage( "%s", logBuf
.c_str() );
705 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
708 logBuf
<< "Resized col " << ev
.GetRowOrCol();
709 wxLogMessage( "%s", logBuf
.c_str() );
715 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
718 if ( ev
.Selecting() )
719 logBuf
<< "Selected ";
721 logBuf
<< "Deselected ";
722 logBuf
<< "cell at row " << ev
.GetRow()
723 << " col " << ev
.GetCol()
724 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
725 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
726 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
727 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
728 wxLogMessage( "%s", logBuf
.c_str() );
730 // you must call Skip() if you want the default processing
731 // to occur in wxGrid
735 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
738 if ( ev
.Selecting() )
739 logBuf
<< "Selected ";
741 logBuf
<< "Deselected ";
742 logBuf
<< "cells from row " << ev
.GetTopRow()
743 << " col " << ev
.GetLeftCol()
744 << " to row " << ev
.GetBottomRow()
745 << " col " << ev
.GetRightCol()
746 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
747 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
748 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
749 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
750 wxLogMessage( "%s", logBuf
.c_str() );
755 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
758 logBuf
<< "Value changed for cell at"
759 << " row " << ev
.GetRow()
760 << " col " << ev
.GetCol();
762 wxLogMessage( "%s", logBuf
.c_str() );
767 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
769 wxLogMessage( "Cell editor shown." );
774 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
776 wxLogMessage( "Cell editor hidden." );
781 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
783 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
785 "mbedward@ozemail.com.au \n\n",
791 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
796 void GridFrame::OnBugsTable(wxCommandEvent
& )
798 BugsGridFrame
*frame
= new BugsGridFrame
;
802 void GridFrame::OnVTable(wxCommandEvent
& )
804 static long s_sizeGrid
= 10000;
807 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
810 s
= wxGetTextFromUser( "Size of the table to create",
814 s
.ToLong( &s_sizeGrid
);
817 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
819 "wxGridDemo question",
824 if ( s_sizeGrid
!= -1 )
826 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
831 // ----------------------------------------------------------------------------
832 // MyGridCellRenderer
833 // ----------------------------------------------------------------------------
835 // do something that the default renderer doesn't here just to show that it is
836 // possible to alter the appearance of the cell beyond what the attributes
838 void MyGridCellRenderer::Draw(wxGrid
& grid
,
839 wxGridCellAttr
& attr
,
845 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
847 dc
.SetPen(*wxGREEN_PEN
);
848 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
849 dc
.DrawEllipse(rect
);
852 // ----------------------------------------------------------------------------
853 // MyGridCellAttrProvider
854 // ----------------------------------------------------------------------------
856 MyGridCellAttrProvider::MyGridCellAttrProvider()
858 m_attrForOddRows
= new wxGridCellAttr
;
859 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
862 MyGridCellAttrProvider::~MyGridCellAttrProvider()
864 m_attrForOddRows
->DecRef();
867 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
) const
869 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
);
875 attr
= m_attrForOddRows
;
880 if ( !attr
->HasBackgroundColour() )
882 wxGridCellAttr
*attrNew
= attr
->Clone();
885 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
893 // ============================================================================
894 // BigGridFrame and BigGridTable: Sample of a non-standard table
895 // ============================================================================
897 BigGridFrame::BigGridFrame(long sizeGrid
)
898 : wxFrame(NULL
, -1, "Plugin Virtual Table",
899 wxDefaultPosition
, wxSize(500, 450))
901 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
902 m_table
= new BigGridTable(sizeGrid
);
904 // VZ: I don't understand why this slows down the display that much,
905 // must profile it...
906 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
908 m_grid
->SetTable(m_table
, TRUE
);
910 #if defined __WXMOTIF__
911 // MB: the grid isn't getting a sensible default size under wxMotif
913 GetClientSize( &cw
, &ch
);
914 m_grid
->SetSize( cw
, ch
);
918 // ============================================================================
919 // BugsGridFrame: a "realistic" table
920 // ============================================================================
922 // ----------------------------------------------------------------------------
924 // ----------------------------------------------------------------------------
947 static const wxString severities
[] =
956 static struct BugsGridData
964 } gs_dataBugsGrid
[] =
966 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
967 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
968 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
969 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
972 static const wxChar
*headers
[Col_Max
] =
982 // ----------------------------------------------------------------------------
984 // ----------------------------------------------------------------------------
986 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
992 return wxGRID_VALUE_NUMBER
;;
995 // fall thorugh (TODO should be a list)
998 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1001 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1004 return wxGRID_VALUE_BOOL
;
1007 wxFAIL_MSG(_T("unknown column"));
1009 return wxEmptyString
;
1012 int BugsGridTable::GetNumberRows()
1014 return WXSIZEOF(gs_dataBugsGrid
);
1017 int BugsGridTable::GetNumberCols()
1022 bool BugsGridTable::IsEmptyCell( int row
, int col
)
1027 wxString
BugsGridTable::GetValue( int row
, int col
)
1029 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1036 wxFAIL_MSG(_T("unexpected column"));
1040 return severities
[gd
.severity
];
1049 return wxEmptyString
;
1052 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1054 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1061 wxFAIL_MSG(_T("unexpected column"));
1067 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1069 if ( severities
[n
] == value
)
1071 gd
.severity
= (Severity
)n
;
1076 if ( n
== WXSIZEOF(severities
) )
1078 wxLogWarning(_T("Invalid severity value '%s'."),
1080 gd
.severity
= Sev_Normal
;
1086 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1090 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1095 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1097 if ( typeName
== wxGRID_VALUE_STRING
)
1101 else if ( typeName
== wxGRID_VALUE_BOOL
)
1103 return col
== Col_Opened
;
1105 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1107 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1115 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1117 return CanGetValueAs(row
, col
, typeName
);
1120 long BugsGridTable::GetValueAsLong( int row
, int col
)
1122 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1136 wxFAIL_MSG(_T("unexpected column"));
1141 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1143 if ( col
== Col_Opened
)
1145 return gs_dataBugsGrid
[row
].opened
;
1149 wxFAIL_MSG(_T("unexpected column"));
1155 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1157 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1166 wxFAIL_MSG(_T("unexpected column"));
1170 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1172 if ( col
== Col_Opened
)
1174 gs_dataBugsGrid
[row
].opened
= value
;
1178 wxFAIL_MSG(_T("unexpected column"));
1182 wxString
BugsGridTable::GetColLabelValue( int col
)
1184 return headers
[col
];
1187 BugsGridTable::BugsGridTable()
1191 // ----------------------------------------------------------------------------
1193 // ----------------------------------------------------------------------------
1195 BugsGridFrame::BugsGridFrame()
1196 : wxFrame(NULL
, -1, "Bugs table",
1197 wxDefaultPosition
, wxSize(500, 300))
1199 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1200 wxGridTableBase
*table
= new BugsGridTable();
1201 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1202 grid
->SetTable(table
, TRUE
);
1204 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1205 *attrRangeEditor
= new wxGridCellAttr
,
1206 *attrCombo
= new wxGridCellAttr
;
1208 attrRO
->SetReadOnly();
1209 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1210 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1213 grid
->SetColAttr(Col_Id
, attrRO
);
1214 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1215 grid
->SetColAttr(Col_Severity
, attrCombo
);
1217 grid
->SetMargins(0, 0);
1220 wxSize size
= grid
->GetSize();
1223 SetClientSize(size
);