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_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
101 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
102 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
103 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
104 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
105 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
106 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
108 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
109 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
113 GridFrame::GridFrame()
114 : wxFrame( (wxFrame
*)NULL
, -1, "wxWindows grid class demo",
118 int gridW
= 600, gridH
= 300;
119 int logW
= gridW
, logH
= 100;
121 wxMenu
*fileMenu
= new wxMenu
;
122 fileMenu
->Append( ID_VTABLE
, "&Virtual table test\tCtrl-V");
123 fileMenu
->Append( ID_BUGS_TABLE
, "&Bugs table test\tCtrl-B");
124 fileMenu
->AppendSeparator();
125 fileMenu
->Append( wxID_EXIT
, "E&xit\tAlt-X" );
127 wxMenu
*viewMenu
= new wxMenu
;
128 viewMenu
->Append( ID_TOGGLEROWLABELS
, "&Row labels", "", TRUE
);
129 viewMenu
->Append( ID_TOGGLECOLLABELS
, "&Col labels", "", TRUE
);
130 viewMenu
->Append( ID_TOGGLEEDIT
, "&Editable", "", TRUE
);
131 viewMenu
->Append( ID_TOGGLEROWSIZING
, "Ro&w drag-resize", "", TRUE
);
132 viewMenu
->Append( ID_TOGGLECOLSIZING
, "C&ol drag-resize", "", TRUE
);
133 viewMenu
->Append( ID_TOGGLEGRIDSIZING
, "&Grid drag-resize", "", TRUE
);
134 viewMenu
->Append( ID_TOGGLEGRIDLINES
, "&Grid Lines", "", TRUE
);
135 viewMenu
->Append( ID_AUTOSIZECOLS
, "&Auto-size cols" );
137 wxMenu
*rowLabelMenu
= new wxMenu
;
139 viewMenu
->Append( ID_ROWLABELALIGN
, "R&ow label alignment",
141 "Change alignment of row labels" );
143 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, "&Horizontal" );
144 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, "&Vertical" );
146 wxMenu
*colLabelMenu
= new wxMenu
;
148 viewMenu
->Append( ID_COLLABELALIGN
, "Col l&abel alignment",
150 "Change alignment of col labels" );
152 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, "&Horizontal" );
153 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, "&Vertical" );
155 wxMenu
*colMenu
= new wxMenu
;
156 colMenu
->Append( ID_SETLABELCOLOUR
, "Set &label colour" );
157 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, "Set label &text colour" );
158 colMenu
->Append( ID_GRIDLINECOLOUR
, "&Grid line colour" );
159 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, "Set cell &foreground colour" );
160 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, "Set cell &background colour" );
162 wxMenu
*editMenu
= new wxMenu
;
163 editMenu
->Append( ID_INSERTROW
, "Insert &row" );
164 editMenu
->Append( ID_INSERTCOL
, "Insert &column" );
165 editMenu
->Append( ID_DELETEROW
, "Delete selected ro&ws" );
166 editMenu
->Append( ID_DELETECOL
, "Delete selected co&ls" );
167 editMenu
->Append( ID_CLEARGRID
, "Cl&ear grid cell contents" );
169 wxMenu
*selectionMenu
= new wxMenu
;
171 editMenu
->Append( ID_CHANGESEL
, "Change &selection mode",
173 "Change selection mode" );
175 selectionMenu
->Append( ID_SELCELLS
, "Select &Cells" );
176 selectionMenu
->Append( ID_SELROWS
, "Select &Rows" );
177 selectionMenu
->Append( ID_SELCOLS
, "Select C&ols" );
179 wxMenu
*helpMenu
= new wxMenu
;
180 helpMenu
->Append( ID_ABOUT
, "&About wxGrid demo" );
182 wxMenuBar
*menuBar
= new wxMenuBar
;
183 menuBar
->Append( fileMenu
, "&File" );
184 menuBar
->Append( viewMenu
, "&View" );
185 menuBar
->Append( colMenu
, "&Colours" );
186 menuBar
->Append( editMenu
, "&Edit" );
187 menuBar
->Append( helpMenu
, "&Help" );
189 SetMenuBar( menuBar
);
191 grid
= new wxGrid( this,
194 wxSize( 400, 300 ) );
196 logWin
= new wxTextCtrl( this,
199 wxPoint( 0, gridH
+ 20 ),
200 wxSize( logW
, logH
),
203 logger
= new wxLogTextCtrl( logWin
);
204 m_logOld
= logger
->SetActiveTarget( logger
);
205 logger
->SetTimestamp( NULL
);
207 // this will create a grid and, by default, an associated grid
209 grid
->CreateGrid( 100, 100 );
211 grid
->SetRowSize( 0, 60 );
212 grid
->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
214 grid
->SetCellValue( 0, 1, "Blah" );
215 grid
->SetCellValue( 0, 2, "Blah" );
216 grid
->SetCellValue( 0, 3, "Read only" );
217 grid
->SetReadOnly( 0, 3 );
219 grid
->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
221 grid
->SetRowSize( 99, 60 );
222 grid
->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
224 grid
->SetCellValue(2, 2, "red");
226 grid
->SetCellTextColour(2, 2, *wxRED
);
227 grid
->SetCellValue(3, 3, "green on grey");
228 grid
->SetCellTextColour(3, 3, *wxGREEN
);
229 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
231 grid
->SetCellValue(4, 4, "a weird looking cell");
232 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
233 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
235 grid
->SetCellValue(3, 0, "1");
236 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
237 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
239 wxGridCellAttr
*attr
;
240 attr
= new wxGridCellAttr
;
241 attr
->SetTextColour(*wxBLUE
);
242 grid
->SetColAttr(5, attr
);
243 attr
= new wxGridCellAttr
;
244 attr
->SetBackgroundColour(*wxBLUE
);
245 grid
->SetRowAttr(5, attr
);
247 grid
->SetCellValue(2, 4, "a wider column");
248 grid
->SetColSize(4, 120);
249 grid
->SetColMinimalWidth(4, 120);
251 grid
->SetColFormatFloat(5);
252 grid
->SetCellValue(0, 5, "3.1415");
253 grid
->SetCellValue(1, 5, "1415");
254 grid
->SetCellValue(2, 5, "12345.67890");
256 grid
->SetColFormatFloat(6, 6, 2);
257 grid
->SetCellValue(0, 6, "3.1415");
258 grid
->SetCellValue(1, 6, "1415");
259 grid
->SetCellValue(2, 6, "12345.67890");
261 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
266 topSizer
->Add( logWin
,
270 SetAutoLayout( TRUE
);
271 SetSizer( topSizer
);
273 topSizer
->Fit( this );
274 topSizer
->SetSizeHints( this );
281 GridFrame::~GridFrame()
283 delete wxLog::SetActiveTarget(m_logOld
);
287 void GridFrame::SetDefaults()
289 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, TRUE
);
290 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, TRUE
);
291 GetMenuBar()->Check( ID_TOGGLEEDIT
, TRUE
);
292 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, TRUE
);
293 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, TRUE
);
294 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, TRUE
);
295 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, TRUE
);
299 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
301 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
303 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
307 grid
->SetRowLabelSize( 0 );
312 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
314 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
316 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
320 grid
->SetColLabelSize( 0 );
325 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
328 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
332 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
334 grid
->EnableDragRowSize(
335 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
339 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
341 grid
->EnableDragColSize(
342 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
345 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
347 grid
->EnableDragGridSize(
348 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
352 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
354 grid
->EnableGridLines(
355 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
359 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
361 grid
->AutoSizeColumns();
366 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
368 wxColourDialog
dlg( NULL
);
369 if ( dlg
.ShowModal() == wxID_OK
)
371 wxColourData retData
;
372 retData
= dlg
.GetColourData();
373 wxColour colour
= retData
.GetColour();
375 grid
->SetLabelBackgroundColour( colour
);
380 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
382 wxColourDialog
dlg( NULL
);
383 if ( dlg
.ShowModal() == wxID_OK
)
385 wxColourData retData
;
386 retData
= dlg
.GetColourData();
387 wxColour colour
= retData
.GetColour();
389 grid
->SetLabelTextColour( colour
);
394 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
397 grid
->GetRowLabelAlignment( &horiz
, &vert
);
402 horiz
= wxALIGN_CENTRE
;
406 horiz
= wxALIGN_RIGHT
;
410 horiz
= wxALIGN_LEFT
;
414 grid
->SetRowLabelAlignment( horiz
, -1 );
417 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
420 grid
->GetRowLabelAlignment( &horiz
, &vert
);
425 vert
= wxALIGN_CENTRE
;
429 vert
= wxALIGN_BOTTOM
;
437 grid
->SetRowLabelAlignment( -1, vert
);
441 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
444 grid
->GetColLabelAlignment( &horiz
, &vert
);
449 horiz
= wxALIGN_CENTRE
;
453 horiz
= wxALIGN_RIGHT
;
457 horiz
= wxALIGN_LEFT
;
461 grid
->SetColLabelAlignment( horiz
, -1 );
465 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
468 grid
->GetColLabelAlignment( &horiz
, &vert
);
473 vert
= wxALIGN_CENTRE
;
477 vert
= wxALIGN_BOTTOM
;
485 grid
->SetColLabelAlignment( -1, vert
);
489 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
491 wxColourDialog
dlg( NULL
);
492 if ( dlg
.ShowModal() == wxID_OK
)
494 wxColourData retData
;
495 retData
= dlg
.GetColourData();
496 wxColour colour
= retData
.GetColour();
498 grid
->SetGridLineColour( colour
);
503 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
505 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
509 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
511 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
515 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
517 if ( grid
->IsSelection() )
520 for ( int n
= 0; n
< grid
->GetNumberRows(); )
521 if ( grid
->IsInSelection( n
, 0 ) )
522 grid
->DeleteRows( n
, 1 );
530 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
532 if ( grid
->IsSelection() )
535 for ( int n
= 0; n
< grid
->GetNumberCols(); )
536 if ( grid
->IsInSelection( 0 , n
) )
537 grid
->DeleteCols( n
, 1 );
545 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
550 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
552 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
555 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
557 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
560 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
562 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
565 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
567 wxColour col
= wxGetColourFromUser(this);
570 grid
->SetDefaultCellTextColour(col
);
575 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
577 wxColour col
= wxGetColourFromUser(this);
580 grid
->SetDefaultCellBackgroundColour(col
);
585 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
588 if ( ev
.GetRow() != -1 )
590 logBuf
<< "Left click on row label " << ev
.GetRow();
592 else if ( ev
.GetCol() != -1 )
594 logBuf
<< "Left click on col label " << ev
.GetCol();
598 logBuf
<< "Left click on corner label";
601 if ( ev
.ShiftDown() ) logBuf
<< " (shift down)";
602 if ( ev
.ControlDown() ) logBuf
<< " (control down)";
603 wxLogMessage( "%s", logBuf
.c_str() );
605 // you must call event skip if you want default grid processing
611 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
614 logBuf
<< "Left click at row " << ev
.GetRow()
615 << " col " << ev
.GetCol();
616 wxLogMessage( "%s", logBuf
.c_str() );
618 // you must call event skip if you want default grid processing
619 // (cell highlighting etc.)
625 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
628 logBuf
<< "Resized row " << ev
.GetRowOrCol();
629 wxLogMessage( "%s", logBuf
.c_str() );
635 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
638 logBuf
<< "Resized col " << ev
.GetRowOrCol();
639 wxLogMessage( "%s", logBuf
.c_str() );
645 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
648 if ( ev
.Selecting() )
649 logBuf
<< "Selected ";
651 logBuf
<< "Deselected ";
652 logBuf
<< "cell at row " << ev
.GetRow()
653 << " col " << ev
.GetCol()
654 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
655 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
656 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
657 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
658 wxLogMessage( "%s", logBuf
.c_str() );
660 // you must call Skip() if you want the default processing
661 // to occur in wxGrid
665 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
668 if ( ev
.Selecting() )
669 logBuf
<< "Selected ";
671 logBuf
<< "Deselected ";
672 logBuf
<< "cells from row " << ev
.GetTopRow()
673 << " col " << ev
.GetLeftCol()
674 << " to row " << ev
.GetBottomRow()
675 << " col " << ev
.GetRightCol()
676 << " ( ControlDown: "<< (ev
.ControlDown() ? 'T':'F')
677 << ", ShiftDown: "<< (ev
.ShiftDown() ? 'T':'F')
678 << ", AltDown: "<< (ev
.AltDown() ? 'T':'F')
679 << ", MetaDown: "<< (ev
.MetaDown() ? 'T':'F') << " )";
680 wxLogMessage( "%s", logBuf
.c_str() );
685 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
688 logBuf
<< "Value changed for cell at"
689 << " row " << ev
.GetRow()
690 << " col " << ev
.GetCol();
692 wxLogMessage( "%s", logBuf
.c_str() );
697 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
699 wxLogMessage( "Cell editor shown." );
704 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
706 wxLogMessage( "Cell editor hidden." );
711 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
713 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
715 "mbedward@ozemail.com.au \n\n",
721 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
726 void GridFrame::OnBugsTable(wxCommandEvent
& )
728 BugsGridFrame
*frame
= new BugsGridFrame
;
732 void GridFrame::OnVTable(wxCommandEvent
& )
734 static long s_sizeGrid
= 10000;
737 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
740 s
= wxGetTextFromUser( "Size of the table to create",
744 s
.ToLong( &s_sizeGrid
);
747 s_sizeGrid
= wxGetNumberFromUser("Size of the table to create",
749 "wxGridDemo question",
754 if ( s_sizeGrid
!= -1 )
756 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
761 // ----------------------------------------------------------------------------
762 // MyGridCellRenderer
763 // ----------------------------------------------------------------------------
765 // do something that the default renderer doesn't here just to show that it is
766 // possible to alter the appearance of the cell beyond what the attributes
768 void MyGridCellRenderer::Draw(wxGrid
& grid
,
769 wxGridCellAttr
& attr
,
775 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
777 dc
.SetPen(*wxGREEN_PEN
);
778 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
779 dc
.DrawEllipse(rect
);
782 // ----------------------------------------------------------------------------
783 // MyGridCellAttrProvider
784 // ----------------------------------------------------------------------------
786 MyGridCellAttrProvider::MyGridCellAttrProvider()
788 m_attrForOddRows
= new wxGridCellAttr
;
789 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
792 MyGridCellAttrProvider::~MyGridCellAttrProvider()
794 m_attrForOddRows
->DecRef();
797 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
) const
799 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
);
805 attr
= m_attrForOddRows
;
810 if ( !attr
->HasBackgroundColour() )
812 wxGridCellAttr
*attrNew
= attr
->Clone();
815 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
823 // ============================================================================
824 // BigGridFrame and BigGridTable: Sample of a non-standard table
825 // ============================================================================
827 BigGridFrame::BigGridFrame(long sizeGrid
)
828 : wxFrame(NULL
, -1, "Plugin Virtual Table",
829 wxDefaultPosition
, wxSize(500, 450))
831 m_grid
= new wxGrid(this, -1, wxDefaultPosition
, wxDefaultSize
);
832 m_table
= new BigGridTable(sizeGrid
);
834 // VZ: I don't understand why this slows down the display that much,
835 // must profile it...
836 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
838 m_grid
->SetTable(m_table
, TRUE
);
840 #if defined __WXMOTIF__
841 // MB: the grid isn't getting a sensible default size under wxMotif
843 GetClientSize( &cw
, &ch
);
844 m_grid
->SetSize( cw
, ch
);
848 // ============================================================================
849 // BugsGridFrame: a "realistic" table
850 // ============================================================================
852 // ----------------------------------------------------------------------------
854 // ----------------------------------------------------------------------------
877 static const wxString severities
[] =
886 static struct BugsGridData
894 } gs_dataBugsGrid
[] =
896 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), TRUE
},
897 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), FALSE
},
898 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), TRUE
},
899 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), FALSE
},
902 static const wxChar
*headers
[Col_Max
] =
912 // ----------------------------------------------------------------------------
914 // ----------------------------------------------------------------------------
916 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
922 return wxGRID_VALUE_NUMBER
;;
925 // fall thorugh (TODO should be a list)
928 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
931 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
934 return wxGRID_VALUE_BOOL
;
937 wxFAIL_MSG(_T("unknown column"));
939 return wxEmptyString
;
942 int BugsGridTable::GetNumberRows()
944 return WXSIZEOF(gs_dataBugsGrid
);
947 int BugsGridTable::GetNumberCols()
952 bool BugsGridTable::IsEmptyCell( int row
, int col
)
957 wxString
BugsGridTable::GetValue( int row
, int col
)
959 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
966 wxFAIL_MSG(_T("unexpected column"));
970 return severities
[gd
.severity
];
979 return wxEmptyString
;
982 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
984 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
991 wxFAIL_MSG(_T("unexpected column"));
997 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
999 if ( severities
[n
] == value
)
1001 gd
.severity
= (Severity
)n
;
1006 if ( n
== WXSIZEOF(severities
) )
1008 wxLogWarning(_T("Invalid severity value '%s'."),
1010 gd
.severity
= Sev_Normal
;
1016 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1020 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1025 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row
), int col
, const wxString
& typeName
)
1027 if ( typeName
== wxGRID_VALUE_STRING
)
1031 else if ( typeName
== wxGRID_VALUE_BOOL
)
1033 return col
== Col_Opened
;
1035 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1037 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1045 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1047 return CanGetValueAs(row
, col
, typeName
);
1050 long BugsGridTable::GetValueAsLong( int row
, int col
)
1052 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1066 wxFAIL_MSG(_T("unexpected column"));
1071 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1073 if ( col
== Col_Opened
)
1075 return gs_dataBugsGrid
[row
].opened
;
1079 wxFAIL_MSG(_T("unexpected column"));
1085 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1087 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1096 wxFAIL_MSG(_T("unexpected column"));
1100 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1102 if ( col
== Col_Opened
)
1104 gs_dataBugsGrid
[row
].opened
= value
;
1108 wxFAIL_MSG(_T("unexpected column"));
1112 wxString
BugsGridTable::GetColLabelValue( int col
)
1114 return headers
[col
];
1117 BugsGridTable::BugsGridTable()
1121 // ----------------------------------------------------------------------------
1123 // ----------------------------------------------------------------------------
1125 BugsGridFrame::BugsGridFrame()
1126 : wxFrame(NULL
, -1, "Bugs table",
1127 wxDefaultPosition
, wxSize(500, 300))
1129 wxGrid
*grid
= new wxGrid(this, -1, wxDefaultPosition
);
1130 wxGridTableBase
*table
= new BugsGridTable();
1131 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1132 grid
->SetTable(table
, TRUE
);
1134 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1135 *attrRangeEditor
= new wxGridCellAttr
,
1136 *attrCombo
= new wxGridCellAttr
;
1138 attrRO
->SetReadOnly();
1139 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1140 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1143 grid
->SetColAttr(Col_Id
, attrRO
);
1144 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1145 grid
->SetColAttr(Col_Severity
, attrCombo
);
1147 grid
->SetMargins(0, 0);
1150 wxSize size
= grid
->GetSize();
1153 SetClientSize(size
);