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