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