1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
5 // Modified by: Santiago Palacios
6 // Copyright: (c) Michael Bedward, Julian Smart, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
29 #include "wx/colordlg.h"
30 #include "wx/fontdlg.h"
31 #include "wx/numdlg.h"
32 #include "wx/aboutdlg.h"
35 #include "wx/headerctrl.h"
36 #include "wx/generic/gridctrl.h"
37 #include "wx/generic/grideditors.h"
41 #ifndef wxHAS_IMAGES_IN_RESOURCES
42 #include "../sample.xpm"
45 // Custom renderer that renders column header cells without borders and in
47 class CustomColumnHeaderRenderer
: public wxGridColumnHeaderRenderer
50 CustomColumnHeaderRenderer(const wxColour
& colFg
, const wxColour
& colBg
)
56 virtual void DrawLabel(const wxGrid
& WXUNUSED(grid
),
58 const wxString
& value
,
62 int WXUNUSED(textOrientation
)) const
64 dc
.SetTextForeground(m_colFg
);
65 dc
.SetFont(wxITALIC_FONT
->Bold());
66 dc
.DrawLabel(value
, rect
, horizAlign
| vertAlign
);
69 virtual void DrawBorder(const wxGrid
& WXUNUSED(grid
),
73 dc
.SetPen(*wxTRANSPARENT_PEN
);
74 dc
.SetBrush(wxBrush(m_colBg
));
75 dc
.DrawRectangle(rect
);
79 const wxColour m_colFg
, m_colBg
;
81 wxDECLARE_NO_COPY_CLASS(CustomColumnHeaderRenderer
);
84 // And a custom attributes provider which uses custom column header renderer
86 class CustomColumnHeadersProvider
: public wxGridCellAttrProvider
89 // by default custom column renderer is not used, call
90 // UseCustomColHeaders() to enable it
91 CustomColumnHeadersProvider()
92 : m_customOddRenderer(*wxYELLOW
, *wxBLUE
),
93 m_customEvenRenderer(*wxWHITE
, *wxBLACK
),
98 // enable or disable the use of custom renderer for column headers
99 void UseCustomColHeaders(bool use
= true) { m_useCustom
= use
; }
102 virtual const wxGridColumnHeaderRenderer
& GetColumnHeaderRenderer(int col
)
104 // if enabled, use custom renderers
107 // and use different ones for odd and even columns -- just to show
109 return col
% 2 ? m_customOddRenderer
: m_customEvenRenderer
;
112 return wxGridCellAttrProvider::GetColumnHeaderRenderer(col
);
116 CustomColumnHeaderRenderer m_customOddRenderer
,
117 m_customEvenRenderer
;
121 wxDECLARE_NO_COPY_CLASS(CustomColumnHeadersProvider
);
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 IMPLEMENT_APP( GridApp
)
130 // ============================================================================
132 // ============================================================================
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 bool GridApp::OnInit()
140 GridFrame
*frame
= new GridFrame
;
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 BEGIN_EVENT_TABLE( GridFrame
, wxFrame
)
151 EVT_MENU( ID_TOGGLEROWLABELS
, GridFrame::ToggleRowLabels
)
152 EVT_MENU( ID_TOGGLECOLLABELS
, GridFrame::ToggleColLabels
)
153 EVT_MENU( ID_TOGGLEEDIT
, GridFrame::ToggleEditing
)
154 EVT_MENU( ID_TOGGLEROWSIZING
, GridFrame::ToggleRowSizing
)
155 EVT_MENU( ID_TOGGLECOLSIZING
, GridFrame::ToggleColSizing
)
156 EVT_MENU( ID_TOGGLECOLMOVING
, GridFrame::ToggleColMoving
)
157 EVT_MENU( ID_TOGGLEGRIDSIZING
, GridFrame::ToggleGridSizing
)
158 EVT_MENU( ID_TOGGLEGRIDDRAGCELL
, GridFrame::ToggleGridDragCell
)
159 EVT_MENU( ID_COLNATIVEHEADER
, GridFrame::SetNativeColHeader
)
160 EVT_MENU( ID_COLDEFAULTHEADER
, GridFrame::SetDefaultColHeader
)
161 EVT_MENU( ID_COLCUSTOMHEADER
, GridFrame::SetCustomColHeader
)
162 EVT_MENU_RANGE( ID_TAB_STOP
, ID_TAB_LEAVE
, GridFrame::SetTabBehaviour
)
163 EVT_MENU( ID_TAB_CUSTOM
, GridFrame::SetTabCustomHandler
)
164 EVT_MENU( ID_TOGGLEGRIDLINES
, GridFrame::ToggleGridLines
)
165 EVT_MENU( ID_AUTOSIZECOLS
, GridFrame::AutoSizeCols
)
166 EVT_MENU( ID_CELLOVERFLOW
, GridFrame::CellOverflow
)
167 EVT_MENU( ID_RESIZECELL
, GridFrame::ResizeCell
)
168 EVT_MENU( ID_SETLABELCOLOUR
, GridFrame::SetLabelColour
)
169 EVT_MENU( ID_SETLABELTEXTCOLOUR
, GridFrame::SetLabelTextColour
)
170 EVT_MENU( ID_SETLABEL_FONT
, GridFrame::SetLabelFont
)
171 EVT_MENU( ID_ROWLABELHORIZALIGN
, GridFrame::SetRowLabelHorizAlignment
)
172 EVT_MENU( ID_ROWLABELVERTALIGN
, GridFrame::SetRowLabelVertAlignment
)
173 EVT_MENU( ID_COLLABELHORIZALIGN
, GridFrame::SetColLabelHorizAlignment
)
174 EVT_MENU( ID_COLLABELVERTALIGN
, GridFrame::SetColLabelVertAlignment
)
175 EVT_MENU( ID_GRIDLINECOLOUR
, GridFrame::SetGridLineColour
)
176 EVT_MENU( ID_INSERTROW
, GridFrame::InsertRow
)
177 EVT_MENU( ID_INSERTCOL
, GridFrame::InsertCol
)
178 EVT_MENU( ID_DELETEROW
, GridFrame::DeleteSelectedRows
)
179 EVT_MENU( ID_DELETECOL
, GridFrame::DeleteSelectedCols
)
180 EVT_MENU( ID_CLEARGRID
, GridFrame::ClearGrid
)
181 EVT_MENU( ID_SELCELLS
, GridFrame::SelectCells
)
182 EVT_MENU( ID_SELROWS
, GridFrame::SelectRows
)
183 EVT_MENU( ID_SELCOLS
, GridFrame::SelectCols
)
184 EVT_MENU( ID_SELROWSORCOLS
, GridFrame::SelectRowsOrCols
)
186 EVT_MENU( ID_SET_CELL_FG_COLOUR
, GridFrame::SetCellFgColour
)
187 EVT_MENU( ID_SET_CELL_BG_COLOUR
, GridFrame::SetCellBgColour
)
189 EVT_MENU( wxID_ABOUT
, GridFrame::About
)
190 EVT_MENU( wxID_EXIT
, GridFrame::OnQuit
)
191 EVT_MENU( ID_VTABLE
, GridFrame::OnVTable
)
192 EVT_MENU( ID_BUGS_TABLE
, GridFrame::OnBugsTable
)
193 EVT_MENU( ID_TABULAR_TABLE
, GridFrame::OnTabularTable
)
195 EVT_MENU( ID_DESELECT_CELL
, GridFrame::DeselectCell
)
196 EVT_MENU( ID_DESELECT_COL
, GridFrame::DeselectCol
)
197 EVT_MENU( ID_DESELECT_ROW
, GridFrame::DeselectRow
)
198 EVT_MENU( ID_DESELECT_ALL
, GridFrame::DeselectAll
)
199 EVT_MENU( ID_SELECT_CELL
, GridFrame::SelectCell
)
200 EVT_MENU( ID_SELECT_COL
, GridFrame::SelectCol
)
201 EVT_MENU( ID_SELECT_ROW
, GridFrame::SelectRow
)
202 EVT_MENU( ID_SELECT_ALL
, GridFrame::SelectAll
)
203 EVT_MENU( ID_SELECT_UNSELECT
, GridFrame::OnAddToSelectToggle
)
205 EVT_MENU( ID_SIZE_ROW
, GridFrame::AutoSizeRow
)
206 EVT_MENU( ID_SIZE_COL
, GridFrame::AutoSizeCol
)
207 EVT_MENU( ID_SIZE_ROW_LABEL
, GridFrame::AutoSizeRowLabel
)
208 EVT_MENU( ID_SIZE_COL_LABEL
, GridFrame::AutoSizeColLabel
)
209 EVT_MENU( ID_SIZE_LABELS_COL
, GridFrame::AutoSizeLabelsCol
)
210 EVT_MENU( ID_SIZE_LABELS_ROW
, GridFrame::AutoSizeLabelsRow
)
211 EVT_MENU( ID_SIZE_GRID
, GridFrame::AutoSizeTable
)
213 EVT_MENU( ID_HIDECOL
, GridFrame::HideCol
)
214 EVT_MENU( ID_SHOWCOL
, GridFrame::ShowCol
)
215 EVT_MENU( ID_HIDEROW
, GridFrame::HideRow
)
216 EVT_MENU( ID_SHOWROW
, GridFrame::ShowRow
)
218 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH
, GridFrame::OnSetHighlightWidth
)
219 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH
, GridFrame::OnSetROHighlightWidth
)
221 EVT_MENU( wxID_PRINT
, GridFrame::OnGridRender
)
222 EVT_MENU( ID_RENDER_COORDS
, GridFrame::OnGridRender
)
224 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick
)
225 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick
)
226 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize
)
227 EVT_GRID_COL_SIZE( GridFrame::OnColSize
)
228 EVT_GRID_COL_AUTO_SIZE( GridFrame::OnColAutoSize
)
229 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell
)
230 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected
)
231 EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging
)
232 EVT_GRID_CELL_CHANGED( GridFrame::OnCellValueChanged
)
233 EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag
)
235 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown
)
236 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden
)
240 GridFrame::GridFrame()
241 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets grid class demo"),
245 SetIcon(wxICON(sample
));
247 wxMenu
*fileMenu
= new wxMenu
;
248 fileMenu
->Append( ID_VTABLE
, wxT("&Virtual table test\tCtrl-V"));
249 fileMenu
->Append( ID_BUGS_TABLE
, wxT("&Bugs table test\tCtrl-B"));
250 fileMenu
->Append( ID_TABULAR_TABLE
, wxT("&Tabular table test\tCtrl-T"));
251 fileMenu
->AppendSeparator();
253 wxMenu
* setupMenu
= new wxMenu
;
255 item
= setupMenu
->AppendCheckItem( ID_RENDER_ROW_LABEL
,
256 "Render row labels" );
258 item
= setupMenu
->AppendCheckItem( ID_RENDER_COL_LABEL
,
259 "Render column labels" );
261 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_LINES
,
262 "Render grid cell lines" );
264 item
= setupMenu
->AppendCheckItem( ID_RENDER_GRID_BORDER
,
267 item
= setupMenu
->AppendCheckItem( ID_RENDER_SELECT_HLIGHT
,
268 "Render selection highlight" );
269 setupMenu
->AppendSeparator();
270 setupMenu
->AppendCheckItem( ID_RENDER_LOMETRIC
,
271 "Use LOMETRIC mapping mode" );
272 setupMenu
->AppendCheckItem( ID_RENDER_DEFAULT_SIZE
,
273 "Use wxDefaultSize" );
274 setupMenu
->AppendCheckItem( ID_RENDER_MARGIN
,
275 "Logical 50 unit margin" );
276 setupMenu
->AppendCheckItem( ID_RENDER_ZOOM
,
279 fileMenu
->AppendSubMenu( setupMenu
, "Render setup" );
280 fileMenu
->Append( wxID_PRINT
, "Render" );
281 fileMenu
->Append( ID_RENDER_COORDS
, "Render G5:P30" );
283 fileMenu
->AppendSeparator();
284 fileMenu
->Append( wxID_EXIT
, wxT("E&xit\tAlt-X") );
286 wxMenu
*viewMenu
= new wxMenu
;
287 viewMenu
->AppendCheckItem(ID_TOGGLEROWLABELS
, "&Row labels");
288 viewMenu
->AppendCheckItem(ID_TOGGLECOLLABELS
, "&Col labels");
289 viewMenu
->AppendCheckItem(ID_TOGGLEEDIT
,"&Editable");
290 viewMenu
->AppendCheckItem(ID_TOGGLEROWSIZING
, "Ro&w drag-resize");
291 viewMenu
->AppendCheckItem(ID_TOGGLECOLSIZING
, "C&ol drag-resize");
292 viewMenu
->AppendCheckItem(ID_TOGGLECOLMOVING
, "Col drag-&move");
293 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDSIZING
, "&Grid drag-resize");
294 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDDRAGCELL
, "&Grid drag-cell");
295 viewMenu
->AppendCheckItem(ID_TOGGLEGRIDLINES
, "&Grid Lines");
296 viewMenu
->AppendCheckItem(ID_SET_HIGHLIGHT_WIDTH
, "&Set Cell Highlight Width...");
297 viewMenu
->AppendCheckItem(ID_SET_RO_HIGHLIGHT_WIDTH
, "&Set Cell RO Highlight Width...");
298 viewMenu
->AppendCheckItem(ID_AUTOSIZECOLS
, "&Auto-size cols");
299 viewMenu
->AppendCheckItem(ID_CELLOVERFLOW
, "&Overflow cells");
300 viewMenu
->AppendCheckItem(ID_RESIZECELL
, "&Resize cell (7,1)");
301 viewMenu
->Append(ID_HIDECOL
, "&Hide column A");
302 viewMenu
->Append(ID_SHOWCOL
, "&Show column A");
303 viewMenu
->Append(ID_HIDEROW
, "&Hide row 2");
304 viewMenu
->Append(ID_SHOWROW
, "&Show row 2");
305 wxMenu
*rowLabelMenu
= new wxMenu
;
307 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("R&ow label alignment"),
309 wxT("Change alignment of row labels") );
311 rowLabelMenu
->AppendRadioItem( ID_ROWLABELHORIZALIGN
, wxT("&Horizontal") );
312 rowLabelMenu
->AppendRadioItem( ID_ROWLABELVERTALIGN
, wxT("&Vertical") );
314 wxMenu
*colLabelMenu
= new wxMenu
;
316 viewMenu
->Append( ID_COLLABELALIGN
, wxT("Col l&abel alignment"),
318 wxT("Change alignment of col labels") );
320 colLabelMenu
->AppendRadioItem( ID_COLLABELHORIZALIGN
, wxT("&Horizontal") );
321 colLabelMenu
->AppendRadioItem( ID_COLLABELVERTALIGN
, wxT("&Vertical") );
323 wxMenu
*colHeaderMenu
= new wxMenu
;
325 viewMenu
->Append( ID_ROWLABELALIGN
, wxT("Col header style"),
327 wxT("Change style of col header") );
329 colHeaderMenu
->AppendRadioItem( ID_COLDEFAULTHEADER
, wxT("&Default") );
330 colHeaderMenu
->AppendRadioItem( ID_COLNATIVEHEADER
, wxT("&Native") );
331 colHeaderMenu
->AppendRadioItem( ID_COLCUSTOMHEADER
, wxT("&Custom") );
333 wxMenu
*tabBehaviourMenu
= new wxMenu
;
334 tabBehaviourMenu
->AppendRadioItem(ID_TAB_STOP
, "&Stop at the boundary");
335 tabBehaviourMenu
->AppendRadioItem(ID_TAB_WRAP
, "&Wrap at the boundary");
336 tabBehaviourMenu
->AppendRadioItem(ID_TAB_LEAVE
, "&Leave the grid");
337 tabBehaviourMenu
->AppendRadioItem(ID_TAB_CUSTOM
, "&Custom tab handler");
338 viewMenu
->AppendSubMenu(tabBehaviourMenu
, "&Tab behaviour");
340 wxMenu
*colMenu
= new wxMenu
;
341 colMenu
->Append( ID_SETLABELCOLOUR
, wxT("Set &label colour...") );
342 colMenu
->Append( ID_SETLABELTEXTCOLOUR
, wxT("Set label &text colour...") );
343 colMenu
->Append( ID_SETLABEL_FONT
, wxT("Set label fo&nt...") );
344 colMenu
->Append( ID_GRIDLINECOLOUR
, wxT("&Grid line colour...") );
345 colMenu
->Append( ID_SET_CELL_FG_COLOUR
, wxT("Set cell &foreground colour...") );
346 colMenu
->Append( ID_SET_CELL_BG_COLOUR
, wxT("Set cell &background colour...") );
348 wxMenu
*editMenu
= new wxMenu
;
349 editMenu
->Append( ID_INSERTROW
, wxT("Insert &row") );
350 editMenu
->Append( ID_INSERTCOL
, wxT("Insert &column") );
351 editMenu
->Append( ID_DELETEROW
, wxT("Delete selected ro&ws") );
352 editMenu
->Append( ID_DELETECOL
, wxT("Delete selected co&ls") );
353 editMenu
->Append( ID_CLEARGRID
, wxT("Cl&ear grid cell contents") );
355 wxMenu
*selectMenu
= new wxMenu
;
356 selectMenu
->Append( ID_SELECT_UNSELECT
, wxT("Add new cells to the selection"),
357 wxT("When off, old selection is deselected before ")
358 wxT("selecting the new cells"), wxITEM_CHECK
);
359 selectMenu
->AppendSeparator();
360 selectMenu
->Append( ID_SELECT_ALL
, wxT("Select all"));
361 selectMenu
->Append( ID_SELECT_ROW
, wxT("Select row 2"));
362 selectMenu
->Append( ID_SELECT_COL
, wxT("Select col 2"));
363 selectMenu
->Append( ID_SELECT_CELL
, wxT("Select cell (3, 1)"));
364 selectMenu
->AppendSeparator();
365 selectMenu
->Append( ID_DESELECT_ALL
, wxT("Deselect all"));
366 selectMenu
->Append( ID_DESELECT_ROW
, wxT("Deselect row 2"));
367 selectMenu
->Append( ID_DESELECT_COL
, wxT("Deselect col 2"));
368 selectMenu
->Append( ID_DESELECT_CELL
, wxT("Deselect cell (3, 1)"));
369 wxMenu
*selectionMenu
= new wxMenu
;
370 selectMenu
->Append( ID_CHANGESEL
, wxT("Change &selection mode"),
372 wxT("Change selection mode") );
374 selectionMenu
->Append( ID_SELCELLS
, wxT("Select &cells") );
375 selectionMenu
->Append( ID_SELROWS
, wxT("Select &rows") );
376 selectionMenu
->Append( ID_SELCOLS
, wxT("Select col&umns") );
377 selectionMenu
->Append( ID_SELROWSORCOLS
, wxT("Select rows &or columns") );
379 wxMenu
*autosizeMenu
= new wxMenu
;
380 autosizeMenu
->Append( ID_SIZE_ROW
, wxT("Selected &row data") );
381 autosizeMenu
->Append( ID_SIZE_COL
, wxT("Selected &column data") );
382 autosizeMenu
->Append( ID_SIZE_ROW_LABEL
, wxT("Selected row la&bel") );
383 autosizeMenu
->Append( ID_SIZE_COL_LABEL
, wxT("Selected column &label") );
384 autosizeMenu
->Append( ID_SIZE_LABELS_COL
, wxT("Column la&bels") );
385 autosizeMenu
->Append( ID_SIZE_LABELS_ROW
, wxT("Row label&s") );
386 autosizeMenu
->Append( ID_SIZE_GRID
, wxT("Entire &grid") );
388 wxMenu
*helpMenu
= new wxMenu
;
389 helpMenu
->Append( wxID_ABOUT
, wxT("&About wxGrid demo") );
391 wxMenuBar
*menuBar
= new wxMenuBar
;
392 menuBar
->Append( fileMenu
, wxT("&File") );
393 menuBar
->Append( viewMenu
, wxT("&Grid") );
394 menuBar
->Append( colMenu
, wxT("&Colours") );
395 menuBar
->Append( editMenu
, wxT("&Edit") );
396 menuBar
->Append( selectMenu
, wxT("&Select") );
397 menuBar
->Append( autosizeMenu
, wxT("&Autosize") );
398 menuBar
->Append( helpMenu
, wxT("&Help") );
400 SetMenuBar( menuBar
);
404 grid
= new wxGrid( this,
407 wxSize( 400, 300 ) );
411 int gridW
= 600, gridH
= 300;
412 int logW
= gridW
, logH
= 100;
414 logWin
= new wxTextCtrl( this,
417 wxPoint( 0, gridH
+ 20 ),
418 wxSize( logW
, logH
),
421 logger
= new wxLogTextCtrl( logWin
);
422 m_logOld
= wxLog::SetActiveTarget( logger
);
423 wxLog::DisableTimestamp();
426 // this will create a grid and, by default, an associated grid
428 grid
->CreateGrid( 0, 0 );
430 grid
->GetTable()->SetAttrProvider(new CustomColumnHeadersProvider());
432 grid
->AppendRows(100);
433 grid
->AppendCols(100);
435 int ir
= grid
->GetNumberRows();
436 grid
->DeleteRows(0, ir
);
437 grid
->AppendRows(ir
);
439 grid
->SetRowSize( 0, 60 );
440 grid
->SetCellValue( 0, 0, wxT("Ctrl+Home\nwill go to\nthis cell") );
442 grid
->SetCellValue( 0, 1, wxT("A long piece of text to demonstrate wrapping.") );
443 grid
->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer
);
444 grid
->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor
);
446 grid
->SetCellValue( 0, 2, wxT("Blah") );
447 grid
->SetCellValue( 0, 3, wxT("Read only") );
448 grid
->SetReadOnly( 0, 3 );
450 grid
->SetCellValue( 0, 4, wxT("Can veto edit this cell") );
452 grid
->SetColSize(10, 150);
453 wxString longtext
= wxT("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\n");
454 longtext
+= wxT("With tabs :\n");
455 longtext
+= wxT("Home,\t\thome\t\t\tagain\n");
456 longtext
+= wxT("Long word at start :\n");
457 longtext
+= wxT("ILikeToBeHereWhen I can\n");
458 longtext
+= wxT("Long word in the middle :\n");
459 longtext
+= wxT("When IComeHome,ColdAnd tired\n");
460 longtext
+= wxT("Long last word :\n");
461 longtext
+= wxT("It's GoodToWarmMyBonesBesideTheFire");
462 grid
->SetCellValue( 0, 10, longtext
);
463 grid
->SetCellRenderer(0 , 10, new wxGridCellAutoWrapStringRenderer
);
464 grid
->SetCellEditor( 0, 10 , new wxGridCellAutoWrapStringEditor
);
465 grid
->SetCellValue( 0, 11, wxT("K1 cell editor blocker") );
467 grid
->SetCellValue( 0, 5, wxT("Press\nCtrl+arrow\nto skip over\ncells") );
469 grid
->SetRowSize( 99, 60 );
470 grid
->SetCellValue(98, 98, "Test background colour setting");
471 grid
->SetCellBackgroundColour(98, 99, wxColour(255, 127, 127));
472 grid
->SetCellBackgroundColour(99, 98, wxColour(255, 127, 127));
473 grid
->SetCellValue( 99, 99, wxT("Ctrl+End\nwill go to\nthis cell") );
474 grid
->SetCellValue( 1, 0, wxT("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
476 grid
->SetCellTextColour(1, 2, *wxRED
);
477 grid
->SetCellBackgroundColour(1, 2, *wxGREEN
);
479 grid
->SetCellValue( 1, 4, wxT("I'm in the middle"));
481 grid
->SetCellValue(2, 2, wxT("red"));
483 grid
->SetCellTextColour(2, 2, *wxRED
);
484 grid
->SetCellValue(3, 3, wxT("green on grey"));
485 grid
->SetCellTextColour(3, 3, *wxGREEN
);
486 grid
->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY
);
488 grid
->SetCellValue(4, 4, wxT("a weird looking cell"));
489 grid
->SetCellAlignment(4, 4, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
490 grid
->SetCellRenderer(4, 4, new MyGridCellRenderer
);
492 grid
->SetCellRenderer(3, 0, new wxGridCellBoolRenderer
);
493 grid
->SetCellEditor(3, 0, new wxGridCellBoolEditor
);
494 grid
->SetCellBackgroundColour(3, 0, wxColour(255, 127, 127));
496 wxGridCellAttr
*attr
;
497 attr
= new wxGridCellAttr
;
498 attr
->SetTextColour(*wxBLUE
);
499 grid
->SetColAttr(5, attr
);
500 attr
= new wxGridCellAttr
;
501 attr
->SetBackgroundColour(*wxRED
);
502 grid
->SetRowAttr(5, attr
);
504 grid
->SetCellValue(2, 4, wxT("a wider column"));
505 grid
->SetColSize(4, 120);
506 grid
->SetColMinimalWidth(4, 120);
508 grid
->SetCellTextColour(5, 8, *wxGREEN
);
509 grid
->SetCellValue(5, 8, wxT("Bg from row attr\nText col from cell attr"));
510 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"));
512 // Some numeric columns with different formatting.
513 grid
->SetColFormatFloat(6);
514 grid
->SetCellValue(0, 6, "Default\nfloat format");
515 grid
->SetCellValue(1, 6, wxString::Format(wxT("%g"), 3.1415));
516 grid
->SetCellValue(2, 6, wxString::Format(wxT("%g"), 1415.0));
517 grid
->SetCellValue(3, 6, wxString::Format(wxT("%g"), 12345.67890));
519 grid
->SetColFormatFloat(7, 6, 2);
520 grid
->SetCellValue(0, 7, "Width 6\nprecision 2");
521 grid
->SetCellValue(1, 7, wxString::Format(wxT("%g"), 3.1415));
522 grid
->SetCellValue(2, 7, wxString::Format(wxT("%g"), 1415.0));
523 grid
->SetCellValue(3, 7, wxString::Format(wxT("%g"), 12345.67890));
525 grid
->SetColFormatCustom(8,
526 wxString::Format("%s:%i,%i,%s", wxGRID_VALUE_FLOAT
, -1, 4, "g"));
527 grid
->SetCellValue(0, 8, "Compact\nformat");
528 grid
->SetCellValue(1, 8, wxT("31415e-4"));
529 grid
->SetCellValue(2, 8, wxT("1415"));
530 grid
->SetCellValue(3, 8, wxT("123456789e-4"));
532 grid
->SetColFormatNumber(9);
533 grid
->SetCellValue(0, 9, "Integer\ncolumn");
534 grid
->SetCellValue(1, 9, "17");
535 grid
->SetCellValue(2, 9, "0");
536 grid
->SetCellValue(3, 9, "-666");
537 grid
->SetCellAlignment(3, 9, wxALIGN_CENTRE
, wxALIGN_TOP
);
538 grid
->SetCellValue(3, 10, "<- This numeric cell should be centred");
540 const wxString choices
[] =
542 wxT("Please select a choice"),
543 wxT("This takes two cells"),
544 wxT("Another choice"),
546 grid
->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices
), choices
));
547 grid
->SetCellSize(4, 0, 1, 2);
548 grid
->SetCellValue(4, 0, choices
[0]);
549 grid
->SetCellOverflow(4, 0, false);
551 grid
->SetCellSize(7, 1, 3, 4);
552 grid
->SetCellAlignment(7, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
553 grid
->SetCellValue(7, 1, wxT("Big box!"));
555 // create a separator-like row: it's grey and it's non-resizable
556 grid
->DisableRowResize(10);
557 grid
->SetRowSize(10, 30);
558 attr
= new wxGridCellAttr
;
559 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
560 grid
->SetRowAttr(10, attr
);
561 grid
->SetCellValue(10, 0, "You can't resize this row interactively -- try it");
563 // this does exactly nothing except testing that SetAttr() handles NULL
564 // attributes and does reference counting correctly
565 grid
->SetAttr(11, 11, NULL
);
566 grid
->SetAttr(11, 11, new wxGridCellAttr
);
567 grid
->SetAttr(11, 11, NULL
);
569 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
575 topSizer
->Add( logWin
,
580 SetSizerAndFit( topSizer
);
587 GridFrame::~GridFrame()
590 delete wxLog::SetActiveTarget(m_logOld
);
595 void GridFrame::SetDefaults()
597 GetMenuBar()->Check( ID_TOGGLEROWLABELS
, true );
598 GetMenuBar()->Check( ID_TOGGLECOLLABELS
, true );
599 GetMenuBar()->Check( ID_TOGGLEEDIT
, true );
600 GetMenuBar()->Check( ID_TOGGLEROWSIZING
, true );
601 GetMenuBar()->Check( ID_TOGGLECOLSIZING
, true );
602 GetMenuBar()->Check( ID_TOGGLECOLMOVING
, false );
603 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING
, true );
604 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL
, false );
605 GetMenuBar()->Check( ID_TOGGLEGRIDLINES
, true );
606 GetMenuBar()->Check( ID_CELLOVERFLOW
, true );
610 void GridFrame::ToggleRowLabels( wxCommandEvent
& WXUNUSED(ev
) )
612 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS
) )
614 grid
->SetRowLabelSize( grid
->GetDefaultRowLabelSize() );
618 grid
->SetRowLabelSize( 0 );
623 void GridFrame::ToggleColLabels( wxCommandEvent
& WXUNUSED(ev
) )
625 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS
) )
627 grid
->SetColLabelSize( grid
->GetDefaultColLabelSize() );
631 grid
->SetColLabelSize( 0 );
636 void GridFrame::ToggleEditing( wxCommandEvent
& WXUNUSED(ev
) )
639 GetMenuBar()->IsChecked( ID_TOGGLEEDIT
) );
643 void GridFrame::ToggleRowSizing( wxCommandEvent
& WXUNUSED(ev
) )
645 grid
->EnableDragRowSize(
646 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING
) );
650 void GridFrame::ToggleColSizing( wxCommandEvent
& WXUNUSED(ev
) )
652 grid
->EnableDragColSize(
653 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING
) );
656 void GridFrame::ToggleColMoving( wxCommandEvent
& WXUNUSED(ev
) )
658 grid
->EnableDragColMove(
659 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING
) );
662 void GridFrame::ToggleGridSizing( wxCommandEvent
& WXUNUSED(ev
) )
664 grid
->EnableDragGridSize(
665 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING
) );
668 void GridFrame::ToggleGridDragCell( wxCommandEvent
& WXUNUSED(ev
) )
670 grid
->EnableDragCell(
671 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL
) );
674 void GridFrame::SetNativeColHeader( wxCommandEvent
& WXUNUSED(ev
) )
676 CustomColumnHeadersProvider
* provider
=
677 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
678 provider
->UseCustomColHeaders(false);
679 grid
->SetUseNativeColLabels(true);
682 void GridFrame::SetCustomColHeader( wxCommandEvent
& WXUNUSED(ev
) )
684 CustomColumnHeadersProvider
* provider
=
685 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
686 provider
->UseCustomColHeaders(true);
687 grid
->SetUseNativeColLabels(false);
690 void GridFrame::SetDefaultColHeader( wxCommandEvent
& WXUNUSED(ev
) )
692 CustomColumnHeadersProvider
* provider
=
693 static_cast<CustomColumnHeadersProvider
*>(grid
->GetTable()->GetAttrProvider());
694 provider
->UseCustomColHeaders(false);
695 grid
->SetUseNativeColLabels(false);
699 void GridFrame::OnGridCustomTab(wxGridEvent
& event
)
701 // just for testing, make the cursor move up and down instead of the usual
703 if ( event
.ShiftDown() )
705 if ( grid
->GetGridCursorRow() > 0 )
706 grid
->MoveCursorUp( false );
710 if ( grid
->GetGridCursorRow() < grid
->GetNumberRows() - 1 )
711 grid
->MoveCursorDown( false );
715 void GridFrame::SetTabBehaviour(wxCommandEvent
& event
)
717 // To make any built-in behaviour work, we need to disable the custom TAB
718 // handler, otherwise it would be overriding them.
719 grid
->Disconnect(wxEVT_GRID_TABBING
,
720 wxGridEventHandler(GridFrame::OnGridCustomTab
));
722 grid
->SetTabBehaviour(
723 static_cast<wxGrid::TabBehaviour
>(event
.GetId() - ID_TAB_STOP
)
727 void GridFrame::SetTabCustomHandler(wxCommandEvent
&)
729 grid
->Connect(wxEVT_GRID_TABBING
,
730 wxGridEventHandler(GridFrame::OnGridCustomTab
),
735 void GridFrame::ToggleGridLines( wxCommandEvent
& WXUNUSED(ev
) )
737 grid
->EnableGridLines(
738 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES
) );
741 void GridFrame::OnSetHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
743 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
745 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
746 wxT("Pen Width"), 11, choices
);
748 int current
= grid
->GetCellHighlightPenWidth();
749 dlg
.SetSelection(current
);
750 if (dlg
.ShowModal() == wxID_OK
) {
751 grid
->SetCellHighlightPenWidth(dlg
.GetSelection());
755 void GridFrame::OnSetROHighlightWidth( wxCommandEvent
& WXUNUSED(ev
) )
757 wxString choices
[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
759 wxSingleChoiceDialog
dlg(this, wxT("Choose the thickness of the highlight pen:"),
760 wxT("Pen Width"), 11, choices
);
762 int current
= grid
->GetCellHighlightROPenWidth();
763 dlg
.SetSelection(current
);
764 if (dlg
.ShowModal() == wxID_OK
) {
765 grid
->SetCellHighlightROPenWidth(dlg
.GetSelection());
771 void GridFrame::AutoSizeCols( wxCommandEvent
& WXUNUSED(ev
) )
773 grid
->AutoSizeColumns();
777 void GridFrame::CellOverflow( wxCommandEvent
& ev
)
779 grid
->SetDefaultCellOverflow(ev
.IsChecked());
783 void GridFrame::ResizeCell( wxCommandEvent
& ev
)
786 grid
->SetCellSize( 7, 1, 5, 5 );
788 grid
->SetCellSize( 7, 1, 1, 5 );
792 void GridFrame::SetLabelColour( wxCommandEvent
& WXUNUSED(ev
) )
794 wxColourDialog
dlg( NULL
);
795 if ( dlg
.ShowModal() == wxID_OK
)
797 wxColourData retData
;
798 retData
= dlg
.GetColourData();
799 wxColour colour
= retData
.GetColour();
801 grid
->SetLabelBackgroundColour( colour
);
806 void GridFrame::SetLabelTextColour( wxCommandEvent
& WXUNUSED(ev
) )
808 wxColourDialog
dlg( NULL
);
809 if ( dlg
.ShowModal() == wxID_OK
)
811 wxColourData retData
;
812 retData
= dlg
.GetColourData();
813 wxColour colour
= retData
.GetColour();
815 grid
->SetLabelTextColour( colour
);
819 void GridFrame::SetLabelFont( wxCommandEvent
& WXUNUSED(ev
) )
821 wxFont font
= wxGetFontFromUser(this);
824 grid
->SetLabelFont(font
);
828 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
831 grid
->GetRowLabelAlignment( &horiz
, &vert
);
836 horiz
= wxALIGN_CENTRE
;
840 horiz
= wxALIGN_RIGHT
;
844 horiz
= wxALIGN_LEFT
;
848 grid
->SetRowLabelAlignment( horiz
, vert
);
851 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
854 grid
->GetRowLabelAlignment( &horiz
, &vert
);
859 vert
= wxALIGN_CENTRE
;
863 vert
= wxALIGN_BOTTOM
;
871 grid
->SetRowLabelAlignment( horiz
, vert
);
875 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent
& WXUNUSED(ev
) )
878 grid
->GetColLabelAlignment( &horiz
, &vert
);
883 horiz
= wxALIGN_CENTRE
;
887 horiz
= wxALIGN_RIGHT
;
891 horiz
= wxALIGN_LEFT
;
895 grid
->SetColLabelAlignment( horiz
, vert
);
899 void GridFrame::SetColLabelVertAlignment( wxCommandEvent
& WXUNUSED(ev
) )
902 grid
->GetColLabelAlignment( &horiz
, &vert
);
907 vert
= wxALIGN_CENTRE
;
911 vert
= wxALIGN_BOTTOM
;
919 grid
->SetColLabelAlignment( horiz
, vert
);
923 void GridFrame::SetGridLineColour( wxCommandEvent
& WXUNUSED(ev
) )
925 wxColourDialog
dlg( NULL
);
926 if ( dlg
.ShowModal() == wxID_OK
)
928 wxColourData retData
;
929 retData
= dlg
.GetColourData();
930 wxColour colour
= retData
.GetColour();
932 grid
->SetGridLineColour( colour
);
937 void GridFrame::InsertRow( wxCommandEvent
& WXUNUSED(ev
) )
939 grid
->InsertRows( grid
->GetGridCursorRow(), 1 );
943 void GridFrame::InsertCol( wxCommandEvent
& WXUNUSED(ev
) )
945 grid
->InsertCols( grid
->GetGridCursorCol(), 1 );
949 void GridFrame::DeleteSelectedRows( wxCommandEvent
& WXUNUSED(ev
) )
951 if ( grid
->IsSelection() )
953 wxGridUpdateLocker
locker(grid
);
954 for ( int n
= 0; n
< grid
->GetNumberRows(); )
956 if ( grid
->IsInSelection( n
, 0 ) )
957 grid
->DeleteRows( n
, 1 );
965 void GridFrame::AutoSizeRow(wxCommandEvent
& WXUNUSED(event
))
967 wxGridUpdateLocker
locker(grid
);
968 const wxArrayInt sels
= grid
->GetSelectedRows();
969 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
971 grid
->AutoSizeRow( sels
[n
], false );
975 void GridFrame::AutoSizeCol(wxCommandEvent
& WXUNUSED(event
))
977 wxGridUpdateLocker
locker(grid
);
978 const wxArrayInt sels
= grid
->GetSelectedCols();
979 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
981 grid
->AutoSizeColumn( sels
[n
], false );
985 void GridFrame::AutoSizeRowLabel(wxCommandEvent
& WXUNUSED(event
))
987 wxGridUpdateLocker
locker(grid
);
988 const wxArrayInt sels
= grid
->GetSelectedRows();
989 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
991 grid
->AutoSizeRowLabelSize( sels
[n
] );
995 void GridFrame::AutoSizeColLabel(wxCommandEvent
& WXUNUSED(event
))
997 wxGridUpdateLocker
locker(grid
);
998 const wxArrayInt sels
= grid
->GetSelectedCols();
999 for ( size_t n
= 0, count
= sels
.size(); n
< count
; n
++ )
1001 grid
->AutoSizeColLabelSize( sels
[n
] );
1005 void GridFrame::AutoSizeLabelsCol(wxCommandEvent
& WXUNUSED(event
))
1007 grid
->SetColLabelSize( wxGRID_AUTOSIZE
);
1010 void GridFrame::AutoSizeLabelsRow(wxCommandEvent
& WXUNUSED(event
))
1012 grid
->SetRowLabelSize( wxGRID_AUTOSIZE
);
1015 void GridFrame::AutoSizeTable(wxCommandEvent
& WXUNUSED(event
))
1022 void GridFrame::DeleteSelectedCols( wxCommandEvent
& WXUNUSED(ev
) )
1024 if ( grid
->IsSelection() )
1026 wxGridUpdateLocker
locker(grid
);
1027 for ( int n
= 0; n
< grid
->GetNumberCols(); )
1029 if ( grid
->IsInSelection( 0 , n
) )
1030 grid
->DeleteCols( n
, 1 );
1038 void GridFrame::ClearGrid( wxCommandEvent
& WXUNUSED(ev
) )
1043 void GridFrame::SelectCells( wxCommandEvent
& WXUNUSED(ev
) )
1045 grid
->SetSelectionMode( wxGrid::wxGridSelectCells
);
1048 void GridFrame::SelectRows( wxCommandEvent
& WXUNUSED(ev
) )
1050 grid
->SetSelectionMode( wxGrid::wxGridSelectRows
);
1053 void GridFrame::SelectCols( wxCommandEvent
& WXUNUSED(ev
) )
1055 grid
->SetSelectionMode( wxGrid::wxGridSelectColumns
);
1058 void GridFrame::SelectRowsOrCols( wxCommandEvent
& WXUNUSED(ev
) )
1060 grid
->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns
);
1063 void GridFrame::SetCellFgColour( wxCommandEvent
& WXUNUSED(ev
) )
1065 wxColour col
= wxGetColourFromUser(this);
1068 grid
->SetDefaultCellTextColour(col
);
1073 void GridFrame::SetCellBgColour( wxCommandEvent
& WXUNUSED(ev
) )
1075 wxColour col
= wxGetColourFromUser(this);
1078 // Check the new Refresh function by passing it a rectangle
1079 // which exactly fits the grid.
1081 wxRect
r(pt
, grid
->GetSize());
1082 grid
->SetDefaultCellBackgroundColour(col
);
1083 grid
->Refresh(true, &r
);
1087 void GridFrame::DeselectCell(wxCommandEvent
& WXUNUSED(event
))
1089 grid
->DeselectCell(3, 1);
1092 void GridFrame::DeselectCol(wxCommandEvent
& WXUNUSED(event
))
1094 grid
->DeselectCol(2);
1097 void GridFrame::DeselectRow(wxCommandEvent
& WXUNUSED(event
))
1099 grid
->DeselectRow(2);
1102 void GridFrame::DeselectAll(wxCommandEvent
& WXUNUSED(event
))
1104 grid
->ClearSelection();
1107 void GridFrame::SelectCell(wxCommandEvent
& WXUNUSED(event
))
1109 grid
->SelectBlock(3, 1, 3, 1, m_addToSel
);
1112 void GridFrame::SelectCol(wxCommandEvent
& WXUNUSED(event
))
1114 grid
->SelectCol(2, m_addToSel
);
1117 void GridFrame::SelectRow(wxCommandEvent
& WXUNUSED(event
))
1119 grid
->SelectRow(2, m_addToSel
);
1122 void GridFrame::SelectAll(wxCommandEvent
& WXUNUSED(event
))
1127 void GridFrame::OnAddToSelectToggle(wxCommandEvent
& event
)
1129 m_addToSel
= event
.IsChecked();
1132 void GridFrame::OnLabelLeftClick( wxGridEvent
& ev
)
1135 if ( ev
.GetRow() != -1 )
1137 logBuf
<< wxT("Left click on row label ") << ev
.GetRow();
1139 else if ( ev
.GetCol() != -1 )
1141 logBuf
<< wxT("Left click on col label ") << ev
.GetCol();
1145 logBuf
<< wxT("Left click on corner label");
1148 if ( ev
.ShiftDown() )
1149 logBuf
<< wxT(" (shift down)");
1150 if ( ev
.ControlDown() )
1151 logBuf
<< wxT(" (control down)");
1152 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1154 // you must call event skip if you want default grid processing
1160 void GridFrame::OnCellLeftClick( wxGridEvent
& ev
)
1162 wxLogMessage(wxT("Left click at row %d, col %d"), ev
.GetRow(), ev
.GetCol());
1164 // you must call event skip if you want default grid processing
1165 // (cell highlighting etc.)
1171 void GridFrame::OnRowSize( wxGridSizeEvent
& ev
)
1173 const int row
= ev
.GetRowOrCol();
1175 wxLogMessage("Resized row %d, new height = %d",
1176 row
, grid
->GetRowSize(row
));
1182 void GridFrame::OnColSize( wxGridSizeEvent
& ev
)
1184 const int col
= ev
.GetRowOrCol();
1186 wxLogMessage("Resized column %d, new width = %d",
1187 col
, grid
->GetColSize(col
));
1192 void GridFrame::OnColAutoSize( wxGridSizeEvent
&event
)
1194 // Fit even-numbered columns to their contents while using the default
1195 // behaviour for the odd-numbered ones to be able to see the difference.
1196 int col
= event
.GetRowOrCol();
1199 wxLogMessage("Auto-sizing column %d to fit its contents", col
);
1200 grid
->AutoSizeColumn(col
);
1208 void GridFrame::OnSelectCell( wxGridEvent
& ev
)
1211 if ( ev
.Selecting() )
1212 logBuf
<< wxT("Selected ");
1214 logBuf
<< wxT("Deselected ");
1215 logBuf
<< wxT("cell at row ") << ev
.GetRow()
1216 << wxT(" col ") << ev
.GetCol()
1217 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1218 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1219 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1220 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1222 //Indicate whether this column was moved
1223 if ( ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() ) != ev
.GetCol() )
1224 logBuf
<< wxT(" *** Column moved, current position: ") << ((wxGrid
*)ev
.GetEventObject())->GetColPos( ev
.GetCol() );
1226 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1228 // you must call Skip() if you want the default processing
1229 // to occur in wxGrid
1233 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent
& ev
)
1236 if ( ev
.Selecting() )
1237 logBuf
<< wxT("Selected ");
1239 logBuf
<< wxT("Deselected ");
1240 logBuf
<< wxT("cells from row ") << ev
.GetTopRow()
1241 << wxT(" col ") << ev
.GetLeftCol()
1242 << wxT(" to row ") << ev
.GetBottomRow()
1243 << wxT(" col ") << ev
.GetRightCol()
1244 << wxT(" ( ControlDown: ")<< (ev
.ControlDown() ? 'T':'F')
1245 << wxT(", ShiftDown: ")<< (ev
.ShiftDown() ? 'T':'F')
1246 << wxT(", AltDown: ")<< (ev
.AltDown() ? 'T':'F')
1247 << wxT(", MetaDown: ")<< (ev
.MetaDown() ? 'T':'F') << wxT(" )");
1248 wxLogMessage( wxT("%s"), logBuf
.c_str() );
1253 void GridFrame::OnCellValueChanging( wxGridEvent
& ev
)
1255 int row
= ev
.GetRow(),
1258 wxLogMessage("Value of cell at (%d, %d): about to change "
1259 "from \"%s\" to \"%s\"",
1261 grid
->GetCellValue(row
, col
), ev
.GetString());
1263 // test how vetoing works
1264 if ( ev
.GetString() == "42" )
1266 wxLogMessage("Vetoing the change.");
1274 void GridFrame::OnCellValueChanged( wxGridEvent
& ev
)
1276 int row
= ev
.GetRow(),
1279 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1282 grid
->GetCellValue(row
, col
), ev
.GetString());
1287 void GridFrame::OnCellBeginDrag( wxGridEvent
& ev
)
1289 wxLogMessage(wxT("Got request to drag cell at row %d, col %d"),
1290 ev
.GetRow(), ev
.GetCol());
1295 void GridFrame::OnEditorShown( wxGridEvent
& ev
)
1298 if ( (ev
.GetCol() == 4) &&
1299 (ev
.GetRow() == 0) &&
1300 (wxMessageBox(wxT("Are you sure you wish to edit this cell"),
1301 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1307 wxLogMessage( wxT("Cell editor shown.") );
1312 void GridFrame::OnEditorHidden( wxGridEvent
& ev
)
1315 if ( (ev
.GetCol() == 4) &&
1316 (ev
.GetRow() == 0) &&
1317 (wxMessageBox(wxT("Are you sure you wish to finish editing this cell"),
1318 wxT("Checking"),wxYES_NO
) == wxNO
) ) {
1324 wxLogMessage( wxT("Cell editor hidden.") );
1329 void GridFrame::About( wxCommandEvent
& WXUNUSED(ev
) )
1331 wxAboutDialogInfo aboutInfo
;
1332 aboutInfo
.SetName(wxT("wxGrid demo"));
1333 aboutInfo
.SetDescription(_("wxGrid sample program"));
1334 aboutInfo
.AddDeveloper(wxT("Michael Bedward"));
1335 aboutInfo
.AddDeveloper(wxT("Julian Smart"));
1336 aboutInfo
.AddDeveloper(wxT("Vadim Zeitlin"));
1338 // this is just to force the generic version of the about
1339 // dialog under wxMSW so that it's easy to test if the grid
1340 // repaints correctly when it has lost focus and a dialog
1341 // (different from the Windows standard message box -- it doesn't
1342 // work with it for some reason) is moved over it.
1343 aboutInfo
.SetWebSite(wxT("http://www.wxwidgets.org"));
1345 wxAboutBox(aboutInfo
);
1349 void GridFrame::OnQuit( wxCommandEvent
& WXUNUSED(ev
) )
1354 void GridFrame::OnBugsTable(wxCommandEvent
& )
1356 BugsGridFrame
*frame
= new BugsGridFrame
;
1360 // ----------------------------------------------------------------------------
1361 // MyGridCellAttrProvider
1362 // ----------------------------------------------------------------------------
1364 MyGridCellAttrProvider::MyGridCellAttrProvider()
1366 m_attrForOddRows
= new wxGridCellAttr
;
1367 m_attrForOddRows
->SetBackgroundColour(*wxLIGHT_GREY
);
1370 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1372 m_attrForOddRows
->DecRef();
1375 wxGridCellAttr
*MyGridCellAttrProvider::GetAttr(int row
, int col
,
1376 wxGridCellAttr::wxAttrKind kind
/* = wxGridCellAttr::Any */) const
1378 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
, col
, kind
);
1384 attr
= m_attrForOddRows
;
1389 if ( !attr
->HasBackgroundColour() )
1391 wxGridCellAttr
*attrNew
= attr
->Clone();
1394 attr
->SetBackgroundColour(*wxLIGHT_GREY
);
1402 void GridFrame::OnVTable(wxCommandEvent
& )
1404 static long s_sizeGrid
= 10000;
1406 s_sizeGrid
= wxGetNumberFromUser(wxT("Size of the table to create"),
1408 wxT("wxGridDemo question"),
1412 if ( s_sizeGrid
!= -1 )
1414 BigGridFrame
* win
= new BigGridFrame(s_sizeGrid
);
1419 // ----------------------------------------------------------------------------
1420 // MyGridCellRenderer
1421 // ----------------------------------------------------------------------------
1423 // do something that the default renderer doesn't here just to show that it is
1424 // possible to alter the appearance of the cell beyond what the attributes
1426 void MyGridCellRenderer::Draw(wxGrid
& grid
,
1427 wxGridCellAttr
& attr
,
1433 wxGridCellStringRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1435 dc
.SetPen(*wxGREEN_PEN
);
1436 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1437 dc
.DrawEllipse(rect
);
1440 // ============================================================================
1441 // BigGridFrame and BigGridTable: Sample of a non-standard table
1442 // ============================================================================
1444 BigGridFrame::BigGridFrame(long sizeGrid
)
1445 : wxFrame(NULL
, wxID_ANY
, wxT("Plugin Virtual Table"),
1446 wxDefaultPosition
, wxSize(500, 450))
1448 m_grid
= new wxGrid(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
1449 m_table
= new BigGridTable(sizeGrid
);
1451 // VZ: I don't understand why this slows down the display that much,
1452 // must profile it...
1453 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1455 m_grid
->SetTable(m_table
, true);
1457 #if defined __WXMOTIF__
1458 // MB: the grid isn't getting a sensible default size under wxMotif
1460 GetClientSize( &cw
, &ch
);
1461 m_grid
->SetSize( cw
, ch
);
1465 // ============================================================================
1466 // BugsGridFrame: a "realistic" table
1467 // ============================================================================
1469 // ----------------------------------------------------------------------------
1471 // ----------------------------------------------------------------------------
1494 static const wxString severities
[] =
1503 static struct BugsGridData
1509 wxChar platform
[12];
1511 } gs_dataBugsGrid
[] =
1513 { 18, wxT("foo doesn't work"), Sev_Major
, 1, wxT("wxMSW"), true },
1514 { 27, wxT("bar crashes"), Sev_Critical
, 1, wxT("all"), false },
1515 { 45, wxT("printing is slow"), Sev_Minor
, 3, wxT("wxMSW"), true },
1516 { 68, wxT("Rectangle() fails"), Sev_Normal
, 1, wxT("wxMSW"), false },
1519 static const wxChar
*headers
[Col_Max
] =
1529 // ----------------------------------------------------------------------------
1531 // ----------------------------------------------------------------------------
1533 wxString
BugsGridTable::GetTypeName(int WXUNUSED(row
), int col
)
1539 return wxGRID_VALUE_NUMBER
;;
1542 // fall thorugh (TODO should be a list)
1545 return wxString::Format(wxT("%s:80"), wxGRID_VALUE_STRING
);
1548 return wxString::Format(wxT("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE
);
1551 return wxGRID_VALUE_BOOL
;
1554 wxFAIL_MSG(wxT("unknown column"));
1556 return wxEmptyString
;
1559 int BugsGridTable::GetNumberRows()
1561 return WXSIZEOF(gs_dataBugsGrid
);
1564 int BugsGridTable::GetNumberCols()
1569 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row
), int WXUNUSED(col
) )
1574 wxString
BugsGridTable::GetValue( int row
, int col
)
1576 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1581 return wxString::Format(wxT("%d"), gd
.id
);
1584 return wxString::Format(wxT("%d"), gd
.prio
);
1587 return gd
.opened
? wxT("1") : wxT("0");
1590 return severities
[gd
.severity
];
1599 return wxEmptyString
;
1602 void BugsGridTable::SetValue( int row
, int col
, const wxString
& value
)
1604 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1611 wxFAIL_MSG(wxT("unexpected column"));
1617 for ( n
= 0; n
< WXSIZEOF(severities
); n
++ )
1619 if ( severities
[n
] == value
)
1621 gd
.severity
= (Severity
)n
;
1626 if ( n
== WXSIZEOF(severities
) )
1628 wxLogWarning(wxT("Invalid severity value '%s'."),
1630 gd
.severity
= Sev_Normal
;
1636 wxStrncpy(gd
.summary
, value
, WXSIZEOF(gd
.summary
));
1640 wxStrncpy(gd
.platform
, value
, WXSIZEOF(gd
.platform
));
1646 BugsGridTable::CanGetValueAs(int WXUNUSED(row
),
1648 const wxString
& typeName
)
1650 if ( typeName
== wxGRID_VALUE_STRING
)
1654 else if ( typeName
== wxGRID_VALUE_BOOL
)
1656 return col
== Col_Opened
;
1658 else if ( typeName
== wxGRID_VALUE_NUMBER
)
1660 return col
== Col_Id
|| col
== Col_Priority
|| col
== Col_Severity
;
1668 bool BugsGridTable::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1670 return CanGetValueAs(row
, col
, typeName
);
1673 long BugsGridTable::GetValueAsLong( int row
, int col
)
1675 const BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1689 wxFAIL_MSG(wxT("unexpected column"));
1694 bool BugsGridTable::GetValueAsBool( int row
, int col
)
1696 if ( col
== Col_Opened
)
1698 return gs_dataBugsGrid
[row
].opened
;
1702 wxFAIL_MSG(wxT("unexpected column"));
1708 void BugsGridTable::SetValueAsLong( int row
, int col
, long value
)
1710 BugsGridData
& gd
= gs_dataBugsGrid
[row
];
1719 wxFAIL_MSG(wxT("unexpected column"));
1723 void BugsGridTable::SetValueAsBool( int row
, int col
, bool value
)
1725 if ( col
== Col_Opened
)
1727 gs_dataBugsGrid
[row
].opened
= value
;
1731 wxFAIL_MSG(wxT("unexpected column"));
1735 wxString
BugsGridTable::GetColLabelValue( int col
)
1737 return headers
[col
];
1740 // ----------------------------------------------------------------------------
1742 // ----------------------------------------------------------------------------
1744 BugsGridFrame::BugsGridFrame()
1745 : wxFrame(NULL
, wxID_ANY
, wxT("Bugs table"))
1747 wxGrid
*grid
= new wxGrid(this, wxID_ANY
);
1748 wxGridTableBase
*table
= new BugsGridTable();
1749 table
->SetAttrProvider(new MyGridCellAttrProvider
);
1750 grid
->SetTable(table
, true);
1752 wxGridCellAttr
*attrRO
= new wxGridCellAttr
,
1753 *attrRangeEditor
= new wxGridCellAttr
,
1754 *attrCombo
= new wxGridCellAttr
;
1756 attrRO
->SetReadOnly();
1757 attrRangeEditor
->SetEditor(new wxGridCellNumberEditor(1, 5));
1758 attrCombo
->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities
),
1761 grid
->SetColAttr(Col_Id
, attrRO
);
1762 grid
->SetColAttr(Col_Priority
, attrRangeEditor
);
1763 grid
->SetColAttr(Col_Severity
, attrCombo
);
1766 SetClientSize(grid
->GetSize());
1769 // ============================================================================
1770 // TabularGrid: grid used for display of tabular data
1771 // ============================================================================
1773 class TabularGridTable
: public wxGridTableBase
1790 TabularGridTable() { m_sortOrder
= NULL
; }
1792 virtual int GetNumberRows() { return ROW_MAX
; }
1793 virtual int GetNumberCols() { return COL_MAX
; }
1795 virtual wxString
GetValue(int row
, int col
)
1798 row
= m_sortOrder
[row
];
1804 return GetNameOrExt(row
, col
);
1807 return wxString::Format("%lu", GetSize(row
));
1810 return GetDate(row
).FormatDate();
1814 wxFAIL_MSG( "unknown column" );
1820 virtual void SetValue(int, int, const wxString
&)
1822 wxFAIL_MSG( "shouldn't be called" );
1825 virtual wxString
GetColLabelValue(int col
)
1827 // notice that column parameter here always refers to the internal
1828 // column index, independently of its position on the screen
1829 static const char *labels
[] = { "Name", "Extension", "Size", "Date" };
1830 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels
) == COL_MAX
, LabelsMismatch
);
1835 virtual void SetColLabelValue(int, const wxString
&)
1837 wxFAIL_MSG( "shouldn't be called" );
1840 void Sort(int col
, bool ascending
)
1842 // we hardcode all sorting orders for simplicity here
1843 static int sortOrders
[COL_MAX
][2][ROW_MAX
] =
1845 // descending ascending
1846 { { 2, 1, 0 }, { 0, 1, 2 } },
1847 { { 2, 1, 0 }, { 0, 1, 2 } },
1848 { { 2, 1, 0 }, { 0, 1, 2 } },
1849 { { 1, 0, 2 }, { 2, 0, 1 } },
1852 m_sortOrder
= col
== wxNOT_FOUND
? NULL
: sortOrders
[col
][ascending
];
1856 wxString
GetNameOrExt(int row
, int col
) const
1859 names
[] = { "autoexec.bat", "boot.ini", "io.sys" };
1860 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == ROW_MAX
, NamesMismatch
);
1862 const wxString
s(names
[row
]);
1863 return col
== COL_NAME
? s
.BeforeFirst('.') : s
.AfterLast('.');
1866 unsigned long GetSize(int row
) const
1868 static const unsigned long
1869 sizes
[] = { 412, 604, 40774 };
1870 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes
) == ROW_MAX
, SizesMismatch
);
1875 wxDateTime
GetDate(int row
) const
1878 dates
[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1879 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates
) == ROW_MAX
, DatesMismatch
);
1882 dt
.ParseISODate(dates
[row
]);
1889 // specialized text control for column indexes entry
1890 class ColIndexEntry
: public wxTextCtrl
1893 ColIndexEntry(wxWindow
*parent
)
1894 : wxTextCtrl(parent
, wxID_ANY
, "")
1896 SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1902 if ( !GetValue().ToULong(&col
) || col
> TabularGridTable::COL_MAX
)
1912 virtual wxSize
DoGetBestSize() const
1914 wxSize size
= wxTextCtrl::DoGetBestSize();
1915 size
.x
= 3*GetCharWidth();
1920 class TabularGridFrame
: public wxFrame
1928 Id_Check_UseNativeHeader
,
1929 Id_Check_DrawNativeLabels
,
1930 Id_Check_ShowRowLabels
,
1931 Id_Check_EnableColMove
1936 void OnToggleUseNativeHeader(wxCommandEvent
&)
1938 m_grid
->UseNativeColHeader(m_chkUseNative
->IsChecked());
1941 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent
& event
)
1943 // we don't draw labels at all, native or otherwise, if we use the
1944 // native header control
1945 event
.Enable( !m_chkUseNative
->GetValue() );
1948 void OnToggleDrawNativeLabels(wxCommandEvent
&)
1950 m_grid
->SetUseNativeColLabels(m_chkDrawNative
->IsChecked());
1953 void OnToggleShowRowLabels(wxCommandEvent
&)
1955 m_grid
->SetRowLabelSize(m_chkShowRowLabels
->IsChecked()
1960 void OnToggleColMove(wxCommandEvent
&)
1962 m_grid
->EnableDragColMove(m_chkEnableColMove
->IsChecked());
1965 void OnShowHideColumn(wxCommandEvent
& event
)
1967 int col
= m_txtColShowHide
->GetCol();
1970 m_grid
->SetColSize(col
,
1971 event
.GetId() == wxID_ADD
? wxGRID_AUTOSIZE
: 0);
1973 UpdateOrderAndVisibility();
1977 void OnMoveColumn(wxCommandEvent
&)
1979 int col
= m_txtColIndex
->GetCol();
1980 int pos
= m_txtColPos
->GetCol();
1981 if ( col
== -1 || pos
== -1 )
1984 m_grid
->SetColPos(col
, pos
);
1986 UpdateOrderAndVisibility();
1989 void OnResetColumnOrder(wxCommandEvent
&)
1991 m_grid
->ResetColPos();
1993 UpdateOrderAndVisibility();
1996 void OnGridColSort(wxGridEvent
& event
)
1998 const int col
= event
.GetCol();
1999 m_table
->Sort(col
, !(m_grid
->IsSortingBy(col
) &&
2000 m_grid
->IsSortOrderAscending()));
2003 void OnGridColMove(wxGridEvent
& event
)
2005 // can't update it yet as the order hasn't been changed, so do it a bit
2007 m_shouldUpdateOrder
= true;
2012 void OnGridColSize(wxGridSizeEvent
& event
)
2014 // we only catch this event to react to the user showing or hiding this
2015 // column using the header control menu and not because we're
2016 // interested in column resizing
2017 UpdateOrderAndVisibility();
2022 void OnIdle(wxIdleEvent
& event
)
2024 if ( m_shouldUpdateOrder
)
2026 m_shouldUpdateOrder
= false;
2027 UpdateOrderAndVisibility();
2033 void UpdateOrderAndVisibility()
2036 for ( int pos
= 0; pos
< TabularGridTable::COL_MAX
; pos
++ )
2038 const int col
= m_grid
->GetColAt(pos
);
2039 const bool isHidden
= m_grid
->GetColSize(col
) == 0;
2050 m_statOrder
->SetLabel(s
);
2055 TabularGridTable
*m_table
;
2056 wxCheckBox
*m_chkUseNative
,
2058 *m_chkShowRowLabels
,
2059 *m_chkEnableColMove
;
2061 ColIndexEntry
*m_txtColIndex
,
2065 wxStaticText
*m_statOrder
;
2067 // fla for EVT_IDLE handler
2068 bool m_shouldUpdateOrder
;
2070 wxDECLARE_NO_COPY_CLASS(TabularGridFrame
);
2071 DECLARE_EVENT_TABLE()
2074 BEGIN_EVENT_TABLE(TabularGridFrame
, wxFrame
)
2075 EVT_CHECKBOX(Id_Check_UseNativeHeader
,
2076 TabularGridFrame::OnToggleUseNativeHeader
)
2077 EVT_CHECKBOX(Id_Check_DrawNativeLabels
,
2078 TabularGridFrame::OnToggleDrawNativeLabels
)
2079 EVT_CHECKBOX(Id_Check_ShowRowLabels
,
2080 TabularGridFrame::OnToggleShowRowLabels
)
2081 EVT_CHECKBOX(Id_Check_EnableColMove
,
2082 TabularGridFrame::OnToggleColMove
)
2084 EVT_UPDATE_UI(Id_Check_DrawNativeLabels
,
2085 TabularGridFrame::OnUpdateDrawNativeLabelsUI
)
2087 EVT_BUTTON(wxID_APPLY
, TabularGridFrame::OnMoveColumn
)
2088 EVT_BUTTON(wxID_RESET
, TabularGridFrame::OnResetColumnOrder
)
2089 EVT_BUTTON(wxID_ADD
, TabularGridFrame::OnShowHideColumn
)
2090 EVT_BUTTON(wxID_DELETE
, TabularGridFrame::OnShowHideColumn
)
2092 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort
)
2093 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove
)
2094 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize
)
2096 EVT_IDLE(TabularGridFrame::OnIdle
)
2099 TabularGridFrame::TabularGridFrame()
2100 : wxFrame(NULL
, wxID_ANY
, "Tabular table")
2102 m_shouldUpdateOrder
= false;
2104 wxPanel
* const panel
= new wxPanel(this);
2106 // create and initialize the grid with the specified data
2107 m_table
= new TabularGridTable
;
2108 m_grid
= new wxGrid(panel
, wxID_ANY
,
2109 wxDefaultPosition
, wxDefaultSize
,
2110 wxBORDER_STATIC
| wxWANTS_CHARS
);
2111 m_grid
->SetTable(m_table
, true, wxGrid::wxGridSelectRows
);
2113 m_grid
->EnableDragColMove();
2114 m_grid
->UseNativeColHeader();
2115 m_grid
->HideRowLabels();
2117 // add it and the other controls to the frame
2118 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
2119 sizerTop
->Add(m_grid
, wxSizerFlags(1).Expand().Border());
2121 wxSizer
* const sizerControls
= new wxBoxSizer(wxHORIZONTAL
);
2123 wxSizer
* const sizerStyles
= new wxBoxSizer(wxVERTICAL
);
2124 m_chkUseNative
= new wxCheckBox(panel
, Id_Check_UseNativeHeader
,
2125 "&Use native header");
2126 m_chkUseNative
->SetValue(true);
2127 sizerStyles
->Add(m_chkUseNative
, wxSizerFlags().Border());
2129 m_chkDrawNative
= new wxCheckBox(panel
, Id_Check_DrawNativeLabels
,
2130 "&Draw native column labels");
2131 sizerStyles
->Add(m_chkDrawNative
, wxSizerFlags().Border());
2133 m_chkShowRowLabels
= new wxCheckBox(panel
, Id_Check_ShowRowLabels
,
2134 "Show &row labels");
2135 sizerStyles
->Add(m_chkShowRowLabels
, wxSizerFlags().Border());
2137 m_chkEnableColMove
= new wxCheckBox(panel
, Id_Check_EnableColMove
,
2138 "Allow column re&ordering");
2139 m_chkEnableColMove
->SetValue(true);
2140 sizerStyles
->Add(m_chkEnableColMove
, wxSizerFlags().Border());
2141 sizerControls
->Add(sizerStyles
);
2143 sizerControls
->AddSpacer(10);
2145 wxSizer
* const sizerColumns
= new wxBoxSizer(wxVERTICAL
);
2146 wxSizer
* const sizerMoveCols
= new wxBoxSizer(wxHORIZONTAL
);
2148 flagsHorz(wxSizerFlags().Border(wxLEFT
| wxRIGHT
).Centre());
2149 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&Move column"),
2151 m_txtColIndex
= new ColIndexEntry(panel
);
2152 sizerMoveCols
->Add(m_txtColIndex
, flagsHorz
);
2153 sizerMoveCols
->Add(new wxStaticText(panel
, wxID_ANY
, "&to"), flagsHorz
);
2154 m_txtColPos
= new ColIndexEntry(panel
);
2155 sizerMoveCols
->Add(m_txtColPos
, flagsHorz
);
2156 sizerMoveCols
->Add(new wxButton(panel
, wxID_APPLY
), flagsHorz
);
2158 sizerColumns
->Add(sizerMoveCols
, wxSizerFlags().Expand().Border(wxBOTTOM
));
2160 wxSizer
* const sizerShowCols
= new wxBoxSizer(wxHORIZONTAL
);
2161 sizerShowCols
->Add(new wxStaticText(panel
, wxID_ANY
, "Current order:"),
2163 m_statOrder
= new wxStaticText(panel
, wxID_ANY
, "<<< default >>>");
2164 sizerShowCols
->Add(m_statOrder
, flagsHorz
);
2165 sizerShowCols
->Add(new wxButton(panel
, wxID_RESET
, "&Reset order"));
2166 sizerColumns
->Add(sizerShowCols
, wxSizerFlags().Expand().Border(wxTOP
));
2168 wxSizer
* const sizerShowHide
= new wxBoxSizer(wxHORIZONTAL
);
2169 sizerShowHide
->Add(new wxStaticText(panel
, wxID_ANY
, "Show/hide column:"),
2171 m_txtColShowHide
= new ColIndexEntry(panel
);
2172 sizerShowHide
->Add(m_txtColShowHide
, flagsHorz
);
2173 sizerShowHide
->Add(new wxButton(panel
, wxID_ADD
, "&Show"), flagsHorz
);
2174 sizerShowHide
->Add(new wxButton(panel
, wxID_DELETE
, "&Hide"), flagsHorz
);
2175 sizerColumns
->Add(sizerShowHide
, wxSizerFlags().Expand().Border(wxTOP
));
2177 sizerControls
->Add(sizerColumns
, wxSizerFlags(1).Expand().Border());
2179 sizerTop
->Add(sizerControls
, wxSizerFlags().Expand().Border());
2181 panel
->SetSizer(sizerTop
);
2183 SetClientSize(panel
->GetBestSize());
2184 SetSizeHints(GetSize());
2189 void GridFrame::OnTabularTable(wxCommandEvent
&)
2191 new TabularGridFrame
;
2194 // Example using wxGrid::Render
2195 // Displays a preset selection or, if it exists, a selection block
2196 // Draws the selection to a wxBitmap and displays the bitmap
2197 void GridFrame::OnGridRender( wxCommandEvent
& event
)
2199 int styleRender
= 0, i
;
2200 bool useLometric
= false, defSize
= false;
2202 wxSize
sizeMargin( 0, 0 );
2203 wxPoint
pointOrigin( 0, 0 );
2205 wxMenu
* menu
= GetMenuBar()->GetMenu( 0 );
2206 wxMenuItem
* menuItem
= menu
->FindItem( ID_RENDER_ROW_LABEL
);
2207 menu
= menuItem
->GetMenu();
2209 if ( menu
->FindItem( ID_RENDER_ROW_LABEL
)->IsChecked() )
2210 styleRender
|= wxGRID_DRAW_ROWS_HEADER
;
2211 if ( menu
->FindItem( ID_RENDER_COL_LABEL
)->IsChecked() )
2212 styleRender
|= wxGRID_DRAW_COLS_HEADER
;
2213 if ( menu
->FindItem( ID_RENDER_GRID_LINES
)->IsChecked() )
2214 styleRender
|= wxGRID_DRAW_CELL_LINES
;
2215 if ( menu
->FindItem( ID_RENDER_GRID_BORDER
)->IsChecked() )
2216 styleRender
|= wxGRID_DRAW_BOX_RECT
;
2217 if ( menu
->FindItem( ID_RENDER_SELECT_HLIGHT
)->IsChecked() )
2218 styleRender
|= wxGRID_DRAW_SELECTION
;
2219 if ( menu
->FindItem( ID_RENDER_LOMETRIC
)->IsChecked() )
2221 if ( menu
->FindItem( ID_RENDER_MARGIN
)->IsChecked() )
2223 pointOrigin
.x
+= 50;
2224 pointOrigin
.y
+= 50;
2225 sizeMargin
.IncBy( 50 );
2227 if ( menu
->FindItem( ID_RENDER_ZOOM
)->IsChecked() )
2229 if ( menu
->FindItem( ID_RENDER_DEFAULT_SIZE
)->IsChecked() )
2232 // init render area coords with a default row and col selection
2233 wxGridCellCoords
topLeft( 0, 0 ), bottomRight( 8, 6 );
2234 // check whether we are printing a block selection
2235 // other selection types not catered for here
2236 if ( event
.GetId() == ID_RENDER_COORDS
)
2238 topLeft
.SetCol( 6 );
2239 topLeft
.SetRow( 4 );
2240 bottomRight
.SetCol( 15 );
2241 bottomRight
.SetRow( 29 );
2243 else if ( grid
->IsSelection() && grid
->GetSelectionBlockTopLeft().Count() )
2245 wxGridCellCoordsArray cells
= grid
->GetSelectionBlockTopLeft();
2246 if ( grid
->GetSelectionBlockBottomRight().Count() )
2248 cells
.Add( grid
->GetSelectionBlockBottomRight()[ 0 ] );
2249 topLeft
.Set( cells
[ 0 ].GetRow(),
2250 cells
[ 0 ].GetCol() );
2251 bottomRight
.Set( cells
[ 1 ].GetRow(),
2252 cells
[ 1 ].GetCol() );
2257 wxSize
sizeRender( 0, 0 );
2258 wxGridSizesInfo sizeinfo
= grid
->GetColSizes();
2259 for ( i
= topLeft
.GetCol(); i
<= bottomRight
.GetCol(); i
++ )
2261 sizeRender
.x
+= sizeinfo
.GetSize( i
);
2265 sizeinfo
= grid
->GetRowSizes();
2266 for ( i
= topLeft
.GetRow(); i
<= bottomRight
.GetRow(); i
++ )
2268 sizeRender
.y
+= sizeinfo
.GetSize( i
);
2271 if ( styleRender
& wxGRID_DRAW_ROWS_HEADER
)
2272 sizeRender
.x
+= grid
->GetRowLabelSize();
2273 if ( styleRender
& wxGRID_DRAW_COLS_HEADER
)
2274 sizeRender
.y
+= grid
->GetColLabelSize();
2276 sizeRender
.x
*= zoom
;
2277 sizeRender
.y
*= zoom
;
2279 // delete any existing render frame and create new one
2280 wxWindow
* win
= FindWindow( "frameRender" );
2284 wxFrame
* frame
= new wxFrame( this, wxID_ANY
, "Grid Render" );
2285 frame
->SetClientSize( 780, 400 );
2286 frame
->SetName( "frameRender" );
2288 wxPanel
* canvas
= new wxPanel( frame
, wxID_ANY
);
2290 // make bitmap large enough for margins if any
2292 sizeRender
.IncBy( sizeMargin
* 2 );
2294 sizeRender
.IncBy( sizeMargin
);
2296 wxBitmap
bmp( sizeRender
);
2297 // don't leave it larger or drawing will be scaled
2298 sizeRender
.DecBy( sizeMargin
* 2 );
2300 wxMemoryDC
memDc(bmp
);
2302 // default row labels have no background colour so set background
2303 memDc
.SetBackground( wxBrush( canvas
->GetBackgroundColour() ) );
2306 // convert sizeRender to mapping mode units if necessary
2309 memDc
.SetMapMode( wxMM_LOMETRIC
);
2310 sizeRender
.x
= memDc
.DeviceToLogicalXRel( sizeRender
.x
);
2311 sizeRender
.y
= memDc
.DeviceToLogicalYRel( sizeRender
.y
);
2314 // pass wxDefaultSize if menu item is checked
2316 sizeRender
= wxDefaultSize
;
2318 grid
->Render( memDc
,
2321 topLeft
, bottomRight
,
2322 wxGridRenderStyle( styleRender
) );
2326 canvas
->Connect( wxEVT_PAINT
,
2327 wxPaintEventHandler(GridFrame::OnRenderPaint
),
2334 void GridFrame::OnRenderPaint( wxPaintEvent
& event
)
2336 wxPanel
* canvas
= ( wxPanel
* )event
.GetEventObject();
2337 wxPaintDC
dc( canvas
);
2338 canvas
->PrepareDC( dc
);
2340 if ( !m_gridBitmap
.IsOk() )
2343 wxMemoryDC
memDc( m_gridBitmap
);
2346 m_gridBitmap
.GetWidth(),
2347 m_gridBitmap
.GetHeight(),
2351 void GridFrame::HideCol( wxCommandEvent
& WXUNUSED(event
) )
2356 void GridFrame::ShowCol( wxCommandEvent
& WXUNUSED(event
) )
2361 void GridFrame::HideRow( wxCommandEvent
& WXUNUSED(event
) )
2366 void GridFrame::ShowRow( wxCommandEvent
& WXUNUSED(event
) )