1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
5 // Modified by: Santiago Palacios
7 // Copyright: (c) Michael Bedward, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/colordlg.h"
31 #include "wx/fontdlg.h"
32 #include "wx/numdlg.h"
33 #include "wx/aboutdlg.h"
36 #include "wx/headerctrl.h"
37 #include "wx/generic/gridctrl.h"
38 #include "wx/generic/grideditors.h"
42 #ifndef wxHAS_IMAGES_IN_RESOURCES
43 #include "../sample.xpm"
46 // Custom renderer that renders column header cells without borders and in
48 class CustomColumnHeaderRenderer
: public wxGridColumnHeaderRenderer
51 CustomColumnHeaderRenderer(const wxColour
& colFg
, const wxColour
& colBg
)
57 virtual void DrawLabel(const wxGrid
& WXUNUSED(grid
),
59 const wxString
& value
,
63 int WXUNUSED(textOrientation
)) const
65 dc
.SetTextForeground(m_colFg
);
66 dc
.SetFont(wxITALIC_FONT
->Bold());
67 dc
.DrawLabel(value
, rect
, horizAlign
| vertAlign
);
70 virtual void DrawBorder(const wxGrid
& WXUNUSED(grid
),
74 dc
.SetPen(*wxTRANSPARENT_PEN
);
75 dc
.SetBrush(wxBrush(m_colBg
));
76 dc
.DrawRectangle(rect
);
80 const wxColour m_colFg
, m_colBg
;
82 wxDECLARE_NO_COPY_CLASS(CustomColumnHeaderRenderer
);
85 // And a custom attributes provider which uses custom column header renderer
87 class CustomColumnHeadersProvider
: public wxGridCellAttrProvider
90 // by default custom column renderer is not used, call
91 // UseCustomColHeaders() to enable it
92 CustomColumnHeadersProvider()
93 : m_customOddRenderer(*wxYELLOW
, *wxBLUE
),
94 m_customEvenRenderer(*wxWHITE
, *wxBLACK
),
99 // enable or disable the use of custom renderer for column headers
100 void UseCustomColHeaders(bool use
= true) { m_useCustom
= use
; }
103 virtual const wxGridColumnHeaderRenderer
& GetColumnHeaderRenderer(int col
)
105 // if enabled, use custom renderers
108 // and use different ones for odd and even columns -- just to show
110 return col
% 2 ? m_customOddRenderer
: m_customEvenRenderer
;
113 return wxGridCellAttrProvider::GetColumnHeaderRenderer(col
);
117 CustomColumnHeaderRenderer m_customOddRenderer
,
118 m_customEvenRenderer
;
122 wxDECLARE_NO_COPY_CLASS(CustomColumnHeadersProvider
);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 IMPLEMENT_APP( GridApp
)
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 bool GridApp::OnInit()
141 GridFrame
*frame
= new GridFrame
;
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
152 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
153 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
154 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
155 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
156 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
157 EVT_MENU( ID_TOGGLECOLMOVING
, GridFrame::ToggleColMoving
)
158 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
159 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
160 EVT_MENU( ID_COLNATIVEHEADER
, GridFrame::SetNativeColHeader
)
161 EVT_MENU( ID_COLDEFAULTHEADER
, GridFrame::SetDefaultColHeader
)
162 EVT_MENU( ID_COLCUSTOMHEADER
, GridFrame::SetCustomColHeader
)
163 EVT_MENU_RANGE( ID_TAB_STOP
, ID_TAB_LEAVE
, GridFrame::SetTabBehaviour
)
164 EVT_MENU( ID_TAB_CUSTOM
, GridFrame::SetTabCustomHandler
)
165 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
166 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
167 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
168 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
169 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
170 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
171 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
172 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
173 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
174 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
175 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
176 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
177 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
178 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
179 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
180 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
181 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
182 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
183 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
184 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
185 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
187 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
188 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
190 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
191 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
192 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
193 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
194 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
196 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
197 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
198 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
199 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
200 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
201 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
202 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
203 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
204 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
206 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
207 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
208 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
209 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
210 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
211 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
212 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
214 EVT_MENU( ID_HIDECOL
, GridFrame::HideCol
)
215 EVT_MENU( ID_SHOWCOL
, GridFrame::ShowCol
)
216 EVT_MENU( ID_HIDEROW
, GridFrame::HideRow
)
217 EVT_MENU( ID_SHOWROW
, GridFrame::ShowRow
)
219 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
220 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
222 EVT_MENU( wxID_PRINT
, GridFrame::OnGridRender
)
223 EVT_MENU( ID_RENDER_COORDS
, GridFrame::OnGridRender
)
225 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
226 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
227 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
228 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
229 EVT_GRID_COL_AUTO_SIZE( GridFrame::OnColAutoSize
)
230 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
231 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
232 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
233 EVT_GRID_CELL_CHANGED( GridFrame::OnCellValueChanged
)
234 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
236 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
237 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
241 GridFrame::GridFrame()
242 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets grid class demo"),
246 SetIcon(wxICON(sample
));
248 wxMenu
*fileMenu
= new wxMenu
;
249 fileMenu
->Append( ID_VTABLE
, wxT("&Virtual table test\tCtrl-V"));
250 fileMenu
->Append( ID_BUGS_TABLE
, wxT("&Bugs table test\tCtrl-B"));
251 fileMenu
->Append( ID_TABULAR_TABLE
, wxT("&Tabular table test\tCtrl-T"));
252 fileMenu
->AppendSeparator();
254 wxMenu
* setupMenu
= new wxMenu
;
256 item
= setupMenu
->AppendCheckItem( ID_RENDER_ROW_LABEL
,
257 "Render row labels" );
259 item
= setupMenu
->AppendCheckItem( ID_RENDER_COL_LABEL
,
260 "Render column labels" );
262 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_LINES
,
263 "Render grid cell lines" );
265 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_BORDER
,
268 item
= setupMenu
->AppendCheckItem( ID_RENDER_SELECT_HLIGHT
,
269 "Render selection highlight" );
270 setupMenu
->AppendSeparator();
271 setupMenu
->AppendCheckItem( ID_RENDER_LOMETRIC
,
272 "Use LOMETRIC mapping mode" );
273 setupMenu
->AppendCheckItem( ID_RENDER_DEFAULT_SIZE
,
274 "Use wxDefaultSize" );
275 setupMenu
->AppendCheckItem( ID_RENDER_MARGIN
,
276 "Logical 50 unit margin" );
277 setupMenu
->AppendCheckItem( ID_RENDER_ZOOM
,
280 fileMenu
->AppendSubMenu( setupMenu
, "Render setup" );
281 fileMenu
->Append( wxID_PRINT
, "Render" );
282 fileMenu
->Append( ID_RENDER_COORDS
, "Render G5:P30" );
284 fileMenu
->AppendSeparator();
285 fileMenu
->Append( wxID_EXIT
, wxT("E&xit\tAlt-X") );
287 wxMenu
*viewMenu
= new wxMenu
;
288 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
289 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
290 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
291 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
292 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
293 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
294 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
295 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
296 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
297 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
298 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
299 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
300 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
301 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
302 viewMenu
->Append(ID_HIDECOL
, "&Hide column A");
303 viewMenu
->Append(ID_SHOWCOL
, "&Show column A");
304 viewMenu
->Append(ID_HIDEROW
, "&Hide row 2");
305 viewMenu
->Append(ID_SHOWROW
, "&Show row 2");
306 wxMenu
*rowLabelMenu
= new wxMenu
;
308 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("R&ow label alignment"),
310 wxT("Change alignment of row labels") );
312 rowLabelMenu
->AppendRadioItem( ID_ROWLABELHORIZALIGN
, wxT("&Horizontal") );
313 rowLabelMenu
->AppendRadioItem( ID_ROWLABELVERTALIGN
, wxT("&Vertical") );
315 wxMenu
*colLabelMenu
= new wxMenu
;
317 viewMenu
->Append( ID_COLLABELALIGN
, wxT("Col l&abel alignment"),
319 wxT("Change alignment of col labels") );
321 colLabelMenu
->AppendRadioItem( ID_COLLABELHORIZALIGN
, wxT("&Horizontal") );
322 colLabelMenu
->AppendRadioItem( ID_COLLABELVERTALIGN
, wxT("&Vertical") );
324 wxMenu
*colHeaderMenu
= new wxMenu
;
326 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("Col header style"),
328 wxT("Change style of col header") );
330 colHeaderMenu
->AppendRadioItem( ID_COLDEFAULTHEADER
, wxT("&Default") );
331 colHeaderMenu
->AppendRadioItem( ID_COLNATIVEHEADER
, wxT("&Native") );
332 colHeaderMenu
->AppendRadioItem( ID_COLCUSTOMHEADER
, wxT("&Custom") );
334 wxMenu
*tabBehaviourMenu
= new wxMenu
;
335 tabBehaviourMenu
->AppendRadioItem(ID_TAB_STOP
, "&Stop at the boundary");
336 tabBehaviourMenu
->AppendRadioItem(ID_TAB_WRAP
, "&Wrap at the boundary");
337 tabBehaviourMenu
->AppendRadioItem(ID_TAB_LEAVE
, "&Leave the grid");
338 tabBehaviourMenu
->AppendRadioItem(ID_TAB_CUSTOM
, "&Custom tab handler");
339 viewMenu
->AppendSubMenu(tabBehaviourMenu
, "&Tab behaviour");
341 wxMenu
*colMenu
= new wxMenu
;
342 colMenu
->Append( ID_SETLABELCOLOUR
, wxT("Set &label colour...") );
343 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, wxT("Set label &text colour...") );
344 colMenu
->Append( ID_SETLABEL_FONT
, wxT("Set label fo&nt...") );
345 colMenu
->Append( ID_GRIDLINECOLOUR
, wxT("&Grid line colour...") );
346 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, wxT("Set cell &foreground colour...") );
347 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, wxT("Set cell &background colour...") );
349 wxMenu
*editMenu
= new wxMenu
;
350 editMenu
->Append( ID_INSERTROW
, wxT("Insert &row") );
351 editMenu
->Append( ID_INSERTCOL
, wxT("Insert &column") );
352 editMenu
->Append( ID_DELETEROW
, wxT("Delete selected ro&ws") );
353 editMenu
->Append( ID_DELETECOL
, wxT("Delete selected co&ls") );
354 editMenu
->Append( ID_CLEARGRID
, wxT("Cl&ear grid cell contents") );
356 wxMenu
*selectMenu
= new wxMenu
;
357 selectMenu
->Append( ID_SELECT_UNSELECT
, wxT("Add new cells to the selection"),
358 wxT("When off, old selection is deselected before ")
359 wxT("selecting the new cells"), wxITEM_CHECK
);
360 selectMenu
->AppendSeparator();
361 selectMenu
->Append( ID_SELECT_ALL
, wxT("Select all"));
362 selectMenu
->Append( ID_SELECT_ROW
, wxT("Select row 2"));
363 selectMenu
->Append( ID_SELECT_COL
, wxT("Select col 2"));
364 selectMenu
->Append( ID_SELECT_CELL
, wxT("Select cell (3, 1)"));
365 selectMenu
->AppendSeparator();
366 selectMenu
->Append( ID_DESELECT_ALL
, wxT("Deselect all"));
367 selectMenu
->Append( ID_DESELECT_ROW
, wxT("Deselect row 2"));
368 selectMenu
->Append( ID_DESELECT_COL
, wxT("Deselect col 2"));
369 selectMenu
->Append( ID_DESELECT_CELL
, wxT("Deselect cell (3, 1)"));
370 wxMenu
*selectionMenu
= new wxMenu
;
371 selectMenu
->Append( ID_CHANGESEL
, wxT("Change &selection mode"),
373 wxT("Change selection mode") );
375 selectionMenu
->Append( ID_SELCELLS
, wxT("Select &cells") );
376 selectionMenu
->Append( ID_SELROWS
, wxT("Select &rows") );
377 selectionMenu
->Append( ID_SELCOLS
, wxT("Select col&umns") );
378 selectionMenu
->Append( ID_SELROWSORCOLS
, wxT("Select rows &or columns") );
380 wxMenu
*autosizeMenu
= new wxMenu
;
381 autosizeMenu
->Append( ID_SIZE_ROW
, wxT("Selected &row data") );
382 autosizeMenu
->Append( ID_SIZE_COL
, wxT("Selected &column data") );
383 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, wxT("Selected row la&bel") );
384 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, wxT("Selected column &label") );
385 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, wxT("Column la&bels") );
386 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, wxT("Row label&s") );
387 autosizeMenu
->Append( ID_SIZE_GRID
, wxT("Entire &grid") );
389 wxMenu
*helpMenu
= new wxMenu
;
390 helpMenu
->Append( wxID_ABOUT
, wxT("&About wxGrid demo") );
392 wxMenuBar
*menuBar
= new wxMenuBar
;
393 menuBar
->Append( fileMenu
, wxT("&File") );
394 menuBar
->Append( viewMenu
, wxT("&Grid") );
395 menuBar
->Append( colMenu
, wxT("&Colours") );
396 menuBar
->Append( editMenu
, wxT("&Edit") );
397 menuBar
->Append( selectMenu
, wxT("&Select") );
398 menuBar
->Append( autosizeMenu
, wxT("&Autosize") );
399 menuBar
->Append( helpMenu
, wxT("&Help") );
401 SetMenuBar( menuBar
);
405 grid
= new wxGrid( this,
408 wxSize( 400, 300 ) );
412 int gridW
= 600, gridH
= 300;
413 int logW
= gridW
, logH
= 100;
415 logWin
= new wxTextCtrl( this,
418 wxPoint( 0, gridH
+ 20 ),
419 wxSize( logW
, logH
),
422 logger
= new wxLogTextCtrl( logWin
);
423 m_logOld
= wxLog::SetActiveTarget( logger
);
424 wxLog::DisableTimestamp();
427 // this will create a grid and, by default, an associated grid
429 grid
->CreateGrid( 0, 0 );
431 grid
->GetTable()->SetAttrProvider(new CustomColumnHeadersProvider());
433 grid
->AppendRows(100);
434 grid
->AppendCols(100);
436 int ir
= grid
->GetNumberRows();
437 grid
->DeleteRows(0, ir
);
438 grid
->AppendRows(ir
);
440 grid
->SetRowSize( 0, 60 );
441 grid
->SetCellValue( 0, 0, wxT("Ctrl+Home\nwill go to\nthis cell") );
443 grid
->SetCellValue( 0, 1, wxT("A long piece of text to demonstrate wrapping.") );
444 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
445 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
447 grid
->SetCellValue( 0, 2, wxT("Blah") );
448 grid
->SetCellValue( 0, 3, wxT("Read only") );
449 grid
->SetReadOnly( 0, 3 );
451 grid
->SetCellValue( 0, 4, wxT("Can veto edit this cell") );
453 grid
->SetColSize(10, 150);
454 wxString longtext
= wxT("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\n");
455 longtext
+= wxT("With tabs :\n");
456 longtext
+= wxT("Home,\t\thome\t\t\tagain\n");
457 longtext
+= wxT("Long word at start :\n");
458 longtext
+= wxT("ILikeToBeHereWhen I can\n");
459 longtext
+= wxT("Long word in the middle :\n");
460 longtext
+= wxT("When IComeHome,ColdAnd tired\n");
461 longtext
+= wxT("Long last word :\n");
462 longtext
+= wxT("It's GoodToWarmMyBonesBesideTheFire");
463 grid
->SetCellValue( 0, 10, longtext
);
464 grid
->SetCellRenderer(0 , 10, new wxGridCellAutoWrapStringRenderer
);
465 grid
->SetCellEditor( 0, 10 , new wxGridCellAutoWrapStringEditor
);
466 grid
->SetCellValue( 0, 11, wxT("K1 cell editor blocker") );
468 grid
->SetCellValue( 0, 5, wxT("Press\nCtrl+arrow\nto skip over\ncells") );
470 grid
->SetRowSize( 99, 60 );
471 grid
->SetCellValue(98, 98, "Test background colour setting");
472 grid
->SetCellBackgroundColour(98, 99, wxColour(255, 127, 127));
473 grid
->SetCellBackgroundColour(99, 98, wxColour(255, 127, 127));
474 grid
->SetCellValue( 99, 99, wxT("Ctrl+End\nwill go to\nthis cell") );
475 grid
->SetCellValue( 1, 0, wxT("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
477 grid
->SetCellTextColour(1, 2, *wxRED
);
478 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
480 grid
->SetCellValue( 1, 4, wxT("I'm in the middle"));
482 grid
->SetCellValue(2, 2, wxT("red"));
484 grid
->SetCellTextColour(2, 2, *wxRED
);
485 grid
->SetCellValue(3, 3, wxT("green on grey"));
486 grid
->SetCellTextColour(3, 3, *wxGREEN
);
487 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
489 grid
->SetCellValue(4, 4, wxT("a weird looking cell"));
490 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
491 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
493 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
494 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
495 grid
->SetCellBackgroundColour(3, 0, wxColour(255, 127, 127));
497 wxGridCellAttr
*attr
;
498 attr
= new wxGridCellAttr
;
499 attr
->SetTextColour(*wxBLUE
);
500 grid
->SetColAttr(5, attr
);
501 attr
= new wxGridCellAttr
;
502 attr
->SetBackgroundColour(*wxRED
);
503 grid
->SetRowAttr(5, attr
);
505 grid
->SetCellValue(2, 4, wxT("a wider column"));
506 grid
->SetColSize(4, 120);
507 grid
->SetColMinimalWidth(4, 120);
509 grid
->SetCellTextColour(5, 8, *wxGREEN
);
510 grid
->SetCellValue(5, 8, wxT("Bg from row attr\nText col from cell attr"));
511 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"));
513 // Some numeric columns with different formatting.
514 grid
->SetColFormatFloat(6);
515 grid
->SetCellValue(0, 6, "Default\nfloat format");
516 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 3.1415));
517 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 1415.0));
518 grid
->SetCellValue(3, 6, wxString::Format(wxT("%g"), 12345.67890));
520 grid
->SetColFormatFloat(7, 6, 2);
521 grid
->SetCellValue(0, 7, "Width 6\nprecision 2");
522 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 3.1415));
523 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 1415.0));
524 grid
->SetCellValue(3, 7, wxString::Format(wxT("%g"), 12345.67890));
526 grid
->SetColFormatCustom(8,
527 wxString::Format("%s:%i,%i,%s", wxGRID_VALUE_FLOAT
, -1, 4, "g"));
528 grid
->SetCellValue(0, 8, "Compact\nformat");
529 grid
->SetCellValue(1, 8, wxT("31415e-4"));
530 grid
->SetCellValue(2, 8, wxT("1415"));
531 grid
->SetCellValue(3, 8, wxT("123456789e-4"));
533 grid
->SetColFormatNumber(9);
534 grid
->SetCellValue(0, 9, "Integer\ncolumn");
535 grid
->SetCellValue(1, 9, "17");
536 grid
->SetCellValue(2, 9, "0");
537 grid
->SetCellValue(3, 9, "-666");
538 grid
->SetCellAlignment(3, 9, wxALIGN_CENTRE
, wxALIGN_TOP
);
539 grid
->SetCellValue(3, 10, "<- This numeric cell should be centred");
541 const wxString choices
[] =
543 wxT("Please select a choice"),
544 wxT("This takes two cells"),
545 wxT("Another choice"),
547 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
548 grid
->SetCellSize(4, 0, 1, 2);
549 grid
->SetCellValue(4, 0, choices
[0]);
550 grid
->SetCellOverflow(4, 0, false);
552 grid
->SetCellSize(7, 1, 3, 4);
553 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
554 grid
->SetCellValue(7, 1, wxT("Big box!"));
556 // create a separator-like row: it's grey and it's non-resizable
557 grid
->DisableRowResize(10);
558 grid
->SetRowSize(10, 30);
559 attr
= new wxGridCellAttr
;
560 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
561 grid
->SetRowAttr(10, attr
);
562 grid
->SetCellValue(10, 0, "You can't resize this row interactively -- try it");
564 // this does exactly nothing except testing that SetAttr() handles NULL
565 // attributes and does reference counting correctly
566 grid
->SetAttr(11, 11, NULL
);
567 grid
->SetAttr(11, 11, new wxGridCellAttr
);
568 grid
->SetAttr(11, 11, NULL
);
570 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
576 topSizer
->Add( logWin
,
581 SetSizerAndFit( topSizer
);
588 GridFrame::~GridFrame()
591 delete wxLog::SetActiveTarget(m_logOld
);
596 void GridFrame::SetDefaults()
598 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
599 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
600 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
601 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
602 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
603 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
604 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
605 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
606 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
607 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
611 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
613 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
615 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
619 grid
->SetRowLabelSize( 0 );
624 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
626 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
628 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
632 grid
->SetColLabelSize( 0 );
637 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
640 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
644 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
646 grid
->EnableDragRowSize(
647 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
651 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
653 grid
->EnableDragColSize(
654 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
657 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
659 grid
->EnableDragColMove(
660 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
663 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
665 grid
->EnableDragGridSize(
666 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
669 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
671 grid
->EnableDragCell(
672 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
675 void GridFrame::SetNativeColHeader( wxCommandEvent
& WXUNUSED(ev
) )
677 CustomColumnHeadersProvider
* provider
=
678 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
679 provider
->UseCustomColHeaders(false);
680 grid
->SetUseNativeColLabels(true);
683 void GridFrame::SetCustomColHeader( wxCommandEvent
& WXUNUSED(ev
) )
685 CustomColumnHeadersProvider
* provider
=
686 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
687 provider
->UseCustomColHeaders(true);
688 grid
->SetUseNativeColLabels(false);
691 void GridFrame::SetDefaultColHeader( wxCommandEvent
& WXUNUSED(ev
) )
693 CustomColumnHeadersProvider
* provider
=
694 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
695 provider
->UseCustomColHeaders(false);
696 grid
->SetUseNativeColLabels(false);
700 void GridFrame::OnGridCustomTab(wxGridEvent
& event
)
702 // just for testing, make the cursor move up and down instead of the usual
704 if ( event
.ShiftDown() )
706 if ( grid
->GetGridCursorRow() > 0 )
707 grid
->MoveCursorUp( false );
711 if ( grid
->GetGridCursorRow() < grid
->GetNumberRows() - 1 )
712 grid
->MoveCursorDown( false );
716 void GridFrame::SetTabBehaviour(wxCommandEvent
& event
)
718 // To make any built-in behaviour work, we need to disable the custom TAB
719 // handler, otherwise it would be overriding them.
720 grid
->Disconnect(wxEVT_GRID_TABBING
,
721 wxGridEventHandler(GridFrame::OnGridCustomTab
));
723 grid
->SetTabBehaviour(
724 static_cast<wxGrid::TabBehaviour
>(event
.GetId() - ID_TAB_STOP
)
728 void GridFrame::SetTabCustomHandler(wxCommandEvent
&)
730 grid
->Connect(wxEVT_GRID_TABBING
,
731 wxGridEventHandler(GridFrame::OnGridCustomTab
),
736 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
738 grid
->EnableGridLines(
739 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
742 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
744 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
746 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
747 wxT("Pen Width"), 11, choices
);
749 int current
= grid
->GetCellHighlightPenWidth();
750 dlg
.SetSelection(current
);
751 if (dlg
.ShowModal() == wxID_OK
) {
752 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
756 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
758 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
760 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
761 wxT("Pen Width"), 11, choices
);
763 int current
= grid
->GetCellHighlightROPenWidth();
764 dlg
.SetSelection(current
);
765 if (dlg
.ShowModal() == wxID_OK
) {
766 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
772 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
774 grid
->AutoSizeColumns();
778 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
780 grid
->SetDefaultCellOverflow(ev
.IsChecked());
784 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
787 grid
->SetCellSize( 7, 1, 5, 5 );
789 grid
->SetCellSize( 7, 1, 1, 5 );
793 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
795 wxColourDialog
dlg( NULL
);
796 if ( dlg
.ShowModal() == wxID_OK
)
798 wxColourData retData
;
799 retData
= dlg
.GetColourData();
800 wxColour colour
= retData
.GetColour();
802 grid
->SetLabelBackgroundColour( colour
);
807 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
809 wxColourDialog
dlg( NULL
);
810 if ( dlg
.ShowModal() == wxID_OK
)
812 wxColourData retData
;
813 retData
= dlg
.GetColourData();
814 wxColour colour
= retData
.GetColour();
816 grid
->SetLabelTextColour( colour
);
820 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
822 wxFont font
= wxGetFontFromUser(this);
825 grid
->SetLabelFont(font
);
829 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
832 grid
->GetRowLabelAlignment( &horiz
, &vert
);
837 horiz
= wxALIGN_CENTRE
;
841 horiz
= wxALIGN_RIGHT
;
845 horiz
= wxALIGN_LEFT
;
849 grid
->SetRowLabelAlignment( horiz
, vert
);
852 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
855 grid
->GetRowLabelAlignment( &horiz
, &vert
);
860 vert
= wxALIGN_CENTRE
;
864 vert
= wxALIGN_BOTTOM
;
872 grid
->SetRowLabelAlignment( horiz
, vert
);
876 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
879 grid
->GetColLabelAlignment( &horiz
, &vert
);
884 horiz
= wxALIGN_CENTRE
;
888 horiz
= wxALIGN_RIGHT
;
892 horiz
= wxALIGN_LEFT
;
896 grid
->SetColLabelAlignment( horiz
, vert
);
900 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
903 grid
->GetColLabelAlignment( &horiz
, &vert
);
908 vert
= wxALIGN_CENTRE
;
912 vert
= wxALIGN_BOTTOM
;
920 grid
->SetColLabelAlignment( horiz
, vert
);
924 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
926 wxColourDialog
dlg( NULL
);
927 if ( dlg
.ShowModal() == wxID_OK
)
929 wxColourData retData
;
930 retData
= dlg
.GetColourData();
931 wxColour colour
= retData
.GetColour();
933 grid
->SetGridLineColour( colour
);
938 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
940 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
944 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
946 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
950 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
952 if ( grid
->IsSelection() )
954 wxGridUpdateLocker
locker(grid
);
955 for ( int n
= 0; n
< grid
->GetNumberRows(); )
957 if ( grid
->IsInSelection( n
, 0 ) )
958 grid
->DeleteRows( n
, 1 );
966 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
968 wxGridUpdateLocker
locker(grid
);
969 const wxArrayInt sels
= grid
->GetSelectedRows();
970 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
972 grid
->AutoSizeRow( sels
[n
], false );
976 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
978 wxGridUpdateLocker
locker(grid
);
979 const wxArrayInt sels
= grid
->GetSelectedCols();
980 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
982 grid
->AutoSizeColumn( sels
[n
], false );
986 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
988 wxGridUpdateLocker
locker(grid
);
989 const wxArrayInt sels
= grid
->GetSelectedRows();
990 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
992 grid
->AutoSizeRowLabelSize( sels
[n
] );
996 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
998 wxGridUpdateLocker
locker(grid
);
999 const wxArrayInt sels
= grid
->GetSelectedCols();
1000 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
1002 grid
->AutoSizeColLabelSize( sels
[n
] );
1006 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
1008 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
1011 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
1013 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
1016 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
1023 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
1025 if ( grid
->IsSelection() )
1027 wxGridUpdateLocker
locker(grid
);
1028 for ( int n
= 0; n
< grid
->GetNumberCols(); )
1030 if ( grid
->IsInSelection( 0 , n
) )
1031 grid
->DeleteCols( n
, 1 );
1039 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
1044 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
1046 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
1049 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
1051 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
1054 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
1056 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
1059 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
1061 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
1064 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
1066 wxColour col
= wxGetColourFromUser(this);
1069 grid
->SetDefaultCellTextColour(col
);
1074 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
1076 wxColour col
= wxGetColourFromUser(this);
1079 // Check the new Refresh function by passing it a rectangle
1080 // which exactly fits the grid.
1082 wxRect
r(pt
, grid
->GetSize());
1083 grid
->SetDefaultCellBackgroundColour(col
);
1084 grid
->Refresh(true, &r
);
1088 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
1090 grid
->DeselectCell(3, 1);
1093 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
1095 grid
->DeselectCol(2);
1098 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
1100 grid
->DeselectRow(2);
1103 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
1105 grid
->ClearSelection();
1108 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
1110 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
1113 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
1115 grid
->SelectCol(2, m_addToSel
);
1118 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
1120 grid
->SelectRow(2, m_addToSel
);
1123 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
1128 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
1130 m_addToSel
= event
.IsChecked();
1133 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
1136 if ( ev
.GetRow() != -1 )
1138 logBuf
<< wxT("Left click on row label ") << ev
.GetRow();
1140 else if ( ev
.GetCol() != -1 )
1142 logBuf
<< wxT("Left click on col label ") << ev
.GetCol();
1146 logBuf
<< wxT("Left click on corner label");
1149 if ( ev
.ShiftDown() )
1150 logBuf
<< wxT(" (shift down)");
1151 if ( ev
.ControlDown() )
1152 logBuf
<< wxT(" (control down)");
1153 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1155 // you must call event skip if you want default grid processing
1161 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
1163 wxLogMessage(wxT("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
1165 // you must call event skip if you want default grid processing
1166 // (cell highlighting etc.)
1172 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
1174 const int row
= ev
.GetRowOrCol();
1176 wxLogMessage("Resized row %d, new height = %d",
1177 row
, grid
->GetRowSize(row
));
1183 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
1185 const int col
= ev
.GetRowOrCol();
1187 wxLogMessage("Resized column %d, new width = %d",
1188 col
, grid
->GetColSize(col
));
1193 void GridFrame::OnColAutoSize( wxGridSizeEvent
&event
)
1195 // Fit even-numbered columns to their contents while using the default
1196 // behaviour for the odd-numbered ones to be able to see the difference.
1197 int col
= event
.GetRowOrCol();
1200 wxLogMessage("Auto-sizing column %d to fit its contents", col
);
1201 grid
->AutoSizeColumn(col
);
1209 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1212 if ( ev
.Selecting() )
1213 logBuf
<< wxT("Selected ");
1215 logBuf
<< wxT("Deselected ");
1216 logBuf
<< wxT("cell at row ") << ev
.GetRow()
1217 << wxT(" col ") << ev
.GetCol()
1218 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1219 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1220 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1221 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1223 //Indicate whether this column was moved
1224 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1225 logBuf
<< wxT(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1227 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1229 // you must call Skip() if you want the default processing
1230 // to occur in wxGrid
1234 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1237 if ( ev
.Selecting() )
1238 logBuf
<< wxT("Selected ");
1240 logBuf
<< wxT("Deselected ");
1241 logBuf
<< wxT("cells from row ") << ev
.GetTopRow()
1242 << wxT(" col ") << ev
.GetLeftCol()
1243 << wxT(" to row ") << ev
.GetBottomRow()
1244 << wxT(" col ") << ev
.GetRightCol()
1245 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1246 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1247 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1248 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1249 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1254 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1256 int row
= ev
.GetRow(),
1259 wxLogMessage("Value of cell at (%d, %d): about to change "
1260 "from \"%s\" to \"%s\"",
1262 grid
->GetCellValue(row
, col
), ev
.GetString());
1264 // test how vetoing works
1265 if ( ev
.GetString() == "42" )
1267 wxLogMessage("Vetoing the change.");
1275 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1277 int row
= ev
.GetRow(),
1280 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1283 grid
->GetCellValue(row
, col
), ev
.GetString());
1288 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1290 wxLogMessage(wxT("Got request to drag cell at row %d, col %d"),
1291 ev
.GetRow(), ev
.GetCol());
1296 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1299 if ( (ev
.GetCol() == 4) &&
1300 (ev
.GetRow() == 0) &&
1301 (wxMessageBox(wxT("Are you sure you wish to edit this cell"),
1302 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1308 wxLogMessage( wxT("Cell editor shown.") );
1313 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1316 if ( (ev
.GetCol() == 4) &&
1317 (ev
.GetRow() == 0) &&
1318 (wxMessageBox(wxT("Are you sure you wish to finish editing this cell"),
1319 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1325 wxLogMessage( wxT("Cell editor hidden.") );
1330 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1332 wxAboutDialogInfo aboutInfo
;
1333 aboutInfo
.SetName(wxT("wxGrid demo"));
1334 aboutInfo
.SetDescription(_("wxGrid sample program"));
1335 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1336 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1337 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1339 // this is just to force the generic version of the about
1340 // dialog under wxMSW so that it's easy to test if the grid
1341 // repaints correctly when it has lost focus and a dialog
1342 // (different from the Windows standard message box -- it doesn't
1343 // work with it for some reason) is moved over it.
1344 aboutInfo
.SetWebSite(wxT("http://www.wxwidgets.org"));
1346 wxAboutBox(aboutInfo
);
1350 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1355 void GridFrame::OnBugsTable(wxCommandEvent
& )
1357 BugsGridFrame
*frame
= new BugsGridFrame
;
1361 // ----------------------------------------------------------------------------
1362 // MyGridCellAttrProvider
1363 // ----------------------------------------------------------------------------
1365 MyGridCellAttrProvider::MyGridCellAttrProvider()
1367 m_attrForOddRows
= new wxGridCellAttr
;
1368 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1371 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1373 m_attrForOddRows
->DecRef();
1376 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1377 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1379 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1385 attr
= m_attrForOddRows
;
1390 if ( !attr
->HasBackgroundColour() )
1392 wxGridCellAttr
*attrNew
= attr
->Clone();
1395 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1403 void GridFrame::OnVTable(wxCommandEvent
& )
1405 static long s_sizeGrid
= 10000;
1407 s_sizeGrid
= wxGetNumberFromUser(wxT("Size of the table to create"),
1409 wxT("wxGridDemo question"),
1413 if ( s_sizeGrid
!= -1 )
1415 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1420 // ----------------------------------------------------------------------------
1421 // MyGridCellRenderer
1422 // ----------------------------------------------------------------------------
1424 // do something that the default renderer doesn't here just to show that it is
1425 // possible to alter the appearance of the cell beyond what the attributes
1427 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1428 wxGridCellAttr
& attr
,
1434 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1436 dc
.SetPen(*wxGREEN_PEN
);
1437 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1438 dc
.DrawEllipse(rect
);
1441 // ============================================================================
1442 // BigGridFrame and BigGridTable: Sample of a non-standard table
1443 // ============================================================================
1445 BigGridFrame::BigGridFrame(long sizeGrid
)
1446 : wxFrame(NULL
, wxID_ANY
, wxT("Plugin Virtual Table"),
1447 wxDefaultPosition
, wxSize(500, 450))
1449 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1450 m_table
= new BigGridTable(sizeGrid
);
1452 // VZ: I don't understand why this slows down the display that much,
1453 // must profile it...
1454 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1456 m_grid
->SetTable(m_table
, true);
1458 #if defined __WXMOTIF__
1459 // MB: the grid isn't getting a sensible default size under wxMotif
1461 GetClientSize( &cw
, &ch
);
1462 m_grid
->SetSize( cw
, ch
);
1466 // ============================================================================
1467 // BugsGridFrame: a "realistic" table
1468 // ============================================================================
1470 // ----------------------------------------------------------------------------
1472 // ----------------------------------------------------------------------------
1495 static const wxString severities
[] =
1504 static struct BugsGridData
1510 wxChar platform
[12];
1512 } gs_dataBugsGrid
[] =
1514 { 18, wxT("foo doesn't work"), Sev_Major
, 1, wxT("wxMSW"), true },
1515 { 27, wxT("bar crashes"), Sev_Critical
, 1, wxT("all"), false },
1516 { 45, wxT("printing is slow"), Sev_Minor
, 3, wxT("wxMSW"), true },
1517 { 68, wxT("Rectangle() fails"), Sev_Normal
, 1, wxT("wxMSW"), false },
1520 static const wxChar
*headers
[Col_Max
] =
1530 // ----------------------------------------------------------------------------
1532 // ----------------------------------------------------------------------------
1534 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1540 return wxGRID_VALUE_NUMBER
;;
1543 // fall thorugh (TODO should be a list)
1546 return wxString::Format(wxT("%s:80"), wxGRID_VALUE_STRING
);
1549 return wxString::Format(wxT("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1552 return wxGRID_VALUE_BOOL
;
1555 wxFAIL_MSG(wxT("unknown column"));
1557 return wxEmptyString
;
1560 int BugsGridTable::GetNumberRows()
1562 return WXSIZEOF(gs_dataBugsGrid
);
1565 int BugsGridTable::GetNumberCols()
1570 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1575 wxString
BugsGridTable::GetValue( int row
, int col
)
1577 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1582 return wxString::Format(wxT("%d"), gd
.id
);
1585 return wxString::Format(wxT("%d"), gd
.prio
);
1588 return gd
.opened
? wxT("1") : wxT("0");
1591 return severities
[gd
.severity
];
1600 return wxEmptyString
;
1603 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1605 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1612 wxFAIL_MSG(wxT("unexpected column"));
1618 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1620 if ( severities
[n
] == value
)
1622 gd
.severity
= (Severity
)n
;
1627 if ( n
== WXSIZEOF(severities
) )
1629 wxLogWarning(wxT("Invalid severity value '%s'."),
1631 gd
.severity
= Sev_Normal
;
1637 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1641 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1647 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1649 const wxString
& typeName
)
1651 if ( typeName
== wxGRID_VALUE_STRING
)
1655 else if ( typeName
== wxGRID_VALUE_BOOL
)
1657 return col
== Col_Opened
;
1659 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1661 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1669 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1671 return CanGetValueAs(row
, col
, typeName
);
1674 long BugsGridTable::GetValueAsLong( int row
, int col
)
1676 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1690 wxFAIL_MSG(wxT("unexpected column"));
1695 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1697 if ( col
== Col_Opened
)
1699 return gs_dataBugsGrid
[row
].opened
;
1703 wxFAIL_MSG(wxT("unexpected column"));
1709 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1711 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1720 wxFAIL_MSG(wxT("unexpected column"));
1724 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1726 if ( col
== Col_Opened
)
1728 gs_dataBugsGrid
[row
].opened
= value
;
1732 wxFAIL_MSG(wxT("unexpected column"));
1736 wxString
BugsGridTable::GetColLabelValue( int col
)
1738 return headers
[col
];
1741 // ----------------------------------------------------------------------------
1743 // ----------------------------------------------------------------------------
1745 BugsGridFrame::BugsGridFrame()
1746 : wxFrame(NULL
, wxID_ANY
, wxT("Bugs table"))
1748 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1749 wxGridTableBase
*table
= new BugsGridTable();
1750 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1751 grid
->SetTable(table
, true);
1753 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1754 *attrRangeEditor
= new wxGridCellAttr
,
1755 *attrCombo
= new wxGridCellAttr
;
1757 attrRO
->SetReadOnly();
1758 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1759 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1762 grid
->SetColAttr(Col_Id
, attrRO
);
1763 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1764 grid
->SetColAttr(Col_Severity
, attrCombo
);
1767 SetClientSize(grid
->GetSize());
1770 // ============================================================================
1771 // TabularGrid: grid used for display of tabular data
1772 // ============================================================================
1774 class TabularGridTable
: public wxGridTableBase
1791 TabularGridTable() { m_sortOrder
= NULL
; }
1793 virtual int GetNumberRows() { return ROW_MAX
; }
1794 virtual int GetNumberCols() { return COL_MAX
; }
1796 virtual wxString
GetValue(int row
, int col
)
1799 row
= m_sortOrder
[row
];
1805 return GetNameOrExt(row
, col
);
1808 return wxString::Format("%lu", GetSize(row
));
1811 return GetDate(row
).FormatDate();
1815 wxFAIL_MSG( "unknown column" );
1821 virtual void SetValue(int, int, const wxString
&)
1823 wxFAIL_MSG( "shouldn't be called" );
1826 virtual wxString
GetColLabelValue(int col
)
1828 // notice that column parameter here always refers to the internal
1829 // column index, independently of its position on the screen
1830 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1831 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1836 virtual void SetColLabelValue(int, const wxString
&)
1838 wxFAIL_MSG( "shouldn't be called" );
1841 void Sort(int col
, bool ascending
)
1843 // we hardcode all sorting orders for simplicity here
1844 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1846 // descending ascending
1847 { { 2, 1, 0 }, { 0, 1, 2 } },
1848 { { 2, 1, 0 }, { 0, 1, 2 } },
1849 { { 2, 1, 0 }, { 0, 1, 2 } },
1850 { { 1, 0, 2 }, { 2, 0, 1 } },
1853 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1857 wxString
GetNameOrExt(int row
, int col
) const
1860 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1861 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1863 const wxString
s(names
[row
]);
1864 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1867 unsigned long GetSize(int row
) const
1869 static const unsigned long
1870 sizes
[] = { 412, 604, 40774 };
1871 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1876 wxDateTime
GetDate(int row
) const
1879 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1880 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1883 dt
.ParseISODate(dates
[row
]);
1890 // specialized text control for column indexes entry
1891 class ColIndexEntry
: public wxTextCtrl
1894 ColIndexEntry(wxWindow
*parent
)
1895 : wxTextCtrl(parent
, wxID_ANY
, "")
1897 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1903 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1913 virtual wxSize
DoGetBestSize() const
1915 wxSize size
= wxTextCtrl::DoGetBestSize();
1916 size
.x
= 3*GetCharWidth();
1921 class TabularGridFrame
: public wxFrame
1929 Id_Check_UseNativeHeader
,
1930 Id_Check_DrawNativeLabels
,
1931 Id_Check_ShowRowLabels
,
1932 Id_Check_EnableColMove
1937 void OnToggleUseNativeHeader(wxCommandEvent
&)
1939 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1942 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1944 // we don't draw labels at all, native or otherwise, if we use the
1945 // native header control
1946 event
.Enable( !m_chkUseNative
->GetValue() );
1949 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1951 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1954 void OnToggleShowRowLabels(wxCommandEvent
&)
1956 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1961 void OnToggleColMove(wxCommandEvent
&)
1963 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1966 void OnShowHideColumn(wxCommandEvent
& event
)
1968 int col
= m_txtColShowHide
->GetCol();
1971 m_grid
->SetColSize(col
,
1972 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1974 UpdateOrderAndVisibility();
1978 void OnMoveColumn(wxCommandEvent
&)
1980 int col
= m_txtColIndex
->GetCol();
1981 int pos
= m_txtColPos
->GetCol();
1982 if ( col
== -1 || pos
== -1 )
1985 m_grid
->SetColPos(col
, pos
);
1987 UpdateOrderAndVisibility();
1990 void OnResetColumnOrder(wxCommandEvent
&)
1992 m_grid
->ResetColPos();
1994 UpdateOrderAndVisibility();
1997 void OnGridColSort(wxGridEvent
& event
)
1999 const int col
= event
.GetCol();
2000 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
2001 m_grid
->IsSortOrderAscending()));
2004 void OnGridColMove(wxGridEvent
& event
)
2006 // can't update it yet as the order hasn't been changed, so do it a bit
2008 m_shouldUpdateOrder
= true;
2013 void OnGridColSize(wxGridSizeEvent
& event
)
2015 // we only catch this event to react to the user showing or hiding this
2016 // column using the header control menu and not because we're
2017 // interested in column resizing
2018 UpdateOrderAndVisibility();
2023 void OnIdle(wxIdleEvent
& event
)
2025 if ( m_shouldUpdateOrder
)
2027 m_shouldUpdateOrder
= false;
2028 UpdateOrderAndVisibility();
2034 void UpdateOrderAndVisibility()
2037 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
2039 const int col
= m_grid
->GetColAt(pos
);
2040 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
2051 m_statOrder
->SetLabel(s
);
2056 TabularGridTable
*m_table
;
2057 wxCheckBox
*m_chkUseNative
,
2059 *m_chkShowRowLabels
,
2060 *m_chkEnableColMove
;
2062 ColIndexEntry
*m_txtColIndex
,
2066 wxStaticText
*m_statOrder
;
2068 // fla for EVT_IDLE handler
2069 bool m_shouldUpdateOrder
;
2071 wxDECLARE_NO_COPY_CLASS(TabularGridFrame
);
2072 DECLARE_EVENT_TABLE()
2075 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
2076 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
2077 TabularGridFrame::OnToggleUseNativeHeader
)
2078 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
2079 TabularGridFrame::OnToggleDrawNativeLabels
)
2080 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
2081 TabularGridFrame::OnToggleShowRowLabels
)
2082 EVT_CHECKBOX(Id_Check_EnableColMove
,
2083 TabularGridFrame::OnToggleColMove
)
2085 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
2086 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
2088 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
2089 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
2090 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
2091 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
2093 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
2094 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
2095 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
2097 EVT_IDLE(TabularGridFrame::OnIdle
)
2100 TabularGridFrame::TabularGridFrame()
2101 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
2103 m_shouldUpdateOrder
= false;
2105 wxPanel
* const panel
= new wxPanel(this);
2107 // create and initialize the grid with the specified data
2108 m_table
= new TabularGridTable
;
2109 m_grid
= new wxGrid(panel
, wxID_ANY
,
2110 wxDefaultPosition
, wxDefaultSize
,
2111 wxBORDER_STATIC
| wxWANTS_CHARS
);
2112 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
2114 m_grid
->EnableDragColMove();
2115 m_grid
->UseNativeColHeader();
2116 m_grid
->HideRowLabels();
2118 // add it and the other controls to the frame
2119 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
2120 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
2122 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
2124 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
2125 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
2126 "&Use native header");
2127 m_chkUseNative
->SetValue(true);
2128 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
2130 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
2131 "&Draw native column labels");
2132 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
2134 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
2135 "Show &row labels");
2136 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
2138 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
2139 "Allow column re&ordering");
2140 m_chkEnableColMove
->SetValue(true);
2141 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
2142 sizerControls
->Add(sizerStyles
);
2144 sizerControls
->AddSpacer(10);
2146 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
2147 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
2149 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
2150 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
2152 m_txtColIndex
= new ColIndexEntry(panel
);
2153 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
2154 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
2155 m_txtColPos
= new ColIndexEntry(panel
);
2156 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
2157 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
2159 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
2161 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
2162 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
2164 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
2165 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
2166 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
2167 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
2169 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
2170 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
2172 m_txtColShowHide
= new ColIndexEntry(panel
);
2173 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
2174 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
2175 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
2176 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
2178 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
2180 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
2182 panel
->SetSizer(sizerTop
);
2184 SetClientSize(panel
->GetBestSize());
2185 SetSizeHints(GetSize());
2190 void GridFrame::OnTabularTable(wxCommandEvent
&)
2192 new TabularGridFrame
;
2195 // Example using wxGrid::Render
2196 // Displays a preset selection or, if it exists, a selection block
2197 // Draws the selection to a wxBitmap and displays the bitmap
2198 void GridFrame::OnGridRender( wxCommandEvent
& event
)
2200 int styleRender
= 0, i
;
2201 bool useLometric
= false, defSize
= false;
2203 wxSize
sizeMargin( 0, 0 );
2204 wxPoint
pointOrigin( 0, 0 );
2206 wxMenu
* menu
= GetMenuBar()->GetMenu( 0 );
2207 wxMenuItem
* menuItem
= menu
->FindItem( ID_RENDER_ROW_LABEL
);
2208 menu
= menuItem
->GetMenu();
2210 if ( menu
->FindItem( ID_RENDER_ROW_LABEL
)->IsChecked() )
2211 styleRender
|= wxGRID_DRAW_ROWS_HEADER
;
2212 if ( menu
->FindItem( ID_RENDER_COL_LABEL
)->IsChecked() )
2213 styleRender
|= wxGRID_DRAW_COLS_HEADER
;
2214 if ( menu
->FindItem( ID_RENDER_GRID_LINES
)->IsChecked() )
2215 styleRender
|= wxGRID_DRAW_CELL_LINES
;
2216 if ( menu
->FindItem( ID_RENDER_GRID_BORDER
)->IsChecked() )
2217 styleRender
|= wxGRID_DRAW_BOX_RECT
;
2218 if ( menu
->FindItem( ID_RENDER_SELECT_HLIGHT
)->IsChecked() )
2219 styleRender
|= wxGRID_DRAW_SELECTION
;
2220 if ( menu
->FindItem( ID_RENDER_LOMETRIC
)->IsChecked() )
2222 if ( menu
->FindItem( ID_RENDER_MARGIN
)->IsChecked() )
2224 pointOrigin
.x
+= 50;
2225 pointOrigin
.y
+= 50;
2226 sizeMargin
.IncBy( 50 );
2228 if ( menu
->FindItem( ID_RENDER_ZOOM
)->IsChecked() )
2230 if ( menu
->FindItem( ID_RENDER_DEFAULT_SIZE
)->IsChecked() )
2233 // init render area coords with a default row and col selection
2234 wxGridCellCoords
topLeft( 0, 0 ), bottomRight( 8, 6 );
2235 // check whether we are printing a block selection
2236 // other selection types not catered for here
2237 if ( event
.GetId() == ID_RENDER_COORDS
)
2239 topLeft
.SetCol( 6 );
2240 topLeft
.SetRow( 4 );
2241 bottomRight
.SetCol( 15 );
2242 bottomRight
.SetRow( 29 );
2244 else if ( grid
->IsSelection() && grid
->GetSelectionBlockTopLeft().Count() )
2246 wxGridCellCoordsArray cells
= grid
->GetSelectionBlockTopLeft();
2247 if ( grid
->GetSelectionBlockBottomRight().Count() )
2249 cells
.Add( grid
->GetSelectionBlockBottomRight()[ 0 ] );
2250 topLeft
.Set( cells
[ 0 ].GetRow(),
2251 cells
[ 0 ].GetCol() );
2252 bottomRight
.Set( cells
[ 1 ].GetRow(),
2253 cells
[ 1 ].GetCol() );
2258 wxSize
sizeRender( 0, 0 );
2259 wxGridSizesInfo sizeinfo
= grid
->GetColSizes();
2260 for ( i
= topLeft
.GetCol(); i
<= bottomRight
.GetCol(); i
++ )
2262 sizeRender
.x
+= sizeinfo
.GetSize( i
);
2266 sizeinfo
= grid
->GetRowSizes();
2267 for ( i
= topLeft
.GetRow(); i
<= bottomRight
.GetRow(); i
++ )
2269 sizeRender
.y
+= sizeinfo
.GetSize( i
);
2272 if ( styleRender
& wxGRID_DRAW_ROWS_HEADER
)
2273 sizeRender
.x
+= grid
->GetRowLabelSize();
2274 if ( styleRender
& wxGRID_DRAW_COLS_HEADER
)
2275 sizeRender
.y
+= grid
->GetColLabelSize();
2277 sizeRender
.x
*= zoom
;
2278 sizeRender
.y
*= zoom
;
2280 // delete any existing render frame and create new one
2281 wxWindow
* win
= FindWindow( "frameRender" );
2285 wxFrame
* frame
= new wxFrame( this, wxID_ANY
, "Grid Render" );
2286 frame
->SetClientSize( 780, 400 );
2287 frame
->SetName( "frameRender" );
2289 wxPanel
* canvas
= new wxPanel( frame
, wxID_ANY
);
2291 // make bitmap large enough for margins if any
2293 sizeRender
.IncBy( sizeMargin
* 2 );
2295 sizeRender
.IncBy( sizeMargin
);
2297 wxBitmap
bmp( sizeRender
);
2298 // don't leave it larger or drawing will be scaled
2299 sizeRender
.DecBy( sizeMargin
* 2 );
2301 wxMemoryDC
memDc(bmp
);
2303 // default row labels have no background colour so set background
2304 memDc
.SetBackground( wxBrush( canvas
->GetBackgroundColour() ) );
2307 // convert sizeRender to mapping mode units if necessary
2310 memDc
.SetMapMode( wxMM_LOMETRIC
);
2311 sizeRender
.x
= memDc
.DeviceToLogicalXRel( sizeRender
.x
);
2312 sizeRender
.y
= memDc
.DeviceToLogicalYRel( sizeRender
.y
);
2315 // pass wxDefaultSize if menu item is checked
2317 sizeRender
= wxDefaultSize
;
2319 grid
->Render( memDc
,
2322 topLeft
, bottomRight
,
2323 wxGridRenderStyle( styleRender
) );
2327 canvas
->Connect( wxEVT_PAINT
,
2328 wxPaintEventHandler(GridFrame::OnRenderPaint
),
2335 void GridFrame::OnRenderPaint( wxPaintEvent
& event
)
2337 wxPanel
* canvas
= ( wxPanel
* )event
.GetEventObject();
2338 wxPaintDC
dc( canvas
);
2339 canvas
->PrepareDC( dc
);
2341 if ( !m_gridBitmap
.IsOk() )
2344 wxMemoryDC
memDc( m_gridBitmap
);
2347 m_gridBitmap
.GetWidth(),
2348 m_gridBitmap
.GetHeight(),
2352 void GridFrame::HideCol( wxCommandEvent
& WXUNUSED(event
) )
2357 void GridFrame::ShowCol( wxCommandEvent
& WXUNUSED(event
) )
2362 void GridFrame::HideRow( wxCommandEvent
& WXUNUSED(event
) )
2367 void GridFrame::ShowRow( wxCommandEvent
& WXUNUSED(event
) )