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