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"
42 #ifndef wxHAS_IMAGES_IN_RESOURCES
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_RANGE( ID_TAB_STOP
, ID_TAB_LEAVE
, GridFrame::SetTabBehaviour
)
164 EVT_MENU( ID_TAB_CUSTOM
, GridFrame::SetTabCustomHandler
)
165 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
166 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
167 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
168 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
169 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
170 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
171 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
172 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
173 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
174 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
175 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
176 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
177 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
178 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
179 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
180 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
181 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
182 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
183 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
184 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
185 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
187 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
188 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
190 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
191 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
192 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
193 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
194 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
196 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
197 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
198 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
199 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
200 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
201 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
202 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
203 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
204 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
206 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
207 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
208 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
209 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
210 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
211 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
212 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
214 EVT_MENU( ID_HIDECOL
, GridFrame::HideCol
)
215 EVT_MENU( ID_SHOWCOL
, GridFrame::ShowCol
)
216 EVT_MENU( ID_HIDEROW
, GridFrame::HideRow
)
217 EVT_MENU( ID_SHOWROW
, GridFrame::ShowRow
)
219 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
220 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
222 EVT_MENU( wxID_PRINT
, GridFrame::OnGridRender
)
223 EVT_MENU( ID_RENDER_COORDS
, GridFrame::OnGridRender
)
225 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
226 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
227 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
228 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
229 EVT_GRID_COL_AUTO_SIZE( GridFrame::OnColAutoSize
)
230 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
231 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
232 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
233 EVT_GRID_CELL_CHANGED( GridFrame::OnCellValueChanged
)
234 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
236 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
237 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
241 GridFrame::GridFrame()
242 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets grid class demo"),
246 SetIcon(wxICON(sample
));
248 wxMenu
*fileMenu
= new wxMenu
;
249 fileMenu
->Append( ID_VTABLE
, wxT("&Virtual table test\tCtrl-V"));
250 fileMenu
->Append( ID_BUGS_TABLE
, wxT("&Bugs table test\tCtrl-B"));
251 fileMenu
->Append( ID_TABULAR_TABLE
, wxT("&Tabular table test\tCtrl-T"));
252 fileMenu
->AppendSeparator();
254 wxMenu
* setupMenu
= new wxMenu
;
256 item
= setupMenu
->AppendCheckItem( ID_RENDER_ROW_LABEL
,
257 "Render row labels" );
259 item
= setupMenu
->AppendCheckItem( ID_RENDER_COL_LABEL
,
260 "Render column labels" );
262 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_LINES
,
263 "Render grid cell lines" );
265 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_BORDER
,
268 item
= setupMenu
->AppendCheckItem( ID_RENDER_SELECT_HLIGHT
,
269 "Render selection highlight" );
270 setupMenu
->AppendSeparator();
271 setupMenu
->AppendCheckItem( ID_RENDER_LOMETRIC
,
272 "Use LOMETRIC mapping mode" );
273 setupMenu
->AppendCheckItem( ID_RENDER_DEFAULT_SIZE
,
274 "Use wxDefaultSize" );
275 setupMenu
->AppendCheckItem( ID_RENDER_MARGIN
,
276 "Logical 50 unit margin" );
277 setupMenu
->AppendCheckItem( ID_RENDER_ZOOM
,
280 fileMenu
->AppendSubMenu( setupMenu
, "Render setup" );
281 fileMenu
->Append( wxID_PRINT
, "Render" );
282 fileMenu
->Append( ID_RENDER_COORDS
, "Render G5:P30" );
284 fileMenu
->AppendSeparator();
285 fileMenu
->Append( wxID_EXIT
, wxT("E&xit\tAlt-X") );
287 wxMenu
*viewMenu
= new wxMenu
;
288 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
289 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
290 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
291 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
292 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
293 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
294 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
295 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
296 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
297 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
298 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
299 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
300 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
301 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
302 viewMenu
->Append(ID_HIDECOL
, "&Hide column A");
303 viewMenu
->Append(ID_SHOWCOL
, "&Show column A");
304 viewMenu
->Append(ID_HIDEROW
, "&Hide row 2");
305 viewMenu
->Append(ID_SHOWROW
, "&Show row 2");
306 wxMenu
*rowLabelMenu
= new wxMenu
;
308 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("R&ow label alignment"),
310 wxT("Change alignment of row labels") );
312 rowLabelMenu
->AppendRadioItem( ID_ROWLABELHORIZALIGN
, wxT("&Horizontal") );
313 rowLabelMenu
->AppendRadioItem( ID_ROWLABELVERTALIGN
, wxT("&Vertical") );
315 wxMenu
*colLabelMenu
= new wxMenu
;
317 viewMenu
->Append( ID_COLLABELALIGN
, wxT("Col l&abel alignment"),
319 wxT("Change alignment of col labels") );
321 colLabelMenu
->AppendRadioItem( ID_COLLABELHORIZALIGN
, wxT("&Horizontal") );
322 colLabelMenu
->AppendRadioItem( ID_COLLABELVERTALIGN
, wxT("&Vertical") );
324 wxMenu
*colHeaderMenu
= new wxMenu
;
326 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("Col header style"),
328 wxT("Change style of col header") );
330 colHeaderMenu
->AppendRadioItem( ID_COLDEFAULTHEADER
, wxT("&Default") );
331 colHeaderMenu
->AppendRadioItem( ID_COLNATIVEHEADER
, wxT("&Native") );
332 colHeaderMenu
->AppendRadioItem( ID_COLCUSTOMHEADER
, wxT("&Custom") );
334 wxMenu
*tabBehaviourMenu
= new wxMenu
;
335 tabBehaviourMenu
->AppendRadioItem(ID_TAB_STOP
, "&Stop at the boundary");
336 tabBehaviourMenu
->AppendRadioItem(ID_TAB_WRAP
, "&Wrap at the boundary");
337 tabBehaviourMenu
->AppendRadioItem(ID_TAB_LEAVE
, "&Leave the grid");
338 tabBehaviourMenu
->AppendRadioItem(ID_TAB_CUSTOM
, "&Custom tab handler");
339 viewMenu
->AppendSubMenu(tabBehaviourMenu
, "&Tab behaviour");
341 wxMenu
*colMenu
= new wxMenu
;
342 colMenu
->Append( ID_SETLABELCOLOUR
, wxT("Set &label colour...") );
343 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, wxT("Set label &text colour...") );
344 colMenu
->Append( ID_SETLABEL_FONT
, wxT("Set label fo&nt...") );
345 colMenu
->Append( ID_GRIDLINECOLOUR
, wxT("&Grid line colour...") );
346 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, wxT("Set cell &foreground colour...") );
347 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, wxT("Set cell &background colour...") );
349 wxMenu
*editMenu
= new wxMenu
;
350 editMenu
->Append( ID_INSERTROW
, wxT("Insert &row") );
351 editMenu
->Append( ID_INSERTCOL
, wxT("Insert &column") );
352 editMenu
->Append( ID_DELETEROW
, wxT("Delete selected ro&ws") );
353 editMenu
->Append( ID_DELETECOL
, wxT("Delete selected co&ls") );
354 editMenu
->Append( ID_CLEARGRID
, wxT("Cl&ear grid cell contents") );
356 wxMenu
*selectMenu
= new wxMenu
;
357 selectMenu
->Append( ID_SELECT_UNSELECT
, wxT("Add new cells to the selection"),
358 wxT("When off, old selection is deselected before ")
359 wxT("selecting the new cells"), wxITEM_CHECK
);
360 selectMenu
->AppendSeparator();
361 selectMenu
->Append( ID_SELECT_ALL
, wxT("Select all"));
362 selectMenu
->Append( ID_SELECT_ROW
, wxT("Select row 2"));
363 selectMenu
->Append( ID_SELECT_COL
, wxT("Select col 2"));
364 selectMenu
->Append( ID_SELECT_CELL
, wxT("Select cell (3, 1)"));
365 selectMenu
->AppendSeparator();
366 selectMenu
->Append( ID_DESELECT_ALL
, wxT("Deselect all"));
367 selectMenu
->Append( ID_DESELECT_ROW
, wxT("Deselect row 2"));
368 selectMenu
->Append( ID_DESELECT_COL
, wxT("Deselect col 2"));
369 selectMenu
->Append( ID_DESELECT_CELL
, wxT("Deselect cell (3, 1)"));
370 wxMenu
*selectionMenu
= new wxMenu
;
371 selectMenu
->Append( ID_CHANGESEL
, wxT("Change &selection mode"),
373 wxT("Change selection mode") );
375 selectionMenu
->Append( ID_SELCELLS
, wxT("Select &cells") );
376 selectionMenu
->Append( ID_SELROWS
, wxT("Select &rows") );
377 selectionMenu
->Append( ID_SELCOLS
, wxT("Select col&umns") );
378 selectionMenu
->Append( ID_SELROWSORCOLS
, wxT("Select rows &or columns") );
380 wxMenu
*autosizeMenu
= new wxMenu
;
381 autosizeMenu
->Append( ID_SIZE_ROW
, wxT("Selected &row data") );
382 autosizeMenu
->Append( ID_SIZE_COL
, wxT("Selected &column data") );
383 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, wxT("Selected row la&bel") );
384 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, wxT("Selected column &label") );
385 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, wxT("Column la&bels") );
386 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, wxT("Row label&s") );
387 autosizeMenu
->Append( ID_SIZE_GRID
, wxT("Entire &grid") );
389 wxMenu
*helpMenu
= new wxMenu
;
390 helpMenu
->Append( wxID_ABOUT
, wxT("&About wxGrid demo") );
392 wxMenuBar
*menuBar
= new wxMenuBar
;
393 menuBar
->Append( fileMenu
, wxT("&File") );
394 menuBar
->Append( viewMenu
, wxT("&Grid") );
395 menuBar
->Append( colMenu
, wxT("&Colours") );
396 menuBar
->Append( editMenu
, wxT("&Edit") );
397 menuBar
->Append( selectMenu
, wxT("&Select") );
398 menuBar
->Append( autosizeMenu
, wxT("&Autosize") );
399 menuBar
->Append( helpMenu
, wxT("&Help") );
401 SetMenuBar( menuBar
);
405 grid
= new wxGrid( this,
408 wxSize( 400, 300 ) );
412 int gridW
= 600, gridH
= 300;
413 int logW
= gridW
, logH
= 100;
415 logWin
= new wxTextCtrl( this,
418 wxPoint( 0, gridH
+ 20 ),
419 wxSize( logW
, logH
),
422 logger
= new wxLogTextCtrl( logWin
);
423 m_logOld
= wxLog::SetActiveTarget( logger
);
424 wxLog::DisableTimestamp();
427 // this will create a grid and, by default, an associated grid
429 grid
->CreateGrid( 0, 0 );
431 grid
->GetTable()->SetAttrProvider(new CustomColumnHeadersProvider());
433 grid
->AppendRows(100);
434 grid
->AppendCols(100);
436 int ir
= grid
->GetNumberRows();
437 grid
->DeleteRows(0, ir
);
438 grid
->AppendRows(ir
);
440 grid
->SetRowSize( 0, 60 );
441 grid
->SetCellValue( 0, 0, wxT("Ctrl+Home\nwill go to\nthis cell") );
443 grid
->SetCellValue( 0, 1, wxT("A long piece of text to demonstrate wrapping.") );
444 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
445 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
447 grid
->SetCellValue( 0, 2, wxT("Blah") );
448 grid
->SetCellValue( 0, 3, wxT("Read only") );
449 grid
->SetReadOnly( 0, 3 );
451 grid
->SetCellValue( 0, 4, wxT("Can veto edit this cell") );
453 grid
->SetCellValue( 0, 5, wxT("Press\nCtrl+arrow\nto skip over\ncells") );
455 grid
->SetRowSize( 99, 60 );
456 grid
->SetCellValue(98, 98, "Test background colour setting");
457 grid
->SetCellBackgroundColour(98, 99, wxColour(255, 127, 127));
458 grid
->SetCellBackgroundColour(99, 98, wxColour(255, 127, 127));
459 grid
->SetCellValue( 99, 99, wxT("Ctrl+End\nwill go to\nthis cell") );
460 grid
->SetCellValue( 1, 0, wxT("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
462 grid
->SetCellTextColour(1, 2, *wxRED
);
463 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
465 grid
->SetCellValue( 1, 4, wxT("I'm in the middle"));
467 grid
->SetCellValue(2, 2, wxT("red"));
469 grid
->SetCellTextColour(2, 2, *wxRED
);
470 grid
->SetCellValue(3, 3, wxT("green on grey"));
471 grid
->SetCellTextColour(3, 3, *wxGREEN
);
472 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
474 grid
->SetCellValue(4, 4, wxT("a weird looking cell"));
475 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
476 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
478 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
479 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
480 grid
->SetCellBackgroundColour(3, 0, wxColour(255, 127, 127));
482 wxGridCellAttr
*attr
;
483 attr
= new wxGridCellAttr
;
484 attr
->SetTextColour(*wxBLUE
);
485 grid
->SetColAttr(5, attr
);
486 attr
= new wxGridCellAttr
;
487 attr
->SetBackgroundColour(*wxRED
);
488 grid
->SetRowAttr(5, attr
);
490 grid
->SetCellValue(2, 4, wxT("a wider column"));
491 grid
->SetColSize(4, 120);
492 grid
->SetColMinimalWidth(4, 120);
494 grid
->SetCellTextColour(5, 8, *wxGREEN
);
495 grid
->SetCellValue(5, 8, wxT("Bg from row attr\nText col from cell attr"));
496 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"));
498 // Some numeric columns with different formatting.
499 grid
->SetColFormatFloat(6);
500 grid
->SetCellValue(0, 6, "Default\nfloat format");
501 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 3.1415));
502 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 1415.0));
503 grid
->SetCellValue(3, 6, wxString::Format(wxT("%g"), 12345.67890));
505 grid
->SetColFormatFloat(7, 6, 2);
506 grid
->SetCellValue(0, 7, "Width 6\nprecision 2");
507 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 3.1415));
508 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 1415.0));
509 grid
->SetCellValue(3, 7, wxString::Format(wxT("%g"), 12345.67890));
511 grid
->SetColFormatCustom(8,
512 wxString::Format("%s:%i,%i,%s", wxGRID_VALUE_FLOAT
, -1, 4, "g"));
513 grid
->SetCellValue(0, 8, "Compact\nformat");
514 grid
->SetCellValue(1, 8, wxT("31415e-4"));
515 grid
->SetCellValue(2, 8, wxT("1415"));
516 grid
->SetCellValue(3, 8, wxT("123456789e-4"));
518 grid
->SetColFormatNumber(9);
519 grid
->SetCellValue(0, 9, "Integer\ncolumn");
520 grid
->SetCellValue(1, 9, "17");
521 grid
->SetCellValue(2, 9, "0");
522 grid
->SetCellValue(3, 9, "-666");
523 grid
->SetCellAlignment(3, 9, wxALIGN_CENTRE
, wxALIGN_TOP
);
524 grid
->SetCellValue(3, 10, "<- This numeric cell should be centred");
526 const wxString choices
[] =
528 wxT("Please select a choice"),
529 wxT("This takes two cells"),
530 wxT("Another choice"),
532 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
533 grid
->SetCellSize(4, 0, 1, 2);
534 grid
->SetCellValue(4, 0, choices
[0]);
535 grid
->SetCellOverflow(4, 0, false);
537 grid
->SetCellSize(7, 1, 3, 4);
538 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
539 grid
->SetCellValue(7, 1, wxT("Big box!"));
541 // create a separator-like row: it's grey and it's non-resizable
542 grid
->DisableRowResize(10);
543 grid
->SetRowSize(10, 30);
544 attr
= new wxGridCellAttr
;
545 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
546 grid
->SetRowAttr(10, attr
);
547 grid
->SetCellValue(10, 0, "You can't resize this row interactively -- try it");
549 // this does exactly nothing except testing that SetAttr() handles NULL
550 // attributes and does reference counting correctly
551 grid
->SetAttr(11, 11, NULL
);
552 grid
->SetAttr(11, 11, new wxGridCellAttr
);
553 grid
->SetAttr(11, 11, NULL
);
555 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
561 topSizer
->Add( logWin
,
566 SetSizerAndFit( topSizer
);
573 GridFrame::~GridFrame()
576 delete wxLog::SetActiveTarget(m_logOld
);
581 void GridFrame::SetDefaults()
583 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
584 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
585 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
586 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
587 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
588 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
589 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
590 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
591 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
592 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
596 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
598 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
600 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
604 grid
->SetRowLabelSize( 0 );
609 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
611 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
613 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
617 grid
->SetColLabelSize( 0 );
622 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
625 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
629 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
631 grid
->EnableDragRowSize(
632 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
636 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
638 grid
->EnableDragColSize(
639 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
642 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
644 grid
->EnableDragColMove(
645 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
648 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
650 grid
->EnableDragGridSize(
651 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
654 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
656 grid
->EnableDragCell(
657 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
660 void GridFrame::SetNativeColHeader( wxCommandEvent
& WXUNUSED(ev
) )
662 CustomColumnHeadersProvider
* provider
=
663 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
664 provider
->UseCustomColHeaders(false);
665 grid
->SetUseNativeColLabels(true);
668 void GridFrame::SetCustomColHeader( wxCommandEvent
& WXUNUSED(ev
) )
670 CustomColumnHeadersProvider
* provider
=
671 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
672 provider
->UseCustomColHeaders(true);
673 grid
->SetUseNativeColLabels(false);
676 void GridFrame::SetDefaultColHeader( wxCommandEvent
& WXUNUSED(ev
) )
678 CustomColumnHeadersProvider
* provider
=
679 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
680 provider
->UseCustomColHeaders(false);
681 grid
->SetUseNativeColLabels(false);
685 void GridFrame::OnGridCustomTab(wxGridEvent
& event
)
687 // just for testing, make the cursor move up and down instead of the usual
689 if ( event
.ShiftDown() )
691 if ( grid
->GetGridCursorRow() > 0 )
692 grid
->MoveCursorUp( false );
696 if ( grid
->GetGridCursorRow() < grid
->GetNumberRows() - 1 )
697 grid
->MoveCursorDown( false );
701 void GridFrame::SetTabBehaviour(wxCommandEvent
& event
)
703 // To make any built-in behaviour work, we need to disable the custom TAB
704 // handler, otherwise it would be overriding them.
705 grid
->Disconnect(wxEVT_GRID_TABBING
,
706 wxGridEventHandler(GridFrame::OnGridCustomTab
));
708 grid
->SetTabBehaviour(
709 static_cast<wxGrid::TabBehaviour
>(event
.GetId() - ID_TAB_STOP
)
713 void GridFrame::SetTabCustomHandler(wxCommandEvent
&)
715 grid
->Connect(wxEVT_GRID_TABBING
,
716 wxGridEventHandler(GridFrame::OnGridCustomTab
),
721 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
723 grid
->EnableGridLines(
724 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
727 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
729 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
731 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
732 wxT("Pen Width"), 11, choices
);
734 int current
= grid
->GetCellHighlightPenWidth();
735 dlg
.SetSelection(current
);
736 if (dlg
.ShowModal() == wxID_OK
) {
737 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
741 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
743 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
745 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
746 wxT("Pen Width"), 11, choices
);
748 int current
= grid
->GetCellHighlightROPenWidth();
749 dlg
.SetSelection(current
);
750 if (dlg
.ShowModal() == wxID_OK
) {
751 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
757 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
759 grid
->AutoSizeColumns();
763 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
765 grid
->SetDefaultCellOverflow(ev
.IsChecked());
769 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
772 grid
->SetCellSize( 7, 1, 5, 5 );
774 grid
->SetCellSize( 7, 1, 1, 5 );
778 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
780 wxColourDialog
dlg( NULL
);
781 if ( dlg
.ShowModal() == wxID_OK
)
783 wxColourData retData
;
784 retData
= dlg
.GetColourData();
785 wxColour colour
= retData
.GetColour();
787 grid
->SetLabelBackgroundColour( colour
);
792 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
794 wxColourDialog
dlg( NULL
);
795 if ( dlg
.ShowModal() == wxID_OK
)
797 wxColourData retData
;
798 retData
= dlg
.GetColourData();
799 wxColour colour
= retData
.GetColour();
801 grid
->SetLabelTextColour( colour
);
805 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
807 wxFont font
= wxGetFontFromUser(this);
810 grid
->SetLabelFont(font
);
814 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
817 grid
->GetRowLabelAlignment( &horiz
, &vert
);
822 horiz
= wxALIGN_CENTRE
;
826 horiz
= wxALIGN_RIGHT
;
830 horiz
= wxALIGN_LEFT
;
834 grid
->SetRowLabelAlignment( horiz
, vert
);
837 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
840 grid
->GetRowLabelAlignment( &horiz
, &vert
);
845 vert
= wxALIGN_CENTRE
;
849 vert
= wxALIGN_BOTTOM
;
857 grid
->SetRowLabelAlignment( horiz
, vert
);
861 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
864 grid
->GetColLabelAlignment( &horiz
, &vert
);
869 horiz
= wxALIGN_CENTRE
;
873 horiz
= wxALIGN_RIGHT
;
877 horiz
= wxALIGN_LEFT
;
881 grid
->SetColLabelAlignment( horiz
, vert
);
885 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
888 grid
->GetColLabelAlignment( &horiz
, &vert
);
893 vert
= wxALIGN_CENTRE
;
897 vert
= wxALIGN_BOTTOM
;
905 grid
->SetColLabelAlignment( horiz
, vert
);
909 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
911 wxColourDialog
dlg( NULL
);
912 if ( dlg
.ShowModal() == wxID_OK
)
914 wxColourData retData
;
915 retData
= dlg
.GetColourData();
916 wxColour colour
= retData
.GetColour();
918 grid
->SetGridLineColour( colour
);
923 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
925 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
929 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
931 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
935 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
937 if ( grid
->IsSelection() )
939 wxGridUpdateLocker
locker(grid
);
940 for ( int n
= 0; n
< grid
->GetNumberRows(); )
942 if ( grid
->IsInSelection( n
, 0 ) )
943 grid
->DeleteRows( n
, 1 );
951 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
953 wxGridUpdateLocker
locker(grid
);
954 const wxArrayInt sels
= grid
->GetSelectedRows();
955 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
957 grid
->AutoSizeRow( sels
[n
], false );
961 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
963 wxGridUpdateLocker
locker(grid
);
964 const wxArrayInt sels
= grid
->GetSelectedCols();
965 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
967 grid
->AutoSizeColumn( sels
[n
], false );
971 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
973 wxGridUpdateLocker
locker(grid
);
974 const wxArrayInt sels
= grid
->GetSelectedRows();
975 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
977 grid
->AutoSizeRowLabelSize( sels
[n
] );
981 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
983 wxGridUpdateLocker
locker(grid
);
984 const wxArrayInt sels
= grid
->GetSelectedCols();
985 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
987 grid
->AutoSizeColLabelSize( sels
[n
] );
991 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
993 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
996 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
998 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
1001 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
1008 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
1010 if ( grid
->IsSelection() )
1012 wxGridUpdateLocker
locker(grid
);
1013 for ( int n
= 0; n
< grid
->GetNumberCols(); )
1015 if ( grid
->IsInSelection( 0 , n
) )
1016 grid
->DeleteCols( n
, 1 );
1024 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
1029 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
1031 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
1034 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
1036 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
1039 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
1041 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
1044 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
1046 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
1049 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
1051 wxColour col
= wxGetColourFromUser(this);
1054 grid
->SetDefaultCellTextColour(col
);
1059 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
1061 wxColour col
= wxGetColourFromUser(this);
1064 // Check the new Refresh function by passing it a rectangle
1065 // which exactly fits the grid.
1067 wxRect
r(pt
, grid
->GetSize());
1068 grid
->SetDefaultCellBackgroundColour(col
);
1069 grid
->Refresh(true, &r
);
1073 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
1075 grid
->DeselectCell(3, 1);
1078 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
1080 grid
->DeselectCol(2);
1083 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
1085 grid
->DeselectRow(2);
1088 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
1090 grid
->ClearSelection();
1093 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
1095 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
1098 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
1100 grid
->SelectCol(2, m_addToSel
);
1103 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
1105 grid
->SelectRow(2, m_addToSel
);
1108 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
1113 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
1115 m_addToSel
= event
.IsChecked();
1118 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
1121 if ( ev
.GetRow() != -1 )
1123 logBuf
<< wxT("Left click on row label ") << ev
.GetRow();
1125 else if ( ev
.GetCol() != -1 )
1127 logBuf
<< wxT("Left click on col label ") << ev
.GetCol();
1131 logBuf
<< wxT("Left click on corner label");
1134 if ( ev
.ShiftDown() )
1135 logBuf
<< wxT(" (shift down)");
1136 if ( ev
.ControlDown() )
1137 logBuf
<< wxT(" (control down)");
1138 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1140 // you must call event skip if you want default grid processing
1146 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
1148 wxLogMessage(wxT("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
1150 // you must call event skip if you want default grid processing
1151 // (cell highlighting etc.)
1157 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
1159 const int row
= ev
.GetRowOrCol();
1161 wxLogMessage("Resized row %d, new height = %d",
1162 row
, grid
->GetRowSize(row
));
1168 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
1170 const int col
= ev
.GetRowOrCol();
1172 wxLogMessage("Resized column %d, new width = %d",
1173 col
, grid
->GetColSize(col
));
1178 void GridFrame::OnColAutoSize( wxGridSizeEvent
&event
)
1180 // Fit even-numbered columns to their contents while using the default
1181 // behaviour for the odd-numbered ones to be able to see the difference.
1182 int col
= event
.GetRowOrCol();
1185 wxLogMessage("Auto-sizing column %d to fit its contents", col
);
1186 grid
->AutoSizeColumn(col
);
1194 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1197 if ( ev
.Selecting() )
1198 logBuf
<< wxT("Selected ");
1200 logBuf
<< wxT("Deselected ");
1201 logBuf
<< wxT("cell at row ") << ev
.GetRow()
1202 << wxT(" col ") << ev
.GetCol()
1203 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1204 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1205 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1206 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1208 //Indicate whether this column was moved
1209 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1210 logBuf
<< wxT(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1212 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1214 // you must call Skip() if you want the default processing
1215 // to occur in wxGrid
1219 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1222 if ( ev
.Selecting() )
1223 logBuf
<< wxT("Selected ");
1225 logBuf
<< wxT("Deselected ");
1226 logBuf
<< wxT("cells from row ") << ev
.GetTopRow()
1227 << wxT(" col ") << ev
.GetLeftCol()
1228 << wxT(" to row ") << ev
.GetBottomRow()
1229 << wxT(" col ") << ev
.GetRightCol()
1230 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1231 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1232 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1233 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1234 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1239 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1241 int row
= ev
.GetRow(),
1244 wxLogMessage("Value of cell at (%d, %d): about to change "
1245 "from \"%s\" to \"%s\"",
1247 grid
->GetCellValue(row
, col
), ev
.GetString());
1249 // test how vetoing works
1250 if ( ev
.GetString() == "42" )
1252 wxLogMessage("Vetoing the change.");
1260 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1262 int row
= ev
.GetRow(),
1265 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1268 grid
->GetCellValue(row
, col
), ev
.GetString());
1273 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1275 wxLogMessage(wxT("Got request to drag cell at row %d, col %d"),
1276 ev
.GetRow(), ev
.GetCol());
1281 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1284 if ( (ev
.GetCol() == 4) &&
1285 (ev
.GetRow() == 0) &&
1286 (wxMessageBox(wxT("Are you sure you wish to edit this cell"),
1287 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1293 wxLogMessage( wxT("Cell editor shown.") );
1298 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1301 if ( (ev
.GetCol() == 4) &&
1302 (ev
.GetRow() == 0) &&
1303 (wxMessageBox(wxT("Are you sure you wish to finish editing this cell"),
1304 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1310 wxLogMessage( wxT("Cell editor hidden.") );
1315 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1317 wxAboutDialogInfo aboutInfo
;
1318 aboutInfo
.SetName(wxT("wxGrid demo"));
1319 aboutInfo
.SetDescription(_("wxGrid sample program"));
1320 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1321 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1322 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1324 // this is just to force the generic version of the about
1325 // dialog under wxMSW so that it's easy to test if the grid
1326 // repaints correctly when it has lost focus and a dialog
1327 // (different from the Windows standard message box -- it doesn't
1328 // work with it for some reason) is moved over it.
1329 aboutInfo
.SetWebSite(wxT("http://www.wxwidgets.org"));
1331 wxAboutBox(aboutInfo
);
1335 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1340 void GridFrame::OnBugsTable(wxCommandEvent
& )
1342 BugsGridFrame
*frame
= new BugsGridFrame
;
1346 // ----------------------------------------------------------------------------
1347 // MyGridCellAttrProvider
1348 // ----------------------------------------------------------------------------
1350 MyGridCellAttrProvider::MyGridCellAttrProvider()
1352 m_attrForOddRows
= new wxGridCellAttr
;
1353 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1356 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1358 m_attrForOddRows
->DecRef();
1361 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1362 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1364 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1370 attr
= m_attrForOddRows
;
1375 if ( !attr
->HasBackgroundColour() )
1377 wxGridCellAttr
*attrNew
= attr
->Clone();
1380 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1388 void GridFrame::OnVTable(wxCommandEvent
& )
1390 static long s_sizeGrid
= 10000;
1392 s_sizeGrid
= wxGetNumberFromUser(wxT("Size of the table to create"),
1394 wxT("wxGridDemo question"),
1398 if ( s_sizeGrid
!= -1 )
1400 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1405 // ----------------------------------------------------------------------------
1406 // MyGridCellRenderer
1407 // ----------------------------------------------------------------------------
1409 // do something that the default renderer doesn't here just to show that it is
1410 // possible to alter the appearance of the cell beyond what the attributes
1412 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1413 wxGridCellAttr
& attr
,
1419 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1421 dc
.SetPen(*wxGREEN_PEN
);
1422 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1423 dc
.DrawEllipse(rect
);
1426 // ============================================================================
1427 // BigGridFrame and BigGridTable: Sample of a non-standard table
1428 // ============================================================================
1430 BigGridFrame::BigGridFrame(long sizeGrid
)
1431 : wxFrame(NULL
, wxID_ANY
, wxT("Plugin Virtual Table"),
1432 wxDefaultPosition
, wxSize(500, 450))
1434 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1435 m_table
= new BigGridTable(sizeGrid
);
1437 // VZ: I don't understand why this slows down the display that much,
1438 // must profile it...
1439 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1441 m_grid
->SetTable(m_table
, true);
1443 #if defined __WXMOTIF__
1444 // MB: the grid isn't getting a sensible default size under wxMotif
1446 GetClientSize( &cw
, &ch
);
1447 m_grid
->SetSize( cw
, ch
);
1451 // ============================================================================
1452 // BugsGridFrame: a "realistic" table
1453 // ============================================================================
1455 // ----------------------------------------------------------------------------
1457 // ----------------------------------------------------------------------------
1480 static const wxString severities
[] =
1489 static struct BugsGridData
1495 wxChar platform
[12];
1497 } gs_dataBugsGrid
[] =
1499 { 18, wxT("foo doesn't work"), Sev_Major
, 1, wxT("wxMSW"), true },
1500 { 27, wxT("bar crashes"), Sev_Critical
, 1, wxT("all"), false },
1501 { 45, wxT("printing is slow"), Sev_Minor
, 3, wxT("wxMSW"), true },
1502 { 68, wxT("Rectangle() fails"), Sev_Normal
, 1, wxT("wxMSW"), false },
1505 static const wxChar
*headers
[Col_Max
] =
1515 // ----------------------------------------------------------------------------
1517 // ----------------------------------------------------------------------------
1519 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1525 return wxGRID_VALUE_NUMBER
;;
1528 // fall thorugh (TODO should be a list)
1531 return wxString::Format(wxT("%s:80"), wxGRID_VALUE_STRING
);
1534 return wxString::Format(wxT("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1537 return wxGRID_VALUE_BOOL
;
1540 wxFAIL_MSG(wxT("unknown column"));
1542 return wxEmptyString
;
1545 int BugsGridTable::GetNumberRows()
1547 return WXSIZEOF(gs_dataBugsGrid
);
1550 int BugsGridTable::GetNumberCols()
1555 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1560 wxString
BugsGridTable::GetValue( int row
, int col
)
1562 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1567 return wxString::Format(wxT("%d"), gd
.id
);
1570 return wxString::Format(wxT("%d"), gd
.prio
);
1573 return gd
.opened
? wxT("1") : wxT("0");
1576 return severities
[gd
.severity
];
1585 return wxEmptyString
;
1588 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1590 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1597 wxFAIL_MSG(wxT("unexpected column"));
1603 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1605 if ( severities
[n
] == value
)
1607 gd
.severity
= (Severity
)n
;
1612 if ( n
== WXSIZEOF(severities
) )
1614 wxLogWarning(wxT("Invalid severity value '%s'."),
1616 gd
.severity
= Sev_Normal
;
1622 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1626 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1632 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1634 const wxString
& typeName
)
1636 if ( typeName
== wxGRID_VALUE_STRING
)
1640 else if ( typeName
== wxGRID_VALUE_BOOL
)
1642 return col
== Col_Opened
;
1644 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1646 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1654 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1656 return CanGetValueAs(row
, col
, typeName
);
1659 long BugsGridTable::GetValueAsLong( int row
, int col
)
1661 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1675 wxFAIL_MSG(wxT("unexpected column"));
1680 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1682 if ( col
== Col_Opened
)
1684 return gs_dataBugsGrid
[row
].opened
;
1688 wxFAIL_MSG(wxT("unexpected column"));
1694 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1696 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1705 wxFAIL_MSG(wxT("unexpected column"));
1709 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1711 if ( col
== Col_Opened
)
1713 gs_dataBugsGrid
[row
].opened
= value
;
1717 wxFAIL_MSG(wxT("unexpected column"));
1721 wxString
BugsGridTable::GetColLabelValue( int col
)
1723 return headers
[col
];
1726 // ----------------------------------------------------------------------------
1728 // ----------------------------------------------------------------------------
1730 BugsGridFrame::BugsGridFrame()
1731 : wxFrame(NULL
, wxID_ANY
, wxT("Bugs table"))
1733 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1734 wxGridTableBase
*table
= new BugsGridTable();
1735 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1736 grid
->SetTable(table
, true);
1738 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1739 *attrRangeEditor
= new wxGridCellAttr
,
1740 *attrCombo
= new wxGridCellAttr
;
1742 attrRO
->SetReadOnly();
1743 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1744 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1747 grid
->SetColAttr(Col_Id
, attrRO
);
1748 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1749 grid
->SetColAttr(Col_Severity
, attrCombo
);
1752 SetClientSize(grid
->GetSize());
1755 // ============================================================================
1756 // TabularGrid: grid used for display of tabular data
1757 // ============================================================================
1759 class TabularGridTable
: public wxGridTableBase
1776 TabularGridTable() { m_sortOrder
= NULL
; }
1778 virtual int GetNumberRows() { return ROW_MAX
; }
1779 virtual int GetNumberCols() { return COL_MAX
; }
1781 virtual wxString
GetValue(int row
, int col
)
1784 row
= m_sortOrder
[row
];
1790 return GetNameOrExt(row
, col
);
1793 return wxString::Format("%lu", GetSize(row
));
1796 return GetDate(row
).FormatDate();
1800 wxFAIL_MSG( "unknown column" );
1806 virtual void SetValue(int, int, const wxString
&)
1808 wxFAIL_MSG( "shouldn't be called" );
1811 virtual wxString
GetColLabelValue(int col
)
1813 // notice that column parameter here always refers to the internal
1814 // column index, independently of its position on the screen
1815 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1816 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1821 virtual void SetColLabelValue(int, const wxString
&)
1823 wxFAIL_MSG( "shouldn't be called" );
1826 void Sort(int col
, bool ascending
)
1828 // we hardcode all sorting orders for simplicity here
1829 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1831 // descending ascending
1832 { { 2, 1, 0 }, { 0, 1, 2 } },
1833 { { 2, 1, 0 }, { 0, 1, 2 } },
1834 { { 2, 1, 0 }, { 0, 1, 2 } },
1835 { { 1, 0, 2 }, { 2, 0, 1 } },
1838 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1842 wxString
GetNameOrExt(int row
, int col
) const
1845 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1846 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1848 const wxString
s(names
[row
]);
1849 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1852 unsigned long GetSize(int row
) const
1854 static const unsigned long
1855 sizes
[] = { 412, 604, 40774 };
1856 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1861 wxDateTime
GetDate(int row
) const
1864 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1865 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1868 dt
.ParseISODate(dates
[row
]);
1875 // specialized text control for column indexes entry
1876 class ColIndexEntry
: public wxTextCtrl
1879 ColIndexEntry(wxWindow
*parent
)
1880 : wxTextCtrl(parent
, wxID_ANY
, "")
1882 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1888 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1898 virtual wxSize
DoGetBestSize() const
1900 wxSize size
= wxTextCtrl::DoGetBestSize();
1901 size
.x
= 3*GetCharWidth();
1906 class TabularGridFrame
: public wxFrame
1914 Id_Check_UseNativeHeader
,
1915 Id_Check_DrawNativeLabels
,
1916 Id_Check_ShowRowLabels
,
1917 Id_Check_EnableColMove
1922 void OnToggleUseNativeHeader(wxCommandEvent
&)
1924 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1927 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1929 // we don't draw labels at all, native or otherwise, if we use the
1930 // native header control
1931 event
.Enable( !m_chkUseNative
->GetValue() );
1934 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1936 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1939 void OnToggleShowRowLabels(wxCommandEvent
&)
1941 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1946 void OnToggleColMove(wxCommandEvent
&)
1948 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1951 void OnShowHideColumn(wxCommandEvent
& event
)
1953 int col
= m_txtColShowHide
->GetCol();
1956 m_grid
->SetColSize(col
,
1957 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1959 UpdateOrderAndVisibility();
1963 void OnMoveColumn(wxCommandEvent
&)
1965 int col
= m_txtColIndex
->GetCol();
1966 int pos
= m_txtColPos
->GetCol();
1967 if ( col
== -1 || pos
== -1 )
1970 m_grid
->SetColPos(col
, pos
);
1972 UpdateOrderAndVisibility();
1975 void OnResetColumnOrder(wxCommandEvent
&)
1977 m_grid
->ResetColPos();
1979 UpdateOrderAndVisibility();
1982 void OnGridColSort(wxGridEvent
& event
)
1984 const int col
= event
.GetCol();
1985 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
1986 m_grid
->IsSortOrderAscending()));
1989 void OnGridColMove(wxGridEvent
& event
)
1991 // can't update it yet as the order hasn't been changed, so do it a bit
1993 m_shouldUpdateOrder
= true;
1998 void OnGridColSize(wxGridSizeEvent
& event
)
2000 // we only catch this event to react to the user showing or hiding this
2001 // column using the header control menu and not because we're
2002 // interested in column resizing
2003 UpdateOrderAndVisibility();
2008 void OnIdle(wxIdleEvent
& event
)
2010 if ( m_shouldUpdateOrder
)
2012 m_shouldUpdateOrder
= false;
2013 UpdateOrderAndVisibility();
2019 void UpdateOrderAndVisibility()
2022 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
2024 const int col
= m_grid
->GetColAt(pos
);
2025 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
2036 m_statOrder
->SetLabel(s
);
2041 TabularGridTable
*m_table
;
2042 wxCheckBox
*m_chkUseNative
,
2044 *m_chkShowRowLabels
,
2045 *m_chkEnableColMove
;
2047 ColIndexEntry
*m_txtColIndex
,
2051 wxStaticText
*m_statOrder
;
2053 // fla for EVT_IDLE handler
2054 bool m_shouldUpdateOrder
;
2056 wxDECLARE_NO_COPY_CLASS(TabularGridFrame
);
2057 DECLARE_EVENT_TABLE()
2060 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
2061 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
2062 TabularGridFrame::OnToggleUseNativeHeader
)
2063 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
2064 TabularGridFrame::OnToggleDrawNativeLabels
)
2065 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
2066 TabularGridFrame::OnToggleShowRowLabels
)
2067 EVT_CHECKBOX(Id_Check_EnableColMove
,
2068 TabularGridFrame::OnToggleColMove
)
2070 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
2071 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
2073 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
2074 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
2075 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
2076 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
2078 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
2079 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
2080 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
2082 EVT_IDLE(TabularGridFrame::OnIdle
)
2085 TabularGridFrame::TabularGridFrame()
2086 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
2088 m_shouldUpdateOrder
= false;
2090 wxPanel
* const panel
= new wxPanel(this);
2092 // create and initialize the grid with the specified data
2093 m_table
= new TabularGridTable
;
2094 m_grid
= new wxGrid(panel
, wxID_ANY
,
2095 wxDefaultPosition
, wxDefaultSize
,
2096 wxBORDER_STATIC
| wxWANTS_CHARS
);
2097 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
2099 m_grid
->EnableDragColMove();
2100 m_grid
->UseNativeColHeader();
2101 m_grid
->HideRowLabels();
2103 // add it and the other controls to the frame
2104 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
2105 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
2107 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
2109 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
2110 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
2111 "&Use native header");
2112 m_chkUseNative
->SetValue(true);
2113 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
2115 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
2116 "&Draw native column labels");
2117 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
2119 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
2120 "Show &row labels");
2121 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
2123 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
2124 "Allow column re&ordering");
2125 m_chkEnableColMove
->SetValue(true);
2126 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
2127 sizerControls
->Add(sizerStyles
);
2129 sizerControls
->AddSpacer(10);
2131 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
2132 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
2134 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
2135 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
2137 m_txtColIndex
= new ColIndexEntry(panel
);
2138 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
2139 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
2140 m_txtColPos
= new ColIndexEntry(panel
);
2141 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
2142 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
2144 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
2146 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
2147 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
2149 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
2150 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
2151 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
2152 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
2154 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
2155 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
2157 m_txtColShowHide
= new ColIndexEntry(panel
);
2158 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
2159 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
2160 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
2161 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
2163 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
2165 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
2167 panel
->SetSizer(sizerTop
);
2169 SetClientSize(panel
->GetBestSize());
2170 SetSizeHints(GetSize());
2175 void GridFrame::OnTabularTable(wxCommandEvent
&)
2177 new TabularGridFrame
;
2180 // Example using wxGrid::Render
2181 // Displays a preset selection or, if it exists, a selection block
2182 // Draws the selection to a wxBitmap and displays the bitmap
2183 void GridFrame::OnGridRender( wxCommandEvent
& event
)
2185 int styleRender
= 0, i
;
2186 bool useLometric
= false, defSize
= false;
2188 wxSize
sizeMargin( 0, 0 );
2189 wxPoint
pointOrigin( 0, 0 );
2191 wxMenu
* menu
= GetMenuBar()->GetMenu( 0 );
2192 wxMenuItem
* menuItem
= menu
->FindItem( ID_RENDER_ROW_LABEL
);
2193 menu
= menuItem
->GetMenu();
2195 if ( menu
->FindItem( ID_RENDER_ROW_LABEL
)->IsChecked() )
2196 styleRender
|= wxGRID_DRAW_ROWS_HEADER
;
2197 if ( menu
->FindItem( ID_RENDER_COL_LABEL
)->IsChecked() )
2198 styleRender
|= wxGRID_DRAW_COLS_HEADER
;
2199 if ( menu
->FindItem( ID_RENDER_GRID_LINES
)->IsChecked() )
2200 styleRender
|= wxGRID_DRAW_CELL_LINES
;
2201 if ( menu
->FindItem( ID_RENDER_GRID_BORDER
)->IsChecked() )
2202 styleRender
|= wxGRID_DRAW_BOX_RECT
;
2203 if ( menu
->FindItem( ID_RENDER_SELECT_HLIGHT
)->IsChecked() )
2204 styleRender
|= wxGRID_DRAW_SELECTION
;
2205 if ( menu
->FindItem( ID_RENDER_LOMETRIC
)->IsChecked() )
2207 if ( menu
->FindItem( ID_RENDER_MARGIN
)->IsChecked() )
2209 pointOrigin
.x
+= 50;
2210 pointOrigin
.y
+= 50;
2211 sizeMargin
.IncBy( 50 );
2213 if ( menu
->FindItem( ID_RENDER_ZOOM
)->IsChecked() )
2215 if ( menu
->FindItem( ID_RENDER_DEFAULT_SIZE
)->IsChecked() )
2218 // init render area coords with a default row and col selection
2219 wxGridCellCoords
topLeft( 0, 0 ), bottomRight( 8, 6 );
2220 // check whether we are printing a block selection
2221 // other selection types not catered for here
2222 if ( event
.GetId() == ID_RENDER_COORDS
)
2224 topLeft
.SetCol( 6 );
2225 topLeft
.SetRow( 4 );
2226 bottomRight
.SetCol( 15 );
2227 bottomRight
.SetRow( 29 );
2229 else if ( grid
->IsSelection() && grid
->GetSelectionBlockTopLeft().Count() )
2231 wxGridCellCoordsArray cells
= grid
->GetSelectionBlockTopLeft();
2232 if ( grid
->GetSelectionBlockBottomRight().Count() )
2234 cells
.Add( grid
->GetSelectionBlockBottomRight()[ 0 ] );
2235 topLeft
.Set( cells
[ 0 ].GetRow(),
2236 cells
[ 0 ].GetCol() );
2237 bottomRight
.Set( cells
[ 1 ].GetRow(),
2238 cells
[ 1 ].GetCol() );
2243 wxSize
sizeRender( 0, 0 );
2244 wxGridSizesInfo sizeinfo
= grid
->GetColSizes();
2245 for ( i
= topLeft
.GetCol(); i
<= bottomRight
.GetCol(); i
++ )
2247 sizeRender
.x
+= sizeinfo
.GetSize( i
);
2251 sizeinfo
= grid
->GetRowSizes();
2252 for ( i
= topLeft
.GetRow(); i
<= bottomRight
.GetRow(); i
++ )
2254 sizeRender
.y
+= sizeinfo
.GetSize( i
);
2257 if ( styleRender
& wxGRID_DRAW_ROWS_HEADER
)
2258 sizeRender
.x
+= grid
->GetRowLabelSize();
2259 if ( styleRender
& wxGRID_DRAW_COLS_HEADER
)
2260 sizeRender
.y
+= grid
->GetColLabelSize();
2262 sizeRender
.x
*= zoom
;
2263 sizeRender
.y
*= zoom
;
2265 // delete any existing render frame and create new one
2266 wxWindow
* win
= FindWindow( "frameRender" );
2270 wxFrame
* frame
= new wxFrame( this, wxID_ANY
, "Grid Render" );
2271 frame
->SetClientSize( 780, 400 );
2272 frame
->SetName( "frameRender" );
2274 wxPanel
* canvas
= new wxPanel( frame
, wxID_ANY
);
2276 // make bitmap large enough for margins if any
2278 sizeRender
.IncBy( sizeMargin
* 2 );
2280 sizeRender
.IncBy( sizeMargin
);
2282 wxBitmap
bmp( sizeRender
);
2283 // don't leave it larger or drawing will be scaled
2284 sizeRender
.DecBy( sizeMargin
* 2 );
2286 wxMemoryDC
memDc(bmp
);
2288 // default row labels have no background colour so set background
2289 memDc
.SetBackground( wxBrush( canvas
->GetBackgroundColour() ) );
2292 // convert sizeRender to mapping mode units if necessary
2295 memDc
.SetMapMode( wxMM_LOMETRIC
);
2296 sizeRender
.x
= memDc
.DeviceToLogicalXRel( sizeRender
.x
);
2297 sizeRender
.y
= memDc
.DeviceToLogicalYRel( sizeRender
.y
);
2300 // pass wxDefaultSize if menu item is checked
2302 sizeRender
= wxDefaultSize
;
2304 grid
->Render( memDc
,
2307 topLeft
, bottomRight
,
2308 wxGridRenderStyle( styleRender
) );
2312 canvas
->Connect( wxEVT_PAINT
,
2313 wxPaintEventHandler(GridFrame::OnRenderPaint
),
2320 void GridFrame::OnRenderPaint( wxPaintEvent
& event
)
2322 wxPanel
* canvas
= ( wxPanel
* )event
.GetEventObject();
2323 wxPaintDC
dc( canvas
);
2324 canvas
->PrepareDC( dc
);
2326 if ( !m_gridBitmap
.IsOk() )
2329 wxMemoryDC
memDc( m_gridBitmap
);
2332 m_gridBitmap
.GetWidth(),
2333 m_gridBitmap
.GetHeight(),
2337 void GridFrame::HideCol( wxCommandEvent
& WXUNUSED(event
) )
2342 void GridFrame::ShowCol( wxCommandEvent
& WXUNUSED(event
) )
2347 void GridFrame::HideRow( wxCommandEvent
& WXUNUSED(event
) )
2352 void GridFrame::ShowRow( wxCommandEvent
& WXUNUSED(event
) )