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