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( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
164 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
165 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
166 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
167 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
168 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
169 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
170 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
171 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
172 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
173 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
174 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
175 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
176 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
177 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
178 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
179 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
180 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
181 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
182 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
183 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
185 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
186 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
188 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
189 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
190 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
191 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
192 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
194 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
195 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
196 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
197 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
198 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
199 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
200 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
201 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
202 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
204 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
205 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
206 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
207 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
208 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
209 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
210 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
212 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
213 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
215 EVT_MENU( wxID_PRINT
, GridFrame::OnGridRender
)
216 EVT_MENU( ID_RENDER_COORDS
, GridFrame::OnGridRender
)
218 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
219 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
220 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
221 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
222 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
223 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
224 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
225 EVT_GRID_CELL_CHANGED( GridFrame::OnCellValueChanged
)
226 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
228 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
229 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
233 GridFrame::GridFrame()
234 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets grid class demo"),
238 SetIcon(wxICON(sample
));
240 wxMenu
*fileMenu
= new wxMenu
;
241 fileMenu
->Append( ID_VTABLE
, wxT("&Virtual table test\tCtrl-V"));
242 fileMenu
->Append( ID_BUGS_TABLE
, wxT("&Bugs table test\tCtrl-B"));
243 fileMenu
->Append( ID_TABULAR_TABLE
, wxT("&Tabular table test\tCtrl-T"));
244 fileMenu
->AppendSeparator();
246 wxMenu
* setupMenu
= new wxMenu
;
248 item
= setupMenu
->AppendCheckItem( ID_RENDER_ROW_LABEL
,
249 "Render row labels" );
251 item
= setupMenu
->AppendCheckItem( ID_RENDER_COL_LABEL
,
252 "Render column labels" );
254 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_LINES
,
255 "Render grid cell lines" );
257 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_BORDER
,
260 item
= setupMenu
->AppendCheckItem( ID_RENDER_SELECT_HLIGHT
,
261 "Render selection highlight" );
262 setupMenu
->AppendSeparator();
263 setupMenu
->AppendCheckItem( ID_RENDER_LOMETRIC
,
264 "Use LOMETRIC mapping mode" );
265 setupMenu
->AppendCheckItem( ID_RENDER_DEFAULT_SIZE
,
266 "Use wxDefaultSize" );
267 setupMenu
->AppendCheckItem( ID_RENDER_MARGIN
,
268 "Logical 50 unit margin" );
269 setupMenu
->AppendCheckItem( ID_RENDER_ZOOM
,
272 fileMenu
->AppendSubMenu( setupMenu
, "Render setup" );
273 fileMenu
->Append( wxID_PRINT
, "Render" );
274 fileMenu
->Append( ID_RENDER_COORDS
, "Render G5:P30" );
276 fileMenu
->AppendSeparator();
277 fileMenu
->Append( wxID_EXIT
, wxT("E&xit\tAlt-X") );
279 wxMenu
*viewMenu
= new wxMenu
;
280 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
281 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
282 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
283 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
284 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
285 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
286 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
287 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
288 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
289 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
290 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
291 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
292 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
293 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
295 wxMenu
*rowLabelMenu
= new wxMenu
;
297 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("R&ow label alignment"),
299 wxT("Change alignment of row labels") );
301 rowLabelMenu
->AppendRadioItem( ID_ROWLABELHORIZALIGN
, wxT("&Horizontal") );
302 rowLabelMenu
->AppendRadioItem( ID_ROWLABELVERTALIGN
, wxT("&Vertical") );
304 wxMenu
*colLabelMenu
= new wxMenu
;
306 viewMenu
->Append( ID_COLLABELALIGN
, wxT("Col l&abel alignment"),
308 wxT("Change alignment of col labels") );
310 colLabelMenu
->AppendRadioItem( ID_COLLABELHORIZALIGN
, wxT("&Horizontal") );
311 colLabelMenu
->AppendRadioItem( ID_COLLABELVERTALIGN
, wxT("&Vertical") );
313 wxMenu
*colHeaderMenu
= new wxMenu
;
315 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("Col header style"),
317 wxT("Change style of col header") );
319 colHeaderMenu
->AppendRadioItem( ID_COLDEFAULTHEADER
, wxT("&Default") );
320 colHeaderMenu
->AppendRadioItem( ID_COLNATIVEHEADER
, wxT("&Native") );
321 colHeaderMenu
->AppendRadioItem( ID_COLCUSTOMHEADER
, wxT("&Custom") );
324 wxMenu
*colMenu
= new wxMenu
;
325 colMenu
->Append( ID_SETLABELCOLOUR
, wxT("Set &label colour...") );
326 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, wxT("Set label &text colour...") );
327 colMenu
->Append( ID_SETLABEL_FONT
, wxT("Set label fo&nt...") );
328 colMenu
->Append( ID_GRIDLINECOLOUR
, wxT("&Grid line colour...") );
329 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, wxT("Set cell &foreground colour...") );
330 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, wxT("Set cell &background colour...") );
332 wxMenu
*editMenu
= new wxMenu
;
333 editMenu
->Append( ID_INSERTROW
, wxT("Insert &row") );
334 editMenu
->Append( ID_INSERTCOL
, wxT("Insert &column") );
335 editMenu
->Append( ID_DELETEROW
, wxT("Delete selected ro&ws") );
336 editMenu
->Append( ID_DELETECOL
, wxT("Delete selected co&ls") );
337 editMenu
->Append( ID_CLEARGRID
, wxT("Cl&ear grid cell contents") );
339 wxMenu
*selectMenu
= new wxMenu
;
340 selectMenu
->Append( ID_SELECT_UNSELECT
, wxT("Add new cells to the selection"),
341 wxT("When off, old selection is deselected before ")
342 wxT("selecting the new cells"), wxITEM_CHECK
);
343 selectMenu
->AppendSeparator();
344 selectMenu
->Append( ID_SELECT_ALL
, wxT("Select all"));
345 selectMenu
->Append( ID_SELECT_ROW
, wxT("Select row 2"));
346 selectMenu
->Append( ID_SELECT_COL
, wxT("Select col 2"));
347 selectMenu
->Append( ID_SELECT_CELL
, wxT("Select cell (3, 1)"));
348 selectMenu
->AppendSeparator();
349 selectMenu
->Append( ID_DESELECT_ALL
, wxT("Deselect all"));
350 selectMenu
->Append( ID_DESELECT_ROW
, wxT("Deselect row 2"));
351 selectMenu
->Append( ID_DESELECT_COL
, wxT("Deselect col 2"));
352 selectMenu
->Append( ID_DESELECT_CELL
, wxT("Deselect cell (3, 1)"));
353 wxMenu
*selectionMenu
= new wxMenu
;
354 selectMenu
->Append( ID_CHANGESEL
, wxT("Change &selection mode"),
356 wxT("Change selection mode") );
358 selectionMenu
->Append( ID_SELCELLS
, wxT("Select &cells") );
359 selectionMenu
->Append( ID_SELROWS
, wxT("Select &rows") );
360 selectionMenu
->Append( ID_SELCOLS
, wxT("Select col&umns") );
361 selectionMenu
->Append( ID_SELROWSORCOLS
, wxT("Select rows &or columns") );
363 wxMenu
*autosizeMenu
= new wxMenu
;
364 autosizeMenu
->Append( ID_SIZE_ROW
, wxT("Selected &row data") );
365 autosizeMenu
->Append( ID_SIZE_COL
, wxT("Selected &column data") );
366 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, wxT("Selected row la&bel") );
367 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, wxT("Selected column &label") );
368 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, wxT("Column la&bels") );
369 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, wxT("Row label&s") );
370 autosizeMenu
->Append( ID_SIZE_GRID
, wxT("Entire &grid") );
372 wxMenu
*helpMenu
= new wxMenu
;
373 helpMenu
->Append( wxID_ABOUT
, wxT("&About wxGrid demo") );
375 wxMenuBar
*menuBar
= new wxMenuBar
;
376 menuBar
->Append( fileMenu
, wxT("&File") );
377 menuBar
->Append( viewMenu
, wxT("&Grid") );
378 menuBar
->Append( colMenu
, wxT("&Colours") );
379 menuBar
->Append( editMenu
, wxT("&Edit") );
380 menuBar
->Append( selectMenu
, wxT("&Select") );
381 menuBar
->Append( autosizeMenu
, wxT("&Autosize") );
382 menuBar
->Append( helpMenu
, wxT("&Help") );
384 SetMenuBar( menuBar
);
388 grid
= new wxGrid( this,
391 wxSize( 400, 300 ) );
395 int gridW
= 600, gridH
= 300;
396 int logW
= gridW
, logH
= 100;
398 logWin
= new wxTextCtrl( this,
401 wxPoint( 0, gridH
+ 20 ),
402 wxSize( logW
, logH
),
405 logger
= new wxLogTextCtrl( logWin
);
406 m_logOld
= wxLog::SetActiveTarget( logger
);
407 wxLog::DisableTimestamp();
410 // this will create a grid and, by default, an associated grid
412 grid
->CreateGrid( 0, 0 );
414 grid
->GetTable()->SetAttrProvider(new CustomColumnHeadersProvider());
416 grid
->AppendRows(100);
417 grid
->AppendCols(100);
419 int ir
= grid
->GetNumberRows();
420 grid
->DeleteRows(0, ir
);
421 grid
->AppendRows(ir
);
423 grid
->SetRowSize( 0, 60 );
424 grid
->SetCellValue( 0, 0, wxT("Ctrl+Home\nwill go to\nthis cell") );
426 grid
->SetCellValue( 0, 1, wxT("A long piece of text to demonstrate wrapping.") );
427 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
428 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
430 grid
->SetCellValue( 0, 2, wxT("Blah") );
431 grid
->SetCellValue( 0, 3, wxT("Read only") );
432 grid
->SetReadOnly( 0, 3 );
434 grid
->SetCellValue( 0, 4, wxT("Can veto edit this cell") );
436 grid
->SetCellValue( 0, 5, wxT("Press\nCtrl+arrow\nto skip over\ncells") );
438 grid
->SetRowSize( 99, 60 );
439 grid
->SetCellValue( 99, 99, wxT("Ctrl+End\nwill go to\nthis cell") );
440 grid
->SetCellValue( 1, 0, wxT("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
442 grid
->SetCellTextColour(1, 2, *wxRED
);
443 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
445 grid
->SetCellValue( 1, 4, wxT("I'm in the middle"));
447 grid
->SetCellValue(2, 2, wxT("red"));
449 grid
->SetCellTextColour(2, 2, *wxRED
);
450 grid
->SetCellValue(3, 3, wxT("green on grey"));
451 grid
->SetCellTextColour(3, 3, *wxGREEN
);
452 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
454 grid
->SetCellValue(4, 4, wxT("a weird looking cell"));
455 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
456 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
458 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
459 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
461 wxGridCellAttr
*attr
;
462 attr
= new wxGridCellAttr
;
463 attr
->SetTextColour(*wxBLUE
);
464 grid
->SetColAttr(5, attr
);
465 attr
= new wxGridCellAttr
;
466 attr
->SetBackgroundColour(*wxRED
);
467 grid
->SetRowAttr(5, attr
);
469 grid
->SetCellValue(2, 4, wxT("a wider column"));
470 grid
->SetColSize(4, 120);
471 grid
->SetColMinimalWidth(4, 120);
473 grid
->SetCellTextColour(5, 8, *wxGREEN
);
474 grid
->SetCellValue(5, 8, wxT("Bg from row attr\nText col from cell attr"));
475 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"));
477 // Some numeric columns with different formatting.
478 grid
->SetColFormatFloat(6);
479 grid
->SetCellValue(0, 6, "Default\nfloat format");
480 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 3.1415));
481 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 1415.0));
482 grid
->SetCellValue(3, 6, wxString::Format(wxT("%g"), 12345.67890));
484 grid
->SetColFormatFloat(7, 6, 2);
485 grid
->SetCellValue(0, 7, "Width 6\nprecision 2");
486 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 3.1415));
487 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 1415.0));
488 grid
->SetCellValue(3, 7, wxString::Format(wxT("%g"), 12345.67890));
490 grid
->SetColFormatCustom(8,
491 wxString::Format("%s:%i,%i,%s", wxGRID_VALUE_FLOAT
, -1, 4, "g"));
492 grid
->SetCellValue(0, 8, "Compact\nformat");
493 grid
->SetCellValue(1, 8, wxT("31415e-4"));
494 grid
->SetCellValue(2, 8, wxT("1415"));
495 grid
->SetCellValue(3, 8, wxT("123456789e-4"));
497 grid
->SetColFormatNumber(9);
498 grid
->SetCellValue(0, 9, "Integer\ncolumn");
499 grid
->SetCellValue(1, 9, "17");
500 grid
->SetCellValue(2, 9, "0");
501 grid
->SetCellValue(3, 9, "-666");
502 grid
->SetCellAlignment(3, 9, wxALIGN_CENTRE
, wxALIGN_TOP
);
503 grid
->SetCellValue(3, 10, "<- This numeric cell should be centred");
505 const wxString choices
[] =
507 wxT("Please select a choice"),
508 wxT("This takes two cells"),
509 wxT("Another choice"),
511 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
512 grid
->SetCellSize(4, 0, 1, 2);
513 grid
->SetCellValue(4, 0, choices
[0]);
514 grid
->SetCellOverflow(4, 0, false);
516 grid
->SetCellSize(7, 1, 3, 4);
517 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
518 grid
->SetCellValue(7, 1, wxT("Big box!"));
520 // create a separator-like row: it's grey and it's non-resizable
521 grid
->DisableRowResize(10);
522 grid
->SetRowSize(10, 30);
523 attr
= new wxGridCellAttr
;
524 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
525 grid
->SetRowAttr(10, attr
);
526 grid
->SetCellValue(10, 0, "You can't resize this row interactively -- try it");
528 // this does exactly nothing except testing that SetAttr() handles NULL
529 // attributes and does reference counting correctly
530 grid
->SetAttr(11, 11, NULL
);
531 grid
->SetAttr(11, 11, new wxGridCellAttr
);
532 grid
->SetAttr(11, 11, NULL
);
534 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
540 topSizer
->Add( logWin
,
545 SetSizerAndFit( topSizer
);
552 GridFrame::~GridFrame()
555 delete wxLog::SetActiveTarget(m_logOld
);
560 void GridFrame::SetDefaults()
562 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
563 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
564 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
565 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
566 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
567 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
568 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
569 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
570 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
571 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
575 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
577 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
579 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
583 grid
->SetRowLabelSize( 0 );
588 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
590 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
592 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
596 grid
->SetColLabelSize( 0 );
601 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
604 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
608 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
610 grid
->EnableDragRowSize(
611 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
615 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
617 grid
->EnableDragColSize(
618 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
621 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
623 grid
->EnableDragColMove(
624 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
627 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
629 grid
->EnableDragGridSize(
630 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
633 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
635 grid
->EnableDragCell(
636 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
639 void GridFrame::SetNativeColHeader( wxCommandEvent
& WXUNUSED(ev
) )
641 CustomColumnHeadersProvider
* provider
=
642 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
643 provider
->UseCustomColHeaders(false);
644 grid
->SetUseNativeColLabels(true);
647 void GridFrame::SetCustomColHeader( wxCommandEvent
& WXUNUSED(ev
) )
649 CustomColumnHeadersProvider
* provider
=
650 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
651 provider
->UseCustomColHeaders(true);
652 grid
->SetUseNativeColLabels(false);
655 void GridFrame::SetDefaultColHeader( wxCommandEvent
& WXUNUSED(ev
) )
657 CustomColumnHeadersProvider
* provider
=
658 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
659 provider
->UseCustomColHeaders(false);
660 grid
->SetUseNativeColLabels(false);
664 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
666 grid
->EnableGridLines(
667 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
670 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
672 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
674 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
675 wxT("Pen Width"), 11, choices
);
677 int current
= grid
->GetCellHighlightPenWidth();
678 dlg
.SetSelection(current
);
679 if (dlg
.ShowModal() == wxID_OK
) {
680 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
684 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
686 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
688 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
689 wxT("Pen Width"), 11, choices
);
691 int current
= grid
->GetCellHighlightROPenWidth();
692 dlg
.SetSelection(current
);
693 if (dlg
.ShowModal() == wxID_OK
) {
694 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
700 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
702 grid
->AutoSizeColumns();
706 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
708 grid
->SetDefaultCellOverflow(ev
.IsChecked());
712 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
715 grid
->SetCellSize( 7, 1, 5, 5 );
717 grid
->SetCellSize( 7, 1, 1, 5 );
721 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
723 wxColourDialog
dlg( NULL
);
724 if ( dlg
.ShowModal() == wxID_OK
)
726 wxColourData retData
;
727 retData
= dlg
.GetColourData();
728 wxColour colour
= retData
.GetColour();
730 grid
->SetLabelBackgroundColour( colour
);
735 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
737 wxColourDialog
dlg( NULL
);
738 if ( dlg
.ShowModal() == wxID_OK
)
740 wxColourData retData
;
741 retData
= dlg
.GetColourData();
742 wxColour colour
= retData
.GetColour();
744 grid
->SetLabelTextColour( colour
);
748 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
750 wxFont font
= wxGetFontFromUser(this);
753 grid
->SetLabelFont(font
);
757 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
760 grid
->GetRowLabelAlignment( &horiz
, &vert
);
765 horiz
= wxALIGN_CENTRE
;
769 horiz
= wxALIGN_RIGHT
;
773 horiz
= wxALIGN_LEFT
;
777 grid
->SetRowLabelAlignment( horiz
, vert
);
780 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
783 grid
->GetRowLabelAlignment( &horiz
, &vert
);
788 vert
= wxALIGN_CENTRE
;
792 vert
= wxALIGN_BOTTOM
;
800 grid
->SetRowLabelAlignment( horiz
, vert
);
804 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
807 grid
->GetColLabelAlignment( &horiz
, &vert
);
812 horiz
= wxALIGN_CENTRE
;
816 horiz
= wxALIGN_RIGHT
;
820 horiz
= wxALIGN_LEFT
;
824 grid
->SetColLabelAlignment( horiz
, vert
);
828 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
831 grid
->GetColLabelAlignment( &horiz
, &vert
);
836 vert
= wxALIGN_CENTRE
;
840 vert
= wxALIGN_BOTTOM
;
848 grid
->SetColLabelAlignment( horiz
, vert
);
852 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
854 wxColourDialog
dlg( NULL
);
855 if ( dlg
.ShowModal() == wxID_OK
)
857 wxColourData retData
;
858 retData
= dlg
.GetColourData();
859 wxColour colour
= retData
.GetColour();
861 grid
->SetGridLineColour( colour
);
866 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
868 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
872 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
874 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
878 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
880 if ( grid
->IsSelection() )
882 wxGridUpdateLocker
locker(grid
);
883 for ( int n
= 0; n
< grid
->GetNumberRows(); )
885 if ( grid
->IsInSelection( n
, 0 ) )
886 grid
->DeleteRows( n
, 1 );
894 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
896 wxGridUpdateLocker
locker(grid
);
897 const wxArrayInt sels
= grid
->GetSelectedRows();
898 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
900 grid
->AutoSizeRow( sels
[n
], false );
904 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
906 wxGridUpdateLocker
locker(grid
);
907 const wxArrayInt sels
= grid
->GetSelectedCols();
908 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
910 grid
->AutoSizeColumn( sels
[n
], false );
914 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
916 wxGridUpdateLocker
locker(grid
);
917 const wxArrayInt sels
= grid
->GetSelectedRows();
918 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
920 grid
->AutoSizeRowLabelSize( sels
[n
] );
924 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
926 wxGridUpdateLocker
locker(grid
);
927 const wxArrayInt sels
= grid
->GetSelectedCols();
928 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
930 grid
->AutoSizeColLabelSize( sels
[n
] );
934 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
936 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
939 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
941 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
944 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
950 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
952 if ( grid
->IsSelection() )
954 wxGridUpdateLocker
locker(grid
);
955 for ( int n
= 0; n
< grid
->GetNumberCols(); )
957 if ( grid
->IsInSelection( 0 , n
) )
958 grid
->DeleteCols( n
, 1 );
966 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
971 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
973 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
976 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
978 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
981 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
983 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
986 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
988 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
991 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
993 wxColour col
= wxGetColourFromUser(this);
996 grid
->SetDefaultCellTextColour(col
);
1001 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
1003 wxColour col
= wxGetColourFromUser(this);
1006 // Check the new Refresh function by passing it a rectangle
1007 // which exactly fits the grid.
1009 wxRect
r(pt
, grid
->GetSize());
1010 grid
->SetDefaultCellBackgroundColour(col
);
1011 grid
->Refresh(true, &r
);
1015 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
1017 grid
->DeselectCell(3, 1);
1020 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
1022 grid
->DeselectCol(2);
1025 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
1027 grid
->DeselectRow(2);
1030 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
1032 grid
->ClearSelection();
1035 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
1037 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
1040 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
1042 grid
->SelectCol(2, m_addToSel
);
1045 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
1047 grid
->SelectRow(2, m_addToSel
);
1050 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
1055 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
1057 m_addToSel
= event
.IsChecked();
1060 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
1063 if ( ev
.GetRow() != -1 )
1065 logBuf
<< wxT("Left click on row label ") << ev
.GetRow();
1067 else if ( ev
.GetCol() != -1 )
1069 logBuf
<< wxT("Left click on col label ") << ev
.GetCol();
1073 logBuf
<< wxT("Left click on corner label");
1076 if ( ev
.ShiftDown() )
1077 logBuf
<< wxT(" (shift down)");
1078 if ( ev
.ControlDown() )
1079 logBuf
<< wxT(" (control down)");
1080 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1082 // you must call event skip if you want default grid processing
1088 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
1090 wxLogMessage(wxT("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
1092 // you must call event skip if you want default grid processing
1093 // (cell highlighting etc.)
1099 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
1101 const int row
= ev
.GetRowOrCol();
1103 wxLogMessage("Resized row %d, new height = %d",
1104 row
, grid
->GetRowSize(row
));
1110 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
1112 const int col
= ev
.GetRowOrCol();
1114 wxLogMessage("Resized column %d, new width = %d",
1115 col
, grid
->GetColSize(col
));
1121 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1124 if ( ev
.Selecting() )
1125 logBuf
<< wxT("Selected ");
1127 logBuf
<< wxT("Deselected ");
1128 logBuf
<< wxT("cell at row ") << ev
.GetRow()
1129 << wxT(" col ") << ev
.GetCol()
1130 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1131 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1132 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1133 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1135 //Indicate whether this column was moved
1136 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1137 logBuf
<< wxT(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1139 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1141 // you must call Skip() if you want the default processing
1142 // to occur in wxGrid
1146 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1149 if ( ev
.Selecting() )
1150 logBuf
<< wxT("Selected ");
1152 logBuf
<< wxT("Deselected ");
1153 logBuf
<< wxT("cells from row ") << ev
.GetTopRow()
1154 << wxT(" col ") << ev
.GetLeftCol()
1155 << wxT(" to row ") << ev
.GetBottomRow()
1156 << wxT(" col ") << ev
.GetRightCol()
1157 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1158 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1159 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1160 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1161 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1166 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1168 int row
= ev
.GetRow(),
1171 wxLogMessage("Value of cell at (%d, %d): about to change "
1172 "from \"%s\" to \"%s\"",
1174 grid
->GetCellValue(row
, col
), ev
.GetString());
1176 // test how vetoing works
1177 if ( ev
.GetString() == "42" )
1179 wxLogMessage("Vetoing the change.");
1187 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1189 int row
= ev
.GetRow(),
1192 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1195 grid
->GetCellValue(row
, col
), ev
.GetString());
1200 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1202 wxLogMessage(wxT("Got request to drag cell at row %d, col %d"),
1203 ev
.GetRow(), ev
.GetCol());
1208 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1211 if ( (ev
.GetCol() == 4) &&
1212 (ev
.GetRow() == 0) &&
1213 (wxMessageBox(wxT("Are you sure you wish to edit this cell"),
1214 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1220 wxLogMessage( wxT("Cell editor shown.") );
1225 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1228 if ( (ev
.GetCol() == 4) &&
1229 (ev
.GetRow() == 0) &&
1230 (wxMessageBox(wxT("Are you sure you wish to finish editing this cell"),
1231 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1237 wxLogMessage( wxT("Cell editor hidden.") );
1242 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1244 wxAboutDialogInfo aboutInfo
;
1245 aboutInfo
.SetName(wxT("wxGrid demo"));
1246 aboutInfo
.SetDescription(_("wxGrid sample program"));
1247 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1248 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1249 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1251 // this is just to force the generic version of the about
1252 // dialog under wxMSW so that it's easy to test if the grid
1253 // repaints correctly when it has lost focus and a dialog
1254 // (different from the Windows standard message box -- it doesn't
1255 // work with it for some reason) is moved over it.
1256 aboutInfo
.SetWebSite(wxT("http://www.wxwidgets.org"));
1258 wxAboutBox(aboutInfo
);
1262 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1267 void GridFrame::OnBugsTable(wxCommandEvent
& )
1269 BugsGridFrame
*frame
= new BugsGridFrame
;
1273 // ----------------------------------------------------------------------------
1274 // MyGridCellAttrProvider
1275 // ----------------------------------------------------------------------------
1277 MyGridCellAttrProvider::MyGridCellAttrProvider()
1279 m_attrForOddRows
= new wxGridCellAttr
;
1280 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1283 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1285 m_attrForOddRows
->DecRef();
1288 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1289 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1291 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1297 attr
= m_attrForOddRows
;
1302 if ( !attr
->HasBackgroundColour() )
1304 wxGridCellAttr
*attrNew
= attr
->Clone();
1307 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1315 void GridFrame::OnVTable(wxCommandEvent
& )
1317 static long s_sizeGrid
= 10000;
1319 s_sizeGrid
= wxGetNumberFromUser(wxT("Size of the table to create"),
1321 wxT("wxGridDemo question"),
1325 if ( s_sizeGrid
!= -1 )
1327 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1332 // ----------------------------------------------------------------------------
1333 // MyGridCellRenderer
1334 // ----------------------------------------------------------------------------
1336 // do something that the default renderer doesn't here just to show that it is
1337 // possible to alter the appearance of the cell beyond what the attributes
1339 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1340 wxGridCellAttr
& attr
,
1346 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1348 dc
.SetPen(*wxGREEN_PEN
);
1349 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1350 dc
.DrawEllipse(rect
);
1353 // ============================================================================
1354 // BigGridFrame and BigGridTable: Sample of a non-standard table
1355 // ============================================================================
1357 BigGridFrame::BigGridFrame(long sizeGrid
)
1358 : wxFrame(NULL
, wxID_ANY
, wxT("Plugin Virtual Table"),
1359 wxDefaultPosition
, wxSize(500, 450))
1361 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1362 m_table
= new BigGridTable(sizeGrid
);
1364 // VZ: I don't understand why this slows down the display that much,
1365 // must profile it...
1366 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1368 m_grid
->SetTable(m_table
, true);
1370 #if defined __WXMOTIF__
1371 // MB: the grid isn't getting a sensible default size under wxMotif
1373 GetClientSize( &cw
, &ch
);
1374 m_grid
->SetSize( cw
, ch
);
1378 // ============================================================================
1379 // BugsGridFrame: a "realistic" table
1380 // ============================================================================
1382 // ----------------------------------------------------------------------------
1384 // ----------------------------------------------------------------------------
1407 static const wxString severities
[] =
1416 static struct BugsGridData
1422 wxChar platform
[12];
1424 } gs_dataBugsGrid
[] =
1426 { 18, wxT("foo doesn't work"), Sev_Major
, 1, wxT("wxMSW"), true },
1427 { 27, wxT("bar crashes"), Sev_Critical
, 1, wxT("all"), false },
1428 { 45, wxT("printing is slow"), Sev_Minor
, 3, wxT("wxMSW"), true },
1429 { 68, wxT("Rectangle() fails"), Sev_Normal
, 1, wxT("wxMSW"), false },
1432 static const wxChar
*headers
[Col_Max
] =
1442 // ----------------------------------------------------------------------------
1444 // ----------------------------------------------------------------------------
1446 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1452 return wxGRID_VALUE_NUMBER
;;
1455 // fall thorugh (TODO should be a list)
1458 return wxString::Format(wxT("%s:80"), wxGRID_VALUE_STRING
);
1461 return wxString::Format(wxT("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1464 return wxGRID_VALUE_BOOL
;
1467 wxFAIL_MSG(wxT("unknown column"));
1469 return wxEmptyString
;
1472 int BugsGridTable::GetNumberRows()
1474 return WXSIZEOF(gs_dataBugsGrid
);
1477 int BugsGridTable::GetNumberCols()
1482 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1487 wxString
BugsGridTable::GetValue( int row
, int col
)
1489 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1494 return wxString::Format(wxT("%d"), gd
.id
);
1497 return wxString::Format(wxT("%d"), gd
.prio
);
1500 return gd
.opened
? wxT("1") : wxT("0");
1503 return severities
[gd
.severity
];
1512 return wxEmptyString
;
1515 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1517 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1524 wxFAIL_MSG(wxT("unexpected column"));
1530 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1532 if ( severities
[n
] == value
)
1534 gd
.severity
= (Severity
)n
;
1539 if ( n
== WXSIZEOF(severities
) )
1541 wxLogWarning(wxT("Invalid severity value '%s'."),
1543 gd
.severity
= Sev_Normal
;
1549 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1553 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1559 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1561 const wxString
& typeName
)
1563 if ( typeName
== wxGRID_VALUE_STRING
)
1567 else if ( typeName
== wxGRID_VALUE_BOOL
)
1569 return col
== Col_Opened
;
1571 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1573 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1581 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1583 return CanGetValueAs(row
, col
, typeName
);
1586 long BugsGridTable::GetValueAsLong( int row
, int col
)
1588 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1602 wxFAIL_MSG(wxT("unexpected column"));
1607 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1609 if ( col
== Col_Opened
)
1611 return gs_dataBugsGrid
[row
].opened
;
1615 wxFAIL_MSG(wxT("unexpected column"));
1621 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1623 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1632 wxFAIL_MSG(wxT("unexpected column"));
1636 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1638 if ( col
== Col_Opened
)
1640 gs_dataBugsGrid
[row
].opened
= value
;
1644 wxFAIL_MSG(wxT("unexpected column"));
1648 wxString
BugsGridTable::GetColLabelValue( int col
)
1650 return headers
[col
];
1653 // ----------------------------------------------------------------------------
1655 // ----------------------------------------------------------------------------
1657 BugsGridFrame::BugsGridFrame()
1658 : wxFrame(NULL
, wxID_ANY
, wxT("Bugs table"))
1660 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1661 wxGridTableBase
*table
= new BugsGridTable();
1662 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1663 grid
->SetTable(table
, true);
1665 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1666 *attrRangeEditor
= new wxGridCellAttr
,
1667 *attrCombo
= new wxGridCellAttr
;
1669 attrRO
->SetReadOnly();
1670 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1671 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1674 grid
->SetColAttr(Col_Id
, attrRO
);
1675 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1676 grid
->SetColAttr(Col_Severity
, attrCombo
);
1679 SetClientSize(grid
->GetSize());
1682 // ============================================================================
1683 // TabularGrid: grid used for display of tabular data
1684 // ============================================================================
1686 class TabularGridTable
: public wxGridTableBase
1703 TabularGridTable() { m_sortOrder
= NULL
; }
1705 virtual int GetNumberRows() { return ROW_MAX
; }
1706 virtual int GetNumberCols() { return COL_MAX
; }
1708 virtual wxString
GetValue(int row
, int col
)
1711 row
= m_sortOrder
[row
];
1717 return GetNameOrExt(row
, col
);
1720 return wxString::Format("%lu", GetSize(row
));
1723 return GetDate(row
).FormatDate();
1727 wxFAIL_MSG( "unknown column" );
1733 virtual void SetValue(int, int, const wxString
&)
1735 wxFAIL_MSG( "shouldn't be called" );
1738 virtual wxString
GetColLabelValue(int col
)
1740 // notice that column parameter here always refers to the internal
1741 // column index, independently of its position on the screen
1742 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1743 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1748 virtual void SetColLabelValue(int, const wxString
&)
1750 wxFAIL_MSG( "shouldn't be called" );
1753 void Sort(int col
, bool ascending
)
1755 // we hardcode all sorting orders for simplicity here
1756 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1758 // descending ascending
1759 { { 2, 1, 0 }, { 0, 1, 2 } },
1760 { { 2, 1, 0 }, { 0, 1, 2 } },
1761 { { 2, 1, 0 }, { 0, 1, 2 } },
1762 { { 1, 0, 2 }, { 2, 0, 1 } },
1765 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1769 wxString
GetNameOrExt(int row
, int col
) const
1772 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1773 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1775 const wxString
s(names
[row
]);
1776 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1779 unsigned long GetSize(int row
) const
1781 static const unsigned long
1782 sizes
[] = { 412, 604, 40774 };
1783 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1788 wxDateTime
GetDate(int row
) const
1791 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1792 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1795 dt
.ParseISODate(dates
[row
]);
1802 // specialized text control for column indexes entry
1803 class ColIndexEntry
: public wxTextCtrl
1806 ColIndexEntry(wxWindow
*parent
)
1807 : wxTextCtrl(parent
, wxID_ANY
, "")
1809 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1815 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1825 virtual wxSize
DoGetBestSize() const
1827 wxSize size
= wxTextCtrl::DoGetBestSize();
1828 size
.x
= 3*GetCharWidth();
1833 class TabularGridFrame
: public wxFrame
1841 Id_Check_UseNativeHeader
,
1842 Id_Check_DrawNativeLabels
,
1843 Id_Check_ShowRowLabels
,
1844 Id_Check_EnableColMove
1849 void OnToggleUseNativeHeader(wxCommandEvent
&)
1851 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1854 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1856 // we don't draw labels at all, native or otherwise, if we use the
1857 // native header control
1858 event
.Enable( !m_chkUseNative
->GetValue() );
1861 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1863 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1866 void OnToggleShowRowLabels(wxCommandEvent
&)
1868 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1873 void OnToggleColMove(wxCommandEvent
&)
1875 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1878 void OnShowHideColumn(wxCommandEvent
& event
)
1880 int col
= m_txtColShowHide
->GetCol();
1883 m_grid
->SetColSize(col
,
1884 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1886 UpdateOrderAndVisibility();
1890 void OnMoveColumn(wxCommandEvent
&)
1892 int col
= m_txtColIndex
->GetCol();
1893 int pos
= m_txtColPos
->GetCol();
1894 if ( col
== -1 || pos
== -1 )
1897 m_grid
->SetColPos(col
, pos
);
1899 UpdateOrderAndVisibility();
1902 void OnResetColumnOrder(wxCommandEvent
&)
1904 m_grid
->ResetColPos();
1906 UpdateOrderAndVisibility();
1909 void OnGridColSort(wxGridEvent
& event
)
1911 const int col
= event
.GetCol();
1912 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
1913 m_grid
->IsSortOrderAscending()));
1916 void OnGridColMove(wxGridEvent
& event
)
1918 // can't update it yet as the order hasn't been changed, so do it a bit
1920 m_shouldUpdateOrder
= true;
1925 void OnGridColSize(wxGridSizeEvent
& event
)
1927 // we only catch this event to react to the user showing or hiding this
1928 // column using the header control menu and not because we're
1929 // interested in column resizing
1930 UpdateOrderAndVisibility();
1935 void OnIdle(wxIdleEvent
& event
)
1937 if ( m_shouldUpdateOrder
)
1939 m_shouldUpdateOrder
= false;
1940 UpdateOrderAndVisibility();
1946 void UpdateOrderAndVisibility()
1949 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
1951 const int col
= m_grid
->GetColAt(pos
);
1952 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
1963 m_statOrder
->SetLabel(s
);
1968 TabularGridTable
*m_table
;
1969 wxCheckBox
*m_chkUseNative
,
1971 *m_chkShowRowLabels
,
1972 *m_chkEnableColMove
;
1974 ColIndexEntry
*m_txtColIndex
,
1978 wxStaticText
*m_statOrder
;
1980 // fla for EVT_IDLE handler
1981 bool m_shouldUpdateOrder
;
1983 wxDECLARE_NO_COPY_CLASS(TabularGridFrame
);
1984 DECLARE_EVENT_TABLE()
1987 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
1988 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
1989 TabularGridFrame::OnToggleUseNativeHeader
)
1990 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
1991 TabularGridFrame::OnToggleDrawNativeLabels
)
1992 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
1993 TabularGridFrame::OnToggleShowRowLabels
)
1994 EVT_CHECKBOX(Id_Check_EnableColMove
,
1995 TabularGridFrame::OnToggleColMove
)
1997 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
1998 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
2000 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
2001 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
2002 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
2003 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
2005 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
2006 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
2007 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
2009 EVT_IDLE(TabularGridFrame::OnIdle
)
2012 TabularGridFrame::TabularGridFrame()
2013 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
2015 m_shouldUpdateOrder
= false;
2017 wxPanel
* const panel
= new wxPanel(this);
2019 // create and initialize the grid with the specified data
2020 m_table
= new TabularGridTable
;
2021 m_grid
= new wxGrid(panel
, wxID_ANY
,
2022 wxDefaultPosition
, wxDefaultSize
,
2023 wxBORDER_STATIC
| wxWANTS_CHARS
);
2024 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
2026 m_grid
->EnableDragColMove();
2027 m_grid
->UseNativeColHeader();
2028 m_grid
->HideRowLabels();
2030 // add it and the other controls to the frame
2031 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
2032 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
2034 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
2036 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
2037 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
2038 "&Use native header");
2039 m_chkUseNative
->SetValue(true);
2040 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
2042 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
2043 "&Draw native column labels");
2044 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
2046 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
2047 "Show &row labels");
2048 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
2050 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
2051 "Allow column re&ordering");
2052 m_chkEnableColMove
->SetValue(true);
2053 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
2054 sizerControls
->Add(sizerStyles
);
2056 sizerControls
->AddSpacer(10);
2058 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
2059 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
2061 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
2062 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
2064 m_txtColIndex
= new ColIndexEntry(panel
);
2065 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
2066 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
2067 m_txtColPos
= new ColIndexEntry(panel
);
2068 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
2069 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
2071 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
2073 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
2074 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
2076 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
2077 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
2078 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
2079 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
2081 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
2082 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
2084 m_txtColShowHide
= new ColIndexEntry(panel
);
2085 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
2086 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
2087 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
2088 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
2090 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
2092 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
2094 panel
->SetSizer(sizerTop
);
2096 SetClientSize(panel
->GetBestSize());
2097 SetSizeHints(GetSize());
2102 void GridFrame::OnTabularTable(wxCommandEvent
&)
2104 new TabularGridFrame
;
2107 // Example using wxGrid::Render
2108 // Displays a preset selection or, if it exists, a selection block
2109 // Draws the selection to a wxBitmap and displays the bitmap
2110 void GridFrame::OnGridRender( wxCommandEvent
& event
)
2112 int styleRender
= 0, i
;
2113 bool useLometric
= false, defSize
= false;
2115 wxSize
sizeMargin( 0, 0 );
2116 wxPoint
pointOrigin( 0, 0 );
2118 wxMenu
* menu
= GetMenuBar()->GetMenu( 0 );
2119 wxMenuItem
* menuItem
= menu
->FindItem( ID_RENDER_ROW_LABEL
);
2120 menu
= menuItem
->GetMenu();
2122 if ( menu
->FindItem( ID_RENDER_ROW_LABEL
)->IsChecked() )
2123 styleRender
|= wxGRID_DRAW_ROWS_HEADER
;
2124 if ( menu
->FindItem( ID_RENDER_COL_LABEL
)->IsChecked() )
2125 styleRender
|= wxGRID_DRAW_COLS_HEADER
;
2126 if ( menu
->FindItem( ID_RENDER_GRID_LINES
)->IsChecked() )
2127 styleRender
|= wxGRID_DRAW_CELL_LINES
;
2128 if ( menu
->FindItem( ID_RENDER_GRID_BORDER
)->IsChecked() )
2129 styleRender
|= wxGRID_DRAW_BOX_RECT
;
2130 if ( menu
->FindItem( ID_RENDER_SELECT_HLIGHT
)->IsChecked() )
2131 styleRender
|= wxGRID_DRAW_SELECTION
;
2132 if ( menu
->FindItem( ID_RENDER_LOMETRIC
)->IsChecked() )
2134 if ( menu
->FindItem( ID_RENDER_MARGIN
)->IsChecked() )
2136 pointOrigin
.x
+= 50;
2137 pointOrigin
.y
+= 50;
2138 sizeMargin
.IncBy( 50 );
2140 if ( menu
->FindItem( ID_RENDER_ZOOM
)->IsChecked() )
2142 if ( menu
->FindItem( ID_RENDER_DEFAULT_SIZE
)->IsChecked() )
2145 // init render area coords with a default row and col selection
2146 wxGridCellCoords
topLeft( 0, 0 ), bottomRight( 8, 6 );
2147 // check whether we are printing a block selection
2148 // other selection types not catered for here
2149 if ( event
.GetId() == ID_RENDER_COORDS
)
2151 topLeft
.SetCol( 6 );
2152 topLeft
.SetRow( 4 );
2153 bottomRight
.SetCol( 15 );
2154 bottomRight
.SetRow( 29 );
2156 else if ( grid
->IsSelection() && grid
->GetSelectionBlockTopLeft().Count() )
2158 wxGridCellCoordsArray cells
= grid
->GetSelectionBlockTopLeft();
2159 if ( grid
->GetSelectionBlockBottomRight().Count() )
2161 cells
.Add( grid
->GetSelectionBlockBottomRight()[ 0 ] );
2162 topLeft
.Set( cells
[ 0 ].GetRow(),
2163 cells
[ 0 ].GetCol() );
2164 bottomRight
.Set( cells
[ 1 ].GetRow(),
2165 cells
[ 1 ].GetCol() );
2170 wxSize
sizeRender( 0, 0 );
2171 wxGridSizesInfo sizeinfo
= grid
->GetColSizes();
2172 for ( i
= topLeft
.GetCol(); i
<= bottomRight
.GetCol(); i
++ )
2174 sizeRender
.x
+= sizeinfo
.GetSize( i
);
2178 sizeinfo
= grid
->GetRowSizes();
2179 for ( i
= topLeft
.GetRow(); i
<= bottomRight
.GetRow(); i
++ )
2181 sizeRender
.y
+= sizeinfo
.GetSize( i
);
2184 if ( styleRender
& wxGRID_DRAW_ROWS_HEADER
)
2185 sizeRender
.x
+= grid
->GetRowLabelSize();
2186 if ( styleRender
& wxGRID_DRAW_COLS_HEADER
)
2187 sizeRender
.y
+= grid
->GetColLabelSize();
2189 sizeRender
.x
*= zoom
;
2190 sizeRender
.y
*= zoom
;
2192 // delete any existing render frame and create new one
2193 wxWindow
* win
= FindWindow( "frameRender" );
2197 wxFrame
* frame
= new wxFrame( this, wxID_ANY
, "Grid Render" );
2198 frame
->SetClientSize( 780, 400 );
2199 frame
->SetName( "frameRender" );
2201 wxPanel
* canvas
= new wxPanel( frame
, wxID_ANY
);
2203 // make bitmap large enough for margins if any
2205 sizeRender
.IncBy( sizeMargin
* 2 );
2207 sizeRender
.IncBy( sizeMargin
);
2209 wxBitmap
bmp( sizeRender
);
2210 // don't leave it larger or drawing will be scaled
2211 sizeRender
.DecBy( sizeMargin
* 2 );
2213 wxMemoryDC
memDc(bmp
);
2215 // default row labels have no background colour so set background
2216 memDc
.SetBackground( wxBrush( canvas
->GetBackgroundColour() ) );
2219 // convert sizeRender to mapping mode units if necessary
2222 memDc
.SetMapMode( wxMM_LOMETRIC
);
2223 wxSize sizePPI
= memDc
.GetPPI();
2224 sizeRender
.x
= memDc
.DeviceToLogicalXRel( sizeRender
.x
);
2225 sizeRender
.y
= memDc
.DeviceToLogicalYRel( sizeRender
.y
);
2228 // pass wxDefaultSize if menu item is checked
2230 sizeRender
= wxDefaultSize
;
2232 grid
->Render( memDc
,
2235 topLeft
, bottomRight
,
2236 wxGridRenderStyle( styleRender
) );
2240 canvas
->Connect( wxEVT_PAINT
,
2241 wxPaintEventHandler(GridFrame::OnRenderPaint
),
2248 void GridFrame::OnRenderPaint( wxPaintEvent
& event
)
2250 wxPanel
* canvas
= ( wxPanel
* )event
.GetEventObject();
2251 wxPaintDC
dc( canvas
);
2252 canvas
->PrepareDC( dc
);
2254 if ( !m_gridBitmap
.IsOk() )
2257 wxMemoryDC
memDc( m_gridBitmap
);
2260 m_gridBitmap
.GetWidth(),
2261 m_gridBitmap
.GetHeight(),