1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
5 // Modified by: Santiago Palacios
7 // Copyright: (c) Michael Bedward, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows licence
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"
33 #include "wx/aboutdlg.h"
36 #include "wx/headerctrl.h"
37 #include "wx/generic/gridctrl.h"
38 #include "wx/generic/grideditors.h"
43 #include "../sample.xpm"
46 // Custom renderer that renders column header cells without borders and in
48 class CustomColumnHeaderRenderer
: public wxGridColumnHeaderRenderer
51 CustomColumnHeaderRenderer(const wxColour
& colFg
, const wxColour
& colBg
)
57 virtual void DrawLabel(const wxGrid
& WXUNUSED(grid
),
59 const wxString
& value
,
63 int WXUNUSED(textOrientation
)) const
65 dc
.SetTextForeground(m_colFg
);
66 dc
.SetFont(wxITALIC_FONT
->Bold());
67 dc
.DrawLabel(value
, rect
, horizAlign
| vertAlign
);
70 virtual void DrawBorder(const wxGrid
& WXUNUSED(grid
),
74 dc
.SetPen(*wxTRANSPARENT_PEN
);
75 dc
.SetBrush(wxBrush(m_colBg
));
76 dc
.DrawRectangle(rect
);
80 const wxColour m_colFg
, m_colBg
;
82 wxDECLARE_NO_COPY_CLASS(CustomColumnHeaderRenderer
);
85 // And a custom attributes provider which uses custom column header renderer
87 class CustomColumnHeadersProvider
: public wxGridCellAttrProvider
90 // by default custom column renderer is not used, call
91 // UseCustomColHeaders() to enable it
92 CustomColumnHeadersProvider()
93 : m_customOddRenderer(*wxYELLOW
, *wxBLUE
),
94 m_customEvenRenderer(*wxWHITE
, *wxBLACK
),
99 // enable or disable the use of custom renderer for column headers
100 void UseCustomColHeaders(bool use
= true) { m_useCustom
= use
; }
103 virtual const wxGridColumnHeaderRenderer
& GetColumnHeaderRenderer(int col
)
105 // if enabled, use custom renderers
108 // and use different ones for odd and even columns -- just to show
110 return col
% 2 ? m_customOddRenderer
: m_customEvenRenderer
;
113 return wxGridCellAttrProvider::GetColumnHeaderRenderer(col
);
117 CustomColumnHeaderRenderer m_customOddRenderer
,
118 m_customEvenRenderer
;
122 wxDECLARE_NO_COPY_CLASS(CustomColumnHeadersProvider
);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 IMPLEMENT_APP( GridApp
)
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 bool GridApp::OnInit()
141 GridFrame
*frame
= new GridFrame
;
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
152 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
153 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
154 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
155 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
156 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
157 EVT_MENU( ID_TOGGLECOLMOVING
, GridFrame::ToggleColMoving
)
158 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
159 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
160 EVT_MENU( ID_COLNATIVEHEADER
, GridFrame::SetNativeColHeader
)
161 EVT_MENU( ID_COLDEFAULTHEADER
, GridFrame::SetDefaultColHeader
)
162 EVT_MENU( ID_COLCUSTOMHEADER
, GridFrame::SetCustomColHeader
)
163 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
164 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
165 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
166 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
167 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
168 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
169 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
170 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
171 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
172 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
173 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
174 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
175 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
176 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
177 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
178 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
179 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
180 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
181 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
182 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
183 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
185 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
186 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
188 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
189 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
190 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
191 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
192 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
194 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
195 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
196 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
197 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
198 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
199 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
200 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
201 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
202 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
204 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
205 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
206 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
207 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
208 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
209 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
210 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
212 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
213 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
215 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
216 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
217 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
218 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
219 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
220 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
221 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
222 EVT_GRID_CELL_CHANGED( GridFrame::OnCellValueChanged
)
223 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
225 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
226 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
230 GridFrame::GridFrame()
231 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets grid class demo"),
235 SetIcon(wxICON(sample
));
237 wxMenu
*fileMenu
= new wxMenu
;
238 fileMenu
->Append( ID_VTABLE
, wxT("&Virtual table test\tCtrl-V"));
239 fileMenu
->Append( ID_BUGS_TABLE
, wxT("&Bugs table test\tCtrl-B"));
240 fileMenu
->Append( ID_TABULAR_TABLE
, wxT("&Tabular table test\tCtrl-T"));
241 fileMenu
->AppendSeparator();
242 fileMenu
->Append( wxID_EXIT
, wxT("E&xit\tAlt-X") );
244 wxMenu
*viewMenu
= new wxMenu
;
245 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
246 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
247 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
248 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
249 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
250 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
251 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
252 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
253 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
254 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
255 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
256 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
257 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
258 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
260 wxMenu
*rowLabelMenu
= new wxMenu
;
262 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("R&ow label alignment"),
264 wxT("Change alignment of row labels") );
266 rowLabelMenu
->AppendRadioItem( ID_ROWLABELHORIZALIGN
, wxT("&Horizontal") );
267 rowLabelMenu
->AppendRadioItem( ID_ROWLABELVERTALIGN
, wxT("&Vertical") );
269 wxMenu
*colLabelMenu
= new wxMenu
;
271 viewMenu
->Append( ID_COLLABELALIGN
, wxT("Col l&abel alignment"),
273 wxT("Change alignment of col labels") );
275 colLabelMenu
->AppendRadioItem( ID_COLLABELHORIZALIGN
, wxT("&Horizontal") );
276 colLabelMenu
->AppendRadioItem( ID_COLLABELVERTALIGN
, wxT("&Vertical") );
278 wxMenu
*colHeaderMenu
= new wxMenu
;
280 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("Col header style"),
282 wxT("Change style of col header") );
284 colHeaderMenu
->AppendRadioItem( ID_COLDEFAULTHEADER
, wxT("&Default") );
285 colHeaderMenu
->AppendRadioItem( ID_COLNATIVEHEADER
, wxT("&Native") );
286 colHeaderMenu
->AppendRadioItem( ID_COLCUSTOMHEADER
, wxT("&Custom") );
289 wxMenu
*colMenu
= new wxMenu
;
290 colMenu
->Append( ID_SETLABELCOLOUR
, wxT("Set &label colour...") );
291 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, wxT("Set label &text colour...") );
292 colMenu
->Append( ID_SETLABEL_FONT
, wxT("Set label fo&nt...") );
293 colMenu
->Append( ID_GRIDLINECOLOUR
, wxT("&Grid line colour...") );
294 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, wxT("Set cell &foreground colour...") );
295 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, wxT("Set cell &background colour...") );
297 wxMenu
*editMenu
= new wxMenu
;
298 editMenu
->Append( ID_INSERTROW
, wxT("Insert &row") );
299 editMenu
->Append( ID_INSERTCOL
, wxT("Insert &column") );
300 editMenu
->Append( ID_DELETEROW
, wxT("Delete selected ro&ws") );
301 editMenu
->Append( ID_DELETECOL
, wxT("Delete selected co&ls") );
302 editMenu
->Append( ID_CLEARGRID
, wxT("Cl&ear grid cell contents") );
304 wxMenu
*selectMenu
= new wxMenu
;
305 selectMenu
->Append( ID_SELECT_UNSELECT
, wxT("Add new cells to the selection"),
306 wxT("When off, old selection is deselected before ")
307 wxT("selecting the new cells"), wxITEM_CHECK
);
308 selectMenu
->AppendSeparator();
309 selectMenu
->Append( ID_SELECT_ALL
, wxT("Select all"));
310 selectMenu
->Append( ID_SELECT_ROW
, wxT("Select row 2"));
311 selectMenu
->Append( ID_SELECT_COL
, wxT("Select col 2"));
312 selectMenu
->Append( ID_SELECT_CELL
, wxT("Select cell (3, 1)"));
313 selectMenu
->AppendSeparator();
314 selectMenu
->Append( ID_DESELECT_ALL
, wxT("Deselect all"));
315 selectMenu
->Append( ID_DESELECT_ROW
, wxT("Deselect row 2"));
316 selectMenu
->Append( ID_DESELECT_COL
, wxT("Deselect col 2"));
317 selectMenu
->Append( ID_DESELECT_CELL
, wxT("Deselect cell (3, 1)"));
318 wxMenu
*selectionMenu
= new wxMenu
;
319 selectMenu
->Append( ID_CHANGESEL
, wxT("Change &selection mode"),
321 wxT("Change selection mode") );
323 selectionMenu
->Append( ID_SELCELLS
, wxT("Select &cells") );
324 selectionMenu
->Append( ID_SELROWS
, wxT("Select &rows") );
325 selectionMenu
->Append( ID_SELCOLS
, wxT("Select col&umns") );
326 selectionMenu
->Append( ID_SELROWSORCOLS
, wxT("Select rows &or columns") );
328 wxMenu
*autosizeMenu
= new wxMenu
;
329 autosizeMenu
->Append( ID_SIZE_ROW
, wxT("Selected &row data") );
330 autosizeMenu
->Append( ID_SIZE_COL
, wxT("Selected &column data") );
331 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, wxT("Selected row la&bel") );
332 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, wxT("Selected column &label") );
333 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, wxT("Column la&bels") );
334 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, wxT("Row label&s") );
335 autosizeMenu
->Append( ID_SIZE_GRID
, wxT("Entire &grid") );
337 wxMenu
*helpMenu
= new wxMenu
;
338 helpMenu
->Append( wxID_ABOUT
, wxT("&About wxGrid demo") );
340 wxMenuBar
*menuBar
= new wxMenuBar
;
341 menuBar
->Append( fileMenu
, wxT("&File") );
342 menuBar
->Append( viewMenu
, wxT("&Grid") );
343 menuBar
->Append( colMenu
, wxT("&Colours") );
344 menuBar
->Append( editMenu
, wxT("&Edit") );
345 menuBar
->Append( selectMenu
, wxT("&Select") );
346 menuBar
->Append( autosizeMenu
, wxT("&Autosize") );
347 menuBar
->Append( helpMenu
, wxT("&Help") );
349 SetMenuBar( menuBar
);
353 grid
= new wxGrid( this,
356 wxSize( 400, 300 ) );
360 int gridW
= 600, gridH
= 300;
361 int logW
= gridW
, logH
= 100;
363 logWin
= new wxTextCtrl( this,
366 wxPoint( 0, gridH
+ 20 ),
367 wxSize( logW
, logH
),
370 logger
= new wxLogTextCtrl( logWin
);
371 m_logOld
= wxLog::SetActiveTarget( logger
);
372 wxLog::DisableTimestamp();
375 // this will create a grid and, by default, an associated grid
377 grid
->CreateGrid( 0, 0 );
379 grid
->GetTable()->SetAttrProvider(new CustomColumnHeadersProvider());
381 grid
->AppendRows(100);
382 grid
->AppendCols(100);
384 int ir
= grid
->GetNumberRows();
385 grid
->DeleteRows(0, ir
);
386 grid
->AppendRows(ir
);
388 grid
->SetRowSize( 0, 60 );
389 grid
->SetCellValue( 0, 0, wxT("Ctrl+Home\nwill go to\nthis cell") );
391 grid
->SetCellValue( 0, 1, wxT("A long piece of text to demonstrate wrapping.") );
392 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
393 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
395 grid
->SetCellValue( 0, 2, wxT("Blah") );
396 grid
->SetCellValue( 0, 3, wxT("Read only") );
397 grid
->SetReadOnly( 0, 3 );
399 grid
->SetCellValue( 0, 4, wxT("Can veto edit this cell") );
401 grid
->SetCellValue( 0, 5, wxT("Press\nCtrl+arrow\nto skip over\ncells") );
403 grid
->SetRowSize( 99, 60 );
404 grid
->SetCellValue( 99, 99, wxT("Ctrl+End\nwill go to\nthis cell") );
405 grid
->SetCellValue( 1, 0, wxT("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
407 grid
->SetCellTextColour(1, 2, *wxRED
);
408 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
410 grid
->SetCellValue( 1, 4, wxT("I'm in the middle"));
412 grid
->SetCellValue(2, 2, wxT("red"));
414 grid
->SetCellTextColour(2, 2, *wxRED
);
415 grid
->SetCellValue(3, 3, wxT("green on grey"));
416 grid
->SetCellTextColour(3, 3, *wxGREEN
);
417 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
419 grid
->SetCellValue(4, 4, wxT("a weird looking cell"));
420 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
421 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
423 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
424 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
426 wxGridCellAttr
*attr
;
427 attr
= new wxGridCellAttr
;
428 attr
->SetTextColour(*wxBLUE
);
429 grid
->SetColAttr(5, attr
);
430 attr
= new wxGridCellAttr
;
431 attr
->SetBackgroundColour(*wxRED
);
432 grid
->SetRowAttr(5, attr
);
434 grid
->SetCellValue(2, 4, wxT("a wider column"));
435 grid
->SetColSize(4, 120);
436 grid
->SetColMinimalWidth(4, 120);
438 grid
->SetCellTextColour(5, 8, *wxGREEN
);
439 grid
->SetCellValue(5, 8, wxT("Bg from row attr\nText col from cell attr"));
440 grid
->SetCellValue(5, 5, wxT("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"));
442 grid
->SetColFormatFloat(6);
443 grid
->SetCellValue(0, 6, wxString::Format(wxT("%g"), 3.1415));
444 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 1415.0));
445 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 12345.67890));
447 grid
->SetColFormatFloat(7, 6, 2);
448 grid
->SetCellValue(0, 7, wxString::Format(wxT("%g"), 3.1415));
449 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 1415.0));
450 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 12345.67890));
452 grid
->SetColFormatNumber(8);
453 grid
->SetCellValue(0, 8, "17");
454 grid
->SetCellValue(1, 8, "0");
455 grid
->SetCellValue(2, 8, "-666");
456 grid
->SetCellAlignment(2, 8, wxALIGN_CENTRE
, wxALIGN_TOP
);
457 grid
->SetCellValue(2, 9, "<- This numeric cell should be centred");
459 const wxString choices
[] =
461 wxT("Please select a choice"),
462 wxT("This takes two cells"),
463 wxT("Another choice"),
465 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
466 grid
->SetCellSize(4, 0, 1, 2);
467 grid
->SetCellValue(4, 0, choices
[0]);
468 grid
->SetCellOverflow(4, 0, false);
470 grid
->SetCellSize(7, 1, 3, 4);
471 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
472 grid
->SetCellValue(7, 1, wxT("Big box!"));
474 // create a separator-like row: it's grey and it's non-resizeable
475 grid
->DisableRowResize(10);
476 grid
->SetRowSize(10, 30);
477 attr
= new wxGridCellAttr
;
478 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
479 grid
->SetRowAttr(10, attr
);
480 grid
->SetCellValue(10, 0, "You can't resize this row interactively -- try it");
482 // this does exactly nothing except testing that SetAttr() handles NULL
483 // attributes and does reference counting correctly
484 grid
->SetAttr(11, 11, NULL
);
485 grid
->SetAttr(11, 11, new wxGridCellAttr
);
486 grid
->SetAttr(11, 11, NULL
);
488 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
494 topSizer
->Add( logWin
,
499 SetSizerAndFit( topSizer
);
506 GridFrame::~GridFrame()
509 delete wxLog::SetActiveTarget(m_logOld
);
514 void GridFrame::SetDefaults()
516 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
517 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
518 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
519 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
520 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
521 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
522 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
523 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
524 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
525 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
529 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
531 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
533 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
537 grid
->SetRowLabelSize( 0 );
542 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
544 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
546 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
550 grid
->SetColLabelSize( 0 );
555 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
558 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
562 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
564 grid
->EnableDragRowSize(
565 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
569 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
571 grid
->EnableDragColSize(
572 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
575 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
577 grid
->EnableDragColMove(
578 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
581 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
583 grid
->EnableDragGridSize(
584 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
587 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
589 grid
->EnableDragCell(
590 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
593 void GridFrame::SetNativeColHeader( wxCommandEvent
& WXUNUSED(ev
) )
595 CustomColumnHeadersProvider
* provider
=
596 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
597 provider
->UseCustomColHeaders(false);
598 grid
->SetUseNativeColLabels(true);
601 void GridFrame::SetCustomColHeader( wxCommandEvent
& WXUNUSED(ev
) )
603 CustomColumnHeadersProvider
* provider
=
604 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
605 provider
->UseCustomColHeaders(true);
606 grid
->SetUseNativeColLabels(false);
609 void GridFrame::SetDefaultColHeader( wxCommandEvent
& WXUNUSED(ev
) )
611 CustomColumnHeadersProvider
* provider
=
612 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
613 provider
->UseCustomColHeaders(false);
614 grid
->SetUseNativeColLabels(false);
618 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
620 grid
->EnableGridLines(
621 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
624 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
626 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
628 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
629 wxT("Pen Width"), 11, choices
);
631 int current
= grid
->GetCellHighlightPenWidth();
632 dlg
.SetSelection(current
);
633 if (dlg
.ShowModal() == wxID_OK
) {
634 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
638 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
640 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
642 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
643 wxT("Pen Width"), 11, choices
);
645 int current
= grid
->GetCellHighlightROPenWidth();
646 dlg
.SetSelection(current
);
647 if (dlg
.ShowModal() == wxID_OK
) {
648 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
654 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
656 grid
->AutoSizeColumns();
660 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
662 grid
->SetDefaultCellOverflow(ev
.IsChecked());
666 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
669 grid
->SetCellSize( 7, 1, 5, 5 );
671 grid
->SetCellSize( 7, 1, 1, 5 );
675 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
677 wxColourDialog
dlg( NULL
);
678 if ( dlg
.ShowModal() == wxID_OK
)
680 wxColourData retData
;
681 retData
= dlg
.GetColourData();
682 wxColour colour
= retData
.GetColour();
684 grid
->SetLabelBackgroundColour( colour
);
689 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
691 wxColourDialog
dlg( NULL
);
692 if ( dlg
.ShowModal() == wxID_OK
)
694 wxColourData retData
;
695 retData
= dlg
.GetColourData();
696 wxColour colour
= retData
.GetColour();
698 grid
->SetLabelTextColour( colour
);
702 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
704 wxFont font
= wxGetFontFromUser(this);
707 grid
->SetLabelFont(font
);
711 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
714 grid
->GetRowLabelAlignment( &horiz
, &vert
);
719 horiz
= wxALIGN_CENTRE
;
723 horiz
= wxALIGN_RIGHT
;
727 horiz
= wxALIGN_LEFT
;
731 grid
->SetRowLabelAlignment( horiz
, vert
);
734 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
737 grid
->GetRowLabelAlignment( &horiz
, &vert
);
742 vert
= wxALIGN_CENTRE
;
746 vert
= wxALIGN_BOTTOM
;
754 grid
->SetRowLabelAlignment( horiz
, vert
);
758 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
761 grid
->GetColLabelAlignment( &horiz
, &vert
);
766 horiz
= wxALIGN_CENTRE
;
770 horiz
= wxALIGN_RIGHT
;
774 horiz
= wxALIGN_LEFT
;
778 grid
->SetColLabelAlignment( horiz
, vert
);
782 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
785 grid
->GetColLabelAlignment( &horiz
, &vert
);
790 vert
= wxALIGN_CENTRE
;
794 vert
= wxALIGN_BOTTOM
;
802 grid
->SetColLabelAlignment( horiz
, vert
);
806 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
808 wxColourDialog
dlg( NULL
);
809 if ( dlg
.ShowModal() == wxID_OK
)
811 wxColourData retData
;
812 retData
= dlg
.GetColourData();
813 wxColour colour
= retData
.GetColour();
815 grid
->SetGridLineColour( colour
);
820 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
822 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
826 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
828 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
832 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
834 if ( grid
->IsSelection() )
836 wxGridUpdateLocker
locker(grid
);
837 for ( int n
= 0; n
< grid
->GetNumberRows(); )
839 if ( grid
->IsInSelection( n
, 0 ) )
840 grid
->DeleteRows( n
, 1 );
848 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
850 wxGridUpdateLocker
locker(grid
);
851 const wxArrayInt sels
= grid
->GetSelectedRows();
852 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
854 grid
->AutoSizeRow( sels
[n
], false );
858 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
860 wxGridUpdateLocker
locker(grid
);
861 const wxArrayInt sels
= grid
->GetSelectedCols();
862 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
864 grid
->AutoSizeColumn( sels
[n
], false );
868 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
870 wxGridUpdateLocker
locker(grid
);
871 const wxArrayInt sels
= grid
->GetSelectedRows();
872 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
874 grid
->AutoSizeRowLabelSize( sels
[n
] );
878 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
880 wxGridUpdateLocker
locker(grid
);
881 const wxArrayInt sels
= grid
->GetSelectedCols();
882 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
884 grid
->AutoSizeColLabelSize( sels
[n
] );
888 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
890 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
893 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
895 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
898 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
904 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
906 if ( grid
->IsSelection() )
908 wxGridUpdateLocker
locker(grid
);
909 for ( int n
= 0; n
< grid
->GetNumberCols(); )
911 if ( grid
->IsInSelection( 0 , n
) )
912 grid
->DeleteCols( n
, 1 );
920 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
925 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
927 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
930 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
932 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
935 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
937 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
940 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
942 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
945 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
947 wxColour col
= wxGetColourFromUser(this);
950 grid
->SetDefaultCellTextColour(col
);
955 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
957 wxColour col
= wxGetColourFromUser(this);
960 // Check the new Refresh function by passing it a rectangle
961 // which exactly fits the grid.
963 wxRect
r(pt
, grid
->GetSize());
964 grid
->SetDefaultCellBackgroundColour(col
);
965 grid
->Refresh(true, &r
);
969 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
971 grid
->DeselectCell(3, 1);
974 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
976 grid
->DeselectCol(2);
979 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
981 grid
->DeselectRow(2);
984 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
986 grid
->ClearSelection();
989 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
991 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
994 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
996 grid
->SelectCol(2, m_addToSel
);
999 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
1001 grid
->SelectRow(2, m_addToSel
);
1004 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
1009 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
1011 m_addToSel
= event
.IsChecked();
1014 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
1017 if ( ev
.GetRow() != -1 )
1019 logBuf
<< wxT("Left click on row label ") << ev
.GetRow();
1021 else if ( ev
.GetCol() != -1 )
1023 logBuf
<< wxT("Left click on col label ") << ev
.GetCol();
1027 logBuf
<< wxT("Left click on corner label");
1030 if ( ev
.ShiftDown() )
1031 logBuf
<< wxT(" (shift down)");
1032 if ( ev
.ControlDown() )
1033 logBuf
<< wxT(" (control down)");
1034 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1036 // you must call event skip if you want default grid processing
1042 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
1044 wxLogMessage(wxT("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
1046 // you must call event skip if you want default grid processing
1047 // (cell highlighting etc.)
1053 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
1055 const int row
= ev
.GetRowOrCol();
1057 wxLogMessage("Resized row %d, new height = %d",
1058 row
, grid
->GetRowSize(row
));
1064 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
1066 const int col
= ev
.GetRowOrCol();
1068 wxLogMessage("Resized column %d, new width = %d",
1069 col
, grid
->GetColSize(col
));
1075 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1078 if ( ev
.Selecting() )
1079 logBuf
<< wxT("Selected ");
1081 logBuf
<< wxT("Deselected ");
1082 logBuf
<< wxT("cell at row ") << ev
.GetRow()
1083 << wxT(" col ") << ev
.GetCol()
1084 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1085 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1086 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1087 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1089 //Indicate whether this column was moved
1090 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1091 logBuf
<< wxT(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1093 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1095 // you must call Skip() if you want the default processing
1096 // to occur in wxGrid
1100 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1103 if ( ev
.Selecting() )
1104 logBuf
<< wxT("Selected ");
1106 logBuf
<< wxT("Deselected ");
1107 logBuf
<< wxT("cells from row ") << ev
.GetTopRow()
1108 << wxT(" col ") << ev
.GetLeftCol()
1109 << wxT(" to row ") << ev
.GetBottomRow()
1110 << wxT(" col ") << ev
.GetRightCol()
1111 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1112 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1113 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1114 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1115 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1120 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1122 int row
= ev
.GetRow(),
1125 wxLogMessage("Value of cell at (%d, %d): about to change "
1126 "from \"%s\" to \"%s\"",
1128 grid
->GetCellValue(row
, col
), ev
.GetString());
1130 // test how vetoing works
1131 if ( ev
.GetString() == "42" )
1133 wxLogMessage("Vetoing the change.");
1141 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1143 int row
= ev
.GetRow(),
1146 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1149 grid
->GetCellValue(row
, col
), ev
.GetString());
1154 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1156 wxLogMessage(wxT("Got request to drag cell at row %d, col %d"),
1157 ev
.GetRow(), ev
.GetCol());
1162 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1165 if ( (ev
.GetCol() == 4) &&
1166 (ev
.GetRow() == 0) &&
1167 (wxMessageBox(wxT("Are you sure you wish to edit this cell"),
1168 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1174 wxLogMessage( wxT("Cell editor shown.") );
1179 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1182 if ( (ev
.GetCol() == 4) &&
1183 (ev
.GetRow() == 0) &&
1184 (wxMessageBox(wxT("Are you sure you wish to finish editing this cell"),
1185 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1191 wxLogMessage( wxT("Cell editor hidden.") );
1196 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1198 wxAboutDialogInfo aboutInfo
;
1199 aboutInfo
.SetName(wxT("wxGrid demo"));
1200 aboutInfo
.SetDescription(_("wxGrid sample program"));
1201 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1202 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1203 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1205 // this is just to force the generic version of the about
1206 // dialog under wxMSW so that it's easy to test if the grid
1207 // repaints correctly when it has lost focus and a dialog
1208 // (different from the Windows standard message box -- it doesn't
1209 // work with it for some reason) is moved over it.
1210 aboutInfo
.SetWebSite(wxT("http://www.wxwidgets.org"));
1212 wxAboutBox(aboutInfo
);
1216 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1221 void GridFrame::OnBugsTable(wxCommandEvent
& )
1223 BugsGridFrame
*frame
= new BugsGridFrame
;
1227 // ----------------------------------------------------------------------------
1228 // MyGridCellAttrProvider
1229 // ----------------------------------------------------------------------------
1231 MyGridCellAttrProvider::MyGridCellAttrProvider()
1233 m_attrForOddRows
= new wxGridCellAttr
;
1234 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1237 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1239 m_attrForOddRows
->DecRef();
1242 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1243 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1245 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1251 attr
= m_attrForOddRows
;
1256 if ( !attr
->HasBackgroundColour() )
1258 wxGridCellAttr
*attrNew
= attr
->Clone();
1261 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1269 void GridFrame::OnVTable(wxCommandEvent
& )
1271 static long s_sizeGrid
= 10000;
1273 s_sizeGrid
= wxGetNumberFromUser(wxT("Size of the table to create"),
1275 wxT("wxGridDemo question"),
1279 if ( s_sizeGrid
!= -1 )
1281 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1286 // ----------------------------------------------------------------------------
1287 // MyGridCellRenderer
1288 // ----------------------------------------------------------------------------
1290 // do something that the default renderer doesn't here just to show that it is
1291 // possible to alter the appearance of the cell beyond what the attributes
1293 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1294 wxGridCellAttr
& attr
,
1300 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1302 dc
.SetPen(*wxGREEN_PEN
);
1303 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1304 dc
.DrawEllipse(rect
);
1307 // ============================================================================
1308 // BigGridFrame and BigGridTable: Sample of a non-standard table
1309 // ============================================================================
1311 BigGridFrame::BigGridFrame(long sizeGrid
)
1312 : wxFrame(NULL
, wxID_ANY
, wxT("Plugin Virtual Table"),
1313 wxDefaultPosition
, wxSize(500, 450))
1315 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1316 m_table
= new BigGridTable(sizeGrid
);
1318 // VZ: I don't understand why this slows down the display that much,
1319 // must profile it...
1320 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1322 m_grid
->SetTable(m_table
, true);
1324 #if defined __WXMOTIF__
1325 // MB: the grid isn't getting a sensible default size under wxMotif
1327 GetClientSize( &cw
, &ch
);
1328 m_grid
->SetSize( cw
, ch
);
1332 // ============================================================================
1333 // BugsGridFrame: a "realistic" table
1334 // ============================================================================
1336 // ----------------------------------------------------------------------------
1338 // ----------------------------------------------------------------------------
1361 static const wxString severities
[] =
1370 static struct BugsGridData
1376 wxChar platform
[12];
1378 } gs_dataBugsGrid
[] =
1380 { 18, wxT("foo doesn't work"), Sev_Major
, 1, wxT("wxMSW"), true },
1381 { 27, wxT("bar crashes"), Sev_Critical
, 1, wxT("all"), false },
1382 { 45, wxT("printing is slow"), Sev_Minor
, 3, wxT("wxMSW"), true },
1383 { 68, wxT("Rectangle() fails"), Sev_Normal
, 1, wxT("wxMSW"), false },
1386 static const wxChar
*headers
[Col_Max
] =
1396 // ----------------------------------------------------------------------------
1398 // ----------------------------------------------------------------------------
1400 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1406 return wxGRID_VALUE_NUMBER
;;
1409 // fall thorugh (TODO should be a list)
1412 return wxString::Format(wxT("%s:80"), wxGRID_VALUE_STRING
);
1415 return wxString::Format(wxT("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1418 return wxGRID_VALUE_BOOL
;
1421 wxFAIL_MSG(wxT("unknown column"));
1423 return wxEmptyString
;
1426 int BugsGridTable::GetNumberRows()
1428 return WXSIZEOF(gs_dataBugsGrid
);
1431 int BugsGridTable::GetNumberCols()
1436 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1441 wxString
BugsGridTable::GetValue( int row
, int col
)
1443 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1448 return wxString::Format(wxT("%d"), gd
.id
);
1451 return wxString::Format(wxT("%d"), gd
.prio
);
1454 return gd
.opened
? wxT("1") : wxT("0");
1457 return severities
[gd
.severity
];
1466 return wxEmptyString
;
1469 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1471 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1478 wxFAIL_MSG(wxT("unexpected column"));
1484 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1486 if ( severities
[n
] == value
)
1488 gd
.severity
= (Severity
)n
;
1493 if ( n
== WXSIZEOF(severities
) )
1495 wxLogWarning(wxT("Invalid severity value '%s'."),
1497 gd
.severity
= Sev_Normal
;
1503 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1507 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1513 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1515 const wxString
& typeName
)
1517 if ( typeName
== wxGRID_VALUE_STRING
)
1521 else if ( typeName
== wxGRID_VALUE_BOOL
)
1523 return col
== Col_Opened
;
1525 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1527 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1535 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1537 return CanGetValueAs(row
, col
, typeName
);
1540 long BugsGridTable::GetValueAsLong( int row
, int col
)
1542 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1556 wxFAIL_MSG(wxT("unexpected column"));
1561 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1563 if ( col
== Col_Opened
)
1565 return gs_dataBugsGrid
[row
].opened
;
1569 wxFAIL_MSG(wxT("unexpected column"));
1575 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1577 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1586 wxFAIL_MSG(wxT("unexpected column"));
1590 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1592 if ( col
== Col_Opened
)
1594 gs_dataBugsGrid
[row
].opened
= value
;
1598 wxFAIL_MSG(wxT("unexpected column"));
1602 wxString
BugsGridTable::GetColLabelValue( int col
)
1604 return headers
[col
];
1607 // ----------------------------------------------------------------------------
1609 // ----------------------------------------------------------------------------
1611 BugsGridFrame::BugsGridFrame()
1612 : wxFrame(NULL
, wxID_ANY
, wxT("Bugs table"))
1614 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1615 wxGridTableBase
*table
= new BugsGridTable();
1616 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1617 grid
->SetTable(table
, true);
1619 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1620 *attrRangeEditor
= new wxGridCellAttr
,
1621 *attrCombo
= new wxGridCellAttr
;
1623 attrRO
->SetReadOnly();
1624 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1625 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1628 grid
->SetColAttr(Col_Id
, attrRO
);
1629 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1630 grid
->SetColAttr(Col_Severity
, attrCombo
);
1633 SetClientSize(grid
->GetSize());
1636 // ============================================================================
1637 // TabularGrid: grid used for display of tabular data
1638 // ============================================================================
1640 class TabularGridTable
: public wxGridTableBase
1657 TabularGridTable() { m_sortOrder
= NULL
; }
1659 virtual int GetNumberRows() { return ROW_MAX
; }
1660 virtual int GetNumberCols() { return COL_MAX
; }
1662 virtual wxString
GetValue(int row
, int col
)
1665 row
= m_sortOrder
[row
];
1671 return GetNameOrExt(row
, col
);
1674 return wxString::Format("%lu", GetSize(row
));
1677 return GetDate(row
).FormatDate();
1681 wxFAIL_MSG( "unknown column" );
1687 virtual void SetValue(int, int, const wxString
&)
1689 wxFAIL_MSG( "shouldn't be called" );
1692 virtual wxString
GetColLabelValue(int col
)
1694 // notice that column parameter here always refers to the internal
1695 // column index, independently of its position on the screen
1696 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1697 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1702 virtual void SetColLabelValue(int, const wxString
&)
1704 wxFAIL_MSG( "shouldn't be called" );
1707 void Sort(int col
, bool ascending
)
1709 // we hardcode all sorting orders for simplicity here
1710 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1712 // descending ascending
1713 { { 2, 1, 0 }, { 0, 1, 2 } },
1714 { { 2, 1, 0 }, { 0, 1, 2 } },
1715 { { 2, 1, 0 }, { 0, 1, 2 } },
1716 { { 1, 0, 2 }, { 2, 0, 1 } },
1719 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1723 wxString
GetNameOrExt(int row
, int col
) const
1726 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1727 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1729 const wxString
s(names
[row
]);
1730 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1733 unsigned long GetSize(int row
) const
1735 static const unsigned long
1736 sizes
[] = { 412, 604, 40774 };
1737 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1742 wxDateTime
GetDate(int row
) const
1745 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1746 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1749 dt
.ParseISODate(dates
[row
]);
1756 // specialized text control for column indexes entry
1757 class ColIndexEntry
: public wxTextCtrl
1760 ColIndexEntry(wxWindow
*parent
)
1761 : wxTextCtrl(parent
, wxID_ANY
, "")
1763 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1769 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1779 virtual wxSize
DoGetBestSize() const
1781 wxSize size
= wxTextCtrl::DoGetBestSize();
1782 size
.x
= 3*GetCharWidth();
1787 class TabularGridFrame
: public wxFrame
1795 Id_Check_UseNativeHeader
,
1796 Id_Check_DrawNativeLabels
,
1797 Id_Check_ShowRowLabels
,
1798 Id_Check_EnableColMove
1803 void OnToggleUseNativeHeader(wxCommandEvent
&)
1805 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1808 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1810 // we don't draw labels at all, native or otherwise, if we use the
1811 // native header control
1812 event
.Enable( !m_chkUseNative
->GetValue() );
1815 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1817 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1820 void OnToggleShowRowLabels(wxCommandEvent
&)
1822 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1827 void OnToggleColMove(wxCommandEvent
&)
1829 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1832 void OnShowHideColumn(wxCommandEvent
& event
)
1834 int col
= m_txtColShowHide
->GetCol();
1837 m_grid
->SetColSize(col
,
1838 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1840 UpdateOrderAndVisibility();
1844 void OnMoveColumn(wxCommandEvent
&)
1846 int col
= m_txtColIndex
->GetCol();
1847 int pos
= m_txtColPos
->GetCol();
1848 if ( col
== -1 || pos
== -1 )
1851 m_grid
->SetColPos(col
, pos
);
1853 UpdateOrderAndVisibility();
1856 void OnResetColumnOrder(wxCommandEvent
&)
1858 m_grid
->ResetColPos();
1860 UpdateOrderAndVisibility();
1863 void OnGridColSort(wxGridEvent
& event
)
1865 const int col
= event
.GetCol();
1866 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
1867 m_grid
->IsSortOrderAscending()));
1870 void OnGridColMove(wxGridEvent
& event
)
1872 // can't update it yet as the order hasn't been changed, so do it a bit
1874 m_shouldUpdateOrder
= true;
1879 void OnGridColSize(wxGridSizeEvent
& event
)
1881 // we only catch this event to react to the user showing or hiding this
1882 // column using the header control menu and not because we're
1883 // interested in column resizing
1884 UpdateOrderAndVisibility();
1889 void OnIdle(wxIdleEvent
& event
)
1891 if ( m_shouldUpdateOrder
)
1893 m_shouldUpdateOrder
= false;
1894 UpdateOrderAndVisibility();
1900 void UpdateOrderAndVisibility()
1903 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
1905 const int col
= m_grid
->GetColAt(pos
);
1906 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
1917 m_statOrder
->SetLabel(s
);
1922 TabularGridTable
*m_table
;
1923 wxCheckBox
*m_chkUseNative
,
1925 *m_chkShowRowLabels
,
1926 *m_chkEnableColMove
;
1928 ColIndexEntry
*m_txtColIndex
,
1932 wxStaticText
*m_statOrder
;
1934 // fla for EVT_IDLE handler
1935 bool m_shouldUpdateOrder
;
1937 wxDECLARE_NO_COPY_CLASS(TabularGridFrame
);
1938 DECLARE_EVENT_TABLE()
1941 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
1942 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
1943 TabularGridFrame::OnToggleUseNativeHeader
)
1944 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
1945 TabularGridFrame::OnToggleDrawNativeLabels
)
1946 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
1947 TabularGridFrame::OnToggleShowRowLabels
)
1948 EVT_CHECKBOX(Id_Check_EnableColMove
,
1949 TabularGridFrame::OnToggleColMove
)
1951 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
1952 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
1954 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
1955 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
1956 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
1957 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
1959 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
1960 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
1961 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
1963 EVT_IDLE(TabularGridFrame::OnIdle
)
1966 TabularGridFrame::TabularGridFrame()
1967 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
1969 m_shouldUpdateOrder
= false;
1971 wxPanel
* const panel
= new wxPanel(this);
1973 // create and initialize the grid with the specified data
1974 m_table
= new TabularGridTable
;
1975 m_grid
= new wxGrid(panel
, wxID_ANY
,
1976 wxDefaultPosition
, wxDefaultSize
,
1977 wxBORDER_STATIC
| wxWANTS_CHARS
);
1978 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
1980 m_grid
->EnableDragColMove();
1981 m_grid
->UseNativeColHeader();
1982 m_grid
->HideRowLabels();
1984 // add it and the other controls to the frame
1985 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
1986 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
1988 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
1990 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
1991 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
1992 "&Use native header");
1993 m_chkUseNative
->SetValue(true);
1994 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
1996 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
1997 "&Draw native column labels");
1998 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
2000 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
2001 "Show &row labels");
2002 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
2004 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
2005 "Allow column re&ordering");
2006 m_chkEnableColMove
->SetValue(true);
2007 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
2008 sizerControls
->Add(sizerStyles
);
2010 sizerControls
->AddSpacer(10);
2012 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
2013 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
2015 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
2016 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
2018 m_txtColIndex
= new ColIndexEntry(panel
);
2019 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
2020 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
2021 m_txtColPos
= new ColIndexEntry(panel
);
2022 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
2023 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
2025 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
2027 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
2028 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
2030 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
2031 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
2032 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
2033 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
2035 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
2036 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
2038 m_txtColShowHide
= new ColIndexEntry(panel
);
2039 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
2040 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
2041 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
2042 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
2044 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
2046 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
2048 panel
->SetSizer(sizerTop
);
2050 SetClientSize(panel
->GetBestSize());
2051 SetSizeHints(GetSize());
2056 void GridFrame::OnTabularTable(wxCommandEvent
&)
2058 new TabularGridFrame
;