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 license
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/colordlg.h"
31 #include "wx/fontdlg.h"
32 #include "wx/numdlg.h"
33 #include "wx/aboutdlg.h"
36 #include "wx/generic/gridctrl.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 IMPLEMENT_APP( GridApp
)
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 bool GridApp::OnInit()
56 GridFrame
*frame
= new GridFrame
;
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
67 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
68 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
69 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
70 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
71 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
72 EVT_MENU( ID_TOGGLECOLMOVING
, GridFrame::ToggleColMoving
)
73 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
74 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
75 EVT_MENU( ID_TOGGLENATIVEHEADER
, GridFrame::ToggleNativeHeader
)
76 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
77 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
78 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
79 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
80 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
81 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
82 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
83 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
84 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
85 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
86 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
87 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
88 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
89 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
90 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
91 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
92 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
93 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
94 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
95 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
96 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
98 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
99 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
101 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
102 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
103 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
104 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
105 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
107 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
108 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
109 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
110 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
111 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
112 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
113 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
114 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
115 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
116 EVT_MENU( ID_SHOW_SELECTION
, GridFrame::OnShowSelection
)
118 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
119 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
120 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
121 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
122 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
123 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
124 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
126 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
127 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
129 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
130 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
131 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
132 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
133 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
134 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
135 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
136 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
138 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
139 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
143 GridFrame::GridFrame()
144 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets grid class demo"),
148 wxMenu
*fileMenu
= new wxMenu
;
149 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
150 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
151 fileMenu
->Append( ID_TABULAR_TABLE
, _T("&Tabular table test\tCtrl-T"));
152 fileMenu
->AppendSeparator();
153 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
155 wxMenu
*viewMenu
= new wxMenu
;
156 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
157 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
158 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
159 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
160 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
161 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
162 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
163 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
164 viewMenu
->AppendCheckItem(ID_TOGGLENATIVEHEADER
, "&Native column headers");
165 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
166 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
167 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
168 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
169 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
170 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
172 wxMenu
*rowLabelMenu
= new wxMenu
;
174 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
176 _T("Change alignment of row labels") );
178 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
179 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
181 wxMenu
*colLabelMenu
= new wxMenu
;
183 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
185 _T("Change alignment of col labels") );
187 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
188 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
190 wxMenu
*colMenu
= new wxMenu
;
191 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
192 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
193 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
194 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
195 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
196 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
198 wxMenu
*editMenu
= new wxMenu
;
199 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
200 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
201 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
202 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
203 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
205 wxMenu
*selectMenu
= new wxMenu
;
206 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
207 _T("When off, old selection is deselected before ")
208 _T("selecting the new cells"), wxITEM_CHECK
);
209 selectMenu
->Append( ID_SHOW_SELECTION
,
210 _T("&Show current selection\tCtrl-Alt-S"));
211 selectMenu
->AppendSeparator();
212 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
213 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
214 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
215 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
216 selectMenu
->AppendSeparator();
217 selectMenu
->Append( ID_DESELECT_ALL
, _T("Deselect all"));
218 selectMenu
->Append( ID_DESELECT_ROW
, _T("Deselect row 2"));
219 selectMenu
->Append( ID_DESELECT_COL
, _T("Deselect col 2"));
220 selectMenu
->Append( ID_DESELECT_CELL
, _T("Deselect cell (3, 1)"));
221 wxMenu
*selectionMenu
= new wxMenu
;
222 selectMenu
->Append( ID_CHANGESEL
, _T("Change &selection mode"),
224 _T("Change selection mode") );
226 selectionMenu
->Append( ID_SELCELLS
, _T("Select &cells") );
227 selectionMenu
->Append( ID_SELROWS
, _T("Select &rows") );
228 selectionMenu
->Append( ID_SELCOLS
, _T("Select col&umns") );
229 selectionMenu
->Append( ID_SELROWSORCOLS
, _T("Select rows &or columns") );
231 wxMenu
*autosizeMenu
= new wxMenu
;
232 autosizeMenu
->Append( ID_SIZE_ROW
, _T("Selected &row data") );
233 autosizeMenu
->Append( ID_SIZE_COL
, _T("Selected &column data") );
234 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, _T("Selected row la&bel") );
235 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, _T("Selected column &label") );
236 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, _T("Column la&bels") );
237 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, _T("Row label&s") );
238 autosizeMenu
->Append( ID_SIZE_GRID
, _T("Entire &grid") );
240 wxMenu
*helpMenu
= new wxMenu
;
241 helpMenu
->Append( wxID_ABOUT
, _T("&About wxGrid demo") );
243 wxMenuBar
*menuBar
= new wxMenuBar
;
244 menuBar
->Append( fileMenu
, _T("&File") );
245 menuBar
->Append( viewMenu
, _T("&Grid") );
246 menuBar
->Append( colMenu
, _T("&Colours") );
247 menuBar
->Append( editMenu
, _T("&Edit") );
248 menuBar
->Append( selectMenu
, _T("&Select") );
249 menuBar
->Append( autosizeMenu
, _T("&Autosize") );
250 menuBar
->Append( helpMenu
, _T("&Help") );
252 SetMenuBar( menuBar
);
256 grid
= new wxGrid( this,
259 wxSize( 400, 300 ) );
262 int gridW
= 600, gridH
= 300;
263 int logW
= gridW
, logH
= 100;
265 logWin
= new wxTextCtrl( this,
268 wxPoint( 0, gridH
+ 20 ),
269 wxSize( logW
, logH
),
272 logger
= new wxLogTextCtrl( logWin
);
273 m_logOld
= wxLog::SetActiveTarget( logger
);
274 wxLog::DisableTimestamp();
277 // this will create a grid and, by default, an associated grid
279 grid
->CreateGrid( 0, 0 );
280 grid
->AppendRows(100);
281 grid
->AppendCols(100);
283 int ir
= grid
->GetNumberRows();
284 grid
->DeleteRows(0, ir
);
285 grid
->AppendRows(ir
);
287 grid
->SetRowSize( 0, 60 );
288 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
290 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
291 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
292 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
294 grid
->SetCellValue( 0, 2, _T("Blah") );
295 grid
->SetCellValue( 0, 3, _T("Read only") );
296 grid
->SetReadOnly( 0, 3 );
298 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
300 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
302 grid
->SetRowSize( 99, 60 );
303 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
304 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
306 grid
->SetCellTextColour(1, 2, *wxRED
);
307 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
309 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
311 grid
->SetCellValue(2, 2, _T("red"));
313 grid
->SetCellTextColour(2, 2, *wxRED
);
314 grid
->SetCellValue(3, 3, _T("green on grey"));
315 grid
->SetCellTextColour(3, 3, *wxGREEN
);
316 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
318 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
319 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
320 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
322 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
323 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
325 wxGridCellAttr
*attr
;
326 attr
= new wxGridCellAttr
;
327 attr
->SetTextColour(*wxBLUE
);
328 grid
->SetColAttr(5, attr
);
329 attr
= new wxGridCellAttr
;
330 attr
->SetBackgroundColour(*wxRED
);
331 grid
->SetRowAttr(5, attr
);
333 grid
->SetCellValue(2, 4, _T("a wider column"));
334 grid
->SetColSize(4, 120);
335 grid
->SetColMinimalWidth(4, 120);
337 grid
->SetCellTextColour(5, 8, *wxGREEN
);
338 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
339 grid
->SetCellValue(5, 5, _T("Bg from row attr Text col from col attr and this text is so long that it covers over many many empty cells but is broken by one that isn't"));
341 grid
->SetColFormatFloat(6);
342 grid
->SetCellValue(0, 6, wxString::Format(wxT("%g"), 3.1415));
343 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 1415.0));
344 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 12345.67890));
346 grid
->SetColFormatFloat(7, 6, 2);
347 grid
->SetCellValue(0, 7, wxString::Format(wxT("%g"), 3.1415));
348 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 1415.0));
349 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 12345.67890));
351 grid
->SetColFormatNumber(8);
352 grid
->SetCellValue(0, 8, "17");
353 grid
->SetCellValue(1, 8, "0");
354 grid
->SetCellValue(2, 8, "-666");
356 const wxString choices
[] =
358 _T("Please select a choice"),
359 _T("This takes two cells"),
360 _T("Another choice"),
362 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
363 grid
->SetCellSize(4, 0, 1, 2);
364 grid
->SetCellValue(4, 0, choices
[0]);
365 grid
->SetCellOverflow(4, 0, false);
367 grid
->SetCellSize(7, 1, 3, 4);
368 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
369 grid
->SetCellValue(7, 1, _T("Big box!"));
371 // this does exactly nothing except testing that SetAttr() handles NULL
372 // attributes and does reference counting correctly
373 grid
->SetAttr(11, 11, NULL
);
374 grid
->SetAttr(11, 11, new wxGridCellAttr
);
375 grid
->SetAttr(11, 11, NULL
);
377 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
383 topSizer
->Add( logWin
,
388 SetSizerAndFit( topSizer
);
395 GridFrame::~GridFrame()
398 delete wxLog::SetActiveTarget(m_logOld
);
403 void GridFrame::SetDefaults()
405 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
406 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
407 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
408 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
409 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
410 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
411 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
412 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
413 GetMenuBar()->Check( ID_TOGGLENATIVEHEADER
, false );
414 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
415 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
419 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
421 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
423 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
427 grid
->SetRowLabelSize( 0 );
432 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
434 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
436 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
440 grid
->SetColLabelSize( 0 );
445 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
448 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
452 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
454 grid
->EnableDragRowSize(
455 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
459 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
461 grid
->EnableDragColSize(
462 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
465 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
467 grid
->EnableDragColMove(
468 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
471 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
473 grid
->EnableDragGridSize(
474 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
477 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
479 grid
->EnableDragCell(
480 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
483 void GridFrame::ToggleNativeHeader( wxCommandEvent
& WXUNUSED(ev
) )
485 grid
->SetUseNativeColLabels(
486 GetMenuBar()->IsChecked( ID_TOGGLENATIVEHEADER
) );
489 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
491 grid
->EnableGridLines(
492 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
495 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
497 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
499 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
500 _T("Pen Width"), 11, choices
);
502 int current
= grid
->GetCellHighlightPenWidth();
503 dlg
.SetSelection(current
);
504 if (dlg
.ShowModal() == wxID_OK
) {
505 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
509 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
511 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
513 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
514 _T("Pen Width"), 11, choices
);
516 int current
= grid
->GetCellHighlightROPenWidth();
517 dlg
.SetSelection(current
);
518 if (dlg
.ShowModal() == wxID_OK
) {
519 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
525 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
527 grid
->AutoSizeColumns();
531 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
533 grid
->SetDefaultCellOverflow(ev
.IsChecked());
537 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
540 grid
->SetCellSize( 7, 1, 5, 5 );
542 grid
->SetCellSize( 7, 1, 1, 5 );
546 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
548 wxColourDialog
dlg( NULL
);
549 if ( dlg
.ShowModal() == wxID_OK
)
551 wxColourData retData
;
552 retData
= dlg
.GetColourData();
553 wxColour colour
= retData
.GetColour();
555 grid
->SetLabelBackgroundColour( colour
);
560 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
562 wxColourDialog
dlg( NULL
);
563 if ( dlg
.ShowModal() == wxID_OK
)
565 wxColourData retData
;
566 retData
= dlg
.GetColourData();
567 wxColour colour
= retData
.GetColour();
569 grid
->SetLabelTextColour( colour
);
573 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
575 wxFont font
= wxGetFontFromUser(this);
578 grid
->SetLabelFont(font
);
582 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
585 grid
->GetRowLabelAlignment( &horiz
, &vert
);
590 horiz
= wxALIGN_CENTRE
;
594 horiz
= wxALIGN_RIGHT
;
598 horiz
= wxALIGN_LEFT
;
602 grid
->SetRowLabelAlignment( horiz
, vert
);
605 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
608 grid
->GetRowLabelAlignment( &horiz
, &vert
);
613 vert
= wxALIGN_CENTRE
;
617 vert
= wxALIGN_BOTTOM
;
625 grid
->SetRowLabelAlignment( horiz
, vert
);
629 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
632 grid
->GetColLabelAlignment( &horiz
, &vert
);
637 horiz
= wxALIGN_CENTRE
;
641 horiz
= wxALIGN_RIGHT
;
645 horiz
= wxALIGN_LEFT
;
649 grid
->SetColLabelAlignment( horiz
, vert
);
653 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
656 grid
->GetColLabelAlignment( &horiz
, &vert
);
661 vert
= wxALIGN_CENTRE
;
665 vert
= wxALIGN_BOTTOM
;
673 grid
->SetColLabelAlignment( horiz
, vert
);
677 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
679 wxColourDialog
dlg( NULL
);
680 if ( dlg
.ShowModal() == wxID_OK
)
682 wxColourData retData
;
683 retData
= dlg
.GetColourData();
684 wxColour colour
= retData
.GetColour();
686 grid
->SetGridLineColour( colour
);
691 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
693 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
697 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
699 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
703 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
705 if ( grid
->IsSelection() )
707 wxGridUpdateLocker
locker(grid
);
708 for ( int n
= 0; n
< grid
->GetNumberRows(); )
710 if ( grid
->IsInSelection( n
, 0 ) )
711 grid
->DeleteRows( n
, 1 );
719 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
721 wxGridUpdateLocker
locker(grid
);
722 const wxArrayInt sels
= grid
->GetSelectedRows();
723 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
725 grid
->AutoSizeRow( sels
[n
], false );
729 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
731 wxGridUpdateLocker
locker(grid
);
732 const wxArrayInt sels
= grid
->GetSelectedCols();
733 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
735 grid
->AutoSizeColumn( sels
[n
], false );
739 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
741 wxGridUpdateLocker
locker(grid
);
742 const wxArrayInt sels
= grid
->GetSelectedRows();
743 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
745 grid
->AutoSizeRowLabelSize( sels
[n
] );
749 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
751 wxGridUpdateLocker
locker(grid
);
752 const wxArrayInt sels
= grid
->GetSelectedCols();
753 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
755 grid
->AutoSizeColLabelSize( sels
[n
] );
759 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
761 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
764 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
766 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
769 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
775 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
777 if ( grid
->IsSelection() )
779 wxGridUpdateLocker
locker(grid
);
780 for ( int n
= 0; n
< grid
->GetNumberCols(); )
782 if ( grid
->IsInSelection( 0 , n
) )
783 grid
->DeleteCols( n
, 1 );
791 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
796 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
798 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
801 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
803 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
806 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
808 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
811 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
813 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
816 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
818 wxColour col
= wxGetColourFromUser(this);
821 grid
->SetDefaultCellTextColour(col
);
826 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
828 wxColour col
= wxGetColourFromUser(this);
831 // Check the new Refresh function by passing it a rectangle
832 // which exactly fits the grid.
834 wxRect
r(pt
, grid
->GetSize());
835 grid
->SetDefaultCellBackgroundColour(col
);
836 grid
->Refresh(true, &r
);
840 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
842 grid
->DeselectCell(3, 1);
845 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
847 grid
->DeselectCol(2);
850 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
852 grid
->DeselectRow(2);
855 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
857 grid
->ClearSelection();
860 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
862 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
865 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
867 grid
->SelectCol(2, m_addToSel
);
870 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
872 grid
->SelectRow(2, m_addToSel
);
875 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
880 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
882 m_addToSel
= event
.IsChecked();
885 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
888 if ( ev
.GetRow() != -1 )
890 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
892 else if ( ev
.GetCol() != -1 )
894 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
898 logBuf
<< _T("Left click on corner label");
901 if ( ev
.ShiftDown() )
902 logBuf
<< _T(" (shift down)");
903 if ( ev
.ControlDown() )
904 logBuf
<< _T(" (control down)");
905 wxLogMessage( wxT("%s"), logBuf
.c_str() );
907 // you must call event skip if you want default grid processing
913 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
915 wxLogMessage(_T("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
917 // you must call event skip if you want default grid processing
918 // (cell highlighting etc.)
924 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
926 wxLogMessage(_T("Resized row %d"), ev
.GetRowOrCol());
932 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
934 wxLogMessage(_T("Resized col %d"), ev
.GetRowOrCol());
940 void GridFrame::OnShowSelection(wxCommandEvent
& WXUNUSED(event
))
942 // max number of elements to dump -- otherwise it can take too much time
943 static const size_t countMax
= 100;
947 switch ( grid
->GetSelectionMode() )
949 case wxGrid::wxGridSelectCells
:
951 const wxGridCellCoordsArray
cells(grid
->GetSelectedCells());
952 size_t count
= cells
.size();
953 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count
);
954 if ( count
> countMax
)
956 wxLogMessage(_T("[too many selected cells, ")
957 _T("showing only the first %lu]"),
958 (unsigned long)countMax
);
962 for ( size_t n
= 0; n
< count
; n
++ )
964 const wxGridCellCoords
& c
= cells
[n
];
965 wxLogMessage(_T(" selected cell %lu: (%d, %d)"),
966 (unsigned long)n
, c
.GetCol(), c
.GetRow());
971 case wxGrid::wxGridSelectRows
:
975 case wxGrid::wxGridSelectColumns
:
977 const wxChar
*plural
, *single
;
985 plural
= _T("columns");
986 single
= _T("column");
989 const wxArrayInt
sels((const wxArrayInt
)(rows
? grid
->GetSelectedRows()
990 : grid
->GetSelectedCols()));
991 size_t count
= sels
.size();
992 wxLogMessage(_T("%lu %s selected:"),
993 (unsigned long)count
, plural
);
994 if ( count
> countMax
)
996 wxLogMessage(_T("[too many selected %s, ")
997 _T("showing only the first %lu]"),
998 plural
, (unsigned long)countMax
);
1002 for ( size_t n
= 0; n
< count
; n
++ )
1004 wxLogMessage(_T(" selected %s %lu: %d"),
1005 single
, (unsigned long)n
, sels
[n
]);
1011 wxFAIL_MSG( _T("unknown wxGrid selection mode") );
1016 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1019 if ( ev
.Selecting() )
1020 logBuf
<< _T("Selected ");
1022 logBuf
<< _T("Deselected ");
1023 logBuf
<< _T("cell at row ") << ev
.GetRow()
1024 << _T(" col ") << ev
.GetCol()
1025 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1026 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1027 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1028 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
1030 //Indicate whether this column was moved
1031 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1032 logBuf
<< _T(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1034 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1036 // you must call Skip() if you want the default processing
1037 // to occur in wxGrid
1041 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1044 if ( ev
.Selecting() )
1045 logBuf
<< _T("Selected ");
1047 logBuf
<< _T("Deselected ");
1048 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
1049 << _T(" col ") << ev
.GetLeftCol()
1050 << _T(" to row ") << ev
.GetBottomRow()
1051 << _T(" col ") << ev
.GetRightCol()
1052 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1053 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1054 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1055 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
1056 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1061 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1063 int row
= ev
.GetRow(),
1066 wxLogMessage(_T("Value changed for cell at row %d, col %d: now \"%s\""),
1067 row
, col
, grid
->GetCellValue(row
, col
).c_str());
1072 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1074 wxLogMessage(_T("Got request to drag cell at row %d, col %d"),
1075 ev
.GetRow(), ev
.GetCol());
1080 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1083 if ( (ev
.GetCol() == 4) &&
1084 (ev
.GetRow() == 0) &&
1085 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
1086 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1092 wxLogMessage( wxT("Cell editor shown.") );
1097 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1100 if ( (ev
.GetCol() == 4) &&
1101 (ev
.GetRow() == 0) &&
1102 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
1103 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1109 wxLogMessage( wxT("Cell editor hidden.") );
1114 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1116 wxAboutDialogInfo aboutInfo
;
1117 aboutInfo
.SetName(wxT("wxGrid demo"));
1118 aboutInfo
.SetDescription(_("wxGrid sample program"));
1119 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1120 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1121 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1123 wxAboutBox(aboutInfo
);
1127 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1132 void GridFrame::OnBugsTable(wxCommandEvent
& )
1134 BugsGridFrame
*frame
= new BugsGridFrame
;
1138 // ----------------------------------------------------------------------------
1139 // MyGridCellAttrProvider
1140 // ----------------------------------------------------------------------------
1142 MyGridCellAttrProvider::MyGridCellAttrProvider()
1144 m_attrForOddRows
= new wxGridCellAttr
;
1145 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1148 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1150 m_attrForOddRows
->DecRef();
1153 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1154 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1156 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1162 attr
= m_attrForOddRows
;
1167 if ( !attr
->HasBackgroundColour() )
1169 wxGridCellAttr
*attrNew
= attr
->Clone();
1172 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1180 void GridFrame::OnVTable(wxCommandEvent
& )
1182 static long s_sizeGrid
= 10000;
1184 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
1186 _T("wxGridDemo question"),
1190 if ( s_sizeGrid
!= -1 )
1192 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1197 // ----------------------------------------------------------------------------
1198 // MyGridCellRenderer
1199 // ----------------------------------------------------------------------------
1201 // do something that the default renderer doesn't here just to show that it is
1202 // possible to alter the appearance of the cell beyond what the attributes
1204 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1205 wxGridCellAttr
& attr
,
1211 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1213 dc
.SetPen(*wxGREEN_PEN
);
1214 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1215 dc
.DrawEllipse(rect
);
1218 // ============================================================================
1219 // BigGridFrame and BigGridTable: Sample of a non-standard table
1220 // ============================================================================
1222 BigGridFrame::BigGridFrame(long sizeGrid
)
1223 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1224 wxDefaultPosition
, wxSize(500, 450))
1226 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1227 m_table
= new BigGridTable(sizeGrid
);
1229 // VZ: I don't understand why this slows down the display that much,
1230 // must profile it...
1231 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1233 m_grid
->SetTable(m_table
, true);
1235 #if defined __WXMOTIF__
1236 // MB: the grid isn't getting a sensible default size under wxMotif
1238 GetClientSize( &cw
, &ch
);
1239 m_grid
->SetSize( cw
, ch
);
1243 // ============================================================================
1244 // BugsGridFrame: a "realistic" table
1245 // ============================================================================
1247 // ----------------------------------------------------------------------------
1249 // ----------------------------------------------------------------------------
1272 static const wxString severities
[] =
1281 static struct BugsGridData
1287 wxChar platform
[12];
1289 } gs_dataBugsGrid
[] =
1291 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1292 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1293 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1294 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1297 static const wxChar
*headers
[Col_Max
] =
1307 // ----------------------------------------------------------------------------
1309 // ----------------------------------------------------------------------------
1311 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1317 return wxGRID_VALUE_NUMBER
;;
1320 // fall thorugh (TODO should be a list)
1323 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1326 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1329 return wxGRID_VALUE_BOOL
;
1332 wxFAIL_MSG(_T("unknown column"));
1334 return wxEmptyString
;
1337 int BugsGridTable::GetNumberRows()
1339 return WXSIZEOF(gs_dataBugsGrid
);
1342 int BugsGridTable::GetNumberCols()
1347 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1352 wxString
BugsGridTable::GetValue( int row
, int col
)
1354 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1359 return wxString::Format(_T("%d"), gd
.id
);
1362 return wxString::Format(_T("%d"), gd
.prio
);
1365 return gd
.opened
? _T("1") : _T("0");
1368 return severities
[gd
.severity
];
1377 return wxEmptyString
;
1380 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1382 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1389 wxFAIL_MSG(_T("unexpected column"));
1395 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1397 if ( severities
[n
] == value
)
1399 gd
.severity
= (Severity
)n
;
1404 if ( n
== WXSIZEOF(severities
) )
1406 wxLogWarning(_T("Invalid severity value '%s'."),
1408 gd
.severity
= Sev_Normal
;
1414 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1418 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1424 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1426 const wxString
& typeName
)
1428 if ( typeName
== wxGRID_VALUE_STRING
)
1432 else if ( typeName
== wxGRID_VALUE_BOOL
)
1434 return col
== Col_Opened
;
1436 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1438 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1446 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1448 return CanGetValueAs(row
, col
, typeName
);
1451 long BugsGridTable::GetValueAsLong( int row
, int col
)
1453 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1467 wxFAIL_MSG(_T("unexpected column"));
1472 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1474 if ( col
== Col_Opened
)
1476 return gs_dataBugsGrid
[row
].opened
;
1480 wxFAIL_MSG(_T("unexpected column"));
1486 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1488 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1497 wxFAIL_MSG(_T("unexpected column"));
1501 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1503 if ( col
== Col_Opened
)
1505 gs_dataBugsGrid
[row
].opened
= value
;
1509 wxFAIL_MSG(_T("unexpected column"));
1513 wxString
BugsGridTable::GetColLabelValue( int col
)
1515 return headers
[col
];
1518 // ----------------------------------------------------------------------------
1520 // ----------------------------------------------------------------------------
1522 BugsGridFrame::BugsGridFrame()
1523 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"))
1525 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1526 wxGridTableBase
*table
= new BugsGridTable();
1527 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1528 grid
->SetTable(table
, true);
1530 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1531 *attrRangeEditor
= new wxGridCellAttr
,
1532 *attrCombo
= new wxGridCellAttr
;
1534 attrRO
->SetReadOnly();
1535 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1536 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1539 grid
->SetColAttr(Col_Id
, attrRO
);
1540 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1541 grid
->SetColAttr(Col_Severity
, attrCombo
);
1544 SetClientSize(grid
->GetSize());
1547 // ============================================================================
1548 // TabularGrid: grid used for display of tabular data
1549 // ============================================================================
1551 class TabularGridTable
: public wxGridTableBase
1568 virtual int GetNumberRows() { return ROW_MAX
; }
1569 virtual int GetNumberCols() { return COL_MAX
; }
1571 virtual wxString
GetValue(int row
, int col
)
1573 // notice that column parameter here always refers to the internal
1574 // column index, independently of its position on the screen
1575 static const char *filedata
[][COL_MAX
] =
1577 { "autoexec", "bat", "412", "Apr 17 2004" },
1578 { "boot", "ini", "604", "May 27 2006" },
1579 { "io", "sys", "40774", "May 31 1994" },
1581 wxCOMPILE_TIME_ASSERT( WXSIZEOF(filedata
) == ROW_MAX
, Mismatch
);
1583 return filedata
[row
][col
];
1586 virtual void SetValue(int, int, const wxString
&)
1588 wxFAIL_MSG( "shouldn't be called" );
1591 virtual wxString
GetColLabelValue(int col
)
1593 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1594 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, Mismatch
);
1599 virtual void SetColLabelValue(int, const wxString
&)
1601 wxFAIL_MSG( "shouldn't be called" );
1605 // specialized text control for column indexes entry
1606 class ColIndexEntry
: public wxTextCtrl
1609 ColIndexEntry(wxWindow
*parent
)
1610 : wxTextCtrl(parent
, wxID_ANY
, "")
1612 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1618 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1628 virtual wxSize
DoGetBestSize() const
1630 wxSize size
= wxTextCtrl::DoGetBestSize();
1631 size
.x
= 3*GetCharWidth();
1636 class TabularGridFrame
: public wxFrame
1645 Id_Check_EnableColMove
1650 void OnToggleUseNativeHeader(wxCommandEvent
&)
1652 m_grid
->SetUseNativeColLabels(m_chkUseNative
->IsChecked());
1655 void OnToggleColMove(wxCommandEvent
&)
1657 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1660 void OnMoveColumn(wxCommandEvent
&)
1662 int col
= m_txtColIndex
->GetCol();
1663 int pos
= m_txtColPos
->GetCol();
1664 if ( col
== -1 || pos
== -1 )
1667 m_grid
->SetColPos(col
, pos
);
1672 void OnGridColMove(wxGridEvent
& event
)
1682 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
1683 s
<< m_grid
->GetColAt(pos
) << ' ';
1685 m_statOrder
->SetLabel(s
);
1690 wxCheckBox
*m_chkUseNative
,
1691 *m_chkEnableColMove
;
1693 ColIndexEntry
*m_txtColIndex
,
1696 wxStaticText
*m_statOrder
;
1698 DECLARE_NO_COPY_CLASS(TabularGridFrame
)
1699 DECLARE_EVENT_TABLE()
1702 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
1703 EVT_CHECKBOX(Id_Check_UseNative
, TabularGridFrame::OnToggleUseNativeHeader
)
1704 EVT_CHECKBOX(Id_Check_EnableColMove
, TabularGridFrame::OnToggleColMove
)
1706 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
1708 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
1711 TabularGridFrame::TabularGridFrame()
1712 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
1714 // create and initialize the grid with the specified data
1715 m_grid
= new wxGrid(this, wxID_ANY
);
1716 m_grid
->SetTable(new TabularGridTable
, true, wxGrid::wxGridSelectRows
);
1718 m_grid
->SetUseNativeColLabels();
1719 m_grid
->EnableDragColMove();
1721 // add it and the other controls to the frame
1722 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
1723 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
1725 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
1727 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
1728 m_chkUseNative
= new wxCheckBox(this, Id_Check_UseNative
,
1729 "&Use native header");
1730 m_chkUseNative
->SetValue(true);
1731 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
1733 m_chkEnableColMove
= new wxCheckBox(this, Id_Check_EnableColMove
,
1734 "Allow column re&ordering");
1735 m_chkEnableColMove
->SetValue(true);
1736 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
1737 sizerControls
->Add(sizerStyles
);
1739 sizerControls
->AddSpacer(10);
1741 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
1742 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
1744 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
1745 sizerMoveCols
->Add(new wxStaticText(this, wxID_ANY
, "&Move column"),
1747 m_txtColIndex
= new ColIndexEntry(this);
1748 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
1749 sizerMoveCols
->Add(new wxStaticText(this, wxID_ANY
, "&to"), flagsHorz
);
1750 m_txtColPos
= new ColIndexEntry(this);
1751 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
1752 sizerMoveCols
->Add(new wxButton(this, wxID_APPLY
), flagsHorz
);
1754 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
1756 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
1757 sizerShowCols
->Add(new wxStaticText(this, wxID_ANY
, "Current order:"),
1759 m_statOrder
= new wxStaticText(this, wxID_ANY
, "<default>");
1760 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
1761 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
1763 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
1765 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
1767 SetSizerAndFit(sizerTop
);
1768 SetBackgroundColour(*wxWHITE
);
1772 void GridFrame::OnTabularTable(wxCommandEvent
&)
1774 new TabularGridFrame
;