]> git.saurik.com Git - wxWidgets.git/blob - samples/grid/griddemo.cpp
Add wxFontInfo class to allow using named parameters for wxFont creation.
[wxWidgets.git] / samples / grid / griddemo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: griddemo.cpp
3 // Purpose: Grid control wxWidgets sample
4 // Author: Michael Bedward
5 // Modified by: Santiago Palacios
6 // RCS-ID: $Id$
7 // Copyright: (c) Michael Bedward, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29
30 #include "wx/colordlg.h"
31 #include "wx/fontdlg.h"
32 #include "wx/numdlg.h"
33 #include "wx/aboutdlg.h"
34
35 #include "wx/grid.h"
36 #include "wx/headerctrl.h"
37 #include "wx/generic/gridctrl.h"
38 #include "wx/generic/grideditors.h"
39
40 #include "griddemo.h"
41
42 #ifndef wxHAS_IMAGES_IN_RESOURCES
43 #include "../sample.xpm"
44 #endif
45
46 // Custom renderer that renders column header cells without borders and in
47 // italic
48 class CustomColumnHeaderRenderer : public wxGridColumnHeaderRenderer
49 {
50 public:
51 CustomColumnHeaderRenderer(const wxColour& colFg, const wxColour& colBg)
52 : m_colFg(colFg),
53 m_colBg(colBg)
54 {
55 }
56
57 virtual void DrawLabel(const wxGrid& WXUNUSED(grid),
58 wxDC& dc,
59 const wxString& value,
60 const wxRect& rect,
61 int horizAlign,
62 int vertAlign,
63 int WXUNUSED(textOrientation)) const
64 {
65 dc.SetTextForeground(m_colFg);
66 dc.SetFont(wxITALIC_FONT->Bold());
67 dc.DrawLabel(value, rect, horizAlign | vertAlign);
68 }
69
70 virtual void DrawBorder(const wxGrid& WXUNUSED(grid),
71 wxDC& dc,
72 wxRect& rect) const
73 {
74 dc.SetPen(*wxTRANSPARENT_PEN);
75 dc.SetBrush(wxBrush(m_colBg));
76 dc.DrawRectangle(rect);
77 }
78
79 private:
80 const wxColour m_colFg, m_colBg;
81
82 wxDECLARE_NO_COPY_CLASS(CustomColumnHeaderRenderer);
83 };
84
85 // And a custom attributes provider which uses custom column header renderer
86 // defined above
87 class CustomColumnHeadersProvider : public wxGridCellAttrProvider
88 {
89 public:
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),
95 m_useCustom(false)
96 {
97 }
98
99 // enable or disable the use of custom renderer for column headers
100 void UseCustomColHeaders(bool use = true) { m_useCustom = use; }
101
102 protected:
103 virtual const wxGridColumnHeaderRenderer& GetColumnHeaderRenderer(int col)
104 {
105 // if enabled, use custom renderers
106 if ( m_useCustom )
107 {
108 // and use different ones for odd and even columns -- just to show
109 // that we can
110 return col % 2 ? m_customOddRenderer : m_customEvenRenderer;
111 }
112
113 return wxGridCellAttrProvider::GetColumnHeaderRenderer(col);
114 }
115
116 private:
117 CustomColumnHeaderRenderer m_customOddRenderer,
118 m_customEvenRenderer;
119
120 bool m_useCustom;
121
122 wxDECLARE_NO_COPY_CLASS(CustomColumnHeadersProvider);
123 };
124
125 // ----------------------------------------------------------------------------
126 // wxWin macros
127 // ----------------------------------------------------------------------------
128
129 IMPLEMENT_APP( GridApp )
130
131 // ============================================================================
132 // implementation
133 // ============================================================================
134
135 // ----------------------------------------------------------------------------
136 // GridApp
137 // ----------------------------------------------------------------------------
138
139 bool GridApp::OnInit()
140 {
141 GridFrame *frame = new GridFrame;
142 frame->Show(true);
143
144 return true;
145 }
146
147 // ----------------------------------------------------------------------------
148 // GridFrame
149 // ----------------------------------------------------------------------------
150
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 )
186
187 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
188 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
189
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)
195
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)
205
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 )
213
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 )
218
219 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH, GridFrame::OnSetHighlightWidth)
220 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH, GridFrame::OnSetROHighlightWidth)
221
222 EVT_MENU( wxID_PRINT, GridFrame::OnGridRender )
223 EVT_MENU( ID_RENDER_COORDS, GridFrame::OnGridRender )
224
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 )
235
236 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
237 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
238 END_EVENT_TABLE()
239
240
241 GridFrame::GridFrame()
242 : wxFrame( (wxFrame *)NULL, wxID_ANY, wxT("wxWidgets grid class demo"),
243 wxDefaultPosition,
244 wxDefaultSize )
245 {
246 SetIcon(wxICON(sample));
247
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();
253
254 wxMenu* setupMenu = new wxMenu;
255 wxMenuItem* item;
256 item = setupMenu->AppendCheckItem( ID_RENDER_ROW_LABEL,
257 "Render row labels" );
258 item->Check();
259 item = setupMenu->AppendCheckItem( ID_RENDER_COL_LABEL,
260 "Render column labels" );
261 item->Check();
262 item = setupMenu->AppendCheckItem( ID_RENDER_GRID_LINES,
263 "Render grid cell lines" );
264 item->Check();
265 item = setupMenu->AppendCheckItem( ID_RENDER_GRID_BORDER,
266 "Render border" );
267 item->Check();
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,
278 "Zoom 125%" );
279
280 fileMenu->AppendSubMenu( setupMenu, "Render setup" );
281 fileMenu->Append( wxID_PRINT, "Render" );
282 fileMenu->Append( ID_RENDER_COORDS, "Render G5:P30" );
283
284 fileMenu->AppendSeparator();
285 fileMenu->Append( wxID_EXIT, wxT("E&xit\tAlt-X") );
286
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;
307
308 viewMenu->Append( ID_ROWLABELALIGN, wxT("R&ow label alignment"),
309 rowLabelMenu,
310 wxT("Change alignment of row labels") );
311
312 rowLabelMenu->AppendRadioItem( ID_ROWLABELHORIZALIGN, wxT("&Horizontal") );
313 rowLabelMenu->AppendRadioItem( ID_ROWLABELVERTALIGN, wxT("&Vertical") );
314
315 wxMenu *colLabelMenu = new wxMenu;
316
317 viewMenu->Append( ID_COLLABELALIGN, wxT("Col l&abel alignment"),
318 colLabelMenu,
319 wxT("Change alignment of col labels") );
320
321 colLabelMenu->AppendRadioItem( ID_COLLABELHORIZALIGN, wxT("&Horizontal") );
322 colLabelMenu->AppendRadioItem( ID_COLLABELVERTALIGN, wxT("&Vertical") );
323
324 wxMenu *colHeaderMenu = new wxMenu;
325
326 viewMenu->Append( ID_ROWLABELALIGN, wxT("Col header style"),
327 colHeaderMenu,
328 wxT("Change style of col header") );
329
330 colHeaderMenu->AppendRadioItem( ID_COLDEFAULTHEADER, wxT("&Default") );
331 colHeaderMenu->AppendRadioItem( ID_COLNATIVEHEADER, wxT("&Native") );
332 colHeaderMenu->AppendRadioItem( ID_COLCUSTOMHEADER, wxT("&Custom") );
333
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");
340
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...") );
348
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") );
355
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"),
372 selectionMenu,
373 wxT("Change selection mode") );
374
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") );
379
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") );
388
389 wxMenu *helpMenu = new wxMenu;
390 helpMenu->Append( wxID_ABOUT, wxT("&About wxGrid demo") );
391
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") );
400
401 SetMenuBar( menuBar );
402
403 m_addToSel = false;
404
405 grid = new wxGrid( this,
406 wxID_ANY,
407 wxPoint( 0, 0 ),
408 wxSize( 400, 300 ) );
409
410
411 #if wxUSE_LOG
412 int gridW = 600, gridH = 300;
413 int logW = gridW, logH = 100;
414
415 logWin = new wxTextCtrl( this,
416 wxID_ANY,
417 wxEmptyString,
418 wxPoint( 0, gridH + 20 ),
419 wxSize( logW, logH ),
420 wxTE_MULTILINE );
421
422 logger = new wxLogTextCtrl( logWin );
423 m_logOld = wxLog::SetActiveTarget( logger );
424 wxLog::DisableTimestamp();
425 #endif // wxUSE_LOG
426
427 // this will create a grid and, by default, an associated grid
428 // table for strings
429 grid->CreateGrid( 0, 0 );
430
431 grid->GetTable()->SetAttrProvider(new CustomColumnHeadersProvider());
432
433 grid->AppendRows(100);
434 grid->AppendCols(100);
435
436 int ir = grid->GetNumberRows();
437 grid->DeleteRows(0, ir);
438 grid->AppendRows(ir);
439
440 grid->SetRowSize( 0, 60 );
441 grid->SetCellValue( 0, 0, wxT("Ctrl+Home\nwill go to\nthis cell") );
442
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);
446
447 grid->SetCellValue( 0, 2, wxT("Blah") );
448 grid->SetCellValue( 0, 3, wxT("Read only") );
449 grid->SetReadOnly( 0, 3 );
450
451 grid->SetCellValue( 0, 4, wxT("Can veto edit this cell") );
452
453 grid->SetCellValue( 0, 5, wxT("Press\nCtrl+arrow\nto skip over\ncells") );
454
455 grid->SetRowSize( 99, 60 );
456 grid->SetCellValue(98, 98, "Test background colour setting");
457 grid->SetCellBackgroundColour(98, 99, wxColour(255, 127, 127));
458 grid->SetCellBackgroundColour(99, 98, wxColour(255, 127, 127));
459 grid->SetCellValue( 99, 99, wxT("Ctrl+End\nwill go to\nthis cell") );
460 grid->SetCellValue( 1, 0, wxT("This default cell will overflow into neighboring cells, but not if you turn overflow off."));
461
462 grid->SetCellTextColour(1, 2, *wxRED);
463 grid->SetCellBackgroundColour(1, 2, *wxGREEN);
464
465 grid->SetCellValue( 1, 4, wxT("I'm in the middle"));
466
467 grid->SetCellValue(2, 2, wxT("red"));
468
469 grid->SetCellTextColour(2, 2, *wxRED);
470 grid->SetCellValue(3, 3, wxT("green on grey"));
471 grid->SetCellTextColour(3, 3, *wxGREEN);
472 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
473
474 grid->SetCellValue(4, 4, wxT("a weird looking cell"));
475 grid->SetCellAlignment(4, 4, wxALIGN_CENTRE, wxALIGN_CENTRE);
476 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
477
478 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
479 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
480 grid->SetCellBackgroundColour(3, 0, wxColour(255, 127, 127));
481
482 wxGridCellAttr *attr;
483 attr = new wxGridCellAttr;
484 attr->SetTextColour(*wxBLUE);
485 grid->SetColAttr(5, attr);
486 attr = new wxGridCellAttr;
487 attr->SetBackgroundColour(*wxRED);
488 grid->SetRowAttr(5, attr);
489
490 grid->SetCellValue(2, 4, wxT("a wider column"));
491 grid->SetColSize(4, 120);
492 grid->SetColMinimalWidth(4, 120);
493
494 grid->SetCellTextColour(5, 8, *wxGREEN);
495 grid->SetCellValue(5, 8, wxT("Bg from row attr\nText col from cell attr"));
496 grid->SetCellValue(5, 5, wxT("Bg from row attr Text col from col attr and this text is so long that it covers over many many empty cells but is broken by one that isn't"));
497
498 // Some numeric columns with different formatting.
499 grid->SetColFormatFloat(6);
500 grid->SetCellValue(0, 6, "Default\nfloat format");
501 grid->SetCellValue(1, 6, wxString::Format(wxT("%g"), 3.1415));
502 grid->SetCellValue(2, 6, wxString::Format(wxT("%g"), 1415.0));
503 grid->SetCellValue(3, 6, wxString::Format(wxT("%g"), 12345.67890));
504
505 grid->SetColFormatFloat(7, 6, 2);
506 grid->SetCellValue(0, 7, "Width 6\nprecision 2");
507 grid->SetCellValue(1, 7, wxString::Format(wxT("%g"), 3.1415));
508 grid->SetCellValue(2, 7, wxString::Format(wxT("%g"), 1415.0));
509 grid->SetCellValue(3, 7, wxString::Format(wxT("%g"), 12345.67890));
510
511 grid->SetColFormatCustom(8,
512 wxString::Format("%s:%i,%i,%s", wxGRID_VALUE_FLOAT, -1, 4, "g"));
513 grid->SetCellValue(0, 8, "Compact\nformat");
514 grid->SetCellValue(1, 8, wxT("31415e-4"));
515 grid->SetCellValue(2, 8, wxT("1415"));
516 grid->SetCellValue(3, 8, wxT("123456789e-4"));
517
518 grid->SetColFormatNumber(9);
519 grid->SetCellValue(0, 9, "Integer\ncolumn");
520 grid->SetCellValue(1, 9, "17");
521 grid->SetCellValue(2, 9, "0");
522 grid->SetCellValue(3, 9, "-666");
523 grid->SetCellAlignment(3, 9, wxALIGN_CENTRE, wxALIGN_TOP);
524 grid->SetCellValue(3, 10, "<- This numeric cell should be centred");
525
526 const wxString choices[] =
527 {
528 wxT("Please select a choice"),
529 wxT("This takes two cells"),
530 wxT("Another choice"),
531 };
532 grid->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices), choices));
533 grid->SetCellSize(4, 0, 1, 2);
534 grid->SetCellValue(4, 0, choices[0]);
535 grid->SetCellOverflow(4, 0, false);
536
537 grid->SetCellSize(7, 1, 3, 4);
538 grid->SetCellAlignment(7, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
539 grid->SetCellValue(7, 1, wxT("Big box!"));
540
541 // create a separator-like row: it's grey and it's non-resizable
542 grid->DisableRowResize(10);
543 grid->SetRowSize(10, 30);
544 attr = new wxGridCellAttr;
545 attr->SetBackgroundColour(*wxLIGHT_GREY);
546 grid->SetRowAttr(10, attr);
547 grid->SetCellValue(10, 0, "You can't resize this row interactively -- try it");
548
549 // this does exactly nothing except testing that SetAttr() handles NULL
550 // attributes and does reference counting correctly
551 grid->SetAttr(11, 11, NULL);
552 grid->SetAttr(11, 11, new wxGridCellAttr);
553 grid->SetAttr(11, 11, NULL);
554
555 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
556 topSizer->Add( grid,
557 1,
558 wxEXPAND );
559
560 #if wxUSE_LOG
561 topSizer->Add( logWin,
562 0,
563 wxEXPAND );
564 #endif // wxUSE_LOG
565
566 SetSizerAndFit( topSizer );
567
568 Centre();
569 SetDefaults();
570 }
571
572
573 GridFrame::~GridFrame()
574 {
575 #if wxUSE_LOG
576 delete wxLog::SetActiveTarget(m_logOld);
577 #endif // wxUSE_LOG
578 }
579
580
581 void GridFrame::SetDefaults()
582 {
583 GetMenuBar()->Check( ID_TOGGLEROWLABELS, true );
584 GetMenuBar()->Check( ID_TOGGLECOLLABELS, true );
585 GetMenuBar()->Check( ID_TOGGLEEDIT, true );
586 GetMenuBar()->Check( ID_TOGGLEROWSIZING, true );
587 GetMenuBar()->Check( ID_TOGGLECOLSIZING, true );
588 GetMenuBar()->Check( ID_TOGGLECOLMOVING, false );
589 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, true );
590 GetMenuBar()->Check( ID_TOGGLEGRIDDRAGCELL, false );
591 GetMenuBar()->Check( ID_TOGGLEGRIDLINES, true );
592 GetMenuBar()->Check( ID_CELLOVERFLOW, true );
593 }
594
595
596 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
597 {
598 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
599 {
600 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
601 }
602 else
603 {
604 grid->SetRowLabelSize( 0 );
605 }
606 }
607
608
609 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
610 {
611 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
612 {
613 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
614 }
615 else
616 {
617 grid->SetColLabelSize( 0 );
618 }
619 }
620
621
622 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
623 {
624 grid->EnableEditing(
625 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
626 }
627
628
629 void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
630 {
631 grid->EnableDragRowSize(
632 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
633 }
634
635
636 void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
637 {
638 grid->EnableDragColSize(
639 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
640 }
641
642 void GridFrame::ToggleColMoving( wxCommandEvent& WXUNUSED(ev) )
643 {
644 grid->EnableDragColMove(
645 GetMenuBar()->IsChecked( ID_TOGGLECOLMOVING ) );
646 }
647
648 void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
649 {
650 grid->EnableDragGridSize(
651 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
652 }
653
654 void GridFrame::ToggleGridDragCell( wxCommandEvent& WXUNUSED(ev) )
655 {
656 grid->EnableDragCell(
657 GetMenuBar()->IsChecked( ID_TOGGLEGRIDDRAGCELL ) );
658 }
659
660 void GridFrame::SetNativeColHeader( wxCommandEvent& WXUNUSED(ev) )
661 {
662 CustomColumnHeadersProvider* provider =
663 static_cast<CustomColumnHeadersProvider*>(grid->GetTable()->GetAttrProvider());
664 provider->UseCustomColHeaders(false);
665 grid->SetUseNativeColLabels(true);
666 }
667
668 void GridFrame::SetCustomColHeader( wxCommandEvent& WXUNUSED(ev) )
669 {
670 CustomColumnHeadersProvider* provider =
671 static_cast<CustomColumnHeadersProvider*>(grid->GetTable()->GetAttrProvider());
672 provider->UseCustomColHeaders(true);
673 grid->SetUseNativeColLabels(false);
674 }
675
676 void GridFrame::SetDefaultColHeader( wxCommandEvent& WXUNUSED(ev) )
677 {
678 CustomColumnHeadersProvider* provider =
679 static_cast<CustomColumnHeadersProvider*>(grid->GetTable()->GetAttrProvider());
680 provider->UseCustomColHeaders(false);
681 grid->SetUseNativeColLabels(false);
682 }
683
684
685 void GridFrame::OnGridCustomTab(wxGridEvent& event)
686 {
687 // just for testing, make the cursor move up and down instead of the usual
688 // left and right
689 if ( event.ShiftDown() )
690 {
691 if ( grid->GetGridCursorRow() > 0 )
692 grid->MoveCursorUp( false );
693 }
694 else
695 {
696 if ( grid->GetGridCursorRow() < grid->GetNumberRows() - 1 )
697 grid->MoveCursorDown( false );
698 }
699 }
700
701 void GridFrame::SetTabBehaviour(wxCommandEvent& event)
702 {
703 // To make any built-in behaviour work, we need to disable the custom TAB
704 // handler, otherwise it would be overriding them.
705 grid->Disconnect(wxEVT_GRID_TABBING,
706 wxGridEventHandler(GridFrame::OnGridCustomTab));
707
708 grid->SetTabBehaviour(
709 static_cast<wxGrid::TabBehaviour>(event.GetId() - ID_TAB_STOP)
710 );
711 }
712
713 void GridFrame::SetTabCustomHandler(wxCommandEvent&)
714 {
715 grid->Connect(wxEVT_GRID_TABBING,
716 wxGridEventHandler(GridFrame::OnGridCustomTab),
717 NULL, this);
718 }
719
720
721 void GridFrame::ToggleGridLines( wxCommandEvent& WXUNUSED(ev) )
722 {
723 grid->EnableGridLines(
724 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES ) );
725 }
726
727 void GridFrame::OnSetHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
728 {
729 wxString choices[] = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("10")};
730
731 wxSingleChoiceDialog dlg(this, wxT("Choose the thickness of the highlight pen:"),
732 wxT("Pen Width"), 11, choices);
733
734 int current = grid->GetCellHighlightPenWidth();
735 dlg.SetSelection(current);
736 if (dlg.ShowModal() == wxID_OK) {
737 grid->SetCellHighlightPenWidth(dlg.GetSelection());
738 }
739 }
740
741 void GridFrame::OnSetROHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
742 {
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")};
744
745 wxSingleChoiceDialog dlg(this, wxT("Choose the thickness of the highlight pen:"),
746 wxT("Pen Width"), 11, choices);
747
748 int current = grid->GetCellHighlightROPenWidth();
749 dlg.SetSelection(current);
750 if (dlg.ShowModal() == wxID_OK) {
751 grid->SetCellHighlightROPenWidth(dlg.GetSelection());
752 }
753 }
754
755
756
757 void GridFrame::AutoSizeCols( wxCommandEvent& WXUNUSED(ev) )
758 {
759 grid->AutoSizeColumns();
760 grid->Refresh();
761 }
762
763 void GridFrame::CellOverflow( wxCommandEvent& ev )
764 {
765 grid->SetDefaultCellOverflow(ev.IsChecked());
766 grid->Refresh();
767 }
768
769 void GridFrame::ResizeCell( wxCommandEvent& ev )
770 {
771 if (ev.IsChecked())
772 grid->SetCellSize( 7, 1, 5, 5 );
773 else
774 grid->SetCellSize( 7, 1, 1, 5 );
775 grid->Refresh();
776 }
777
778 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
779 {
780 wxColourDialog dlg( NULL );
781 if ( dlg.ShowModal() == wxID_OK )
782 {
783 wxColourData retData;
784 retData = dlg.GetColourData();
785 wxColour colour = retData.GetColour();
786
787 grid->SetLabelBackgroundColour( colour );
788 }
789 }
790
791
792 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
793 {
794 wxColourDialog dlg( NULL );
795 if ( dlg.ShowModal() == wxID_OK )
796 {
797 wxColourData retData;
798 retData = dlg.GetColourData();
799 wxColour colour = retData.GetColour();
800
801 grid->SetLabelTextColour( colour );
802 }
803 }
804
805 void GridFrame::SetLabelFont( wxCommandEvent& WXUNUSED(ev) )
806 {
807 wxFont font = wxGetFontFromUser(this);
808 if ( font.IsOk() )
809 {
810 grid->SetLabelFont(font);
811 }
812 }
813
814 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
815 {
816 int horiz, vert;
817 grid->GetRowLabelAlignment( &horiz, &vert );
818
819 switch ( horiz )
820 {
821 case wxALIGN_LEFT:
822 horiz = wxALIGN_CENTRE;
823 break;
824
825 case wxALIGN_CENTRE:
826 horiz = wxALIGN_RIGHT;
827 break;
828
829 case wxALIGN_RIGHT:
830 horiz = wxALIGN_LEFT;
831 break;
832 }
833
834 grid->SetRowLabelAlignment( horiz, vert );
835 }
836
837 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
838 {
839 int horiz, vert;
840 grid->GetRowLabelAlignment( &horiz, &vert );
841
842 switch ( vert )
843 {
844 case wxALIGN_TOP:
845 vert = wxALIGN_CENTRE;
846 break;
847
848 case wxALIGN_CENTRE:
849 vert = wxALIGN_BOTTOM;
850 break;
851
852 case wxALIGN_BOTTOM:
853 vert = wxALIGN_TOP;
854 break;
855 }
856
857 grid->SetRowLabelAlignment( horiz, vert );
858 }
859
860
861 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
862 {
863 int horiz, vert;
864 grid->GetColLabelAlignment( &horiz, &vert );
865
866 switch ( horiz )
867 {
868 case wxALIGN_LEFT:
869 horiz = wxALIGN_CENTRE;
870 break;
871
872 case wxALIGN_CENTRE:
873 horiz = wxALIGN_RIGHT;
874 break;
875
876 case wxALIGN_RIGHT:
877 horiz = wxALIGN_LEFT;
878 break;
879 }
880
881 grid->SetColLabelAlignment( horiz, vert );
882 }
883
884
885 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
886 {
887 int horiz, vert;
888 grid->GetColLabelAlignment( &horiz, &vert );
889
890 switch ( vert )
891 {
892 case wxALIGN_TOP:
893 vert = wxALIGN_CENTRE;
894 break;
895
896 case wxALIGN_CENTRE:
897 vert = wxALIGN_BOTTOM;
898 break;
899
900 case wxALIGN_BOTTOM:
901 vert = wxALIGN_TOP;
902 break;
903 }
904
905 grid->SetColLabelAlignment( horiz, vert );
906 }
907
908
909 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
910 {
911 wxColourDialog dlg( NULL );
912 if ( dlg.ShowModal() == wxID_OK )
913 {
914 wxColourData retData;
915 retData = dlg.GetColourData();
916 wxColour colour = retData.GetColour();
917
918 grid->SetGridLineColour( colour );
919 }
920 }
921
922
923 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
924 {
925 grid->InsertRows( grid->GetGridCursorRow(), 1 );
926 }
927
928
929 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
930 {
931 grid->InsertCols( grid->GetGridCursorCol(), 1 );
932 }
933
934
935 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
936 {
937 if ( grid->IsSelection() )
938 {
939 wxGridUpdateLocker locker(grid);
940 for ( int n = 0; n < grid->GetNumberRows(); )
941 {
942 if ( grid->IsInSelection( n , 0 ) )
943 grid->DeleteRows( n, 1 );
944 else
945 n++;
946 }
947 }
948 }
949
950
951 void GridFrame::AutoSizeRow(wxCommandEvent& WXUNUSED(event))
952 {
953 wxGridUpdateLocker locker(grid);
954 const wxArrayInt sels = grid->GetSelectedRows();
955 for ( size_t n = 0, count = sels.size(); n < count; n++ )
956 {
957 grid->AutoSizeRow( sels[n], false );
958 }
959 }
960
961 void GridFrame::AutoSizeCol(wxCommandEvent& WXUNUSED(event))
962 {
963 wxGridUpdateLocker locker(grid);
964 const wxArrayInt sels = grid->GetSelectedCols();
965 for ( size_t n = 0, count = sels.size(); n < count; n++ )
966 {
967 grid->AutoSizeColumn( sels[n], false );
968 }
969 }
970
971 void GridFrame::AutoSizeRowLabel(wxCommandEvent& WXUNUSED(event))
972 {
973 wxGridUpdateLocker locker(grid);
974 const wxArrayInt sels = grid->GetSelectedRows();
975 for ( size_t n = 0, count = sels.size(); n < count; n++ )
976 {
977 grid->AutoSizeRowLabelSize( sels[n] );
978 }
979 }
980
981 void GridFrame::AutoSizeColLabel(wxCommandEvent& WXUNUSED(event))
982 {
983 wxGridUpdateLocker locker(grid);
984 const wxArrayInt sels = grid->GetSelectedCols();
985 for ( size_t n = 0, count = sels.size(); n < count; n++ )
986 {
987 grid->AutoSizeColLabelSize( sels[n] );
988 }
989 }
990
991 void GridFrame::AutoSizeLabelsCol(wxCommandEvent& WXUNUSED(event))
992 {
993 grid->SetColLabelSize( wxGRID_AUTOSIZE );
994 }
995
996 void GridFrame::AutoSizeLabelsRow(wxCommandEvent& WXUNUSED(event))
997 {
998 grid->SetRowLabelSize( wxGRID_AUTOSIZE );
999 }
1000
1001 void GridFrame::AutoSizeTable(wxCommandEvent& WXUNUSED(event))
1002 {
1003 grid->AutoSize();
1004 Layout();
1005 }
1006
1007
1008 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
1009 {
1010 if ( grid->IsSelection() )
1011 {
1012 wxGridUpdateLocker locker(grid);
1013 for ( int n = 0; n < grid->GetNumberCols(); )
1014 {
1015 if ( grid->IsInSelection( 0 , n ) )
1016 grid->DeleteCols( n, 1 );
1017 else
1018 n++;
1019 }
1020 }
1021 }
1022
1023
1024 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
1025 {
1026 grid->ClearGrid();
1027 }
1028
1029 void GridFrame::SelectCells( wxCommandEvent& WXUNUSED(ev) )
1030 {
1031 grid->SetSelectionMode( wxGrid::wxGridSelectCells );
1032 }
1033
1034 void GridFrame::SelectRows( wxCommandEvent& WXUNUSED(ev) )
1035 {
1036 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
1037 }
1038
1039 void GridFrame::SelectCols( wxCommandEvent& WXUNUSED(ev) )
1040 {
1041 grid->SetSelectionMode( wxGrid::wxGridSelectColumns );
1042 }
1043
1044 void GridFrame::SelectRowsOrCols( wxCommandEvent& WXUNUSED(ev) )
1045 {
1046 grid->SetSelectionMode( wxGrid::wxGridSelectRowsOrColumns );
1047 }
1048
1049 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
1050 {
1051 wxColour col = wxGetColourFromUser(this);
1052 if ( col.IsOk() )
1053 {
1054 grid->SetDefaultCellTextColour(col);
1055 grid->Refresh();
1056 }
1057 }
1058
1059 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
1060 {
1061 wxColour col = wxGetColourFromUser(this);
1062 if ( col.IsOk() )
1063 {
1064 // Check the new Refresh function by passing it a rectangle
1065 // which exactly fits the grid.
1066 wxPoint pt(0, 0);
1067 wxRect r(pt, grid->GetSize());
1068 grid->SetDefaultCellBackgroundColour(col);
1069 grid->Refresh(true, &r);
1070 }
1071 }
1072
1073 void GridFrame::DeselectCell(wxCommandEvent& WXUNUSED(event))
1074 {
1075 grid->DeselectCell(3, 1);
1076 }
1077
1078 void GridFrame::DeselectCol(wxCommandEvent& WXUNUSED(event))
1079 {
1080 grid->DeselectCol(2);
1081 }
1082
1083 void GridFrame::DeselectRow(wxCommandEvent& WXUNUSED(event))
1084 {
1085 grid->DeselectRow(2);
1086 }
1087
1088 void GridFrame::DeselectAll(wxCommandEvent& WXUNUSED(event))
1089 {
1090 grid->ClearSelection();
1091 }
1092
1093 void GridFrame::SelectCell(wxCommandEvent& WXUNUSED(event))
1094 {
1095 grid->SelectBlock(3, 1, 3, 1, m_addToSel);
1096 }
1097
1098 void GridFrame::SelectCol(wxCommandEvent& WXUNUSED(event))
1099 {
1100 grid->SelectCol(2, m_addToSel);
1101 }
1102
1103 void GridFrame::SelectRow(wxCommandEvent& WXUNUSED(event))
1104 {
1105 grid->SelectRow(2, m_addToSel);
1106 }
1107
1108 void GridFrame::SelectAll(wxCommandEvent& WXUNUSED(event))
1109 {
1110 grid->SelectAll();
1111 }
1112
1113 void GridFrame::OnAddToSelectToggle(wxCommandEvent& event)
1114 {
1115 m_addToSel = event.IsChecked();
1116 }
1117
1118 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
1119 {
1120 wxString logBuf;
1121 if ( ev.GetRow() != -1 )
1122 {
1123 logBuf << wxT("Left click on row label ") << ev.GetRow();
1124 }
1125 else if ( ev.GetCol() != -1 )
1126 {
1127 logBuf << wxT("Left click on col label ") << ev.GetCol();
1128 }
1129 else
1130 {
1131 logBuf << wxT("Left click on corner label");
1132 }
1133
1134 if ( ev.ShiftDown() )
1135 logBuf << wxT(" (shift down)");
1136 if ( ev.ControlDown() )
1137 logBuf << wxT(" (control down)");
1138 wxLogMessage( wxT("%s"), logBuf.c_str() );
1139
1140 // you must call event skip if you want default grid processing
1141 //
1142 ev.Skip();
1143 }
1144
1145
1146 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
1147 {
1148 wxLogMessage(wxT("Left click at row %d, col %d"), ev.GetRow(), ev.GetCol());
1149
1150 // you must call event skip if you want default grid processing
1151 // (cell highlighting etc.)
1152 //
1153 ev.Skip();
1154 }
1155
1156
1157 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
1158 {
1159 const int row = ev.GetRowOrCol();
1160
1161 wxLogMessage("Resized row %d, new height = %d",
1162 row, grid->GetRowSize(row));
1163
1164 ev.Skip();
1165 }
1166
1167
1168 void GridFrame::OnColSize( wxGridSizeEvent& ev )
1169 {
1170 const int col = ev.GetRowOrCol();
1171
1172 wxLogMessage("Resized column %d, new width = %d",
1173 col, grid->GetColSize(col));
1174
1175 ev.Skip();
1176 }
1177
1178 void GridFrame::OnColAutoSize( wxGridSizeEvent &event )
1179 {
1180 // Fit even-numbered columns to their contents while using the default
1181 // behaviour for the odd-numbered ones to be able to see the difference.
1182 int col = event.GetRowOrCol();
1183 if ( col % 2 )
1184 {
1185 wxLogMessage("Auto-sizing column %d to fit its contents", col);
1186 grid->AutoSizeColumn(col);
1187 }
1188 else
1189 {
1190 event.Skip();
1191 }
1192 }
1193
1194 void GridFrame::OnSelectCell( wxGridEvent& ev )
1195 {
1196 wxString logBuf;
1197 if ( ev.Selecting() )
1198 logBuf << wxT("Selected ");
1199 else
1200 logBuf << wxT("Deselected ");
1201 logBuf << wxT("cell at row ") << ev.GetRow()
1202 << wxT(" col ") << ev.GetCol()
1203 << wxT(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
1204 << wxT(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
1205 << wxT(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
1206 << wxT(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << wxT(" )");
1207
1208 //Indicate whether this column was moved
1209 if ( ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() ) != ev.GetCol() )
1210 logBuf << wxT(" *** Column moved, current position: ") << ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() );
1211
1212 wxLogMessage( wxT("%s"), logBuf.c_str() );
1213
1214 // you must call Skip() if you want the default processing
1215 // to occur in wxGrid
1216 ev.Skip();
1217 }
1218
1219 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
1220 {
1221 wxString logBuf;
1222 if ( ev.Selecting() )
1223 logBuf << wxT("Selected ");
1224 else
1225 logBuf << wxT("Deselected ");
1226 logBuf << wxT("cells from row ") << ev.GetTopRow()
1227 << wxT(" col ") << ev.GetLeftCol()
1228 << wxT(" to row ") << ev.GetBottomRow()
1229 << wxT(" col ") << ev.GetRightCol()
1230 << wxT(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F')
1231 << wxT(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F')
1232 << wxT(", AltDown: ")<< (ev.AltDown() ? 'T':'F')
1233 << wxT(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << wxT(" )");
1234 wxLogMessage( wxT("%s"), logBuf.c_str() );
1235
1236 ev.Skip();
1237 }
1238
1239 void GridFrame::OnCellValueChanging( wxGridEvent& ev )
1240 {
1241 int row = ev.GetRow(),
1242 col = ev.GetCol();
1243
1244 wxLogMessage("Value of cell at (%d, %d): about to change "
1245 "from \"%s\" to \"%s\"",
1246 row, col,
1247 grid->GetCellValue(row, col), ev.GetString());
1248
1249 // test how vetoing works
1250 if ( ev.GetString() == "42" )
1251 {
1252 wxLogMessage("Vetoing the change.");
1253 ev.Veto();
1254 return;
1255 }
1256
1257 ev.Skip();
1258 }
1259
1260 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
1261 {
1262 int row = ev.GetRow(),
1263 col = ev.GetCol();
1264
1265 wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" "
1266 "(was \"%s\")",
1267 row, col,
1268 grid->GetCellValue(row, col), ev.GetString());
1269
1270 ev.Skip();
1271 }
1272
1273 void GridFrame::OnCellBeginDrag( wxGridEvent& ev )
1274 {
1275 wxLogMessage(wxT("Got request to drag cell at row %d, col %d"),
1276 ev.GetRow(), ev.GetCol());
1277
1278 ev.Skip();
1279 }
1280
1281 void GridFrame::OnEditorShown( wxGridEvent& ev )
1282 {
1283
1284 if ( (ev.GetCol() == 4) &&
1285 (ev.GetRow() == 0) &&
1286 (wxMessageBox(wxT("Are you sure you wish to edit this cell"),
1287 wxT("Checking"),wxYES_NO) == wxNO ) ) {
1288
1289 ev.Veto();
1290 return;
1291 }
1292
1293 wxLogMessage( wxT("Cell editor shown.") );
1294
1295 ev.Skip();
1296 }
1297
1298 void GridFrame::OnEditorHidden( wxGridEvent& ev )
1299 {
1300
1301 if ( (ev.GetCol() == 4) &&
1302 (ev.GetRow() == 0) &&
1303 (wxMessageBox(wxT("Are you sure you wish to finish editing this cell"),
1304 wxT("Checking"),wxYES_NO) == wxNO ) ) {
1305
1306 ev.Veto();
1307 return;
1308 }
1309
1310 wxLogMessage( wxT("Cell editor hidden.") );
1311
1312 ev.Skip();
1313 }
1314
1315 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
1316 {
1317 wxAboutDialogInfo aboutInfo;
1318 aboutInfo.SetName(wxT("wxGrid demo"));
1319 aboutInfo.SetDescription(_("wxGrid sample program"));
1320 aboutInfo.AddDeveloper(wxT("Michael Bedward"));
1321 aboutInfo.AddDeveloper(wxT("Julian Smart"));
1322 aboutInfo.AddDeveloper(wxT("Vadim Zeitlin"));
1323
1324 // this is just to force the generic version of the about
1325 // dialog under wxMSW so that it's easy to test if the grid
1326 // repaints correctly when it has lost focus and a dialog
1327 // (different from the Windows standard message box -- it doesn't
1328 // work with it for some reason) is moved over it.
1329 aboutInfo.SetWebSite(wxT("http://www.wxwidgets.org"));
1330
1331 wxAboutBox(aboutInfo);
1332 }
1333
1334
1335 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
1336 {
1337 Close( true );
1338 }
1339
1340 void GridFrame::OnBugsTable(wxCommandEvent& )
1341 {
1342 BugsGridFrame *frame = new BugsGridFrame;
1343 frame->Show(true);
1344 }
1345
1346 // ----------------------------------------------------------------------------
1347 // MyGridCellAttrProvider
1348 // ----------------------------------------------------------------------------
1349
1350 MyGridCellAttrProvider::MyGridCellAttrProvider()
1351 {
1352 m_attrForOddRows = new wxGridCellAttr;
1353 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
1354 }
1355
1356 MyGridCellAttrProvider::~MyGridCellAttrProvider()
1357 {
1358 m_attrForOddRows->DecRef();
1359 }
1360
1361 wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col,
1362 wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const
1363 {
1364 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
1365
1366 if ( row % 2 )
1367 {
1368 if ( !attr )
1369 {
1370 attr = m_attrForOddRows;
1371 attr->IncRef();
1372 }
1373 else
1374 {
1375 if ( !attr->HasBackgroundColour() )
1376 {
1377 wxGridCellAttr *attrNew = attr->Clone();
1378 attr->DecRef();
1379 attr = attrNew;
1380 attr->SetBackgroundColour(*wxLIGHT_GREY);
1381 }
1382 }
1383 }
1384
1385 return attr;
1386 }
1387
1388 void GridFrame::OnVTable(wxCommandEvent& )
1389 {
1390 static long s_sizeGrid = 10000;
1391
1392 s_sizeGrid = wxGetNumberFromUser(wxT("Size of the table to create"),
1393 wxT("Size: "),
1394 wxT("wxGridDemo question"),
1395 s_sizeGrid,
1396 0, 32000, this);
1397
1398 if ( s_sizeGrid != -1 )
1399 {
1400 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
1401 win->Show(true);
1402 }
1403 }
1404
1405 // ----------------------------------------------------------------------------
1406 // MyGridCellRenderer
1407 // ----------------------------------------------------------------------------
1408
1409 // do something that the default renderer doesn't here just to show that it is
1410 // possible to alter the appearance of the cell beyond what the attributes
1411 // allow
1412 void MyGridCellRenderer::Draw(wxGrid& grid,
1413 wxGridCellAttr& attr,
1414 wxDC& dc,
1415 const wxRect& rect,
1416 int row, int col,
1417 bool isSelected)
1418 {
1419 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1420
1421 dc.SetPen(*wxGREEN_PEN);
1422 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1423 dc.DrawEllipse(rect);
1424 }
1425
1426 // ============================================================================
1427 // BigGridFrame and BigGridTable: Sample of a non-standard table
1428 // ============================================================================
1429
1430 BigGridFrame::BigGridFrame(long sizeGrid)
1431 : wxFrame(NULL, wxID_ANY, wxT("Plugin Virtual Table"),
1432 wxDefaultPosition, wxSize(500, 450))
1433 {
1434 m_grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
1435 m_table = new BigGridTable(sizeGrid);
1436
1437 // VZ: I don't understand why this slows down the display that much,
1438 // must profile it...
1439 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1440
1441 m_grid->SetTable(m_table, true);
1442
1443 #if defined __WXMOTIF__
1444 // MB: the grid isn't getting a sensible default size under wxMotif
1445 int cw, ch;
1446 GetClientSize( &cw, &ch );
1447 m_grid->SetSize( cw, ch );
1448 #endif
1449 }
1450
1451 // ============================================================================
1452 // BugsGridFrame: a "realistic" table
1453 // ============================================================================
1454
1455 // ----------------------------------------------------------------------------
1456 // bugs table data
1457 // ----------------------------------------------------------------------------
1458
1459 enum Columns
1460 {
1461 Col_Id,
1462 Col_Summary,
1463 Col_Severity,
1464 Col_Priority,
1465 Col_Platform,
1466 Col_Opened,
1467 Col_Max
1468 };
1469
1470 enum Severity
1471 {
1472 Sev_Wish,
1473 Sev_Minor,
1474 Sev_Normal,
1475 Sev_Major,
1476 Sev_Critical,
1477 Sev_Max
1478 };
1479
1480 static const wxString severities[] =
1481 {
1482 wxT("wishlist"),
1483 wxT("minor"),
1484 wxT("normal"),
1485 wxT("major"),
1486 wxT("critical"),
1487 };
1488
1489 static struct BugsGridData
1490 {
1491 int id;
1492 wxChar summary[80];
1493 Severity severity;
1494 int prio;
1495 wxChar platform[12];
1496 bool opened;
1497 } gs_dataBugsGrid [] =
1498 {
1499 { 18, wxT("foo doesn't work"), Sev_Major, 1, wxT("wxMSW"), true },
1500 { 27, wxT("bar crashes"), Sev_Critical, 1, wxT("all"), false },
1501 { 45, wxT("printing is slow"), Sev_Minor, 3, wxT("wxMSW"), true },
1502 { 68, wxT("Rectangle() fails"), Sev_Normal, 1, wxT("wxMSW"), false },
1503 };
1504
1505 static const wxChar *headers[Col_Max] =
1506 {
1507 wxT("Id"),
1508 wxT("Summary"),
1509 wxT("Severity"),
1510 wxT("Priority"),
1511 wxT("Platform"),
1512 wxT("Opened?"),
1513 };
1514
1515 // ----------------------------------------------------------------------------
1516 // BugsGridTable
1517 // ----------------------------------------------------------------------------
1518
1519 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
1520 {
1521 switch ( col )
1522 {
1523 case Col_Id:
1524 case Col_Priority:
1525 return wxGRID_VALUE_NUMBER;;
1526
1527 case Col_Severity:
1528 // fall thorugh (TODO should be a list)
1529
1530 case Col_Summary:
1531 return wxString::Format(wxT("%s:80"), wxGRID_VALUE_STRING);
1532
1533 case Col_Platform:
1534 return wxString::Format(wxT("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
1535
1536 case Col_Opened:
1537 return wxGRID_VALUE_BOOL;
1538 }
1539
1540 wxFAIL_MSG(wxT("unknown column"));
1541
1542 return wxEmptyString;
1543 }
1544
1545 int BugsGridTable::GetNumberRows()
1546 {
1547 return WXSIZEOF(gs_dataBugsGrid);
1548 }
1549
1550 int BugsGridTable::GetNumberCols()
1551 {
1552 return Col_Max;
1553 }
1554
1555 bool BugsGridTable::IsEmptyCell( int WXUNUSED(row), int WXUNUSED(col) )
1556 {
1557 return false;
1558 }
1559
1560 wxString BugsGridTable::GetValue( int row, int col )
1561 {
1562 const BugsGridData& gd = gs_dataBugsGrid[row];
1563
1564 switch ( col )
1565 {
1566 case Col_Id:
1567 return wxString::Format(wxT("%d"), gd.id);
1568
1569 case Col_Priority:
1570 return wxString::Format(wxT("%d"), gd.prio);
1571
1572 case Col_Opened:
1573 return gd.opened ? wxT("1") : wxT("0");
1574
1575 case Col_Severity:
1576 return severities[gd.severity];
1577
1578 case Col_Summary:
1579 return gd.summary;
1580
1581 case Col_Platform:
1582 return gd.platform;
1583 }
1584
1585 return wxEmptyString;
1586 }
1587
1588 void BugsGridTable::SetValue( int row, int col, const wxString& value )
1589 {
1590 BugsGridData& gd = gs_dataBugsGrid[row];
1591
1592 switch ( col )
1593 {
1594 case Col_Id:
1595 case Col_Priority:
1596 case Col_Opened:
1597 wxFAIL_MSG(wxT("unexpected column"));
1598 break;
1599
1600 case Col_Severity:
1601 {
1602 size_t n;
1603 for ( n = 0; n < WXSIZEOF(severities); n++ )
1604 {
1605 if ( severities[n] == value )
1606 {
1607 gd.severity = (Severity)n;
1608 break;
1609 }
1610 }
1611
1612 if ( n == WXSIZEOF(severities) )
1613 {
1614 wxLogWarning(wxT("Invalid severity value '%s'."),
1615 value.c_str());
1616 gd.severity = Sev_Normal;
1617 }
1618 }
1619 break;
1620
1621 case Col_Summary:
1622 wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
1623 break;
1624
1625 case Col_Platform:
1626 wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
1627 break;
1628 }
1629 }
1630
1631 bool
1632 BugsGridTable::CanGetValueAs(int WXUNUSED(row),
1633 int col,
1634 const wxString& typeName)
1635 {
1636 if ( typeName == wxGRID_VALUE_STRING )
1637 {
1638 return true;
1639 }
1640 else if ( typeName == wxGRID_VALUE_BOOL )
1641 {
1642 return col == Col_Opened;
1643 }
1644 else if ( typeName == wxGRID_VALUE_NUMBER )
1645 {
1646 return col == Col_Id || col == Col_Priority || col == Col_Severity;
1647 }
1648 else
1649 {
1650 return false;
1651 }
1652 }
1653
1654 bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
1655 {
1656 return CanGetValueAs(row, col, typeName);
1657 }
1658
1659 long BugsGridTable::GetValueAsLong( int row, int col )
1660 {
1661 const BugsGridData& gd = gs_dataBugsGrid[row];
1662
1663 switch ( col )
1664 {
1665 case Col_Id:
1666 return gd.id;
1667
1668 case Col_Priority:
1669 return gd.prio;
1670
1671 case Col_Severity:
1672 return gd.severity;
1673
1674 default:
1675 wxFAIL_MSG(wxT("unexpected column"));
1676 return -1;
1677 }
1678 }
1679
1680 bool BugsGridTable::GetValueAsBool( int row, int col )
1681 {
1682 if ( col == Col_Opened )
1683 {
1684 return gs_dataBugsGrid[row].opened;
1685 }
1686 else
1687 {
1688 wxFAIL_MSG(wxT("unexpected column"));
1689
1690 return false;
1691 }
1692 }
1693
1694 void BugsGridTable::SetValueAsLong( int row, int col, long value )
1695 {
1696 BugsGridData& gd = gs_dataBugsGrid[row];
1697
1698 switch ( col )
1699 {
1700 case Col_Priority:
1701 gd.prio = value;
1702 break;
1703
1704 default:
1705 wxFAIL_MSG(wxT("unexpected column"));
1706 }
1707 }
1708
1709 void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1710 {
1711 if ( col == Col_Opened )
1712 {
1713 gs_dataBugsGrid[row].opened = value;
1714 }
1715 else
1716 {
1717 wxFAIL_MSG(wxT("unexpected column"));
1718 }
1719 }
1720
1721 wxString BugsGridTable::GetColLabelValue( int col )
1722 {
1723 return headers[col];
1724 }
1725
1726 // ----------------------------------------------------------------------------
1727 // BugsGridFrame
1728 // ----------------------------------------------------------------------------
1729
1730 BugsGridFrame::BugsGridFrame()
1731 : wxFrame(NULL, wxID_ANY, wxT("Bugs table"))
1732 {
1733 wxGrid *grid = new wxGrid(this, wxID_ANY);
1734 wxGridTableBase *table = new BugsGridTable();
1735 table->SetAttrProvider(new MyGridCellAttrProvider);
1736 grid->SetTable(table, true);
1737
1738 wxGridCellAttr *attrRO = new wxGridCellAttr,
1739 *attrRangeEditor = new wxGridCellAttr,
1740 *attrCombo = new wxGridCellAttr;
1741
1742 attrRO->SetReadOnly();
1743 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
1744 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1745 severities));
1746
1747 grid->SetColAttr(Col_Id, attrRO);
1748 grid->SetColAttr(Col_Priority, attrRangeEditor);
1749 grid->SetColAttr(Col_Severity, attrCombo);
1750
1751 grid->Fit();
1752 SetClientSize(grid->GetSize());
1753 }
1754
1755 // ============================================================================
1756 // TabularGrid: grid used for display of tabular data
1757 // ============================================================================
1758
1759 class TabularGridTable : public wxGridTableBase
1760 {
1761 public:
1762 enum
1763 {
1764 COL_NAME,
1765 COL_EXT,
1766 COL_SIZE,
1767 COL_DATE,
1768 COL_MAX
1769 };
1770
1771 enum
1772 {
1773 ROW_MAX = 3
1774 };
1775
1776 TabularGridTable() { m_sortOrder = NULL; }
1777
1778 virtual int GetNumberRows() { return ROW_MAX; }
1779 virtual int GetNumberCols() { return COL_MAX; }
1780
1781 virtual wxString GetValue(int row, int col)
1782 {
1783 if ( m_sortOrder )
1784 row = m_sortOrder[row];
1785
1786 switch ( col )
1787 {
1788 case COL_NAME:
1789 case COL_EXT:
1790 return GetNameOrExt(row, col);
1791
1792 case COL_SIZE:
1793 return wxString::Format("%lu", GetSize(row));
1794
1795 case COL_DATE:
1796 return GetDate(row).FormatDate();
1797
1798 case COL_MAX:
1799 default:
1800 wxFAIL_MSG( "unknown column" );
1801 }
1802
1803 return wxString();
1804 }
1805
1806 virtual void SetValue(int, int, const wxString&)
1807 {
1808 wxFAIL_MSG( "shouldn't be called" );
1809 }
1810
1811 virtual wxString GetColLabelValue(int col)
1812 {
1813 // notice that column parameter here always refers to the internal
1814 // column index, independently of its position on the screen
1815 static const char *labels[] = { "Name", "Extension", "Size", "Date" };
1816 wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels) == COL_MAX, LabelsMismatch );
1817
1818 return labels[col];
1819 }
1820
1821 virtual void SetColLabelValue(int, const wxString&)
1822 {
1823 wxFAIL_MSG( "shouldn't be called" );
1824 }
1825
1826 void Sort(int col, bool ascending)
1827 {
1828 // we hardcode all sorting orders for simplicity here
1829 static int sortOrders[COL_MAX][2][ROW_MAX] =
1830 {
1831 // descending ascending
1832 { { 2, 1, 0 }, { 0, 1, 2 } },
1833 { { 2, 1, 0 }, { 0, 1, 2 } },
1834 { { 2, 1, 0 }, { 0, 1, 2 } },
1835 { { 1, 0, 2 }, { 2, 0, 1 } },
1836 };
1837
1838 m_sortOrder = col == wxNOT_FOUND ? NULL : sortOrders[col][ascending];
1839 }
1840
1841 private:
1842 wxString GetNameOrExt(int row, int col) const
1843 {
1844 static const char *
1845 names[] = { "autoexec.bat", "boot.ini", "io.sys" };
1846 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names) == ROW_MAX, NamesMismatch );
1847
1848 const wxString s(names[row]);
1849 return col == COL_NAME ? s.BeforeFirst('.') : s.AfterLast('.');
1850 }
1851
1852 unsigned long GetSize(int row) const
1853 {
1854 static const unsigned long
1855 sizes[] = { 412, 604, 40774 };
1856 wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes) == ROW_MAX, SizesMismatch );
1857
1858 return sizes[row];
1859 }
1860
1861 wxDateTime GetDate(int row) const
1862 {
1863 static const char *
1864 dates[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
1865 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates) == ROW_MAX, DatesMismatch );
1866
1867 wxDateTime dt;
1868 dt.ParseISODate(dates[row]);
1869 return dt;
1870 }
1871
1872 int *m_sortOrder;
1873 };
1874
1875 // specialized text control for column indexes entry
1876 class ColIndexEntry : public wxTextCtrl
1877 {
1878 public:
1879 ColIndexEntry(wxWindow *parent)
1880 : wxTextCtrl(parent, wxID_ANY, "")
1881 {
1882 SetValidator(wxTextValidator(wxFILTER_NUMERIC));
1883 }
1884
1885 int GetCol()
1886 {
1887 unsigned long col;
1888 if ( !GetValue().ToULong(&col) || col > TabularGridTable::COL_MAX )
1889 {
1890 SetFocus();
1891 return -1;
1892 }
1893
1894 return col;
1895 }
1896
1897 protected:
1898 virtual wxSize DoGetBestSize() const
1899 {
1900 wxSize size = wxTextCtrl::DoGetBestSize();
1901 size.x = 3*GetCharWidth();
1902 return size;
1903 }
1904 };
1905
1906 class TabularGridFrame : public wxFrame
1907 {
1908 public:
1909 TabularGridFrame();
1910
1911 private:
1912 enum // control ids
1913 {
1914 Id_Check_UseNativeHeader,
1915 Id_Check_DrawNativeLabels,
1916 Id_Check_ShowRowLabels,
1917 Id_Check_EnableColMove
1918 };
1919
1920 // event handlers
1921
1922 void OnToggleUseNativeHeader(wxCommandEvent&)
1923 {
1924 m_grid->UseNativeColHeader(m_chkUseNative->IsChecked());
1925 }
1926
1927 void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent& event)
1928 {
1929 // we don't draw labels at all, native or otherwise, if we use the
1930 // native header control
1931 event.Enable( !m_chkUseNative->GetValue() );
1932 }
1933
1934 void OnToggleDrawNativeLabels(wxCommandEvent&)
1935 {
1936 m_grid->SetUseNativeColLabels(m_chkDrawNative->IsChecked());
1937 }
1938
1939 void OnToggleShowRowLabels(wxCommandEvent&)
1940 {
1941 m_grid->SetRowLabelSize(m_chkShowRowLabels->IsChecked()
1942 ? wxGRID_AUTOSIZE
1943 : 0);
1944 }
1945
1946 void OnToggleColMove(wxCommandEvent&)
1947 {
1948 m_grid->EnableDragColMove(m_chkEnableColMove->IsChecked());
1949 }
1950
1951 void OnShowHideColumn(wxCommandEvent& event)
1952 {
1953 int col = m_txtColShowHide->GetCol();
1954 if ( col != -1 )
1955 {
1956 m_grid->SetColSize(col,
1957 event.GetId() == wxID_ADD ? wxGRID_AUTOSIZE : 0);
1958
1959 UpdateOrderAndVisibility();
1960 }
1961 }
1962
1963 void OnMoveColumn(wxCommandEvent&)
1964 {
1965 int col = m_txtColIndex->GetCol();
1966 int pos = m_txtColPos->GetCol();
1967 if ( col == -1 || pos == -1 )
1968 return;
1969
1970 m_grid->SetColPos(col, pos);
1971
1972 UpdateOrderAndVisibility();
1973 }
1974
1975 void OnResetColumnOrder(wxCommandEvent&)
1976 {
1977 m_grid->ResetColPos();
1978
1979 UpdateOrderAndVisibility();
1980 }
1981
1982 void OnGridColSort(wxGridEvent& event)
1983 {
1984 const int col = event.GetCol();
1985 m_table->Sort(col, !(m_grid->IsSortingBy(col) &&
1986 m_grid->IsSortOrderAscending()));
1987 }
1988
1989 void OnGridColMove(wxGridEvent& event)
1990 {
1991 // can't update it yet as the order hasn't been changed, so do it a bit
1992 // later
1993 m_shouldUpdateOrder = true;
1994
1995 event.Skip();
1996 }
1997
1998 void OnGridColSize(wxGridSizeEvent& event)
1999 {
2000 // we only catch this event to react to the user showing or hiding this
2001 // column using the header control menu and not because we're
2002 // interested in column resizing
2003 UpdateOrderAndVisibility();
2004
2005 event.Skip();
2006 }
2007
2008 void OnIdle(wxIdleEvent& event)
2009 {
2010 if ( m_shouldUpdateOrder )
2011 {
2012 m_shouldUpdateOrder = false;
2013 UpdateOrderAndVisibility();
2014 }
2015
2016 event.Skip();
2017 }
2018
2019 void UpdateOrderAndVisibility()
2020 {
2021 wxString s;
2022 for ( int pos = 0; pos < TabularGridTable::COL_MAX; pos++ )
2023 {
2024 const int col = m_grid->GetColAt(pos);
2025 const bool isHidden = m_grid->GetColSize(col) == 0;
2026
2027 if ( isHidden )
2028 s << '[';
2029 s << col;
2030 if ( isHidden )
2031 s << ']';
2032
2033 s << ' ';
2034 }
2035
2036 m_statOrder->SetLabel(s);
2037 }
2038
2039 // controls
2040 wxGrid *m_grid;
2041 TabularGridTable *m_table;
2042 wxCheckBox *m_chkUseNative,
2043 *m_chkDrawNative,
2044 *m_chkShowRowLabels,
2045 *m_chkEnableColMove;
2046
2047 ColIndexEntry *m_txtColIndex,
2048 *m_txtColPos,
2049 *m_txtColShowHide;
2050
2051 wxStaticText *m_statOrder;
2052
2053 // fla for EVT_IDLE handler
2054 bool m_shouldUpdateOrder;
2055
2056 wxDECLARE_NO_COPY_CLASS(TabularGridFrame);
2057 DECLARE_EVENT_TABLE()
2058 };
2059
2060 BEGIN_EVENT_TABLE(TabularGridFrame, wxFrame)
2061 EVT_CHECKBOX(Id_Check_UseNativeHeader,
2062 TabularGridFrame::OnToggleUseNativeHeader)
2063 EVT_CHECKBOX(Id_Check_DrawNativeLabels,
2064 TabularGridFrame::OnToggleDrawNativeLabels)
2065 EVT_CHECKBOX(Id_Check_ShowRowLabels,
2066 TabularGridFrame::OnToggleShowRowLabels)
2067 EVT_CHECKBOX(Id_Check_EnableColMove,
2068 TabularGridFrame::OnToggleColMove)
2069
2070 EVT_UPDATE_UI(Id_Check_DrawNativeLabels,
2071 TabularGridFrame::OnUpdateDrawNativeLabelsUI)
2072
2073 EVT_BUTTON(wxID_APPLY, TabularGridFrame::OnMoveColumn)
2074 EVT_BUTTON(wxID_RESET, TabularGridFrame::OnResetColumnOrder)
2075 EVT_BUTTON(wxID_ADD, TabularGridFrame::OnShowHideColumn)
2076 EVT_BUTTON(wxID_DELETE, TabularGridFrame::OnShowHideColumn)
2077
2078 EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort)
2079 EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove)
2080 EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize)
2081
2082 EVT_IDLE(TabularGridFrame::OnIdle)
2083 END_EVENT_TABLE()
2084
2085 TabularGridFrame::TabularGridFrame()
2086 : wxFrame(NULL, wxID_ANY, "Tabular table")
2087 {
2088 m_shouldUpdateOrder = false;
2089
2090 wxPanel * const panel = new wxPanel(this);
2091
2092 // create and initialize the grid with the specified data
2093 m_table = new TabularGridTable;
2094 m_grid = new wxGrid(panel, wxID_ANY,
2095 wxDefaultPosition, wxDefaultSize,
2096 wxBORDER_STATIC | wxWANTS_CHARS);
2097 m_grid->SetTable(m_table, true, wxGrid::wxGridSelectRows);
2098
2099 m_grid->EnableDragColMove();
2100 m_grid->UseNativeColHeader();
2101 m_grid->HideRowLabels();
2102
2103 // add it and the other controls to the frame
2104 wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
2105 sizerTop->Add(m_grid, wxSizerFlags(1).Expand().Border());
2106
2107 wxSizer * const sizerControls = new wxBoxSizer(wxHORIZONTAL);
2108
2109 wxSizer * const sizerStyles = new wxBoxSizer(wxVERTICAL);
2110 m_chkUseNative = new wxCheckBox(panel, Id_Check_UseNativeHeader,
2111 "&Use native header");
2112 m_chkUseNative->SetValue(true);
2113 sizerStyles->Add(m_chkUseNative, wxSizerFlags().Border());
2114
2115 m_chkDrawNative = new wxCheckBox(panel, Id_Check_DrawNativeLabels,
2116 "&Draw native column labels");
2117 sizerStyles->Add(m_chkDrawNative, wxSizerFlags().Border());
2118
2119 m_chkShowRowLabels = new wxCheckBox(panel, Id_Check_ShowRowLabels,
2120 "Show &row labels");
2121 sizerStyles->Add(m_chkShowRowLabels, wxSizerFlags().Border());
2122
2123 m_chkEnableColMove = new wxCheckBox(panel, Id_Check_EnableColMove,
2124 "Allow column re&ordering");
2125 m_chkEnableColMove->SetValue(true);
2126 sizerStyles->Add(m_chkEnableColMove, wxSizerFlags().Border());
2127 sizerControls->Add(sizerStyles);
2128
2129 sizerControls->AddSpacer(10);
2130
2131 wxSizer * const sizerColumns = new wxBoxSizer(wxVERTICAL);
2132 wxSizer * const sizerMoveCols = new wxBoxSizer(wxHORIZONTAL);
2133 const wxSizerFlags
2134 flagsHorz(wxSizerFlags().Border(wxLEFT | wxRIGHT).Centre());
2135 sizerMoveCols->Add(new wxStaticText(panel, wxID_ANY, "&Move column"),
2136 flagsHorz);
2137 m_txtColIndex = new ColIndexEntry(panel);
2138 sizerMoveCols->Add(m_txtColIndex, flagsHorz);
2139 sizerMoveCols->Add(new wxStaticText(panel, wxID_ANY, "&to"), flagsHorz);
2140 m_txtColPos = new ColIndexEntry(panel);
2141 sizerMoveCols->Add(m_txtColPos, flagsHorz);
2142 sizerMoveCols->Add(new wxButton(panel, wxID_APPLY), flagsHorz);
2143
2144 sizerColumns->Add(sizerMoveCols, wxSizerFlags().Expand().Border(wxBOTTOM));
2145
2146 wxSizer * const sizerShowCols = new wxBoxSizer(wxHORIZONTAL);
2147 sizerShowCols->Add(new wxStaticText(panel, wxID_ANY, "Current order:"),
2148 flagsHorz);
2149 m_statOrder = new wxStaticText(panel, wxID_ANY, "<<< default >>>");
2150 sizerShowCols->Add(m_statOrder, flagsHorz);
2151 sizerShowCols->Add(new wxButton(panel, wxID_RESET, "&Reset order"));
2152 sizerColumns->Add(sizerShowCols, wxSizerFlags().Expand().Border(wxTOP));
2153
2154 wxSizer * const sizerShowHide = new wxBoxSizer(wxHORIZONTAL);
2155 sizerShowHide->Add(new wxStaticText(panel, wxID_ANY, "Show/hide column:"),
2156 flagsHorz);
2157 m_txtColShowHide = new ColIndexEntry(panel);
2158 sizerShowHide->Add(m_txtColShowHide, flagsHorz);
2159 sizerShowHide->Add(new wxButton(panel, wxID_ADD, "&Show"), flagsHorz);
2160 sizerShowHide->Add(new wxButton(panel, wxID_DELETE, "&Hide"), flagsHorz);
2161 sizerColumns->Add(sizerShowHide, wxSizerFlags().Expand().Border(wxTOP));
2162
2163 sizerControls->Add(sizerColumns, wxSizerFlags(1).Expand().Border());
2164
2165 sizerTop->Add(sizerControls, wxSizerFlags().Expand().Border());
2166
2167 panel->SetSizer(sizerTop);
2168
2169 SetClientSize(panel->GetBestSize());
2170 SetSizeHints(GetSize());
2171
2172 Show();
2173 }
2174
2175 void GridFrame::OnTabularTable(wxCommandEvent&)
2176 {
2177 new TabularGridFrame;
2178 }
2179
2180 // Example using wxGrid::Render
2181 // Displays a preset selection or, if it exists, a selection block
2182 // Draws the selection to a wxBitmap and displays the bitmap
2183 void GridFrame::OnGridRender( wxCommandEvent& event )
2184 {
2185 int styleRender = 0, i;
2186 bool useLometric = false, defSize = false;
2187 double zoom = 1;
2188 wxSize sizeMargin( 0, 0 );
2189 wxPoint pointOrigin( 0, 0 );
2190
2191 wxMenu* menu = GetMenuBar()->GetMenu( 0 );
2192 wxMenuItem* menuItem = menu->FindItem( ID_RENDER_ROW_LABEL );
2193 menu = menuItem->GetMenu();
2194
2195 if ( menu->FindItem( ID_RENDER_ROW_LABEL )->IsChecked() )
2196 styleRender |= wxGRID_DRAW_ROWS_HEADER;
2197 if ( menu->FindItem( ID_RENDER_COL_LABEL )->IsChecked() )
2198 styleRender |= wxGRID_DRAW_COLS_HEADER;
2199 if ( menu->FindItem( ID_RENDER_GRID_LINES )->IsChecked() )
2200 styleRender |= wxGRID_DRAW_CELL_LINES;
2201 if ( menu->FindItem( ID_RENDER_GRID_BORDER )->IsChecked() )
2202 styleRender |= wxGRID_DRAW_BOX_RECT;
2203 if ( menu->FindItem( ID_RENDER_SELECT_HLIGHT )->IsChecked() )
2204 styleRender |= wxGRID_DRAW_SELECTION;
2205 if ( menu->FindItem( ID_RENDER_LOMETRIC )->IsChecked() )
2206 useLometric = true;
2207 if ( menu->FindItem( ID_RENDER_MARGIN )->IsChecked() )
2208 {
2209 pointOrigin.x += 50;
2210 pointOrigin.y += 50;
2211 sizeMargin.IncBy( 50 );
2212 }
2213 if ( menu->FindItem( ID_RENDER_ZOOM )->IsChecked() )
2214 zoom = 1.25;
2215 if ( menu->FindItem( ID_RENDER_DEFAULT_SIZE )->IsChecked() )
2216 defSize = true;
2217
2218 // init render area coords with a default row and col selection
2219 wxGridCellCoords topLeft( 0, 0 ), bottomRight( 8, 6 );
2220 // check whether we are printing a block selection
2221 // other selection types not catered for here
2222 if ( event.GetId() == ID_RENDER_COORDS )
2223 {
2224 topLeft.SetCol( 6 );
2225 topLeft.SetRow( 4 );
2226 bottomRight.SetCol( 15 );
2227 bottomRight.SetRow( 29 );
2228 }
2229 else if ( grid->IsSelection() && grid->GetSelectionBlockTopLeft().Count() )
2230 {
2231 wxGridCellCoordsArray cells = grid->GetSelectionBlockTopLeft();
2232 if ( grid->GetSelectionBlockBottomRight().Count() )
2233 {
2234 cells.Add( grid->GetSelectionBlockBottomRight()[ 0 ] );
2235 topLeft.Set( cells[ 0 ].GetRow(),
2236 cells[ 0 ].GetCol() );
2237 bottomRight.Set( cells[ 1 ].GetRow(),
2238 cells[ 1 ].GetCol() );
2239 }
2240 }
2241
2242 // sum col widths
2243 wxSize sizeRender( 0, 0 );
2244 wxGridSizesInfo sizeinfo = grid->GetColSizes();
2245 for ( i = topLeft.GetCol(); i <= bottomRight.GetCol(); i++ )
2246 {
2247 sizeRender.x += sizeinfo.GetSize( i );
2248 }
2249
2250 // sum row heights
2251 sizeinfo = grid->GetRowSizes();
2252 for ( i = topLeft.GetRow(); i <= bottomRight.GetRow(); i++ )
2253 {
2254 sizeRender.y += sizeinfo.GetSize( i );
2255 }
2256
2257 if ( styleRender & wxGRID_DRAW_ROWS_HEADER )
2258 sizeRender.x += grid->GetRowLabelSize();
2259 if ( styleRender & wxGRID_DRAW_COLS_HEADER )
2260 sizeRender.y += grid->GetColLabelSize();
2261
2262 sizeRender.x *= zoom;
2263 sizeRender.y *= zoom;
2264
2265 // delete any existing render frame and create new one
2266 wxWindow* win = FindWindow( "frameRender" );
2267 if ( win )
2268 win->Destroy();
2269
2270 wxFrame* frame = new wxFrame( this, wxID_ANY, "Grid Render" );
2271 frame->SetClientSize( 780, 400 );
2272 frame->SetName( "frameRender" );
2273
2274 wxPanel* canvas = new wxPanel( frame, wxID_ANY );
2275
2276 // make bitmap large enough for margins if any
2277 if ( !defSize )
2278 sizeRender.IncBy( sizeMargin * 2 );
2279 else
2280 sizeRender.IncBy( sizeMargin );
2281
2282 wxBitmap bmp( sizeRender );
2283 // don't leave it larger or drawing will be scaled
2284 sizeRender.DecBy( sizeMargin * 2 );
2285
2286 wxMemoryDC memDc(bmp);
2287
2288 // default row labels have no background colour so set background
2289 memDc.SetBackground( wxBrush( canvas->GetBackgroundColour() ) );
2290 memDc.Clear();
2291
2292 // convert sizeRender to mapping mode units if necessary
2293 if ( useLometric )
2294 {
2295 memDc.SetMapMode( wxMM_LOMETRIC );
2296 sizeRender.x = memDc.DeviceToLogicalXRel( sizeRender.x );
2297 sizeRender.y = memDc.DeviceToLogicalYRel( sizeRender.y );
2298 }
2299
2300 // pass wxDefaultSize if menu item is checked
2301 if ( defSize )
2302 sizeRender = wxDefaultSize;
2303
2304 grid->Render( memDc,
2305 pointOrigin,
2306 sizeRender,
2307 topLeft, bottomRight,
2308 wxGridRenderStyle( styleRender ) );
2309
2310 m_gridBitmap = bmp;
2311
2312 canvas->Connect( wxEVT_PAINT,
2313 wxPaintEventHandler(GridFrame::OnRenderPaint),
2314 NULL,
2315 this );
2316
2317 frame->Show();
2318 }
2319
2320 void GridFrame::OnRenderPaint( wxPaintEvent& event )
2321 {
2322 wxPanel* canvas = ( wxPanel* )event.GetEventObject();
2323 wxPaintDC dc( canvas );
2324 canvas->PrepareDC( dc );
2325
2326 if ( !m_gridBitmap.IsOk() )
2327 return;;
2328
2329 wxMemoryDC memDc( m_gridBitmap );
2330
2331 dc.Blit( 0, 0,
2332 m_gridBitmap.GetWidth(),
2333 m_gridBitmap.GetHeight(),
2334 &memDc, 0, 0 );
2335 }
2336
2337 void GridFrame::HideCol( wxCommandEvent& WXUNUSED(event) )
2338 {
2339 grid->HideCol(0);
2340 }
2341
2342 void GridFrame::ShowCol( wxCommandEvent& WXUNUSED(event) )
2343 {
2344 grid->ShowCol(0);
2345 }
2346
2347 void GridFrame::HideRow( wxCommandEvent& WXUNUSED(event) )
2348 {
2349 grid->HideRow(1);
2350 }
2351
2352 void GridFrame::ShowRow( wxCommandEvent& WXUNUSED(event) )
2353 {
2354 grid->ShowRow(1);
2355 }