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