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