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_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
215 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
217 EVT_MENU( wxID_PRINT
, GridFrame::OnGridRender
)
218 EVT_MENU( ID_RENDER_COORDS
, GridFrame::OnGridRender
)
220 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
221 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
222 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
223 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
224 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
225 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
226 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
227 EVT_GRID_CELL_CHANGED( GridFrame::OnCellValueChanged
)
228 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
230 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
231 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
235 GridFrame::GridFrame()
236 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets grid class demo"),
240 SetIcon(wxICON(sample
));
242 wxMenu
*fileMenu
= new wxMenu
;
243 fileMenu
->Append( ID_VTABLE
, wxT("&Virtual table test\tCtrl-V"));
244 fileMenu
->Append( ID_BUGS_TABLE
, wxT("&Bugs table test\tCtrl-B"));
245 fileMenu
->Append( ID_TABULAR_TABLE
, wxT("&Tabular table test\tCtrl-T"));
246 fileMenu
->AppendSeparator();
248 wxMenu
* setupMenu
= new wxMenu
;
250 item
= setupMenu
->AppendCheckItem( ID_RENDER_ROW_LABEL
,
251 "Render row labels" );
253 item
= setupMenu
->AppendCheckItem( ID_RENDER_COL_LABEL
,
254 "Render column labels" );
256 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_LINES
,
257 "Render grid cell lines" );
259 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_BORDER
,
262 item
= setupMenu
->AppendCheckItem( ID_RENDER_SELECT_HLIGHT
,
263 "Render selection highlight" );
264 setupMenu
->AppendSeparator();
265 setupMenu
->AppendCheckItem( ID_RENDER_LOMETRIC
,
266 "Use LOMETRIC mapping mode" );
267 setupMenu
->AppendCheckItem( ID_RENDER_DEFAULT_SIZE
,
268 "Use wxDefaultSize" );
269 setupMenu
->AppendCheckItem( ID_RENDER_MARGIN
,
270 "Logical 50 unit margin" );
271 setupMenu
->AppendCheckItem( ID_RENDER_ZOOM
,
274 fileMenu
->AppendSubMenu( setupMenu
, "Render setup" );
275 fileMenu
->Append( wxID_PRINT
, "Render" );
276 fileMenu
->Append( ID_RENDER_COORDS
, "Render G5:P30" );
278 fileMenu
->AppendSeparator();
279 fileMenu
->Append( wxID_EXIT
, wxT("E&xit\tAlt-X") );
281 wxMenu
*viewMenu
= new wxMenu
;
282 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
283 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
284 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
285 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
286 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
287 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
288 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
289 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
290 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
291 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
292 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
293 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
294 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
295 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
297 wxMenu
*rowLabelMenu
= new wxMenu
;
299 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("R&ow label alignment"),
301 wxT("Change alignment of row labels") );
303 rowLabelMenu
->AppendRadioItem( ID_ROWLABELHORIZALIGN
, wxT("&Horizontal") );
304 rowLabelMenu
->AppendRadioItem( ID_ROWLABELVERTALIGN
, wxT("&Vertical") );
306 wxMenu
*colLabelMenu
= new wxMenu
;
308 viewMenu
->Append( ID_COLLABELALIGN
, wxT("Col l&abel alignment"),
310 wxT("Change alignment of col labels") );
312 colLabelMenu
->AppendRadioItem( ID_COLLABELHORIZALIGN
, wxT("&Horizontal") );
313 colLabelMenu
->AppendRadioItem( ID_COLLABELVERTALIGN
, wxT("&Vertical") );
315 wxMenu
*colHeaderMenu
= new wxMenu
;
317 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("Col header style"),
319 wxT("Change style of col header") );
321 colHeaderMenu
->AppendRadioItem( ID_COLDEFAULTHEADER
, wxT("&Default") );
322 colHeaderMenu
->AppendRadioItem( ID_COLNATIVEHEADER
, wxT("&Native") );
323 colHeaderMenu
->AppendRadioItem( ID_COLCUSTOMHEADER
, wxT("&Custom") );
325 wxMenu
*tabBehaviourMenu
= new wxMenu
;
326 tabBehaviourMenu
->AppendRadioItem(ID_TAB_STOP
, "&Stop at the boundary");
327 tabBehaviourMenu
->AppendRadioItem(ID_TAB_WRAP
, "&Wrap at the boundary");
328 tabBehaviourMenu
->AppendRadioItem(ID_TAB_LEAVE
, "&Leave the grid");
329 tabBehaviourMenu
->AppendRadioItem(ID_TAB_CUSTOM
, "&Custom tab handler");
330 viewMenu
->AppendSubMenu(tabBehaviourMenu
, "&Tab behaviour");
332 wxMenu
*colMenu
= new wxMenu
;
333 colMenu
->Append( ID_SETLABELCOLOUR
, wxT("Set &label colour...") );
334 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, wxT("Set label &text colour...") );
335 colMenu
->Append( ID_SETLABEL_FONT
, wxT("Set label fo&nt...") );
336 colMenu
->Append( ID_GRIDLINECOLOUR
, wxT("&Grid line colour...") );
337 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, wxT("Set cell &foreground colour...") );
338 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, wxT("Set cell &background colour...") );
340 wxMenu
*editMenu
= new wxMenu
;
341 editMenu
->Append( ID_INSERTROW
, wxT("Insert &row") );
342 editMenu
->Append( ID_INSERTCOL
, wxT("Insert &column") );
343 editMenu
->Append( ID_DELETEROW
, wxT("Delete selected ro&ws") );
344 editMenu
->Append( ID_DELETECOL
, wxT("Delete selected co&ls") );
345 editMenu
->Append( ID_CLEARGRID
, wxT("Cl&ear grid cell contents") );
347 wxMenu
*selectMenu
= new wxMenu
;
348 selectMenu
->Append( ID_SELECT_UNSELECT
, wxT("Add new cells to the selection"),
349 wxT("When off, old selection is deselected before ")
350 wxT("selecting the new cells"), wxITEM_CHECK
);
351 selectMenu
->AppendSeparator();
352 selectMenu
->Append( ID_SELECT_ALL
, wxT("Select all"));
353 selectMenu
->Append( ID_SELECT_ROW
, wxT("Select row 2"));
354 selectMenu
->Append( ID_SELECT_COL
, wxT("Select col 2"));
355 selectMenu
->Append( ID_SELECT_CELL
, wxT("Select cell (3, 1)"));
356 selectMenu
->AppendSeparator();
357 selectMenu
->Append( ID_DESELECT_ALL
, wxT("Deselect all"));
358 selectMenu
->Append( ID_DESELECT_ROW
, wxT("Deselect row 2"));
359 selectMenu
->Append( ID_DESELECT_COL
, wxT("Deselect col 2"));
360 selectMenu
->Append( ID_DESELECT_CELL
, wxT("Deselect cell (3, 1)"));
361 wxMenu
*selectionMenu
= new wxMenu
;
362 selectMenu
->Append( ID_CHANGESEL
, wxT("Change &selection mode"),
364 wxT("Change selection mode") );
366 selectionMenu
->Append( ID_SELCELLS
, wxT("Select &cells") );
367 selectionMenu
->Append( ID_SELROWS
, wxT("Select &rows") );
368 selectionMenu
->Append( ID_SELCOLS
, wxT("Select col&umns") );
369 selectionMenu
->Append( ID_SELROWSORCOLS
, wxT("Select rows &or columns") );
371 wxMenu
*autosizeMenu
= new wxMenu
;
372 autosizeMenu
->Append( ID_SIZE_ROW
, wxT("Selected &row data") );
373 autosizeMenu
->Append( ID_SIZE_COL
, wxT("Selected &column data") );
374 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, wxT("Selected row la&bel") );
375 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, wxT("Selected column &label") );
376 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, wxT("Column la&bels") );
377 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, wxT("Row label&s") );
378 autosizeMenu
->Append( ID_SIZE_GRID
, wxT("Entire &grid") );
380 wxMenu
*helpMenu
= new wxMenu
;
381 helpMenu
->Append( wxID_ABOUT
, wxT("&About wxGrid demo") );
383 wxMenuBar
*menuBar
= new wxMenuBar
;
384 menuBar
->Append( fileMenu
, wxT("&File") );
385 menuBar
->Append( viewMenu
, wxT("&Grid") );
386 menuBar
->Append( colMenu
, wxT("&Colours") );
387 menuBar
->Append( editMenu
, wxT("&Edit") );
388 menuBar
->Append( selectMenu
, wxT("&Select") );
389 menuBar
->Append( autosizeMenu
, wxT("&Autosize") );
390 menuBar
->Append( helpMenu
, wxT("&Help") );
392 SetMenuBar( menuBar
);
396 grid
= new wxGrid( this,
399 wxSize( 400, 300 ) );
403 int gridW
= 600, gridH
= 300;
404 int logW
= gridW
, logH
= 100;
406 logWin
= new wxTextCtrl( this,
409 wxPoint( 0, gridH
+ 20 ),
410 wxSize( logW
, logH
),
413 logger
= new wxLogTextCtrl( logWin
);
414 m_logOld
= wxLog::SetActiveTarget( logger
);
415 wxLog::DisableTimestamp();
418 // this will create a grid and, by default, an associated grid
420 grid
->CreateGrid( 0, 0 );
422 grid
->GetTable()->SetAttrProvider(new CustomColumnHeadersProvider());
424 grid
->AppendRows(100);
425 grid
->AppendCols(100);
427 int ir
= grid
->GetNumberRows();
428 grid
->DeleteRows(0, ir
);
429 grid
->AppendRows(ir
);
431 grid
->SetRowSize( 0, 60 );
432 grid
->SetCellValue( 0, 0, wxT("Ctrl+Home\nwill go to\nthis cell") );
434 grid
->SetCellValue( 0, 1, wxT("A long piece of text to demonstrate wrapping.") );
435 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
436 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
438 grid
->SetCellValue( 0, 2, wxT("Blah") );
439 grid
->SetCellValue( 0, 3, wxT("Read only") );
440 grid
->SetReadOnly( 0, 3 );
442 grid
->SetCellValue( 0, 4, wxT("Can veto edit this cell") );
444 grid
->SetCellValue( 0, 5, wxT("Press\nCtrl+arrow\nto skip over\ncells") );
446 grid
->SetRowSize( 99, 60 );
447 grid
->SetCellValue(98, 98, "Test background colour setting");
448 grid
->SetCellBackgroundColour(98, 99, wxColour(255, 127, 127));
449 grid
->SetCellBackgroundColour(99, 98, wxColour(255, 127, 127));
450 grid
->SetCellValue( 99, 99, wxT("Ctrl+End\nwill go to\nthis cell") );
451 grid
->SetCellValue( 1, 0, wxT("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
453 grid
->SetCellTextColour(1, 2, *wxRED
);
454 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
456 grid
->SetCellValue( 1, 4, wxT("I'm in the middle"));
458 grid
->SetCellValue(2, 2, wxT("red"));
460 grid
->SetCellTextColour(2, 2, *wxRED
);
461 grid
->SetCellValue(3, 3, wxT("green on grey"));
462 grid
->SetCellTextColour(3, 3, *wxGREEN
);
463 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
465 grid
->SetCellValue(4, 4, wxT("a weird looking cell"));
466 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
467 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
469 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
470 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
471 grid
->SetCellBackgroundColour(3, 0, wxColour(255, 127, 127));
473 wxGridCellAttr
*attr
;
474 attr
= new wxGridCellAttr
;
475 attr
->SetTextColour(*wxBLUE
);
476 grid
->SetColAttr(5, attr
);
477 attr
= new wxGridCellAttr
;
478 attr
->SetBackgroundColour(*wxRED
);
479 grid
->SetRowAttr(5, attr
);
481 grid
->SetCellValue(2, 4, wxT("a wider column"));
482 grid
->SetColSize(4, 120);
483 grid
->SetColMinimalWidth(4, 120);
485 grid
->SetCellTextColour(5, 8, *wxGREEN
);
486 grid
->SetCellValue(5, 8, wxT("Bg from row attr\nText col from cell attr"));
487 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"));
489 // Some numeric columns with different formatting.
490 grid
->SetColFormatFloat(6);
491 grid
->SetCellValue(0, 6, "Default\nfloat format");
492 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 3.1415));
493 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 1415.0));
494 grid
->SetCellValue(3, 6, wxString::Format(wxT("%g"), 12345.67890));
496 grid
->SetColFormatFloat(7, 6, 2);
497 grid
->SetCellValue(0, 7, "Width 6\nprecision 2");
498 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 3.1415));
499 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 1415.0));
500 grid
->SetCellValue(3, 7, wxString::Format(wxT("%g"), 12345.67890));
502 grid
->SetColFormatCustom(8,
503 wxString::Format("%s:%i,%i,%s", wxGRID_VALUE_FLOAT
, -1, 4, "g"));
504 grid
->SetCellValue(0, 8, "Compact\nformat");
505 grid
->SetCellValue(1, 8, wxT("31415e-4"));
506 grid
->SetCellValue(2, 8, wxT("1415"));
507 grid
->SetCellValue(3, 8, wxT("123456789e-4"));
509 grid
->SetColFormatNumber(9);
510 grid
->SetCellValue(0, 9, "Integer\ncolumn");
511 grid
->SetCellValue(1, 9, "17");
512 grid
->SetCellValue(2, 9, "0");
513 grid
->SetCellValue(3, 9, "-666");
514 grid
->SetCellAlignment(3, 9, wxALIGN_CENTRE
, wxALIGN_TOP
);
515 grid
->SetCellValue(3, 10, "<- This numeric cell should be centred");
517 const wxString choices
[] =
519 wxT("Please select a choice"),
520 wxT("This takes two cells"),
521 wxT("Another choice"),
523 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
524 grid
->SetCellSize(4, 0, 1, 2);
525 grid
->SetCellValue(4, 0, choices
[0]);
526 grid
->SetCellOverflow(4, 0, false);
528 grid
->SetCellSize(7, 1, 3, 4);
529 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
530 grid
->SetCellValue(7, 1, wxT("Big box!"));
532 // create a separator-like row: it's grey and it's non-resizable
533 grid
->DisableRowResize(10);
534 grid
->SetRowSize(10, 30);
535 attr
= new wxGridCellAttr
;
536 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
537 grid
->SetRowAttr(10, attr
);
538 grid
->SetCellValue(10, 0, "You can't resize this row interactively -- try it");
540 // this does exactly nothing except testing that SetAttr() handles NULL
541 // attributes and does reference counting correctly
542 grid
->SetAttr(11, 11, NULL
);
543 grid
->SetAttr(11, 11, new wxGridCellAttr
);
544 grid
->SetAttr(11, 11, NULL
);
546 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
552 topSizer
->Add( logWin
,
557 SetSizerAndFit( topSizer
);
564 GridFrame::~GridFrame()
567 delete wxLog::SetActiveTarget(m_logOld
);
572 void GridFrame::SetDefaults()
574 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
575 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
576 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
577 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
578 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
579 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
580 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
581 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
582 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
583 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
587 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
589 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
591 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
595 grid
->SetRowLabelSize( 0 );
600 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
602 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
604 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
608 grid
->SetColLabelSize( 0 );
613 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
616 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
620 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
622 grid
->EnableDragRowSize(
623 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
627 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
629 grid
->EnableDragColSize(
630 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
633 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
635 grid
->EnableDragColMove(
636 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
639 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
641 grid
->EnableDragGridSize(
642 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
645 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
647 grid
->EnableDragCell(
648 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
651 void GridFrame::SetNativeColHeader( wxCommandEvent
& WXUNUSED(ev
) )
653 CustomColumnHeadersProvider
* provider
=
654 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
655 provider
->UseCustomColHeaders(false);
656 grid
->SetUseNativeColLabels(true);
659 void GridFrame::SetCustomColHeader( wxCommandEvent
& WXUNUSED(ev
) )
661 CustomColumnHeadersProvider
* provider
=
662 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
663 provider
->UseCustomColHeaders(true);
664 grid
->SetUseNativeColLabels(false);
667 void GridFrame::SetDefaultColHeader( wxCommandEvent
& WXUNUSED(ev
) )
669 CustomColumnHeadersProvider
* provider
=
670 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
671 provider
->UseCustomColHeaders(false);
672 grid
->SetUseNativeColLabels(false);
676 void GridFrame::OnGridCustomTab(wxGridEvent
& event
)
678 // just for testing, make the cursor move up and down instead of the usual
680 if ( event
.ShiftDown() )
682 if ( grid
->GetGridCursorRow() > 0 )
683 grid
->MoveCursorUp( false );
687 if ( grid
->GetGridCursorRow() < grid
->GetNumberRows() - 1 )
688 grid
->MoveCursorDown( false );
692 void GridFrame::SetTabBehaviour(wxCommandEvent
& event
)
694 // To make any built-in behaviour work, we need to disable the custom TAB
695 // handler, otherwise it would be overriding them.
696 grid
->Disconnect(wxEVT_GRID_TABBING
,
697 wxGridEventHandler(GridFrame::OnGridCustomTab
));
699 grid
->SetTabBehaviour(
700 static_cast<wxGrid::TabBehaviour
>(event
.GetId() - ID_TAB_STOP
)
704 void GridFrame::SetTabCustomHandler(wxCommandEvent
&)
706 grid
->Connect(wxEVT_GRID_TABBING
,
707 wxGridEventHandler(GridFrame::OnGridCustomTab
),
712 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
714 grid
->EnableGridLines(
715 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
718 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
720 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
722 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
723 wxT("Pen Width"), 11, choices
);
725 int current
= grid
->GetCellHighlightPenWidth();
726 dlg
.SetSelection(current
);
727 if (dlg
.ShowModal() == wxID_OK
) {
728 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
732 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
734 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
736 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
737 wxT("Pen Width"), 11, choices
);
739 int current
= grid
->GetCellHighlightROPenWidth();
740 dlg
.SetSelection(current
);
741 if (dlg
.ShowModal() == wxID_OK
) {
742 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
748 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
750 grid
->AutoSizeColumns();
754 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
756 grid
->SetDefaultCellOverflow(ev
.IsChecked());
760 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
763 grid
->SetCellSize( 7, 1, 5, 5 );
765 grid
->SetCellSize( 7, 1, 1, 5 );
769 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
771 wxColourDialog
dlg( NULL
);
772 if ( dlg
.ShowModal() == wxID_OK
)
774 wxColourData retData
;
775 retData
= dlg
.GetColourData();
776 wxColour colour
= retData
.GetColour();
778 grid
->SetLabelBackgroundColour( colour
);
783 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
785 wxColourDialog
dlg( NULL
);
786 if ( dlg
.ShowModal() == wxID_OK
)
788 wxColourData retData
;
789 retData
= dlg
.GetColourData();
790 wxColour colour
= retData
.GetColour();
792 grid
->SetLabelTextColour( colour
);
796 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
798 wxFont font
= wxGetFontFromUser(this);
801 grid
->SetLabelFont(font
);
805 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
808 grid
->GetRowLabelAlignment( &horiz
, &vert
);
813 horiz
= wxALIGN_CENTRE
;
817 horiz
= wxALIGN_RIGHT
;
821 horiz
= wxALIGN_LEFT
;
825 grid
->SetRowLabelAlignment( horiz
, vert
);
828 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
831 grid
->GetRowLabelAlignment( &horiz
, &vert
);
836 vert
= wxALIGN_CENTRE
;
840 vert
= wxALIGN_BOTTOM
;
848 grid
->SetRowLabelAlignment( horiz
, vert
);
852 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
855 grid
->GetColLabelAlignment( &horiz
, &vert
);
860 horiz
= wxALIGN_CENTRE
;
864 horiz
= wxALIGN_RIGHT
;
868 horiz
= wxALIGN_LEFT
;
872 grid
->SetColLabelAlignment( horiz
, vert
);
876 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
879 grid
->GetColLabelAlignment( &horiz
, &vert
);
884 vert
= wxALIGN_CENTRE
;
888 vert
= wxALIGN_BOTTOM
;
896 grid
->SetColLabelAlignment( horiz
, vert
);
900 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
902 wxColourDialog
dlg( NULL
);
903 if ( dlg
.ShowModal() == wxID_OK
)
905 wxColourData retData
;
906 retData
= dlg
.GetColourData();
907 wxColour colour
= retData
.GetColour();
909 grid
->SetGridLineColour( colour
);
914 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
916 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
920 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
922 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
926 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
928 if ( grid
->IsSelection() )
930 wxGridUpdateLocker
locker(grid
);
931 for ( int n
= 0; n
< grid
->GetNumberRows(); )
933 if ( grid
->IsInSelection( n
, 0 ) )
934 grid
->DeleteRows( n
, 1 );
942 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
944 wxGridUpdateLocker
locker(grid
);
945 const wxArrayInt sels
= grid
->GetSelectedRows();
946 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
948 grid
->AutoSizeRow( sels
[n
], false );
952 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
954 wxGridUpdateLocker
locker(grid
);
955 const wxArrayInt sels
= grid
->GetSelectedCols();
956 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
958 grid
->AutoSizeColumn( sels
[n
], false );
962 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
964 wxGridUpdateLocker
locker(grid
);
965 const wxArrayInt sels
= grid
->GetSelectedRows();
966 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
968 grid
->AutoSizeRowLabelSize( sels
[n
] );
972 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
974 wxGridUpdateLocker
locker(grid
);
975 const wxArrayInt sels
= grid
->GetSelectedCols();
976 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
978 grid
->AutoSizeColLabelSize( sels
[n
] );
982 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
984 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
987 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
989 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
992 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
999 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
1001 if ( grid
->IsSelection() )
1003 wxGridUpdateLocker
locker(grid
);
1004 for ( int n
= 0; n
< grid
->GetNumberCols(); )
1006 if ( grid
->IsInSelection( 0 , n
) )
1007 grid
->DeleteCols( n
, 1 );
1015 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
1020 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
1022 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
1025 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
1027 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
1030 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
1032 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
1035 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
1037 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
1040 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
1042 wxColour col
= wxGetColourFromUser(this);
1045 grid
->SetDefaultCellTextColour(col
);
1050 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
1052 wxColour col
= wxGetColourFromUser(this);
1055 // Check the new Refresh function by passing it a rectangle
1056 // which exactly fits the grid.
1058 wxRect
r(pt
, grid
->GetSize());
1059 grid
->SetDefaultCellBackgroundColour(col
);
1060 grid
->Refresh(true, &r
);
1064 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
1066 grid
->DeselectCell(3, 1);
1069 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
1071 grid
->DeselectCol(2);
1074 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
1076 grid
->DeselectRow(2);
1079 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
1081 grid
->ClearSelection();
1084 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
1086 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
1089 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
1091 grid
->SelectCol(2, m_addToSel
);
1094 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
1096 grid
->SelectRow(2, m_addToSel
);
1099 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
1104 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
1106 m_addToSel
= event
.IsChecked();
1109 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
1112 if ( ev
.GetRow() != -1 )
1114 logBuf
<< wxT("Left click on row label ") << ev
.GetRow();
1116 else if ( ev
.GetCol() != -1 )
1118 logBuf
<< wxT("Left click on col label ") << ev
.GetCol();
1122 logBuf
<< wxT("Left click on corner label");
1125 if ( ev
.ShiftDown() )
1126 logBuf
<< wxT(" (shift down)");
1127 if ( ev
.ControlDown() )
1128 logBuf
<< wxT(" (control down)");
1129 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1131 // you must call event skip if you want default grid processing
1137 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
1139 wxLogMessage(wxT("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
1141 // you must call event skip if you want default grid processing
1142 // (cell highlighting etc.)
1148 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
1150 const int row
= ev
.GetRowOrCol();
1152 wxLogMessage("Resized row %d, new height = %d",
1153 row
, grid
->GetRowSize(row
));
1159 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
1161 const int col
= ev
.GetRowOrCol();
1163 wxLogMessage("Resized column %d, new width = %d",
1164 col
, grid
->GetColSize(col
));
1170 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1173 if ( ev
.Selecting() )
1174 logBuf
<< wxT("Selected ");
1176 logBuf
<< wxT("Deselected ");
1177 logBuf
<< wxT("cell at row ") << ev
.GetRow()
1178 << wxT(" col ") << ev
.GetCol()
1179 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1180 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1181 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1182 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1184 //Indicate whether this column was moved
1185 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1186 logBuf
<< wxT(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1188 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1190 // you must call Skip() if you want the default processing
1191 // to occur in wxGrid
1195 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1198 if ( ev
.Selecting() )
1199 logBuf
<< wxT("Selected ");
1201 logBuf
<< wxT("Deselected ");
1202 logBuf
<< wxT("cells from row ") << ev
.GetTopRow()
1203 << wxT(" col ") << ev
.GetLeftCol()
1204 << wxT(" to row ") << ev
.GetBottomRow()
1205 << wxT(" col ") << ev
.GetRightCol()
1206 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1207 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1208 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1209 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1210 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1215 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1217 int row
= ev
.GetRow(),
1220 wxLogMessage("Value of cell at (%d, %d): about to change "
1221 "from \"%s\" to \"%s\"",
1223 grid
->GetCellValue(row
, col
), ev
.GetString());
1225 // test how vetoing works
1226 if ( ev
.GetString() == "42" )
1228 wxLogMessage("Vetoing the change.");
1236 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1238 int row
= ev
.GetRow(),
1241 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1244 grid
->GetCellValue(row
, col
), ev
.GetString());
1249 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1251 wxLogMessage(wxT("Got request to drag cell at row %d, col %d"),
1252 ev
.GetRow(), ev
.GetCol());
1257 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1260 if ( (ev
.GetCol() == 4) &&
1261 (ev
.GetRow() == 0) &&
1262 (wxMessageBox(wxT("Are you sure you wish to edit this cell"),
1263 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1269 wxLogMessage( wxT("Cell editor shown.") );
1274 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1277 if ( (ev
.GetCol() == 4) &&
1278 (ev
.GetRow() == 0) &&
1279 (wxMessageBox(wxT("Are you sure you wish to finish editing this cell"),
1280 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1286 wxLogMessage( wxT("Cell editor hidden.") );
1291 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1293 wxAboutDialogInfo aboutInfo
;
1294 aboutInfo
.SetName(wxT("wxGrid demo"));
1295 aboutInfo
.SetDescription(_("wxGrid sample program"));
1296 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1297 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1298 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1300 // this is just to force the generic version of the about
1301 // dialog under wxMSW so that it's easy to test if the grid
1302 // repaints correctly when it has lost focus and a dialog
1303 // (different from the Windows standard message box -- it doesn't
1304 // work with it for some reason) is moved over it.
1305 aboutInfo
.SetWebSite(wxT("http://www.wxwidgets.org"));
1307 wxAboutBox(aboutInfo
);
1311 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1316 void GridFrame::OnBugsTable(wxCommandEvent
& )
1318 BugsGridFrame
*frame
= new BugsGridFrame
;
1322 // ----------------------------------------------------------------------------
1323 // MyGridCellAttrProvider
1324 // ----------------------------------------------------------------------------
1326 MyGridCellAttrProvider::MyGridCellAttrProvider()
1328 m_attrForOddRows
= new wxGridCellAttr
;
1329 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1332 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1334 m_attrForOddRows
->DecRef();
1337 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1338 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1340 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1346 attr
= m_attrForOddRows
;
1351 if ( !attr
->HasBackgroundColour() )
1353 wxGridCellAttr
*attrNew
= attr
->Clone();
1356 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1364 void GridFrame::OnVTable(wxCommandEvent
& )
1366 static long s_sizeGrid
= 10000;
1368 s_sizeGrid
= wxGetNumberFromUser(wxT("Size of the table to create"),
1370 wxT("wxGridDemo question"),
1374 if ( s_sizeGrid
!= -1 )
1376 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1381 // ----------------------------------------------------------------------------
1382 // MyGridCellRenderer
1383 // ----------------------------------------------------------------------------
1385 // do something that the default renderer doesn't here just to show that it is
1386 // possible to alter the appearance of the cell beyond what the attributes
1388 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1389 wxGridCellAttr
& attr
,
1395 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1397 dc
.SetPen(*wxGREEN_PEN
);
1398 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1399 dc
.DrawEllipse(rect
);
1402 // ============================================================================
1403 // BigGridFrame and BigGridTable: Sample of a non-standard table
1404 // ============================================================================
1406 BigGridFrame::BigGridFrame(long sizeGrid
)
1407 : wxFrame(NULL
, wxID_ANY
, wxT("Plugin Virtual Table"),
1408 wxDefaultPosition
, wxSize(500, 450))
1410 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1411 m_table
= new BigGridTable(sizeGrid
);
1413 // VZ: I don't understand why this slows down the display that much,
1414 // must profile it...
1415 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1417 m_grid
->SetTable(m_table
, true);
1419 #if defined __WXMOTIF__
1420 // MB: the grid isn't getting a sensible default size under wxMotif
1422 GetClientSize( &cw
, &ch
);
1423 m_grid
->SetSize( cw
, ch
);
1427 // ============================================================================
1428 // BugsGridFrame: a "realistic" table
1429 // ============================================================================
1431 // ----------------------------------------------------------------------------
1433 // ----------------------------------------------------------------------------
1456 static const wxString severities
[] =
1465 static struct BugsGridData
1471 wxChar platform
[12];
1473 } gs_dataBugsGrid
[] =
1475 { 18, wxT("foo doesn't work"), Sev_Major
, 1, wxT("wxMSW"), true },
1476 { 27, wxT("bar crashes"), Sev_Critical
, 1, wxT("all"), false },
1477 { 45, wxT("printing is slow"), Sev_Minor
, 3, wxT("wxMSW"), true },
1478 { 68, wxT("Rectangle() fails"), Sev_Normal
, 1, wxT("wxMSW"), false },
1481 static const wxChar
*headers
[Col_Max
] =
1491 // ----------------------------------------------------------------------------
1493 // ----------------------------------------------------------------------------
1495 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1501 return wxGRID_VALUE_NUMBER
;;
1504 // fall thorugh (TODO should be a list)
1507 return wxString::Format(wxT("%s:80"), wxGRID_VALUE_STRING
);
1510 return wxString::Format(wxT("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1513 return wxGRID_VALUE_BOOL
;
1516 wxFAIL_MSG(wxT("unknown column"));
1518 return wxEmptyString
;
1521 int BugsGridTable::GetNumberRows()
1523 return WXSIZEOF(gs_dataBugsGrid
);
1526 int BugsGridTable::GetNumberCols()
1531 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1536 wxString
BugsGridTable::GetValue( int row
, int col
)
1538 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1543 return wxString::Format(wxT("%d"), gd
.id
);
1546 return wxString::Format(wxT("%d"), gd
.prio
);
1549 return gd
.opened
? wxT("1") : wxT("0");
1552 return severities
[gd
.severity
];
1561 return wxEmptyString
;
1564 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1566 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1573 wxFAIL_MSG(wxT("unexpected column"));
1579 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1581 if ( severities
[n
] == value
)
1583 gd
.severity
= (Severity
)n
;
1588 if ( n
== WXSIZEOF(severities
) )
1590 wxLogWarning(wxT("Invalid severity value '%s'."),
1592 gd
.severity
= Sev_Normal
;
1598 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1602 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1608 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1610 const wxString
& typeName
)
1612 if ( typeName
== wxGRID_VALUE_STRING
)
1616 else if ( typeName
== wxGRID_VALUE_BOOL
)
1618 return col
== Col_Opened
;
1620 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1622 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1630 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1632 return CanGetValueAs(row
, col
, typeName
);
1635 long BugsGridTable::GetValueAsLong( int row
, int col
)
1637 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1651 wxFAIL_MSG(wxT("unexpected column"));
1656 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1658 if ( col
== Col_Opened
)
1660 return gs_dataBugsGrid
[row
].opened
;
1664 wxFAIL_MSG(wxT("unexpected column"));
1670 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1672 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1681 wxFAIL_MSG(wxT("unexpected column"));
1685 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1687 if ( col
== Col_Opened
)
1689 gs_dataBugsGrid
[row
].opened
= value
;
1693 wxFAIL_MSG(wxT("unexpected column"));
1697 wxString
BugsGridTable::GetColLabelValue( int col
)
1699 return headers
[col
];
1702 // ----------------------------------------------------------------------------
1704 // ----------------------------------------------------------------------------
1706 BugsGridFrame::BugsGridFrame()
1707 : wxFrame(NULL
, wxID_ANY
, wxT("Bugs table"))
1709 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1710 wxGridTableBase
*table
= new BugsGridTable();
1711 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1712 grid
->SetTable(table
, true);
1714 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1715 *attrRangeEditor
= new wxGridCellAttr
,
1716 *attrCombo
= new wxGridCellAttr
;
1718 attrRO
->SetReadOnly();
1719 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1720 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1723 grid
->SetColAttr(Col_Id
, attrRO
);
1724 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1725 grid
->SetColAttr(Col_Severity
, attrCombo
);
1728 SetClientSize(grid
->GetSize());
1731 // ============================================================================
1732 // TabularGrid: grid used for display of tabular data
1733 // ============================================================================
1735 class TabularGridTable
: public wxGridTableBase
1752 TabularGridTable() { m_sortOrder
= NULL
; }
1754 virtual int GetNumberRows() { return ROW_MAX
; }
1755 virtual int GetNumberCols() { return COL_MAX
; }
1757 virtual wxString
GetValue(int row
, int col
)
1760 row
= m_sortOrder
[row
];
1766 return GetNameOrExt(row
, col
);
1769 return wxString::Format("%lu", GetSize(row
));
1772 return GetDate(row
).FormatDate();
1776 wxFAIL_MSG( "unknown column" );
1782 virtual void SetValue(int, int, const wxString
&)
1784 wxFAIL_MSG( "shouldn't be called" );
1787 virtual wxString
GetColLabelValue(int col
)
1789 // notice that column parameter here always refers to the internal
1790 // column index, independently of its position on the screen
1791 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1792 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1797 virtual void SetColLabelValue(int, const wxString
&)
1799 wxFAIL_MSG( "shouldn't be called" );
1802 void Sort(int col
, bool ascending
)
1804 // we hardcode all sorting orders for simplicity here
1805 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1807 // descending ascending
1808 { { 2, 1, 0 }, { 0, 1, 2 } },
1809 { { 2, 1, 0 }, { 0, 1, 2 } },
1810 { { 2, 1, 0 }, { 0, 1, 2 } },
1811 { { 1, 0, 2 }, { 2, 0, 1 } },
1814 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1818 wxString
GetNameOrExt(int row
, int col
) const
1821 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1822 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1824 const wxString
s(names
[row
]);
1825 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1828 unsigned long GetSize(int row
) const
1830 static const unsigned long
1831 sizes
[] = { 412, 604, 40774 };
1832 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1837 wxDateTime
GetDate(int row
) const
1840 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1841 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1844 dt
.ParseISODate(dates
[row
]);
1851 // specialized text control for column indexes entry
1852 class ColIndexEntry
: public wxTextCtrl
1855 ColIndexEntry(wxWindow
*parent
)
1856 : wxTextCtrl(parent
, wxID_ANY
, "")
1858 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1864 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1874 virtual wxSize
DoGetBestSize() const
1876 wxSize size
= wxTextCtrl::DoGetBestSize();
1877 size
.x
= 3*GetCharWidth();
1882 class TabularGridFrame
: public wxFrame
1890 Id_Check_UseNativeHeader
,
1891 Id_Check_DrawNativeLabels
,
1892 Id_Check_ShowRowLabels
,
1893 Id_Check_EnableColMove
1898 void OnToggleUseNativeHeader(wxCommandEvent
&)
1900 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1903 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1905 // we don't draw labels at all, native or otherwise, if we use the
1906 // native header control
1907 event
.Enable( !m_chkUseNative
->GetValue() );
1910 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1912 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1915 void OnToggleShowRowLabels(wxCommandEvent
&)
1917 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1922 void OnToggleColMove(wxCommandEvent
&)
1924 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1927 void OnShowHideColumn(wxCommandEvent
& event
)
1929 int col
= m_txtColShowHide
->GetCol();
1932 m_grid
->SetColSize(col
,
1933 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1935 UpdateOrderAndVisibility();
1939 void OnMoveColumn(wxCommandEvent
&)
1941 int col
= m_txtColIndex
->GetCol();
1942 int pos
= m_txtColPos
->GetCol();
1943 if ( col
== -1 || pos
== -1 )
1946 m_grid
->SetColPos(col
, pos
);
1948 UpdateOrderAndVisibility();
1951 void OnResetColumnOrder(wxCommandEvent
&)
1953 m_grid
->ResetColPos();
1955 UpdateOrderAndVisibility();
1958 void OnGridColSort(wxGridEvent
& event
)
1960 const int col
= event
.GetCol();
1961 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
1962 m_grid
->IsSortOrderAscending()));
1965 void OnGridColMove(wxGridEvent
& event
)
1967 // can't update it yet as the order hasn't been changed, so do it a bit
1969 m_shouldUpdateOrder
= true;
1974 void OnGridColSize(wxGridSizeEvent
& event
)
1976 // we only catch this event to react to the user showing or hiding this
1977 // column using the header control menu and not because we're
1978 // interested in column resizing
1979 UpdateOrderAndVisibility();
1984 void OnIdle(wxIdleEvent
& event
)
1986 if ( m_shouldUpdateOrder
)
1988 m_shouldUpdateOrder
= false;
1989 UpdateOrderAndVisibility();
1995 void UpdateOrderAndVisibility()
1998 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
2000 const int col
= m_grid
->GetColAt(pos
);
2001 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
2012 m_statOrder
->SetLabel(s
);
2017 TabularGridTable
*m_table
;
2018 wxCheckBox
*m_chkUseNative
,
2020 *m_chkShowRowLabels
,
2021 *m_chkEnableColMove
;
2023 ColIndexEntry
*m_txtColIndex
,
2027 wxStaticText
*m_statOrder
;
2029 // fla for EVT_IDLE handler
2030 bool m_shouldUpdateOrder
;
2032 wxDECLARE_NO_COPY_CLASS(TabularGridFrame
);
2033 DECLARE_EVENT_TABLE()
2036 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
2037 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
2038 TabularGridFrame::OnToggleUseNativeHeader
)
2039 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
2040 TabularGridFrame::OnToggleDrawNativeLabels
)
2041 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
2042 TabularGridFrame::OnToggleShowRowLabels
)
2043 EVT_CHECKBOX(Id_Check_EnableColMove
,
2044 TabularGridFrame::OnToggleColMove
)
2046 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
2047 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
2049 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
2050 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
2051 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
2052 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
2054 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
2055 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
2056 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
2058 EVT_IDLE(TabularGridFrame::OnIdle
)
2061 TabularGridFrame::TabularGridFrame()
2062 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
2064 m_shouldUpdateOrder
= false;
2066 wxPanel
* const panel
= new wxPanel(this);
2068 // create and initialize the grid with the specified data
2069 m_table
= new TabularGridTable
;
2070 m_grid
= new wxGrid(panel
, wxID_ANY
,
2071 wxDefaultPosition
, wxDefaultSize
,
2072 wxBORDER_STATIC
| wxWANTS_CHARS
);
2073 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
2075 m_grid
->EnableDragColMove();
2076 m_grid
->UseNativeColHeader();
2077 m_grid
->HideRowLabels();
2079 // add it and the other controls to the frame
2080 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
2081 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
2083 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
2085 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
2086 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
2087 "&Use native header");
2088 m_chkUseNative
->SetValue(true);
2089 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
2091 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
2092 "&Draw native column labels");
2093 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
2095 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
2096 "Show &row labels");
2097 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
2099 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
2100 "Allow column re&ordering");
2101 m_chkEnableColMove
->SetValue(true);
2102 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
2103 sizerControls
->Add(sizerStyles
);
2105 sizerControls
->AddSpacer(10);
2107 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
2108 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
2110 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
2111 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
2113 m_txtColIndex
= new ColIndexEntry(panel
);
2114 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
2115 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
2116 m_txtColPos
= new ColIndexEntry(panel
);
2117 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
2118 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
2120 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
2122 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
2123 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
2125 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
2126 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
2127 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
2128 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
2130 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
2131 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
2133 m_txtColShowHide
= new ColIndexEntry(panel
);
2134 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
2135 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
2136 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
2137 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
2139 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
2141 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
2143 panel
->SetSizer(sizerTop
);
2145 SetClientSize(panel
->GetBestSize());
2146 SetSizeHints(GetSize());
2151 void GridFrame::OnTabularTable(wxCommandEvent
&)
2153 new TabularGridFrame
;
2156 // Example using wxGrid::Render
2157 // Displays a preset selection or, if it exists, a selection block
2158 // Draws the selection to a wxBitmap and displays the bitmap
2159 void GridFrame::OnGridRender( wxCommandEvent
& event
)
2161 int styleRender
= 0, i
;
2162 bool useLometric
= false, defSize
= false;
2164 wxSize
sizeMargin( 0, 0 );
2165 wxPoint
pointOrigin( 0, 0 );
2167 wxMenu
* menu
= GetMenuBar()->GetMenu( 0 );
2168 wxMenuItem
* menuItem
= menu
->FindItem( ID_RENDER_ROW_LABEL
);
2169 menu
= menuItem
->GetMenu();
2171 if ( menu
->FindItem( ID_RENDER_ROW_LABEL
)->IsChecked() )
2172 styleRender
|= wxGRID_DRAW_ROWS_HEADER
;
2173 if ( menu
->FindItem( ID_RENDER_COL_LABEL
)->IsChecked() )
2174 styleRender
|= wxGRID_DRAW_COLS_HEADER
;
2175 if ( menu
->FindItem( ID_RENDER_GRID_LINES
)->IsChecked() )
2176 styleRender
|= wxGRID_DRAW_CELL_LINES
;
2177 if ( menu
->FindItem( ID_RENDER_GRID_BORDER
)->IsChecked() )
2178 styleRender
|= wxGRID_DRAW_BOX_RECT
;
2179 if ( menu
->FindItem( ID_RENDER_SELECT_HLIGHT
)->IsChecked() )
2180 styleRender
|= wxGRID_DRAW_SELECTION
;
2181 if ( menu
->FindItem( ID_RENDER_LOMETRIC
)->IsChecked() )
2183 if ( menu
->FindItem( ID_RENDER_MARGIN
)->IsChecked() )
2185 pointOrigin
.x
+= 50;
2186 pointOrigin
.y
+= 50;
2187 sizeMargin
.IncBy( 50 );
2189 if ( menu
->FindItem( ID_RENDER_ZOOM
)->IsChecked() )
2191 if ( menu
->FindItem( ID_RENDER_DEFAULT_SIZE
)->IsChecked() )
2194 // init render area coords with a default row and col selection
2195 wxGridCellCoords
topLeft( 0, 0 ), bottomRight( 8, 6 );
2196 // check whether we are printing a block selection
2197 // other selection types not catered for here
2198 if ( event
.GetId() == ID_RENDER_COORDS
)
2200 topLeft
.SetCol( 6 );
2201 topLeft
.SetRow( 4 );
2202 bottomRight
.SetCol( 15 );
2203 bottomRight
.SetRow( 29 );
2205 else if ( grid
->IsSelection() && grid
->GetSelectionBlockTopLeft().Count() )
2207 wxGridCellCoordsArray cells
= grid
->GetSelectionBlockTopLeft();
2208 if ( grid
->GetSelectionBlockBottomRight().Count() )
2210 cells
.Add( grid
->GetSelectionBlockBottomRight()[ 0 ] );
2211 topLeft
.Set( cells
[ 0 ].GetRow(),
2212 cells
[ 0 ].GetCol() );
2213 bottomRight
.Set( cells
[ 1 ].GetRow(),
2214 cells
[ 1 ].GetCol() );
2219 wxSize
sizeRender( 0, 0 );
2220 wxGridSizesInfo sizeinfo
= grid
->GetColSizes();
2221 for ( i
= topLeft
.GetCol(); i
<= bottomRight
.GetCol(); i
++ )
2223 sizeRender
.x
+= sizeinfo
.GetSize( i
);
2227 sizeinfo
= grid
->GetRowSizes();
2228 for ( i
= topLeft
.GetRow(); i
<= bottomRight
.GetRow(); i
++ )
2230 sizeRender
.y
+= sizeinfo
.GetSize( i
);
2233 if ( styleRender
& wxGRID_DRAW_ROWS_HEADER
)
2234 sizeRender
.x
+= grid
->GetRowLabelSize();
2235 if ( styleRender
& wxGRID_DRAW_COLS_HEADER
)
2236 sizeRender
.y
+= grid
->GetColLabelSize();
2238 sizeRender
.x
*= zoom
;
2239 sizeRender
.y
*= zoom
;
2241 // delete any existing render frame and create new one
2242 wxWindow
* win
= FindWindow( "frameRender" );
2246 wxFrame
* frame
= new wxFrame( this, wxID_ANY
, "Grid Render" );
2247 frame
->SetClientSize( 780, 400 );
2248 frame
->SetName( "frameRender" );
2250 wxPanel
* canvas
= new wxPanel( frame
, wxID_ANY
);
2252 // make bitmap large enough for margins if any
2254 sizeRender
.IncBy( sizeMargin
* 2 );
2256 sizeRender
.IncBy( sizeMargin
);
2258 wxBitmap
bmp( sizeRender
);
2259 // don't leave it larger or drawing will be scaled
2260 sizeRender
.DecBy( sizeMargin
* 2 );
2262 wxMemoryDC
memDc(bmp
);
2264 // default row labels have no background colour so set background
2265 memDc
.SetBackground( wxBrush( canvas
->GetBackgroundColour() ) );
2268 // convert sizeRender to mapping mode units if necessary
2271 memDc
.SetMapMode( wxMM_LOMETRIC
);
2272 sizeRender
.x
= memDc
.DeviceToLogicalXRel( sizeRender
.x
);
2273 sizeRender
.y
= memDc
.DeviceToLogicalYRel( sizeRender
.y
);
2276 // pass wxDefaultSize if menu item is checked
2278 sizeRender
= wxDefaultSize
;
2280 grid
->Render( memDc
,
2283 topLeft
, bottomRight
,
2284 wxGridRenderStyle( styleRender
) );
2288 canvas
->Connect( wxEVT_PAINT
,
2289 wxPaintEventHandler(GridFrame::OnRenderPaint
),
2296 void GridFrame::OnRenderPaint( wxPaintEvent
& event
)
2298 wxPanel
* canvas
= ( wxPanel
* )event
.GetEventObject();
2299 wxPaintDC
dc( canvas
);
2300 canvas
->PrepareDC( dc
);
2302 if ( !m_gridBitmap
.IsOk() )
2305 wxMemoryDC
memDc( m_gridBitmap
);
2308 m_gridBitmap
.GetWidth(),
2309 m_gridBitmap
.GetHeight(),