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_SELECT_CELL( GridFrame::OnSelectCell
)
230 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
231 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
232 EVT_GRID_CELL_CHANGED( GridFrame::OnCellValueChanged
)
233 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
235 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
236 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
240 GridFrame::GridFrame()
241 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets grid class demo"),
245 SetIcon(wxICON(sample
));
247 wxMenu
*fileMenu
= new wxMenu
;
248 fileMenu
->Append( ID_VTABLE
, wxT("&Virtual table test\tCtrl-V"));
249 fileMenu
->Append( ID_BUGS_TABLE
, wxT("&Bugs table test\tCtrl-B"));
250 fileMenu
->Append( ID_TABULAR_TABLE
, wxT("&Tabular table test\tCtrl-T"));
251 fileMenu
->AppendSeparator();
253 wxMenu
* setupMenu
= new wxMenu
;
255 item
= setupMenu
->AppendCheckItem( ID_RENDER_ROW_LABEL
,
256 "Render row labels" );
258 item
= setupMenu
->AppendCheckItem( ID_RENDER_COL_LABEL
,
259 "Render column labels" );
261 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_LINES
,
262 "Render grid cell lines" );
264 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_BORDER
,
267 item
= setupMenu
->AppendCheckItem( ID_RENDER_SELECT_HLIGHT
,
268 "Render selection highlight" );
269 setupMenu
->AppendSeparator();
270 setupMenu
->AppendCheckItem( ID_RENDER_LOMETRIC
,
271 "Use LOMETRIC mapping mode" );
272 setupMenu
->AppendCheckItem( ID_RENDER_DEFAULT_SIZE
,
273 "Use wxDefaultSize" );
274 setupMenu
->AppendCheckItem( ID_RENDER_MARGIN
,
275 "Logical 50 unit margin" );
276 setupMenu
->AppendCheckItem( ID_RENDER_ZOOM
,
279 fileMenu
->AppendSubMenu( setupMenu
, "Render setup" );
280 fileMenu
->Append( wxID_PRINT
, "Render" );
281 fileMenu
->Append( ID_RENDER_COORDS
, "Render G5:P30" );
283 fileMenu
->AppendSeparator();
284 fileMenu
->Append( wxID_EXIT
, wxT("E&xit\tAlt-X") );
286 wxMenu
*viewMenu
= new wxMenu
;
287 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
288 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
289 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
290 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
291 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
292 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
293 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
294 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
295 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
296 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
297 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
298 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
299 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
300 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
301 viewMenu
->Append(ID_HIDECOL
, "&Hide column A");
302 viewMenu
->Append(ID_SHOWCOL
, "&Show column A");
303 viewMenu
->Append(ID_HIDEROW
, "&Hide row 2");
304 viewMenu
->Append(ID_SHOWROW
, "&Show row 2");
305 wxMenu
*rowLabelMenu
= new wxMenu
;
307 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("R&ow label alignment"),
309 wxT("Change alignment of row labels") );
311 rowLabelMenu
->AppendRadioItem( ID_ROWLABELHORIZALIGN
, wxT("&Horizontal") );
312 rowLabelMenu
->AppendRadioItem( ID_ROWLABELVERTALIGN
, wxT("&Vertical") );
314 wxMenu
*colLabelMenu
= new wxMenu
;
316 viewMenu
->Append( ID_COLLABELALIGN
, wxT("Col l&abel alignment"),
318 wxT("Change alignment of col labels") );
320 colLabelMenu
->AppendRadioItem( ID_COLLABELHORIZALIGN
, wxT("&Horizontal") );
321 colLabelMenu
->AppendRadioItem( ID_COLLABELVERTALIGN
, wxT("&Vertical") );
323 wxMenu
*colHeaderMenu
= new wxMenu
;
325 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("Col header style"),
327 wxT("Change style of col header") );
329 colHeaderMenu
->AppendRadioItem( ID_COLDEFAULTHEADER
, wxT("&Default") );
330 colHeaderMenu
->AppendRadioItem( ID_COLNATIVEHEADER
, wxT("&Native") );
331 colHeaderMenu
->AppendRadioItem( ID_COLCUSTOMHEADER
, wxT("&Custom") );
333 wxMenu
*tabBehaviourMenu
= new wxMenu
;
334 tabBehaviourMenu
->AppendRadioItem(ID_TAB_STOP
, "&Stop at the boundary");
335 tabBehaviourMenu
->AppendRadioItem(ID_TAB_WRAP
, "&Wrap at the boundary");
336 tabBehaviourMenu
->AppendRadioItem(ID_TAB_LEAVE
, "&Leave the grid");
337 tabBehaviourMenu
->AppendRadioItem(ID_TAB_CUSTOM
, "&Custom tab handler");
338 viewMenu
->AppendSubMenu(tabBehaviourMenu
, "&Tab behaviour");
340 wxMenu
*colMenu
= new wxMenu
;
341 colMenu
->Append( ID_SETLABELCOLOUR
, wxT("Set &label colour...") );
342 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, wxT("Set label &text colour...") );
343 colMenu
->Append( ID_SETLABEL_FONT
, wxT("Set label fo&nt...") );
344 colMenu
->Append( ID_GRIDLINECOLOUR
, wxT("&Grid line colour...") );
345 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, wxT("Set cell &foreground colour...") );
346 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, wxT("Set cell &background colour...") );
348 wxMenu
*editMenu
= new wxMenu
;
349 editMenu
->Append( ID_INSERTROW
, wxT("Insert &row") );
350 editMenu
->Append( ID_INSERTCOL
, wxT("Insert &column") );
351 editMenu
->Append( ID_DELETEROW
, wxT("Delete selected ro&ws") );
352 editMenu
->Append( ID_DELETECOL
, wxT("Delete selected co&ls") );
353 editMenu
->Append( ID_CLEARGRID
, wxT("Cl&ear grid cell contents") );
355 wxMenu
*selectMenu
= new wxMenu
;
356 selectMenu
->Append( ID_SELECT_UNSELECT
, wxT("Add new cells to the selection"),
357 wxT("When off, old selection is deselected before ")
358 wxT("selecting the new cells"), wxITEM_CHECK
);
359 selectMenu
->AppendSeparator();
360 selectMenu
->Append( ID_SELECT_ALL
, wxT("Select all"));
361 selectMenu
->Append( ID_SELECT_ROW
, wxT("Select row 2"));
362 selectMenu
->Append( ID_SELECT_COL
, wxT("Select col 2"));
363 selectMenu
->Append( ID_SELECT_CELL
, wxT("Select cell (3, 1)"));
364 selectMenu
->AppendSeparator();
365 selectMenu
->Append( ID_DESELECT_ALL
, wxT("Deselect all"));
366 selectMenu
->Append( ID_DESELECT_ROW
, wxT("Deselect row 2"));
367 selectMenu
->Append( ID_DESELECT_COL
, wxT("Deselect col 2"));
368 selectMenu
->Append( ID_DESELECT_CELL
, wxT("Deselect cell (3, 1)"));
369 wxMenu
*selectionMenu
= new wxMenu
;
370 selectMenu
->Append( ID_CHANGESEL
, wxT("Change &selection mode"),
372 wxT("Change selection mode") );
374 selectionMenu
->Append( ID_SELCELLS
, wxT("Select &cells") );
375 selectionMenu
->Append( ID_SELROWS
, wxT("Select &rows") );
376 selectionMenu
->Append( ID_SELCOLS
, wxT("Select col&umns") );
377 selectionMenu
->Append( ID_SELROWSORCOLS
, wxT("Select rows &or columns") );
379 wxMenu
*autosizeMenu
= new wxMenu
;
380 autosizeMenu
->Append( ID_SIZE_ROW
, wxT("Selected &row data") );
381 autosizeMenu
->Append( ID_SIZE_COL
, wxT("Selected &column data") );
382 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, wxT("Selected row la&bel") );
383 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, wxT("Selected column &label") );
384 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, wxT("Column la&bels") );
385 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, wxT("Row label&s") );
386 autosizeMenu
->Append( ID_SIZE_GRID
, wxT("Entire &grid") );
388 wxMenu
*helpMenu
= new wxMenu
;
389 helpMenu
->Append( wxID_ABOUT
, wxT("&About wxGrid demo") );
391 wxMenuBar
*menuBar
= new wxMenuBar
;
392 menuBar
->Append( fileMenu
, wxT("&File") );
393 menuBar
->Append( viewMenu
, wxT("&Grid") );
394 menuBar
->Append( colMenu
, wxT("&Colours") );
395 menuBar
->Append( editMenu
, wxT("&Edit") );
396 menuBar
->Append( selectMenu
, wxT("&Select") );
397 menuBar
->Append( autosizeMenu
, wxT("&Autosize") );
398 menuBar
->Append( helpMenu
, wxT("&Help") );
400 SetMenuBar( menuBar
);
404 grid
= new wxGrid( this,
407 wxSize( 400, 300 ) );
411 int gridW
= 600, gridH
= 300;
412 int logW
= gridW
, logH
= 100;
414 logWin
= new wxTextCtrl( this,
417 wxPoint( 0, gridH
+ 20 ),
418 wxSize( logW
, logH
),
421 logger
= new wxLogTextCtrl( logWin
);
422 m_logOld
= wxLog::SetActiveTarget( logger
);
423 wxLog::DisableTimestamp();
426 // this will create a grid and, by default, an associated grid
428 grid
->CreateGrid( 0, 0 );
430 grid
->GetTable()->SetAttrProvider(new CustomColumnHeadersProvider());
432 grid
->AppendRows(100);
433 grid
->AppendCols(100);
435 int ir
= grid
->GetNumberRows();
436 grid
->DeleteRows(0, ir
);
437 grid
->AppendRows(ir
);
439 grid
->SetRowSize( 0, 60 );
440 grid
->SetCellValue( 0, 0, wxT("Ctrl+Home\nwill go to\nthis cell") );
442 grid
->SetCellValue( 0, 1, wxT("A long piece of text to demonstrate wrapping.") );
443 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
444 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
446 grid
->SetCellValue( 0, 2, wxT("Blah") );
447 grid
->SetCellValue( 0, 3, wxT("Read only") );
448 grid
->SetReadOnly( 0, 3 );
450 grid
->SetCellValue( 0, 4, wxT("Can veto edit this cell") );
452 grid
->SetCellValue( 0, 5, wxT("Press\nCtrl+arrow\nto skip over\ncells") );
454 grid
->SetRowSize( 99, 60 );
455 grid
->SetCellValue(98, 98, "Test background colour setting");
456 grid
->SetCellBackgroundColour(98, 99, wxColour(255, 127, 127));
457 grid
->SetCellBackgroundColour(99, 98, wxColour(255, 127, 127));
458 grid
->SetCellValue( 99, 99, wxT("Ctrl+End\nwill go to\nthis cell") );
459 grid
->SetCellValue( 1, 0, wxT("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
461 grid
->SetCellTextColour(1, 2, *wxRED
);
462 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
464 grid
->SetCellValue( 1, 4, wxT("I'm in the middle"));
466 grid
->SetCellValue(2, 2, wxT("red"));
468 grid
->SetCellTextColour(2, 2, *wxRED
);
469 grid
->SetCellValue(3, 3, wxT("green on grey"));
470 grid
->SetCellTextColour(3, 3, *wxGREEN
);
471 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
473 grid
->SetCellValue(4, 4, wxT("a weird looking cell"));
474 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
475 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
477 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
478 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
479 grid
->SetCellBackgroundColour(3, 0, wxColour(255, 127, 127));
481 wxGridCellAttr
*attr
;
482 attr
= new wxGridCellAttr
;
483 attr
->SetTextColour(*wxBLUE
);
484 grid
->SetColAttr(5, attr
);
485 attr
= new wxGridCellAttr
;
486 attr
->SetBackgroundColour(*wxRED
);
487 grid
->SetRowAttr(5, attr
);
489 grid
->SetCellValue(2, 4, wxT("a wider column"));
490 grid
->SetColSize(4, 120);
491 grid
->SetColMinimalWidth(4, 120);
493 grid
->SetCellTextColour(5, 8, *wxGREEN
);
494 grid
->SetCellValue(5, 8, wxT("Bg from row attr\nText col from cell attr"));
495 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"));
497 // Some numeric columns with different formatting.
498 grid
->SetColFormatFloat(6);
499 grid
->SetCellValue(0, 6, "Default\nfloat format");
500 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 3.1415));
501 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 1415.0));
502 grid
->SetCellValue(3, 6, wxString::Format(wxT("%g"), 12345.67890));
504 grid
->SetColFormatFloat(7, 6, 2);
505 grid
->SetCellValue(0, 7, "Width 6\nprecision 2");
506 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 3.1415));
507 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 1415.0));
508 grid
->SetCellValue(3, 7, wxString::Format(wxT("%g"), 12345.67890));
510 grid
->SetColFormatCustom(8,
511 wxString::Format("%s:%i,%i,%s", wxGRID_VALUE_FLOAT
, -1, 4, "g"));
512 grid
->SetCellValue(0, 8, "Compact\nformat");
513 grid
->SetCellValue(1, 8, wxT("31415e-4"));
514 grid
->SetCellValue(2, 8, wxT("1415"));
515 grid
->SetCellValue(3, 8, wxT("123456789e-4"));
517 grid
->SetColFormatNumber(9);
518 grid
->SetCellValue(0, 9, "Integer\ncolumn");
519 grid
->SetCellValue(1, 9, "17");
520 grid
->SetCellValue(2, 9, "0");
521 grid
->SetCellValue(3, 9, "-666");
522 grid
->SetCellAlignment(3, 9, wxALIGN_CENTRE
, wxALIGN_TOP
);
523 grid
->SetCellValue(3, 10, "<- This numeric cell should be centred");
525 const wxString choices
[] =
527 wxT("Please select a choice"),
528 wxT("This takes two cells"),
529 wxT("Another choice"),
531 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
532 grid
->SetCellSize(4, 0, 1, 2);
533 grid
->SetCellValue(4, 0, choices
[0]);
534 grid
->SetCellOverflow(4, 0, false);
536 grid
->SetCellSize(7, 1, 3, 4);
537 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
538 grid
->SetCellValue(7, 1, wxT("Big box!"));
540 // create a separator-like row: it's grey and it's non-resizable
541 grid
->DisableRowResize(10);
542 grid
->SetRowSize(10, 30);
543 attr
= new wxGridCellAttr
;
544 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
545 grid
->SetRowAttr(10, attr
);
546 grid
->SetCellValue(10, 0, "You can't resize this row interactively -- try it");
548 // this does exactly nothing except testing that SetAttr() handles NULL
549 // attributes and does reference counting correctly
550 grid
->SetAttr(11, 11, NULL
);
551 grid
->SetAttr(11, 11, new wxGridCellAttr
);
552 grid
->SetAttr(11, 11, NULL
);
554 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
560 topSizer
->Add( logWin
,
565 SetSizerAndFit( topSizer
);
572 GridFrame::~GridFrame()
575 delete wxLog::SetActiveTarget(m_logOld
);
580 void GridFrame::SetDefaults()
582 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
583 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
584 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
585 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
586 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
587 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
588 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
589 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
590 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
591 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
595 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
597 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
599 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
603 grid
->SetRowLabelSize( 0 );
608 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
610 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
612 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
616 grid
->SetColLabelSize( 0 );
621 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
624 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
628 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
630 grid
->EnableDragRowSize(
631 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
635 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
637 grid
->EnableDragColSize(
638 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
641 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
643 grid
->EnableDragColMove(
644 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
647 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
649 grid
->EnableDragGridSize(
650 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
653 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
655 grid
->EnableDragCell(
656 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
659 void GridFrame::SetNativeColHeader( wxCommandEvent
& WXUNUSED(ev
) )
661 CustomColumnHeadersProvider
* provider
=
662 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
663 provider
->UseCustomColHeaders(false);
664 grid
->SetUseNativeColLabels(true);
667 void GridFrame::SetCustomColHeader( wxCommandEvent
& WXUNUSED(ev
) )
669 CustomColumnHeadersProvider
* provider
=
670 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
671 provider
->UseCustomColHeaders(true);
672 grid
->SetUseNativeColLabels(false);
675 void GridFrame::SetDefaultColHeader( wxCommandEvent
& WXUNUSED(ev
) )
677 CustomColumnHeadersProvider
* provider
=
678 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
679 provider
->UseCustomColHeaders(false);
680 grid
->SetUseNativeColLabels(false);
684 void GridFrame::OnGridCustomTab(wxGridEvent
& event
)
686 // just for testing, make the cursor move up and down instead of the usual
688 if ( event
.ShiftDown() )
690 if ( grid
->GetGridCursorRow() > 0 )
691 grid
->MoveCursorUp( false );
695 if ( grid
->GetGridCursorRow() < grid
->GetNumberRows() - 1 )
696 grid
->MoveCursorDown( false );
700 void GridFrame::SetTabBehaviour(wxCommandEvent
& event
)
702 // To make any built-in behaviour work, we need to disable the custom TAB
703 // handler, otherwise it would be overriding them.
704 grid
->Disconnect(wxEVT_GRID_TABBING
,
705 wxGridEventHandler(GridFrame::OnGridCustomTab
));
707 grid
->SetTabBehaviour(
708 static_cast<wxGrid::TabBehaviour
>(event
.GetId() - ID_TAB_STOP
)
712 void GridFrame::SetTabCustomHandler(wxCommandEvent
&)
714 grid
->Connect(wxEVT_GRID_TABBING
,
715 wxGridEventHandler(GridFrame::OnGridCustomTab
),
720 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
722 grid
->EnableGridLines(
723 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
726 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
728 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
730 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
731 wxT("Pen Width"), 11, choices
);
733 int current
= grid
->GetCellHighlightPenWidth();
734 dlg
.SetSelection(current
);
735 if (dlg
.ShowModal() == wxID_OK
) {
736 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
740 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
742 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
744 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
745 wxT("Pen Width"), 11, choices
);
747 int current
= grid
->GetCellHighlightROPenWidth();
748 dlg
.SetSelection(current
);
749 if (dlg
.ShowModal() == wxID_OK
) {
750 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
756 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
758 grid
->AutoSizeColumns();
762 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
764 grid
->SetDefaultCellOverflow(ev
.IsChecked());
768 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
771 grid
->SetCellSize( 7, 1, 5, 5 );
773 grid
->SetCellSize( 7, 1, 1, 5 );
777 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
779 wxColourDialog
dlg( NULL
);
780 if ( dlg
.ShowModal() == wxID_OK
)
782 wxColourData retData
;
783 retData
= dlg
.GetColourData();
784 wxColour colour
= retData
.GetColour();
786 grid
->SetLabelBackgroundColour( colour
);
791 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
793 wxColourDialog
dlg( NULL
);
794 if ( dlg
.ShowModal() == wxID_OK
)
796 wxColourData retData
;
797 retData
= dlg
.GetColourData();
798 wxColour colour
= retData
.GetColour();
800 grid
->SetLabelTextColour( colour
);
804 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
806 wxFont font
= wxGetFontFromUser(this);
809 grid
->SetLabelFont(font
);
813 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
816 grid
->GetRowLabelAlignment( &horiz
, &vert
);
821 horiz
= wxALIGN_CENTRE
;
825 horiz
= wxALIGN_RIGHT
;
829 horiz
= wxALIGN_LEFT
;
833 grid
->SetRowLabelAlignment( horiz
, vert
);
836 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
839 grid
->GetRowLabelAlignment( &horiz
, &vert
);
844 vert
= wxALIGN_CENTRE
;
848 vert
= wxALIGN_BOTTOM
;
856 grid
->SetRowLabelAlignment( horiz
, vert
);
860 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
863 grid
->GetColLabelAlignment( &horiz
, &vert
);
868 horiz
= wxALIGN_CENTRE
;
872 horiz
= wxALIGN_RIGHT
;
876 horiz
= wxALIGN_LEFT
;
880 grid
->SetColLabelAlignment( horiz
, vert
);
884 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
887 grid
->GetColLabelAlignment( &horiz
, &vert
);
892 vert
= wxALIGN_CENTRE
;
896 vert
= wxALIGN_BOTTOM
;
904 grid
->SetColLabelAlignment( horiz
, vert
);
908 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
910 wxColourDialog
dlg( NULL
);
911 if ( dlg
.ShowModal() == wxID_OK
)
913 wxColourData retData
;
914 retData
= dlg
.GetColourData();
915 wxColour colour
= retData
.GetColour();
917 grid
->SetGridLineColour( colour
);
922 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
924 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
928 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
930 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
934 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
936 if ( grid
->IsSelection() )
938 wxGridUpdateLocker
locker(grid
);
939 for ( int n
= 0; n
< grid
->GetNumberRows(); )
941 if ( grid
->IsInSelection( n
, 0 ) )
942 grid
->DeleteRows( n
, 1 );
950 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
952 wxGridUpdateLocker
locker(grid
);
953 const wxArrayInt sels
= grid
->GetSelectedRows();
954 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
956 grid
->AutoSizeRow( sels
[n
], false );
960 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
962 wxGridUpdateLocker
locker(grid
);
963 const wxArrayInt sels
= grid
->GetSelectedCols();
964 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
966 grid
->AutoSizeColumn( sels
[n
], false );
970 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
972 wxGridUpdateLocker
locker(grid
);
973 const wxArrayInt sels
= grid
->GetSelectedRows();
974 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
976 grid
->AutoSizeRowLabelSize( sels
[n
] );
980 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
982 wxGridUpdateLocker
locker(grid
);
983 const wxArrayInt sels
= grid
->GetSelectedCols();
984 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
986 grid
->AutoSizeColLabelSize( sels
[n
] );
990 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
992 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
995 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
997 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
1000 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
1007 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
1009 if ( grid
->IsSelection() )
1011 wxGridUpdateLocker
locker(grid
);
1012 for ( int n
= 0; n
< grid
->GetNumberCols(); )
1014 if ( grid
->IsInSelection( 0 , n
) )
1015 grid
->DeleteCols( n
, 1 );
1023 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
1028 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
1030 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
1033 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
1035 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
1038 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
1040 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
1043 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
1045 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
1048 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
1050 wxColour col
= wxGetColourFromUser(this);
1053 grid
->SetDefaultCellTextColour(col
);
1058 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
1060 wxColour col
= wxGetColourFromUser(this);
1063 // Check the new Refresh function by passing it a rectangle
1064 // which exactly fits the grid.
1066 wxRect
r(pt
, grid
->GetSize());
1067 grid
->SetDefaultCellBackgroundColour(col
);
1068 grid
->Refresh(true, &r
);
1072 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
1074 grid
->DeselectCell(3, 1);
1077 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
1079 grid
->DeselectCol(2);
1082 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
1084 grid
->DeselectRow(2);
1087 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
1089 grid
->ClearSelection();
1092 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
1094 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
1097 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
1099 grid
->SelectCol(2, m_addToSel
);
1102 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
1104 grid
->SelectRow(2, m_addToSel
);
1107 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
1112 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
1114 m_addToSel
= event
.IsChecked();
1117 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
1120 if ( ev
.GetRow() != -1 )
1122 logBuf
<< wxT("Left click on row label ") << ev
.GetRow();
1124 else if ( ev
.GetCol() != -1 )
1126 logBuf
<< wxT("Left click on col label ") << ev
.GetCol();
1130 logBuf
<< wxT("Left click on corner label");
1133 if ( ev
.ShiftDown() )
1134 logBuf
<< wxT(" (shift down)");
1135 if ( ev
.ControlDown() )
1136 logBuf
<< wxT(" (control down)");
1137 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1139 // you must call event skip if you want default grid processing
1145 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
1147 wxLogMessage(wxT("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
1149 // you must call event skip if you want default grid processing
1150 // (cell highlighting etc.)
1156 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
1158 const int row
= ev
.GetRowOrCol();
1160 wxLogMessage("Resized row %d, new height = %d",
1161 row
, grid
->GetRowSize(row
));
1167 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
1169 const int col
= ev
.GetRowOrCol();
1171 wxLogMessage("Resized column %d, new width = %d",
1172 col
, grid
->GetColSize(col
));
1178 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1181 if ( ev
.Selecting() )
1182 logBuf
<< wxT("Selected ");
1184 logBuf
<< wxT("Deselected ");
1185 logBuf
<< wxT("cell at row ") << ev
.GetRow()
1186 << wxT(" col ") << ev
.GetCol()
1187 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1188 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1189 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1190 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1192 //Indicate whether this column was moved
1193 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1194 logBuf
<< wxT(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1196 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1198 // you must call Skip() if you want the default processing
1199 // to occur in wxGrid
1203 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1206 if ( ev
.Selecting() )
1207 logBuf
<< wxT("Selected ");
1209 logBuf
<< wxT("Deselected ");
1210 logBuf
<< wxT("cells from row ") << ev
.GetTopRow()
1211 << wxT(" col ") << ev
.GetLeftCol()
1212 << wxT(" to row ") << ev
.GetBottomRow()
1213 << wxT(" col ") << ev
.GetRightCol()
1214 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1215 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1216 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1217 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1218 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1223 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1225 int row
= ev
.GetRow(),
1228 wxLogMessage("Value of cell at (%d, %d): about to change "
1229 "from \"%s\" to \"%s\"",
1231 grid
->GetCellValue(row
, col
), ev
.GetString());
1233 // test how vetoing works
1234 if ( ev
.GetString() == "42" )
1236 wxLogMessage("Vetoing the change.");
1244 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1246 int row
= ev
.GetRow(),
1249 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1252 grid
->GetCellValue(row
, col
), ev
.GetString());
1257 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1259 wxLogMessage(wxT("Got request to drag cell at row %d, col %d"),
1260 ev
.GetRow(), ev
.GetCol());
1265 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1268 if ( (ev
.GetCol() == 4) &&
1269 (ev
.GetRow() == 0) &&
1270 (wxMessageBox(wxT("Are you sure you wish to edit this cell"),
1271 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1277 wxLogMessage( wxT("Cell editor shown.") );
1282 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1285 if ( (ev
.GetCol() == 4) &&
1286 (ev
.GetRow() == 0) &&
1287 (wxMessageBox(wxT("Are you sure you wish to finish editing this cell"),
1288 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1294 wxLogMessage( wxT("Cell editor hidden.") );
1299 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1301 wxAboutDialogInfo aboutInfo
;
1302 aboutInfo
.SetName(wxT("wxGrid demo"));
1303 aboutInfo
.SetDescription(_("wxGrid sample program"));
1304 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1305 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1306 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1308 // this is just to force the generic version of the about
1309 // dialog under wxMSW so that it's easy to test if the grid
1310 // repaints correctly when it has lost focus and a dialog
1311 // (different from the Windows standard message box -- it doesn't
1312 // work with it for some reason) is moved over it.
1313 aboutInfo
.SetWebSite(wxT("http://www.wxwidgets.org"));
1315 wxAboutBox(aboutInfo
);
1319 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1324 void GridFrame::OnBugsTable(wxCommandEvent
& )
1326 BugsGridFrame
*frame
= new BugsGridFrame
;
1330 // ----------------------------------------------------------------------------
1331 // MyGridCellAttrProvider
1332 // ----------------------------------------------------------------------------
1334 MyGridCellAttrProvider::MyGridCellAttrProvider()
1336 m_attrForOddRows
= new wxGridCellAttr
;
1337 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1340 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1342 m_attrForOddRows
->DecRef();
1345 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1346 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1348 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1354 attr
= m_attrForOddRows
;
1359 if ( !attr
->HasBackgroundColour() )
1361 wxGridCellAttr
*attrNew
= attr
->Clone();
1364 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1372 void GridFrame::OnVTable(wxCommandEvent
& )
1374 static long s_sizeGrid
= 10000;
1376 s_sizeGrid
= wxGetNumberFromUser(wxT("Size of the table to create"),
1378 wxT("wxGridDemo question"),
1382 if ( s_sizeGrid
!= -1 )
1384 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1389 // ----------------------------------------------------------------------------
1390 // MyGridCellRenderer
1391 // ----------------------------------------------------------------------------
1393 // do something that the default renderer doesn't here just to show that it is
1394 // possible to alter the appearance of the cell beyond what the attributes
1396 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1397 wxGridCellAttr
& attr
,
1403 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1405 dc
.SetPen(*wxGREEN_PEN
);
1406 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1407 dc
.DrawEllipse(rect
);
1410 // ============================================================================
1411 // BigGridFrame and BigGridTable: Sample of a non-standard table
1412 // ============================================================================
1414 BigGridFrame::BigGridFrame(long sizeGrid
)
1415 : wxFrame(NULL
, wxID_ANY
, wxT("Plugin Virtual Table"),
1416 wxDefaultPosition
, wxSize(500, 450))
1418 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1419 m_table
= new BigGridTable(sizeGrid
);
1421 // VZ: I don't understand why this slows down the display that much,
1422 // must profile it...
1423 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1425 m_grid
->SetTable(m_table
, true);
1427 #if defined __WXMOTIF__
1428 // MB: the grid isn't getting a sensible default size under wxMotif
1430 GetClientSize( &cw
, &ch
);
1431 m_grid
->SetSize( cw
, ch
);
1435 // ============================================================================
1436 // BugsGridFrame: a "realistic" table
1437 // ============================================================================
1439 // ----------------------------------------------------------------------------
1441 // ----------------------------------------------------------------------------
1464 static const wxString severities
[] =
1473 static struct BugsGridData
1479 wxChar platform
[12];
1481 } gs_dataBugsGrid
[] =
1483 { 18, wxT("foo doesn't work"), Sev_Major
, 1, wxT("wxMSW"), true },
1484 { 27, wxT("bar crashes"), Sev_Critical
, 1, wxT("all"), false },
1485 { 45, wxT("printing is slow"), Sev_Minor
, 3, wxT("wxMSW"), true },
1486 { 68, wxT("Rectangle() fails"), Sev_Normal
, 1, wxT("wxMSW"), false },
1489 static const wxChar
*headers
[Col_Max
] =
1499 // ----------------------------------------------------------------------------
1501 // ----------------------------------------------------------------------------
1503 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1509 return wxGRID_VALUE_NUMBER
;;
1512 // fall thorugh (TODO should be a list)
1515 return wxString::Format(wxT("%s:80"), wxGRID_VALUE_STRING
);
1518 return wxString::Format(wxT("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1521 return wxGRID_VALUE_BOOL
;
1524 wxFAIL_MSG(wxT("unknown column"));
1526 return wxEmptyString
;
1529 int BugsGridTable::GetNumberRows()
1531 return WXSIZEOF(gs_dataBugsGrid
);
1534 int BugsGridTable::GetNumberCols()
1539 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1544 wxString
BugsGridTable::GetValue( int row
, int col
)
1546 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1551 return wxString::Format(wxT("%d"), gd
.id
);
1554 return wxString::Format(wxT("%d"), gd
.prio
);
1557 return gd
.opened
? wxT("1") : wxT("0");
1560 return severities
[gd
.severity
];
1569 return wxEmptyString
;
1572 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1574 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1581 wxFAIL_MSG(wxT("unexpected column"));
1587 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1589 if ( severities
[n
] == value
)
1591 gd
.severity
= (Severity
)n
;
1596 if ( n
== WXSIZEOF(severities
) )
1598 wxLogWarning(wxT("Invalid severity value '%s'."),
1600 gd
.severity
= Sev_Normal
;
1606 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1610 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1616 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1618 const wxString
& typeName
)
1620 if ( typeName
== wxGRID_VALUE_STRING
)
1624 else if ( typeName
== wxGRID_VALUE_BOOL
)
1626 return col
== Col_Opened
;
1628 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1630 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1638 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1640 return CanGetValueAs(row
, col
, typeName
);
1643 long BugsGridTable::GetValueAsLong( int row
, int col
)
1645 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1659 wxFAIL_MSG(wxT("unexpected column"));
1664 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1666 if ( col
== Col_Opened
)
1668 return gs_dataBugsGrid
[row
].opened
;
1672 wxFAIL_MSG(wxT("unexpected column"));
1678 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1680 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1689 wxFAIL_MSG(wxT("unexpected column"));
1693 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1695 if ( col
== Col_Opened
)
1697 gs_dataBugsGrid
[row
].opened
= value
;
1701 wxFAIL_MSG(wxT("unexpected column"));
1705 wxString
BugsGridTable::GetColLabelValue( int col
)
1707 return headers
[col
];
1710 // ----------------------------------------------------------------------------
1712 // ----------------------------------------------------------------------------
1714 BugsGridFrame::BugsGridFrame()
1715 : wxFrame(NULL
, wxID_ANY
, wxT("Bugs table"))
1717 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1718 wxGridTableBase
*table
= new BugsGridTable();
1719 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1720 grid
->SetTable(table
, true);
1722 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1723 *attrRangeEditor
= new wxGridCellAttr
,
1724 *attrCombo
= new wxGridCellAttr
;
1726 attrRO
->SetReadOnly();
1727 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1728 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1731 grid
->SetColAttr(Col_Id
, attrRO
);
1732 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1733 grid
->SetColAttr(Col_Severity
, attrCombo
);
1736 SetClientSize(grid
->GetSize());
1739 // ============================================================================
1740 // TabularGrid: grid used for display of tabular data
1741 // ============================================================================
1743 class TabularGridTable
: public wxGridTableBase
1760 TabularGridTable() { m_sortOrder
= NULL
; }
1762 virtual int GetNumberRows() { return ROW_MAX
; }
1763 virtual int GetNumberCols() { return COL_MAX
; }
1765 virtual wxString
GetValue(int row
, int col
)
1768 row
= m_sortOrder
[row
];
1774 return GetNameOrExt(row
, col
);
1777 return wxString::Format("%lu", GetSize(row
));
1780 return GetDate(row
).FormatDate();
1784 wxFAIL_MSG( "unknown column" );
1790 virtual void SetValue(int, int, const wxString
&)
1792 wxFAIL_MSG( "shouldn't be called" );
1795 virtual wxString
GetColLabelValue(int col
)
1797 // notice that column parameter here always refers to the internal
1798 // column index, independently of its position on the screen
1799 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1800 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1805 virtual void SetColLabelValue(int, const wxString
&)
1807 wxFAIL_MSG( "shouldn't be called" );
1810 void Sort(int col
, bool ascending
)
1812 // we hardcode all sorting orders for simplicity here
1813 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1815 // descending ascending
1816 { { 2, 1, 0 }, { 0, 1, 2 } },
1817 { { 2, 1, 0 }, { 0, 1, 2 } },
1818 { { 2, 1, 0 }, { 0, 1, 2 } },
1819 { { 1, 0, 2 }, { 2, 0, 1 } },
1822 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1826 wxString
GetNameOrExt(int row
, int col
) const
1829 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1830 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1832 const wxString
s(names
[row
]);
1833 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1836 unsigned long GetSize(int row
) const
1838 static const unsigned long
1839 sizes
[] = { 412, 604, 40774 };
1840 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1845 wxDateTime
GetDate(int row
) const
1848 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1849 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1852 dt
.ParseISODate(dates
[row
]);
1859 // specialized text control for column indexes entry
1860 class ColIndexEntry
: public wxTextCtrl
1863 ColIndexEntry(wxWindow
*parent
)
1864 : wxTextCtrl(parent
, wxID_ANY
, "")
1866 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1872 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1882 virtual wxSize
DoGetBestSize() const
1884 wxSize size
= wxTextCtrl::DoGetBestSize();
1885 size
.x
= 3*GetCharWidth();
1890 class TabularGridFrame
: public wxFrame
1898 Id_Check_UseNativeHeader
,
1899 Id_Check_DrawNativeLabels
,
1900 Id_Check_ShowRowLabels
,
1901 Id_Check_EnableColMove
1906 void OnToggleUseNativeHeader(wxCommandEvent
&)
1908 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1911 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1913 // we don't draw labels at all, native or otherwise, if we use the
1914 // native header control
1915 event
.Enable( !m_chkUseNative
->GetValue() );
1918 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1920 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1923 void OnToggleShowRowLabels(wxCommandEvent
&)
1925 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1930 void OnToggleColMove(wxCommandEvent
&)
1932 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1935 void OnShowHideColumn(wxCommandEvent
& event
)
1937 int col
= m_txtColShowHide
->GetCol();
1940 m_grid
->SetColSize(col
,
1941 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1943 UpdateOrderAndVisibility();
1947 void OnMoveColumn(wxCommandEvent
&)
1949 int col
= m_txtColIndex
->GetCol();
1950 int pos
= m_txtColPos
->GetCol();
1951 if ( col
== -1 || pos
== -1 )
1954 m_grid
->SetColPos(col
, pos
);
1956 UpdateOrderAndVisibility();
1959 void OnResetColumnOrder(wxCommandEvent
&)
1961 m_grid
->ResetColPos();
1963 UpdateOrderAndVisibility();
1966 void OnGridColSort(wxGridEvent
& event
)
1968 const int col
= event
.GetCol();
1969 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
1970 m_grid
->IsSortOrderAscending()));
1973 void OnGridColMove(wxGridEvent
& event
)
1975 // can't update it yet as the order hasn't been changed, so do it a bit
1977 m_shouldUpdateOrder
= true;
1982 void OnGridColSize(wxGridSizeEvent
& event
)
1984 // we only catch this event to react to the user showing or hiding this
1985 // column using the header control menu and not because we're
1986 // interested in column resizing
1987 UpdateOrderAndVisibility();
1992 void OnIdle(wxIdleEvent
& event
)
1994 if ( m_shouldUpdateOrder
)
1996 m_shouldUpdateOrder
= false;
1997 UpdateOrderAndVisibility();
2003 void UpdateOrderAndVisibility()
2006 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
2008 const int col
= m_grid
->GetColAt(pos
);
2009 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
2020 m_statOrder
->SetLabel(s
);
2025 TabularGridTable
*m_table
;
2026 wxCheckBox
*m_chkUseNative
,
2028 *m_chkShowRowLabels
,
2029 *m_chkEnableColMove
;
2031 ColIndexEntry
*m_txtColIndex
,
2035 wxStaticText
*m_statOrder
;
2037 // fla for EVT_IDLE handler
2038 bool m_shouldUpdateOrder
;
2040 wxDECLARE_NO_COPY_CLASS(TabularGridFrame
);
2041 DECLARE_EVENT_TABLE()
2044 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
2045 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
2046 TabularGridFrame::OnToggleUseNativeHeader
)
2047 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
2048 TabularGridFrame::OnToggleDrawNativeLabels
)
2049 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
2050 TabularGridFrame::OnToggleShowRowLabels
)
2051 EVT_CHECKBOX(Id_Check_EnableColMove
,
2052 TabularGridFrame::OnToggleColMove
)
2054 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
2055 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
2057 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
2058 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
2059 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
2060 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
2062 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
2063 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
2064 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
2066 EVT_IDLE(TabularGridFrame::OnIdle
)
2069 TabularGridFrame::TabularGridFrame()
2070 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
2072 m_shouldUpdateOrder
= false;
2074 wxPanel
* const panel
= new wxPanel(this);
2076 // create and initialize the grid with the specified data
2077 m_table
= new TabularGridTable
;
2078 m_grid
= new wxGrid(panel
, wxID_ANY
,
2079 wxDefaultPosition
, wxDefaultSize
,
2080 wxBORDER_STATIC
| wxWANTS_CHARS
);
2081 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
2083 m_grid
->EnableDragColMove();
2084 m_grid
->UseNativeColHeader();
2085 m_grid
->HideRowLabels();
2087 // add it and the other controls to the frame
2088 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
2089 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
2091 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
2093 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
2094 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
2095 "&Use native header");
2096 m_chkUseNative
->SetValue(true);
2097 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
2099 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
2100 "&Draw native column labels");
2101 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
2103 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
2104 "Show &row labels");
2105 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
2107 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
2108 "Allow column re&ordering");
2109 m_chkEnableColMove
->SetValue(true);
2110 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
2111 sizerControls
->Add(sizerStyles
);
2113 sizerControls
->AddSpacer(10);
2115 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
2116 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
2118 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
2119 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
2121 m_txtColIndex
= new ColIndexEntry(panel
);
2122 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
2123 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
2124 m_txtColPos
= new ColIndexEntry(panel
);
2125 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
2126 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
2128 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
2130 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
2131 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
2133 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
2134 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
2135 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
2136 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
2138 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
2139 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
2141 m_txtColShowHide
= new ColIndexEntry(panel
);
2142 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
2143 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
2144 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
2145 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
2147 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
2149 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
2151 panel
->SetSizer(sizerTop
);
2153 SetClientSize(panel
->GetBestSize());
2154 SetSizeHints(GetSize());
2159 void GridFrame::OnTabularTable(wxCommandEvent
&)
2161 new TabularGridFrame
;
2164 // Example using wxGrid::Render
2165 // Displays a preset selection or, if it exists, a selection block
2166 // Draws the selection to a wxBitmap and displays the bitmap
2167 void GridFrame::OnGridRender( wxCommandEvent
& event
)
2169 int styleRender
= 0, i
;
2170 bool useLometric
= false, defSize
= false;
2172 wxSize
sizeMargin( 0, 0 );
2173 wxPoint
pointOrigin( 0, 0 );
2175 wxMenu
* menu
= GetMenuBar()->GetMenu( 0 );
2176 wxMenuItem
* menuItem
= menu
->FindItem( ID_RENDER_ROW_LABEL
);
2177 menu
= menuItem
->GetMenu();
2179 if ( menu
->FindItem( ID_RENDER_ROW_LABEL
)->IsChecked() )
2180 styleRender
|= wxGRID_DRAW_ROWS_HEADER
;
2181 if ( menu
->FindItem( ID_RENDER_COL_LABEL
)->IsChecked() )
2182 styleRender
|= wxGRID_DRAW_COLS_HEADER
;
2183 if ( menu
->FindItem( ID_RENDER_GRID_LINES
)->IsChecked() )
2184 styleRender
|= wxGRID_DRAW_CELL_LINES
;
2185 if ( menu
->FindItem( ID_RENDER_GRID_BORDER
)->IsChecked() )
2186 styleRender
|= wxGRID_DRAW_BOX_RECT
;
2187 if ( menu
->FindItem( ID_RENDER_SELECT_HLIGHT
)->IsChecked() )
2188 styleRender
|= wxGRID_DRAW_SELECTION
;
2189 if ( menu
->FindItem( ID_RENDER_LOMETRIC
)->IsChecked() )
2191 if ( menu
->FindItem( ID_RENDER_MARGIN
)->IsChecked() )
2193 pointOrigin
.x
+= 50;
2194 pointOrigin
.y
+= 50;
2195 sizeMargin
.IncBy( 50 );
2197 if ( menu
->FindItem( ID_RENDER_ZOOM
)->IsChecked() )
2199 if ( menu
->FindItem( ID_RENDER_DEFAULT_SIZE
)->IsChecked() )
2202 // init render area coords with a default row and col selection
2203 wxGridCellCoords
topLeft( 0, 0 ), bottomRight( 8, 6 );
2204 // check whether we are printing a block selection
2205 // other selection types not catered for here
2206 if ( event
.GetId() == ID_RENDER_COORDS
)
2208 topLeft
.SetCol( 6 );
2209 topLeft
.SetRow( 4 );
2210 bottomRight
.SetCol( 15 );
2211 bottomRight
.SetRow( 29 );
2213 else if ( grid
->IsSelection() && grid
->GetSelectionBlockTopLeft().Count() )
2215 wxGridCellCoordsArray cells
= grid
->GetSelectionBlockTopLeft();
2216 if ( grid
->GetSelectionBlockBottomRight().Count() )
2218 cells
.Add( grid
->GetSelectionBlockBottomRight()[ 0 ] );
2219 topLeft
.Set( cells
[ 0 ].GetRow(),
2220 cells
[ 0 ].GetCol() );
2221 bottomRight
.Set( cells
[ 1 ].GetRow(),
2222 cells
[ 1 ].GetCol() );
2227 wxSize
sizeRender( 0, 0 );
2228 wxGridSizesInfo sizeinfo
= grid
->GetColSizes();
2229 for ( i
= topLeft
.GetCol(); i
<= bottomRight
.GetCol(); i
++ )
2231 sizeRender
.x
+= sizeinfo
.GetSize( i
);
2235 sizeinfo
= grid
->GetRowSizes();
2236 for ( i
= topLeft
.GetRow(); i
<= bottomRight
.GetRow(); i
++ )
2238 sizeRender
.y
+= sizeinfo
.GetSize( i
);
2241 if ( styleRender
& wxGRID_DRAW_ROWS_HEADER
)
2242 sizeRender
.x
+= grid
->GetRowLabelSize();
2243 if ( styleRender
& wxGRID_DRAW_COLS_HEADER
)
2244 sizeRender
.y
+= grid
->GetColLabelSize();
2246 sizeRender
.x
*= zoom
;
2247 sizeRender
.y
*= zoom
;
2249 // delete any existing render frame and create new one
2250 wxWindow
* win
= FindWindow( "frameRender" );
2254 wxFrame
* frame
= new wxFrame( this, wxID_ANY
, "Grid Render" );
2255 frame
->SetClientSize( 780, 400 );
2256 frame
->SetName( "frameRender" );
2258 wxPanel
* canvas
= new wxPanel( frame
, wxID_ANY
);
2260 // make bitmap large enough for margins if any
2262 sizeRender
.IncBy( sizeMargin
* 2 );
2264 sizeRender
.IncBy( sizeMargin
);
2266 wxBitmap
bmp( sizeRender
);
2267 // don't leave it larger or drawing will be scaled
2268 sizeRender
.DecBy( sizeMargin
* 2 );
2270 wxMemoryDC
memDc(bmp
);
2272 // default row labels have no background colour so set background
2273 memDc
.SetBackground( wxBrush( canvas
->GetBackgroundColour() ) );
2276 // convert sizeRender to mapping mode units if necessary
2279 memDc
.SetMapMode( wxMM_LOMETRIC
);
2280 sizeRender
.x
= memDc
.DeviceToLogicalXRel( sizeRender
.x
);
2281 sizeRender
.y
= memDc
.DeviceToLogicalYRel( sizeRender
.y
);
2284 // pass wxDefaultSize if menu item is checked
2286 sizeRender
= wxDefaultSize
;
2288 grid
->Render( memDc
,
2291 topLeft
, bottomRight
,
2292 wxGridRenderStyle( styleRender
) );
2296 canvas
->Connect( wxEVT_PAINT
,
2297 wxPaintEventHandler(GridFrame::OnRenderPaint
),
2304 void GridFrame::OnRenderPaint( wxPaintEvent
& event
)
2306 wxPanel
* canvas
= ( wxPanel
* )event
.GetEventObject();
2307 wxPaintDC
dc( canvas
);
2308 canvas
->PrepareDC( dc
);
2310 if ( !m_gridBitmap
.IsOk() )
2313 wxMemoryDC
memDc( m_gridBitmap
);
2316 m_gridBitmap
.GetWidth(),
2317 m_gridBitmap
.GetHeight(),
2321 void GridFrame::HideCol( wxCommandEvent
& WXUNUSED(event
) )
2326 void GridFrame::ShowCol( wxCommandEvent
& WXUNUSED(event
) )
2331 void GridFrame::HideRow( wxCommandEvent
& WXUNUSED(event
) )
2336 void GridFrame::ShowRow( wxCommandEvent
& WXUNUSED(event
) )