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/headerctrl.h"
37 #include "wx/generic/gridctrl.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 IMPLEMENT_APP( GridApp
)
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 bool GridApp::OnInit()
57 GridFrame
*frame
= new GridFrame
;
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
68 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
69 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
70 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
71 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
72 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
73 EVT_MENU( ID_TOGGLECOLMOVING
, GridFrame::ToggleColMoving
)
74 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
75 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
76 EVT_MENU( ID_TOGGLENATIVEHEADER
, GridFrame::ToggleNativeHeader
)
77 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
78 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
79 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
80 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
81 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
82 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
83 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
84 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
85 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
86 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
87 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
88 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
89 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
90 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
91 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
92 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
93 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
94 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
95 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
96 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
97 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
99 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
100 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
102 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
103 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
104 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
105 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
106 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
108 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
109 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
110 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
111 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
112 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
113 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
114 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
115 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
116 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
117 EVT_MENU( ID_SHOW_SELECTION
, GridFrame::OnShowSelection
)
119 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
120 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
121 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
122 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
123 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
124 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
125 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
127 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
128 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
130 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
131 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
132 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
133 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
134 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
135 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
136 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
137 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged
)
138 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
140 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
141 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
145 GridFrame::GridFrame()
146 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets grid class demo"),
150 wxMenu
*fileMenu
= new wxMenu
;
151 fileMenu
->Append( ID_VTABLE
, _T("&Virtual table test\tCtrl-V"));
152 fileMenu
->Append( ID_BUGS_TABLE
, _T("&Bugs table test\tCtrl-B"));
153 fileMenu
->Append( ID_TABULAR_TABLE
, _T("&Tabular table test\tCtrl-T"));
154 fileMenu
->AppendSeparator();
155 fileMenu
->Append( wxID_EXIT
, _T("E&xit\tAlt-X") );
157 wxMenu
*viewMenu
= new wxMenu
;
158 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
159 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
160 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
161 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
162 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
163 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
164 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
165 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
166 viewMenu
->AppendCheckItem(ID_TOGGLENATIVEHEADER
, "&Native column headers");
167 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
168 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
169 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
170 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
171 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
172 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
174 wxMenu
*rowLabelMenu
= new wxMenu
;
176 viewMenu
->Append( ID_ROWLABELALIGN
, _T("R&ow label alignment"),
178 _T("Change alignment of row labels") );
180 rowLabelMenu
->Append( ID_ROWLABELHORIZALIGN
, _T("&Horizontal") );
181 rowLabelMenu
->Append( ID_ROWLABELVERTALIGN
, _T("&Vertical") );
183 wxMenu
*colLabelMenu
= new wxMenu
;
185 viewMenu
->Append( ID_COLLABELALIGN
, _T("Col l&abel alignment"),
187 _T("Change alignment of col labels") );
189 colLabelMenu
->Append( ID_COLLABELHORIZALIGN
, _T("&Horizontal") );
190 colLabelMenu
->Append( ID_COLLABELVERTALIGN
, _T("&Vertical") );
192 wxMenu
*colMenu
= new wxMenu
;
193 colMenu
->Append( ID_SETLABELCOLOUR
, _T("Set &label colour...") );
194 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, _T("Set label &text colour...") );
195 colMenu
->Append( ID_SETLABEL_FONT
, _T("Set label fo&nt...") );
196 colMenu
->Append( ID_GRIDLINECOLOUR
, _T("&Grid line colour...") );
197 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, _T("Set cell &foreground colour...") );
198 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, _T("Set cell &background colour...") );
200 wxMenu
*editMenu
= new wxMenu
;
201 editMenu
->Append( ID_INSERTROW
, _T("Insert &row") );
202 editMenu
->Append( ID_INSERTCOL
, _T("Insert &column") );
203 editMenu
->Append( ID_DELETEROW
, _T("Delete selected ro&ws") );
204 editMenu
->Append( ID_DELETECOL
, _T("Delete selected co&ls") );
205 editMenu
->Append( ID_CLEARGRID
, _T("Cl&ear grid cell contents") );
207 wxMenu
*selectMenu
= new wxMenu
;
208 selectMenu
->Append( ID_SELECT_UNSELECT
, _T("Add new cells to the selection"),
209 _T("When off, old selection is deselected before ")
210 _T("selecting the new cells"), wxITEM_CHECK
);
211 selectMenu
->Append( ID_SHOW_SELECTION
,
212 _T("&Show current selection\tCtrl-Alt-S"));
213 selectMenu
->AppendSeparator();
214 selectMenu
->Append( ID_SELECT_ALL
, _T("Select all"));
215 selectMenu
->Append( ID_SELECT_ROW
, _T("Select row 2"));
216 selectMenu
->Append( ID_SELECT_COL
, _T("Select col 2"));
217 selectMenu
->Append( ID_SELECT_CELL
, _T("Select cell (3, 1)"));
218 selectMenu
->AppendSeparator();
219 selectMenu
->Append( ID_DESELECT_ALL
, _T("Deselect all"));
220 selectMenu
->Append( ID_DESELECT_ROW
, _T("Deselect row 2"));
221 selectMenu
->Append( ID_DESELECT_COL
, _T("Deselect col 2"));
222 selectMenu
->Append( ID_DESELECT_CELL
, _T("Deselect cell (3, 1)"));
223 wxMenu
*selectionMenu
= new wxMenu
;
224 selectMenu
->Append( ID_CHANGESEL
, _T("Change &selection mode"),
226 _T("Change selection mode") );
228 selectionMenu
->Append( ID_SELCELLS
, _T("Select &cells") );
229 selectionMenu
->Append( ID_SELROWS
, _T("Select &rows") );
230 selectionMenu
->Append( ID_SELCOLS
, _T("Select col&umns") );
231 selectionMenu
->Append( ID_SELROWSORCOLS
, _T("Select rows &or columns") );
233 wxMenu
*autosizeMenu
= new wxMenu
;
234 autosizeMenu
->Append( ID_SIZE_ROW
, _T("Selected &row data") );
235 autosizeMenu
->Append( ID_SIZE_COL
, _T("Selected &column data") );
236 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, _T("Selected row la&bel") );
237 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, _T("Selected column &label") );
238 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, _T("Column la&bels") );
239 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, _T("Row label&s") );
240 autosizeMenu
->Append( ID_SIZE_GRID
, _T("Entire &grid") );
242 wxMenu
*helpMenu
= new wxMenu
;
243 helpMenu
->Append( wxID_ABOUT
, _T("&About wxGrid demo") );
245 wxMenuBar
*menuBar
= new wxMenuBar
;
246 menuBar
->Append( fileMenu
, _T("&File") );
247 menuBar
->Append( viewMenu
, _T("&Grid") );
248 menuBar
->Append( colMenu
, _T("&Colours") );
249 menuBar
->Append( editMenu
, _T("&Edit") );
250 menuBar
->Append( selectMenu
, _T("&Select") );
251 menuBar
->Append( autosizeMenu
, _T("&Autosize") );
252 menuBar
->Append( helpMenu
, _T("&Help") );
254 SetMenuBar( menuBar
);
258 grid
= new wxGrid( this,
261 wxSize( 400, 300 ) );
264 int gridW
= 600, gridH
= 300;
265 int logW
= gridW
, logH
= 100;
267 logWin
= new wxTextCtrl( this,
270 wxPoint( 0, gridH
+ 20 ),
271 wxSize( logW
, logH
),
274 logger
= new wxLogTextCtrl( logWin
);
275 m_logOld
= wxLog::SetActiveTarget( logger
);
276 wxLog::DisableTimestamp();
279 // this will create a grid and, by default, an associated grid
281 grid
->CreateGrid( 0, 0 );
282 grid
->AppendRows(100);
283 grid
->AppendCols(100);
285 int ir
= grid
->GetNumberRows();
286 grid
->DeleteRows(0, ir
);
287 grid
->AppendRows(ir
);
289 grid
->SetRowSize( 0, 60 );
290 grid
->SetCellValue( 0, 0, _T("Ctrl+Home\nwill go to\nthis cell") );
292 grid
->SetCellValue( 0, 1, _T("A long piece of text to demonstrate wrapping.") );
293 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
294 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
296 grid
->SetCellValue( 0, 2, _T("Blah") );
297 grid
->SetCellValue( 0, 3, _T("Read only") );
298 grid
->SetReadOnly( 0, 3 );
300 grid
->SetCellValue( 0, 4, _T("Can veto edit this cell") );
302 grid
->SetCellValue( 0, 5, _T("Press\nCtrl+arrow\nto skip over\ncells") );
304 grid
->SetRowSize( 99, 60 );
305 grid
->SetCellValue( 99, 99, _T("Ctrl+End\nwill go to\nthis cell") );
306 grid
->SetCellValue( 1, 0, _T("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
308 grid
->SetCellTextColour(1, 2, *wxRED
);
309 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
311 grid
->SetCellValue( 1, 4, _T("I'm in the middle"));
313 grid
->SetCellValue(2, 2, _T("red"));
315 grid
->SetCellTextColour(2, 2, *wxRED
);
316 grid
->SetCellValue(3, 3, _T("green on grey"));
317 grid
->SetCellTextColour(3, 3, *wxGREEN
);
318 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
320 grid
->SetCellValue(4, 4, _T("a weird looking cell"));
321 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
322 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
324 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
325 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
327 wxGridCellAttr
*attr
;
328 attr
= new wxGridCellAttr
;
329 attr
->SetTextColour(*wxBLUE
);
330 grid
->SetColAttr(5, attr
);
331 attr
= new wxGridCellAttr
;
332 attr
->SetBackgroundColour(*wxRED
);
333 grid
->SetRowAttr(5, attr
);
335 grid
->SetCellValue(2, 4, _T("a wider column"));
336 grid
->SetColSize(4, 120);
337 grid
->SetColMinimalWidth(4, 120);
339 grid
->SetCellTextColour(5, 8, *wxGREEN
);
340 grid
->SetCellValue(5, 8, _T("Bg from row attr\nText col from cell attr"));
341 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"));
343 grid
->SetColFormatFloat(6);
344 grid
->SetCellValue(0, 6, wxString::Format(wxT("%g"), 3.1415));
345 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 1415.0));
346 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 12345.67890));
348 grid
->SetColFormatFloat(7, 6, 2);
349 grid
->SetCellValue(0, 7, wxString::Format(wxT("%g"), 3.1415));
350 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 1415.0));
351 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 12345.67890));
353 grid
->SetColFormatNumber(8);
354 grid
->SetCellValue(0, 8, "17");
355 grid
->SetCellValue(1, 8, "0");
356 grid
->SetCellValue(2, 8, "-666");
358 const wxString choices
[] =
360 _T("Please select a choice"),
361 _T("This takes two cells"),
362 _T("Another choice"),
364 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
365 grid
->SetCellSize(4, 0, 1, 2);
366 grid
->SetCellValue(4, 0, choices
[0]);
367 grid
->SetCellOverflow(4, 0, false);
369 grid
->SetCellSize(7, 1, 3, 4);
370 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
371 grid
->SetCellValue(7, 1, _T("Big box!"));
373 // this does exactly nothing except testing that SetAttr() handles NULL
374 // attributes and does reference counting correctly
375 grid
->SetAttr(11, 11, NULL
);
376 grid
->SetAttr(11, 11, new wxGridCellAttr
);
377 grid
->SetAttr(11, 11, NULL
);
379 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
385 topSizer
->Add( logWin
,
390 SetSizerAndFit( topSizer
);
397 GridFrame::~GridFrame()
400 delete wxLog::SetActiveTarget(m_logOld
);
405 void GridFrame::SetDefaults()
407 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
408 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
409 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
410 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
411 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
412 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
413 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
414 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
415 GetMenuBar()->Check( ID_TOGGLENATIVEHEADER
, false );
416 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
417 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
421 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
423 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
425 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
429 grid
->SetRowLabelSize( 0 );
434 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
436 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
438 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
442 grid
->SetColLabelSize( 0 );
447 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
450 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
454 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
456 grid
->EnableDragRowSize(
457 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
461 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
463 grid
->EnableDragColSize(
464 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
467 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
469 grid
->EnableDragColMove(
470 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
473 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
475 grid
->EnableDragGridSize(
476 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
479 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
481 grid
->EnableDragCell(
482 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
485 void GridFrame::ToggleNativeHeader( wxCommandEvent
& WXUNUSED(ev
) )
487 grid
->SetUseNativeColLabels(
488 GetMenuBar()->IsChecked( ID_TOGGLENATIVEHEADER
) );
491 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
493 grid
->EnableGridLines(
494 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
497 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
499 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
501 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
502 _T("Pen Width"), 11, choices
);
504 int current
= grid
->GetCellHighlightPenWidth();
505 dlg
.SetSelection(current
);
506 if (dlg
.ShowModal() == wxID_OK
) {
507 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
511 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
513 wxString choices
[] = { _T("0"), _T("1"), _T("2"), _T("3"), _T("4"), _T("5"), _T("6"), _T("7"), _T("8"), _T("9"), _T("10")};
515 wxSingleChoiceDialog
dlg(this, _T("Choose the thickness of the highlight pen:"),
516 _T("Pen Width"), 11, choices
);
518 int current
= grid
->GetCellHighlightROPenWidth();
519 dlg
.SetSelection(current
);
520 if (dlg
.ShowModal() == wxID_OK
) {
521 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
527 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
529 grid
->AutoSizeColumns();
533 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
535 grid
->SetDefaultCellOverflow(ev
.IsChecked());
539 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
542 grid
->SetCellSize( 7, 1, 5, 5 );
544 grid
->SetCellSize( 7, 1, 1, 5 );
548 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
550 wxColourDialog
dlg( NULL
);
551 if ( dlg
.ShowModal() == wxID_OK
)
553 wxColourData retData
;
554 retData
= dlg
.GetColourData();
555 wxColour colour
= retData
.GetColour();
557 grid
->SetLabelBackgroundColour( colour
);
562 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
564 wxColourDialog
dlg( NULL
);
565 if ( dlg
.ShowModal() == wxID_OK
)
567 wxColourData retData
;
568 retData
= dlg
.GetColourData();
569 wxColour colour
= retData
.GetColour();
571 grid
->SetLabelTextColour( colour
);
575 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
577 wxFont font
= wxGetFontFromUser(this);
580 grid
->SetLabelFont(font
);
584 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
587 grid
->GetRowLabelAlignment( &horiz
, &vert
);
592 horiz
= wxALIGN_CENTRE
;
596 horiz
= wxALIGN_RIGHT
;
600 horiz
= wxALIGN_LEFT
;
604 grid
->SetRowLabelAlignment( horiz
, vert
);
607 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
610 grid
->GetRowLabelAlignment( &horiz
, &vert
);
615 vert
= wxALIGN_CENTRE
;
619 vert
= wxALIGN_BOTTOM
;
627 grid
->SetRowLabelAlignment( horiz
, vert
);
631 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
634 grid
->GetColLabelAlignment( &horiz
, &vert
);
639 horiz
= wxALIGN_CENTRE
;
643 horiz
= wxALIGN_RIGHT
;
647 horiz
= wxALIGN_LEFT
;
651 grid
->SetColLabelAlignment( horiz
, vert
);
655 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
658 grid
->GetColLabelAlignment( &horiz
, &vert
);
663 vert
= wxALIGN_CENTRE
;
667 vert
= wxALIGN_BOTTOM
;
675 grid
->SetColLabelAlignment( horiz
, vert
);
679 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
681 wxColourDialog
dlg( NULL
);
682 if ( dlg
.ShowModal() == wxID_OK
)
684 wxColourData retData
;
685 retData
= dlg
.GetColourData();
686 wxColour colour
= retData
.GetColour();
688 grid
->SetGridLineColour( colour
);
693 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
695 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
699 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
701 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
705 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
707 if ( grid
->IsSelection() )
709 wxGridUpdateLocker
locker(grid
);
710 for ( int n
= 0; n
< grid
->GetNumberRows(); )
712 if ( grid
->IsInSelection( n
, 0 ) )
713 grid
->DeleteRows( n
, 1 );
721 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
723 wxGridUpdateLocker
locker(grid
);
724 const wxArrayInt sels
= grid
->GetSelectedRows();
725 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
727 grid
->AutoSizeRow( sels
[n
], false );
731 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
733 wxGridUpdateLocker
locker(grid
);
734 const wxArrayInt sels
= grid
->GetSelectedCols();
735 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
737 grid
->AutoSizeColumn( sels
[n
], false );
741 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
743 wxGridUpdateLocker
locker(grid
);
744 const wxArrayInt sels
= grid
->GetSelectedRows();
745 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
747 grid
->AutoSizeRowLabelSize( sels
[n
] );
751 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
753 wxGridUpdateLocker
locker(grid
);
754 const wxArrayInt sels
= grid
->GetSelectedCols();
755 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
757 grid
->AutoSizeColLabelSize( sels
[n
] );
761 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
763 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
766 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
768 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
771 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
777 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
779 if ( grid
->IsSelection() )
781 wxGridUpdateLocker
locker(grid
);
782 for ( int n
= 0; n
< grid
->GetNumberCols(); )
784 if ( grid
->IsInSelection( 0 , n
) )
785 grid
->DeleteCols( n
, 1 );
793 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
798 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
800 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
803 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
805 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
808 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
810 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
813 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
815 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
818 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
820 wxColour col
= wxGetColourFromUser(this);
823 grid
->SetDefaultCellTextColour(col
);
828 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
830 wxColour col
= wxGetColourFromUser(this);
833 // Check the new Refresh function by passing it a rectangle
834 // which exactly fits the grid.
836 wxRect
r(pt
, grid
->GetSize());
837 grid
->SetDefaultCellBackgroundColour(col
);
838 grid
->Refresh(true, &r
);
842 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
844 grid
->DeselectCell(3, 1);
847 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
849 grid
->DeselectCol(2);
852 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
854 grid
->DeselectRow(2);
857 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
859 grid
->ClearSelection();
862 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
864 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
867 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
869 grid
->SelectCol(2, m_addToSel
);
872 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
874 grid
->SelectRow(2, m_addToSel
);
877 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
882 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
884 m_addToSel
= event
.IsChecked();
887 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
890 if ( ev
.GetRow() != -1 )
892 logBuf
<< _T("Left click on row label ") << ev
.GetRow();
894 else if ( ev
.GetCol() != -1 )
896 logBuf
<< _T("Left click on col label ") << ev
.GetCol();
900 logBuf
<< _T("Left click on corner label");
903 if ( ev
.ShiftDown() )
904 logBuf
<< _T(" (shift down)");
905 if ( ev
.ControlDown() )
906 logBuf
<< _T(" (control down)");
907 wxLogMessage( wxT("%s"), logBuf
.c_str() );
909 // you must call event skip if you want default grid processing
915 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
917 wxLogMessage(_T("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
919 // you must call event skip if you want default grid processing
920 // (cell highlighting etc.)
926 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
928 wxLogMessage(_T("Resized row %d"), ev
.GetRowOrCol());
934 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
936 wxLogMessage(_T("Resized col %d"), ev
.GetRowOrCol());
942 void GridFrame::OnShowSelection(wxCommandEvent
& WXUNUSED(event
))
944 // max number of elements to dump -- otherwise it can take too much time
945 static const size_t countMax
= 100;
949 switch ( grid
->GetSelectionMode() )
951 case wxGrid::wxGridSelectCells
:
953 const wxGridCellCoordsArray
cells(grid
->GetSelectedCells());
954 size_t count
= cells
.size();
955 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count
);
956 if ( count
> countMax
)
958 wxLogMessage(_T("[too many selected cells, ")
959 _T("showing only the first %lu]"),
960 (unsigned long)countMax
);
964 for ( size_t n
= 0; n
< count
; n
++ )
966 const wxGridCellCoords
& c
= cells
[n
];
967 wxLogMessage(_T(" selected cell %lu: (%d, %d)"),
968 (unsigned long)n
, c
.GetCol(), c
.GetRow());
973 case wxGrid::wxGridSelectRows
:
977 case wxGrid::wxGridSelectColumns
:
979 const wxChar
*plural
, *single
;
987 plural
= _T("columns");
988 single
= _T("column");
991 const wxArrayInt
sels((const wxArrayInt
)(rows
? grid
->GetSelectedRows()
992 : grid
->GetSelectedCols()));
993 size_t count
= sels
.size();
994 wxLogMessage(_T("%lu %s selected:"),
995 (unsigned long)count
, plural
);
996 if ( count
> countMax
)
998 wxLogMessage(_T("[too many selected %s, ")
999 _T("showing only the first %lu]"),
1000 plural
, (unsigned long)countMax
);
1004 for ( size_t n
= 0; n
< count
; n
++ )
1006 wxLogMessage(_T(" selected %s %lu: %d"),
1007 single
, (unsigned long)n
, sels
[n
]);
1013 wxFAIL_MSG( _T("unknown wxGrid selection mode") );
1018 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1021 if ( ev
.Selecting() )
1022 logBuf
<< _T("Selected ");
1024 logBuf
<< _T("Deselected ");
1025 logBuf
<< _T("cell at row ") << ev
.GetRow()
1026 << _T(" col ") << ev
.GetCol()
1027 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1028 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1029 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1030 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
1032 //Indicate whether this column was moved
1033 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1034 logBuf
<< _T(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1036 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1038 // you must call Skip() if you want the default processing
1039 // to occur in wxGrid
1043 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1046 if ( ev
.Selecting() )
1047 logBuf
<< _T("Selected ");
1049 logBuf
<< _T("Deselected ");
1050 logBuf
<< _T("cells from row ") << ev
.GetTopRow()
1051 << _T(" col ") << ev
.GetLeftCol()
1052 << _T(" to row ") << ev
.GetBottomRow()
1053 << _T(" col ") << ev
.GetRightCol()
1054 << _T(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1055 << _T(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1056 << _T(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1057 << _T(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << _T(" )");
1058 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1063 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1065 int row
= ev
.GetRow(),
1068 wxLogMessage("Value of cell at (%d, %d): about to change "
1069 "from \"%s\" to \"%s\"",
1071 grid
->GetCellValue(row
, col
), ev
.GetString());
1073 // test how vetoing works
1074 if ( ev
.GetString() == "42" )
1076 wxLogMessage("Vetoing the change.");
1084 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1086 int row
= ev
.GetRow(),
1089 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1092 grid
->GetCellValue(row
, col
), ev
.GetString());
1097 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1099 wxLogMessage(_T("Got request to drag cell at row %d, col %d"),
1100 ev
.GetRow(), ev
.GetCol());
1105 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1108 if ( (ev
.GetCol() == 4) &&
1109 (ev
.GetRow() == 0) &&
1110 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
1111 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1117 wxLogMessage( wxT("Cell editor shown.") );
1122 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1125 if ( (ev
.GetCol() == 4) &&
1126 (ev
.GetRow() == 0) &&
1127 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
1128 _T("Checking"),wxYES_NO
) == wxNO
) ) {
1134 wxLogMessage( wxT("Cell editor hidden.") );
1139 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1141 wxAboutDialogInfo aboutInfo
;
1142 aboutInfo
.SetName(wxT("wxGrid demo"));
1143 aboutInfo
.SetDescription(_("wxGrid sample program"));
1144 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1145 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1146 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1148 wxAboutBox(aboutInfo
);
1152 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1157 void GridFrame::OnBugsTable(wxCommandEvent
& )
1159 BugsGridFrame
*frame
= new BugsGridFrame
;
1163 // ----------------------------------------------------------------------------
1164 // MyGridCellAttrProvider
1165 // ----------------------------------------------------------------------------
1167 MyGridCellAttrProvider::MyGridCellAttrProvider()
1169 m_attrForOddRows
= new wxGridCellAttr
;
1170 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1173 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1175 m_attrForOddRows
->DecRef();
1178 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1179 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1181 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1187 attr
= m_attrForOddRows
;
1192 if ( !attr
->HasBackgroundColour() )
1194 wxGridCellAttr
*attrNew
= attr
->Clone();
1197 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1205 void GridFrame::OnVTable(wxCommandEvent
& )
1207 static long s_sizeGrid
= 10000;
1209 s_sizeGrid
= wxGetNumberFromUser(_T("Size of the table to create"),
1211 _T("wxGridDemo question"),
1215 if ( s_sizeGrid
!= -1 )
1217 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1222 // ----------------------------------------------------------------------------
1223 // MyGridCellRenderer
1224 // ----------------------------------------------------------------------------
1226 // do something that the default renderer doesn't here just to show that it is
1227 // possible to alter the appearance of the cell beyond what the attributes
1229 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1230 wxGridCellAttr
& attr
,
1236 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1238 dc
.SetPen(*wxGREEN_PEN
);
1239 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1240 dc
.DrawEllipse(rect
);
1243 // ============================================================================
1244 // BigGridFrame and BigGridTable: Sample of a non-standard table
1245 // ============================================================================
1247 BigGridFrame::BigGridFrame(long sizeGrid
)
1248 : wxFrame(NULL
, wxID_ANY
, _T("Plugin Virtual Table"),
1249 wxDefaultPosition
, wxSize(500, 450))
1251 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1252 m_table
= new BigGridTable(sizeGrid
);
1254 // VZ: I don't understand why this slows down the display that much,
1255 // must profile it...
1256 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1258 m_grid
->SetTable(m_table
, true);
1260 #if defined __WXMOTIF__
1261 // MB: the grid isn't getting a sensible default size under wxMotif
1263 GetClientSize( &cw
, &ch
);
1264 m_grid
->SetSize( cw
, ch
);
1268 // ============================================================================
1269 // BugsGridFrame: a "realistic" table
1270 // ============================================================================
1272 // ----------------------------------------------------------------------------
1274 // ----------------------------------------------------------------------------
1297 static const wxString severities
[] =
1306 static struct BugsGridData
1312 wxChar platform
[12];
1314 } gs_dataBugsGrid
[] =
1316 { 18, _T("foo doesn't work"), Sev_Major
, 1, _T("wxMSW"), true },
1317 { 27, _T("bar crashes"), Sev_Critical
, 1, _T("all"), false },
1318 { 45, _T("printing is slow"), Sev_Minor
, 3, _T("wxMSW"), true },
1319 { 68, _T("Rectangle() fails"), Sev_Normal
, 1, _T("wxMSW"), false },
1322 static const wxChar
*headers
[Col_Max
] =
1332 // ----------------------------------------------------------------------------
1334 // ----------------------------------------------------------------------------
1336 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1342 return wxGRID_VALUE_NUMBER
;;
1345 // fall thorugh (TODO should be a list)
1348 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING
);
1351 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1354 return wxGRID_VALUE_BOOL
;
1357 wxFAIL_MSG(_T("unknown column"));
1359 return wxEmptyString
;
1362 int BugsGridTable::GetNumberRows()
1364 return WXSIZEOF(gs_dataBugsGrid
);
1367 int BugsGridTable::GetNumberCols()
1372 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1377 wxString
BugsGridTable::GetValue( int row
, int col
)
1379 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1384 return wxString::Format(_T("%d"), gd
.id
);
1387 return wxString::Format(_T("%d"), gd
.prio
);
1390 return gd
.opened
? _T("1") : _T("0");
1393 return severities
[gd
.severity
];
1402 return wxEmptyString
;
1405 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1407 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1414 wxFAIL_MSG(_T("unexpected column"));
1420 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1422 if ( severities
[n
] == value
)
1424 gd
.severity
= (Severity
)n
;
1429 if ( n
== WXSIZEOF(severities
) )
1431 wxLogWarning(_T("Invalid severity value '%s'."),
1433 gd
.severity
= Sev_Normal
;
1439 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1443 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1449 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1451 const wxString
& typeName
)
1453 if ( typeName
== wxGRID_VALUE_STRING
)
1457 else if ( typeName
== wxGRID_VALUE_BOOL
)
1459 return col
== Col_Opened
;
1461 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1463 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1471 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1473 return CanGetValueAs(row
, col
, typeName
);
1476 long BugsGridTable::GetValueAsLong( int row
, int col
)
1478 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1492 wxFAIL_MSG(_T("unexpected column"));
1497 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1499 if ( col
== Col_Opened
)
1501 return gs_dataBugsGrid
[row
].opened
;
1505 wxFAIL_MSG(_T("unexpected column"));
1511 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1513 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1522 wxFAIL_MSG(_T("unexpected column"));
1526 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1528 if ( col
== Col_Opened
)
1530 gs_dataBugsGrid
[row
].opened
= value
;
1534 wxFAIL_MSG(_T("unexpected column"));
1538 wxString
BugsGridTable::GetColLabelValue( int col
)
1540 return headers
[col
];
1543 // ----------------------------------------------------------------------------
1545 // ----------------------------------------------------------------------------
1547 BugsGridFrame::BugsGridFrame()
1548 : wxFrame(NULL
, wxID_ANY
, _T("Bugs table"))
1550 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1551 wxGridTableBase
*table
= new BugsGridTable();
1552 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1553 grid
->SetTable(table
, true);
1555 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1556 *attrRangeEditor
= new wxGridCellAttr
,
1557 *attrCombo
= new wxGridCellAttr
;
1559 attrRO
->SetReadOnly();
1560 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1561 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1564 grid
->SetColAttr(Col_Id
, attrRO
);
1565 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1566 grid
->SetColAttr(Col_Severity
, attrCombo
);
1569 SetClientSize(grid
->GetSize());
1572 // ============================================================================
1573 // TabularGrid: grid used for display of tabular data
1574 // ============================================================================
1576 class TabularGridTable
: public wxGridTableBase
1593 TabularGridTable() { m_sortOrder
= NULL
; }
1595 virtual int GetNumberRows() { return ROW_MAX
; }
1596 virtual int GetNumberCols() { return COL_MAX
; }
1598 virtual wxString
GetValue(int row
, int col
)
1601 row
= m_sortOrder
[row
];
1607 return GetNameOrExt(row
, col
);
1610 return wxString::Format("%lu", GetSize(row
));
1613 return GetDate(row
).FormatDate();
1617 wxFAIL_MSG( "unknown column" );
1623 virtual void SetValue(int, int, const wxString
&)
1625 wxFAIL_MSG( "shouldn't be called" );
1628 virtual wxString
GetColLabelValue(int col
)
1630 // notice that column parameter here always refers to the internal
1631 // column index, independently of its position on the screen
1632 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1633 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1638 virtual void SetColLabelValue(int, const wxString
&)
1640 wxFAIL_MSG( "shouldn't be called" );
1643 void Sort(int col
, bool ascending
)
1645 // we hardcode all sorting orders for simplicity here
1646 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1648 // descending ascending
1649 { { 2, 1, 0 }, { 0, 1, 2 } },
1650 { { 2, 1, 0 }, { 0, 1, 2 } },
1651 { { 2, 1, 0 }, { 0, 1, 2 } },
1652 { { 1, 0, 2 }, { 2, 0, 1 } },
1655 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1659 wxString
GetNameOrExt(int row
, int col
) const
1662 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1663 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1665 const wxString
s(names
[row
]);
1666 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1669 unsigned long GetSize(int row
) const
1671 static const unsigned long
1672 sizes
[] = { 412, 604, 40774 };
1673 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1678 wxDateTime
GetDate(int row
) const
1681 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1682 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1685 dt
.ParseISODate(dates
[row
]);
1692 // specialized text control for column indexes entry
1693 class ColIndexEntry
: public wxTextCtrl
1696 ColIndexEntry(wxWindow
*parent
)
1697 : wxTextCtrl(parent
, wxID_ANY
, "")
1699 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1705 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1715 virtual wxSize
DoGetBestSize() const
1717 wxSize size
= wxTextCtrl::DoGetBestSize();
1718 size
.x
= 3*GetCharWidth();
1723 class TabularGridFrame
: public wxFrame
1731 Id_Check_UseNativeHeader
,
1732 Id_Check_DrawNativeLabels
,
1733 Id_Check_ShowRowLabels
,
1734 Id_Check_EnableColMove
1739 void OnToggleUseNativeHeader(wxCommandEvent
&)
1741 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1744 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1746 // we don't draw labels at all, native or otherwise, if we use the
1747 // native header control
1748 event
.Enable( !m_chkUseNative
->GetValue() );
1751 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1753 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1756 void OnToggleShowRowLabels(wxCommandEvent
&)
1758 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1763 void OnToggleColMove(wxCommandEvent
&)
1765 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1768 void OnShowHideColumn(wxCommandEvent
& event
)
1770 int col
= m_txtColShowHide
->GetCol();
1773 m_grid
->SetColSize(col
,
1774 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1776 UpdateOrderAndVisibility();
1780 void OnMoveColumn(wxCommandEvent
&)
1782 int col
= m_txtColIndex
->GetCol();
1783 int pos
= m_txtColPos
->GetCol();
1784 if ( col
== -1 || pos
== -1 )
1787 m_grid
->SetColPos(col
, pos
);
1789 UpdateOrderAndVisibility();
1792 void OnResetColumnOrder(wxCommandEvent
&)
1794 m_grid
->ResetColPos();
1796 UpdateOrderAndVisibility();
1799 void OnGridColSort(wxGridEvent
& event
)
1801 const int col
= event
.GetCol();
1802 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
1803 m_grid
->IsSortOrderAscending()));
1806 void OnGridColMove(wxGridEvent
& event
)
1808 // can't update it yet as the order hasn't been changed, so do it a bit
1810 m_shouldUpdateOrder
= true;
1815 void OnGridColSize(wxGridSizeEvent
& event
)
1817 // we only catch this event to react to the user showing or hiding this
1818 // column using the header control menu and not because we're
1819 // interested in column resizing
1820 UpdateOrderAndVisibility();
1825 void OnIdle(wxIdleEvent
& event
)
1827 if ( m_shouldUpdateOrder
)
1829 m_shouldUpdateOrder
= false;
1830 UpdateOrderAndVisibility();
1836 void UpdateOrderAndVisibility()
1839 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
1841 const int col
= m_grid
->GetColAt(pos
);
1842 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
1853 m_statOrder
->SetLabel(s
);
1858 TabularGridTable
*m_table
;
1859 wxCheckBox
*m_chkUseNative
,
1861 *m_chkShowRowLabels
,
1862 *m_chkEnableColMove
;
1864 ColIndexEntry
*m_txtColIndex
,
1868 wxStaticText
*m_statOrder
;
1870 // fla for EVT_IDLE handler
1871 bool m_shouldUpdateOrder
;
1873 DECLARE_NO_COPY_CLASS(TabularGridFrame
)
1874 DECLARE_EVENT_TABLE()
1877 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
1878 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
1879 TabularGridFrame::OnToggleUseNativeHeader
)
1880 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
1881 TabularGridFrame::OnToggleDrawNativeLabels
)
1882 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
1883 TabularGridFrame::OnToggleShowRowLabels
)
1884 EVT_CHECKBOX(Id_Check_EnableColMove
,
1885 TabularGridFrame::OnToggleColMove
)
1887 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
1888 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
1890 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
1891 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
1892 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
1893 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
1895 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
1896 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
1897 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
1899 EVT_IDLE(TabularGridFrame::OnIdle
)
1902 TabularGridFrame::TabularGridFrame()
1903 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
1905 m_shouldUpdateOrder
= false;
1907 wxPanel
* const panel
= new wxPanel(this);
1909 // create and initialize the grid with the specified data
1910 m_table
= new TabularGridTable
;
1911 m_grid
= new wxGrid(panel
, wxID_ANY
,
1912 wxDefaultPosition
, wxDefaultSize
,
1913 wxBORDER_STATIC
| wxWANTS_CHARS
);
1914 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
1916 m_grid
->EnableDragColMove();
1917 m_grid
->UseNativeColHeader();
1918 m_grid
->HideRowLabels();
1920 // add it and the other controls to the frame
1921 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
1922 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
1924 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
1926 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
1927 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
1928 "&Use native header");
1929 m_chkUseNative
->SetValue(true);
1930 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
1932 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
1933 "&Draw native column labels");
1934 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
1936 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
1937 "Show &row labels");
1938 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
1940 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
1941 "Allow column re&ordering");
1942 m_chkEnableColMove
->SetValue(true);
1943 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
1944 sizerControls
->Add(sizerStyles
);
1946 sizerControls
->AddSpacer(10);
1948 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
1949 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
1951 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
1952 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
1954 m_txtColIndex
= new ColIndexEntry(panel
);
1955 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
1956 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
1957 m_txtColPos
= new ColIndexEntry(panel
);
1958 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
1959 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
1961 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
1963 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
1964 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
1966 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
1967 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
1968 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
1969 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
1971 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
1972 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
1974 m_txtColShowHide
= new ColIndexEntry(panel
);
1975 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
1976 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
1977 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
1978 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
1980 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
1982 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
1984 panel
->SetSizer(sizerTop
);
1986 SetClientSize(panel
->GetBestSize());
1987 SetSizeHints(GetSize());
1992 void GridFrame::OnTabularTable(wxCommandEvent
&)
1994 new TabularGridFrame
;