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