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