]> git.saurik.com Git - wxWidgets.git/blame - samples/newgrid/griddemo.cpp
fixed stream code breakage caused by the previous check in
[wxWidgets.git] / samples / newgrid / griddemo.cpp
CommitLineData
10434f3c
MB
1/////////////////////////////////////////////////////////////////////////////
2// Name: griddemo.cpp
3// Purpose: Grid control wxWindows sample
4// Author: Michael Bedward
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Michael Bedward, Julian Smart
8// Licence: wxWindows license
9/////////////////////////////////////////////////////////////////////////////
e442cc0d 10
ab79958a
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
e442cc0d 19#ifdef __GNUG__
ab79958a
VZ
20 #pragma implementation
21 #pragma interface
e442cc0d
MB
22#endif
23
24// For compilers that support precompilation, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28#pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
bf9adb3a 32 #include "wx/wx.h"
e442cc0d
MB
33#endif
34
2f6c54eb
VZ
35#include "wx/colordlg.h"
36#include "wx/fontdlg.h"
37
e442cc0d 38#include "wx/grid.h"
d10f4bf9 39#include "wx/generic/gridctrl.h"
e442cc0d
MB
40
41#include "griddemo.h"
42
ab79958a
VZ
43// ----------------------------------------------------------------------------
44// wxWin macros
45// ----------------------------------------------------------------------------
46
e442cc0d
MB
47IMPLEMENT_APP( GridApp )
48
ab79958a
VZ
49// ============================================================================
50// implementation
51// ============================================================================
e442cc0d 52
ab79958a
VZ
53// ----------------------------------------------------------------------------
54// GridApp
55// ----------------------------------------------------------------------------
e442cc0d
MB
56
57bool GridApp::OnInit()
58{
59 GridFrame *frame = new GridFrame;
60 frame->Show( TRUE );
f0102d2a 61
e442cc0d
MB
62 return TRUE;
63}
64
ab79958a
VZ
65// ----------------------------------------------------------------------------
66// GridFrame
67// ----------------------------------------------------------------------------
e442cc0d
MB
68
69BEGIN_EVENT_TABLE( GridFrame, wxFrame )
70 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
f0102d2a 71 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
f445b853 72 EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
dd16fdae 73 EVT_MENU( ID_TOGGLEROWSIZING, GridFrame::ToggleRowSizing )
ea179c7b 74 EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing )
4cfa5de6 75 EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
f6bcfd97
BP
76 EVT_MENU( ID_TOGGLEGRIDLINES, GridFrame::ToggleGridLines )
77 EVT_MENU( ID_AUTOSIZECOLS, GridFrame::AutoSizeCols )
ecd69fb6
SN
78 EVT_MENU( ID_CELLOVERFLOW, GridFrame::CellOverflow )
79 EVT_MENU( ID_RESIZECELL, GridFrame::ResizeCell )
f0102d2a
VZ
80 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
81 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
bf9adb3a 82 EVT_MENU( ID_SETLABEL_FONT, GridFrame::SetLabelFont )
f0102d2a
VZ
83 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
84 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
85 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
86 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
e442cc0d 87 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
0eee5283
MB
88 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
89 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
f445b853
MB
90 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
91 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
e442cc0d 92 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
043d16b2
SN
93 EVT_MENU( ID_SELCELLS, GridFrame::SelectCells )
94 EVT_MENU( ID_SELROWS, GridFrame::SelectRows )
95 EVT_MENU( ID_SELCOLS, GridFrame::SelectCols )
f0102d2a 96
ab79958a
VZ
97 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
98 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
99
e442cc0d
MB
100 EVT_MENU( ID_ABOUT, GridFrame::About )
101 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
f97c9b5b 102 EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
35f97e95 103 EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
671bcff5 104 EVT_MENU( ID_SMALL_GRID, GridFrame::OnSmallGrid)
e442cc0d 105
14e5a313
VZ
106 EVT_MENU( ID_DESELECT_CELL, GridFrame::DeselectCell)
107 EVT_MENU( ID_DESELECT_COL, GridFrame::DeselectCol)
108 EVT_MENU( ID_DESELECT_ROW, GridFrame::DeselectRow)
109 EVT_MENU( ID_DESELECT_ALL, GridFrame::DeselectAll)
110 EVT_MENU( ID_SELECT_CELL, GridFrame::SelectCell)
111 EVT_MENU( ID_SELECT_COL, GridFrame::SelectCol)
112 EVT_MENU( ID_SELECT_ROW, GridFrame::SelectRow)
113 EVT_MENU( ID_SELECT_ALL, GridFrame::SelectAll)
93521c94 114 EVT_MENU( ID_SELECT_UNSELECT, GridFrame::OnAddToSelectToggle)
14e5a313 115
d2520c85
RD
116 EVT_MENU( ID_SET_HIGHLIGHT_WIDTH, GridFrame::OnSetHighlightWidth)
117 EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH, GridFrame::OnSetROHighlightWidth)
118
5795d034
MB
119 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
120 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
121 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
122 EVT_GRID_COL_SIZE( GridFrame::OnColSize )
c336585e 123 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
5795d034
MB
124 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
125 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
b54ba671
VZ
126
127 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
128 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
e442cc0d
MB
129END_EVENT_TABLE()
130
f0102d2a 131
e442cc0d
MB
132GridFrame::GridFrame()
133 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
134 wxDefaultPosition,
135 wxDefaultSize )
136{
137 int gridW = 600, gridH = 300;
07296f0b 138 int logW = gridW, logH = 100;
f0102d2a 139
e442cc0d 140 wxMenu *fileMenu = new wxMenu;
43947979 141 fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V");
35f97e95 142 fileMenu->Append( ID_BUGS_TABLE, "&Bugs table test\tCtrl-B");
671bcff5 143 fileMenu->Append( ID_SMALL_GRID, "&Small Grid test\tCtrl-S");
f97c9b5b 144 fileMenu->AppendSeparator();
f0102d2a
VZ
145 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
146
e442cc0d
MB
147 wxMenu *viewMenu = new wxMenu;
148 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
149 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
f445b853 150 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
dd16fdae
MB
151 viewMenu->Append( ID_TOGGLEROWSIZING, "Ro&w drag-resize", "", TRUE );
152 viewMenu->Append( ID_TOGGLECOLSIZING, "C&ol drag-resize", "", TRUE );
4cfa5de6 153 viewMenu->Append( ID_TOGGLEGRIDSIZING, "&Grid drag-resize", "", TRUE );
f6bcfd97 154 viewMenu->Append( ID_TOGGLEGRIDLINES, "&Grid Lines", "", TRUE );
d2520c85
RD
155 viewMenu->Append( ID_SET_HIGHLIGHT_WIDTH, "&Set Cell Highlight Width...", "" );
156 viewMenu->Append( ID_SET_RO_HIGHLIGHT_WIDTH, "&Set Cell RO Highlight Width...", "" );
f6bcfd97 157 viewMenu->Append( ID_AUTOSIZECOLS, "&Auto-size cols" );
ecd69fb6
SN
158 viewMenu->Append( ID_CELLOVERFLOW, "&Overflow cells", "", TRUE );
159 viewMenu->Append( ID_RESIZECELL, "&Resize cell (7,1)", "", TRUE );
f0102d2a 160
e442cc0d
MB
161 wxMenu *rowLabelMenu = new wxMenu;
162
163 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
164 rowLabelMenu,
165 "Change alignment of row labels" );
166
167 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
f0102d2a
VZ
168 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
169
e442cc0d
MB
170 wxMenu *colLabelMenu = new wxMenu;
171
172 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
173 colLabelMenu,
174 "Change alignment of col labels" );
175
176 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
177 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
178
ab79958a 179 wxMenu *colMenu = new wxMenu;
bf9adb3a
VZ
180 colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour..." );
181 colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour..." );
182 colMenu->Append( ID_SETLABEL_FONT, "Set label fo&nt..." );
183 colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour..." );
184 colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour..." );
185 colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour..." );
e442cc0d 186
0eee5283
MB
187 wxMenu *editMenu = new wxMenu;
188 editMenu->Append( ID_INSERTROW, "Insert &row" );
189 editMenu->Append( ID_INSERTCOL, "Insert &column" );
f445b853
MB
190 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
191 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
0eee5283 192 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
f0102d2a 193
14e5a313 194 wxMenu *selectMenu = new wxMenu;
93521c94
VZ
195 selectMenu->Append( ID_SELECT_UNSELECT, "Add new cells to the selection",
196 "When off, old selection is deselected before "
197 "selecting the new cells", TRUE );
14e5a313
VZ
198 selectMenu->Append( ID_SELECT_ALL, "Select all");
199 selectMenu->Append( ID_SELECT_ROW, "Select row 2");
200 selectMenu->Append( ID_SELECT_COL, "Select col 2");
201 selectMenu->Append( ID_SELECT_CELL, "Select cell (3, 1)");
202 selectMenu->Append( ID_DESELECT_ALL, "Deselect all");
203 selectMenu->Append( ID_DESELECT_ROW, "Deselect row 2");
204 selectMenu->Append( ID_DESELECT_COL, "Deselect col 2");
205 selectMenu->Append( ID_DESELECT_CELL, "Deselect cell (3, 1)");
043d16b2 206 wxMenu *selectionMenu = new wxMenu;
14e5a313 207 selectMenu->Append( ID_CHANGESEL, "Change &selection mode",
5c8fc7c1
SN
208 selectionMenu,
209 "Change selection mode" );
043d16b2
SN
210
211 selectionMenu->Append( ID_SELCELLS, "Select &Cells" );
212 selectionMenu->Append( ID_SELROWS, "Select &Rows" );
213 selectionMenu->Append( ID_SELCOLS, "Select C&ols" );
214
14e5a313 215
e442cc0d
MB
216 wxMenu *helpMenu = new wxMenu;
217 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
f0102d2a 218
e442cc0d
MB
219 wxMenuBar *menuBar = new wxMenuBar;
220 menuBar->Append( fileMenu, "&File" );
221 menuBar->Append( viewMenu, "&View" );
ab79958a 222 menuBar->Append( colMenu, "&Colours" );
0eee5283 223 menuBar->Append( editMenu, "&Edit" );
14e5a313 224 menuBar->Append( selectMenu, "&Select" );
e442cc0d
MB
225 menuBar->Append( helpMenu, "&Help" );
226
227 SetMenuBar( menuBar );
228
93521c94
VZ
229 m_addToSel = FALSE;
230
e442cc0d
MB
231 grid = new wxGrid( this,
232 -1,
233 wxPoint( 0, 0 ),
234 wxSize( 400, 300 ) );
f0102d2a 235
e442cc0d
MB
236 logWin = new wxTextCtrl( this,
237 -1,
238 wxEmptyString,
239 wxPoint( 0, gridH + 20 ),
240 wxSize( logW, logH ),
241 wxTE_MULTILINE );
f0102d2a 242
e442cc0d 243 logger = new wxLogTextCtrl( logWin );
66b3609e 244 m_logOld = logger->SetActiveTarget( logger );
e442cc0d
MB
245 logger->SetTimestamp( NULL );
246
247 // this will create a grid and, by default, an associated grid
984ef9dc 248 // table for strings
816be743 249 grid->CreateGrid( 100, 100 );
f2d76237 250
e442cc0d
MB
251 grid->SetRowSize( 0, 60 );
252 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
f0102d2a 253
d10f4bf9
VZ
254 grid->SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." );
255 grid->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer);
256 grid->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor);
671bcff5 257
e442cc0d 258 grid->SetCellValue( 0, 2, "Blah" );
283b7808
VZ
259 grid->SetCellValue( 0, 3, "Read only" );
260 grid->SetReadOnly( 0, 3 );
671bcff5 261
fe7b9ed6 262 grid->SetCellValue( 0, 4, "Can veto edit this cell" );
e442cc0d
MB
263
264 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
265
266 grid->SetRowSize( 99, 60 );
267 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
ecd69fb6
SN
268 grid->SetCellValue( 1, 0, "This default cell will overflow into neighboring cells, but not if you turn overflow off.");
269
270 grid->SetCellTextColour(1, 2, *wxRED);
271 grid->SetCellBackgroundColour(1, 2, *wxGREEN);
272
273 grid->SetCellValue( 1, 4, "I'm in the middle");
f445b853 274
b99be8fb 275 grid->SetCellValue(2, 2, "red");
ab79958a 276
b99be8fb 277 grid->SetCellTextColour(2, 2, *wxRED);
2e9a6788 278 grid->SetCellValue(3, 3, "green on grey");
b99be8fb 279 grid->SetCellTextColour(3, 3, *wxGREEN);
2e9a6788 280 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
b99be8fb 281
ab79958a 282 grid->SetCellValue(4, 4, "a weird looking cell");
0767cb6f 283 grid->SetCellAlignment(4, 4, wxALIGN_CENTRE, wxALIGN_CENTRE);
ab79958a
VZ
284 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
285
19d7140e 286 grid->SetCellValue(3, 0, "0");
508011ce
VZ
287 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
288 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
289
758cbedf
VZ
290 wxGridCellAttr *attr;
291 attr = new wxGridCellAttr;
292 attr->SetTextColour(*wxBLUE);
293 grid->SetColAttr(5, attr);
294 attr = new wxGridCellAttr;
19d7140e 295 attr->SetBackgroundColour(*wxRED);
758cbedf
VZ
296 grid->SetRowAttr(5, attr);
297
43947979
VZ
298 grid->SetCellValue(2, 4, "a wider column");
299 grid->SetColSize(4, 120);
300 grid->SetColMinimalWidth(4, 120);
283b7808 301
19d7140e
VZ
302 grid->SetCellTextColour(5, 8, *wxGREEN);
303 grid->SetCellValue(5, 8, "Bg from row attr\nText col from cell attr");
ecd69fb6 304 grid->SetCellValue(5, 5, "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");
0b190b0f 305
19d7140e 306 grid->SetColFormatFloat(6);
0b190b0f
VZ
307 grid->SetCellValue(0, 6, "3.1415");
308 grid->SetCellValue(1, 6, "1415");
309 grid->SetCellValue(2, 6, "12345.67890");
310
19d7140e
VZ
311 grid->SetColFormatFloat(7, 6, 2);
312 grid->SetCellValue(0, 7, "3.1415");
313 grid->SetCellValue(1, 7, "1415");
314 grid->SetCellValue(2, 7, "12345.67890");
315
ecd69fb6
SN
316 const wxString choices[] =
317 {
318 _T("Please select a choice"),
319 _T("This takes two cells"),
320 _T("Another choice"),
321 };
322 grid->SetCellEditor(4, 0, new wxGridCellChoiceEditor(WXSIZEOF(choices), choices));
323 grid->SetCellSize(4, 0, 1, 2);
324 grid->SetCellValue(4, 0, choices[0]);
325 grid->SetCellOverflow(4, 0, FALSE);
326
327 grid->SetCellSize(7, 1, 3, 4);
328 grid->SetCellAlignment(7, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
329 grid->SetCellValue(7, 1, "Big box!");
330
e442cc0d
MB
331 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
332 topSizer->Add( grid,
333 1,
334 wxEXPAND );
f0102d2a 335
e442cc0d 336 topSizer->Add( logWin,
f0102d2a 337 0,
e442cc0d 338 wxEXPAND );
f0102d2a 339
e442cc0d
MB
340 SetAutoLayout( TRUE );
341 SetSizer( topSizer );
f0102d2a 342
e442cc0d
MB
343 topSizer->Fit( this );
344 topSizer->SetSizeHints( this );
f0102d2a 345
e442cc0d
MB
346 Centre();
347 SetDefaults();
348}
349
350
351GridFrame::~GridFrame()
352{
66b3609e 353 delete wxLog::SetActiveTarget(m_logOld);
e442cc0d
MB
354}
355
f445b853 356
e442cc0d
MB
357void GridFrame::SetDefaults()
358{
359 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
360 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
f445b853 361 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
dd16fdae
MB
362 GetMenuBar()->Check( ID_TOGGLEROWSIZING, TRUE );
363 GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE );
4cfa5de6 364 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, TRUE );
f6bcfd97 365 GetMenuBar()->Check( ID_TOGGLEGRIDLINES, TRUE );
ecd69fb6 366 GetMenuBar()->Check( ID_CELLOVERFLOW, TRUE );
e442cc0d
MB
367}
368
369
370void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
371{
372 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
373 {
374 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
375 }
376 else
377 {
378 grid->SetRowLabelSize( 0 );
379 }
380}
381
382
383void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
384{
385 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
386 {
387 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
388 }
389 else
390 {
391 grid->SetColLabelSize( 0 );
392 }
393}
394
395
f445b853 396void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
e442cc0d 397{
f445b853
MB
398 grid->EnableEditing(
399 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
e442cc0d
MB
400}
401
402
dd16fdae
MB
403void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
404{
ea179c7b 405 grid->EnableDragRowSize(
dd16fdae
MB
406 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
407}
408
409
410void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
411{
ea179c7b 412 grid->EnableDragColSize(
dd16fdae
MB
413 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
414}
415
4cfa5de6
RD
416void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
417{
418 grid->EnableDragGridSize(
419 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
420}
421
dd16fdae 422
f6bcfd97
BP
423void GridFrame::ToggleGridLines( wxCommandEvent& WXUNUSED(ev) )
424{
425 grid->EnableGridLines(
426 GetMenuBar()->IsChecked( ID_TOGGLEGRIDLINES ) );
427}
428
d2520c85
RD
429void GridFrame::OnSetHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
430{
431 wxString choices[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
432
433 wxSingleChoiceDialog dlg(this, "Choose the thickness of the highlight pen:",
434 "Pen Width", 11, choices);
435
436 int current = grid->GetCellHighlightPenWidth();
437 dlg.SetSelection(current);
438 if (dlg.ShowModal() == wxID_OK) {
439 grid->SetCellHighlightPenWidth(dlg.GetSelection());
440 }
441}
442
443void GridFrame::OnSetROHighlightWidth( wxCommandEvent& WXUNUSED(ev) )
444{
445 wxString choices[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
446
447 wxSingleChoiceDialog dlg(this, "Choose the thickness of the highlight pen:",
448 "Pen Width", 11, choices);
449
450 int current = grid->GetCellHighlightROPenWidth();
451 dlg.SetSelection(current);
452 if (dlg.ShowModal() == wxID_OK) {
453 grid->SetCellHighlightROPenWidth(dlg.GetSelection());
454 }
455}
456
457
f6bcfd97
BP
458
459void GridFrame::AutoSizeCols( wxCommandEvent& WXUNUSED(ev) )
460{
461 grid->AutoSizeColumns();
462 grid->Refresh();
463}
464
ecd69fb6
SN
465void GridFrame::CellOverflow( wxCommandEvent& ev )
466{
467 grid->SetDefaultCellOverflow(ev.IsChecked());
468 grid->Refresh();
469}
470
471void GridFrame::ResizeCell( wxCommandEvent& ev )
472{
473 if (ev.IsChecked())
474 grid->SetCellSize( 7, 1, 5, 5 );
475 else
476 grid->SetCellSize( 7, 1, 1, 5 );
477 grid->Refresh();
478}
f6bcfd97 479
e442cc0d
MB
480void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
481{
482 wxColourDialog dlg( NULL );
483 if ( dlg.ShowModal() == wxID_OK )
484 {
485 wxColourData retData;
486 retData = dlg.GetColourData();
487 wxColour colour = retData.GetColour();
488
489 grid->SetLabelBackgroundColour( colour );
490 }
491}
492
493
494void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
495{
496 wxColourDialog dlg( NULL );
497 if ( dlg.ShowModal() == wxID_OK )
498 {
499 wxColourData retData;
500 retData = dlg.GetColourData();
501 wxColour colour = retData.GetColour();
502
503 grid->SetLabelTextColour( colour );
504 }
505}
506
bf9adb3a
VZ
507void GridFrame::SetLabelFont( wxCommandEvent& WXUNUSED(ev) )
508{
509 wxFont font = wxGetFontFromUser(this);
510 if ( font.Ok() )
511 {
512 grid->SetLabelFont(font);
513 }
514}
e442cc0d
MB
515
516void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
517{
518 int horiz, vert;
519 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 520
e442cc0d
MB
521 switch ( horiz )
522 {
0767cb6f
MB
523 case wxALIGN_LEFT:
524 horiz = wxALIGN_CENTRE;
e442cc0d 525 break;
f0102d2a 526
0767cb6f
MB
527 case wxALIGN_CENTRE:
528 horiz = wxALIGN_RIGHT;
e442cc0d
MB
529 break;
530
0767cb6f
MB
531 case wxALIGN_RIGHT:
532 horiz = wxALIGN_LEFT;
e442cc0d
MB
533 break;
534 }
535
536 grid->SetRowLabelAlignment( horiz, -1 );
537}
538
539void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
540{
541 int horiz, vert;
542 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 543
e442cc0d
MB
544 switch ( vert )
545 {
0767cb6f
MB
546 case wxALIGN_TOP:
547 vert = wxALIGN_CENTRE;
e442cc0d 548 break;
f0102d2a 549
0767cb6f
MB
550 case wxALIGN_CENTRE:
551 vert = wxALIGN_BOTTOM;
e442cc0d
MB
552 break;
553
0767cb6f
MB
554 case wxALIGN_BOTTOM:
555 vert = wxALIGN_TOP;
e442cc0d
MB
556 break;
557 }
558
559 grid->SetRowLabelAlignment( -1, vert );
560}
561
562
563void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
564{
565 int horiz, vert;
566 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 567
e442cc0d
MB
568 switch ( horiz )
569 {
0767cb6f
MB
570 case wxALIGN_LEFT:
571 horiz = wxALIGN_CENTRE;
e442cc0d 572 break;
f0102d2a 573
0767cb6f
MB
574 case wxALIGN_CENTRE:
575 horiz = wxALIGN_RIGHT;
e442cc0d
MB
576 break;
577
0767cb6f
MB
578 case wxALIGN_RIGHT:
579 horiz = wxALIGN_LEFT;
e442cc0d
MB
580 break;
581 }
582
583 grid->SetColLabelAlignment( horiz, -1 );
584}
585
586
587void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
588{
589 int horiz, vert;
590 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 591
e442cc0d
MB
592 switch ( vert )
593 {
0767cb6f
MB
594 case wxALIGN_TOP:
595 vert = wxALIGN_CENTRE;
e442cc0d 596 break;
f0102d2a 597
0767cb6f
MB
598 case wxALIGN_CENTRE:
599 vert = wxALIGN_BOTTOM;
e442cc0d
MB
600 break;
601
0767cb6f
MB
602 case wxALIGN_BOTTOM:
603 vert = wxALIGN_TOP;
e442cc0d
MB
604 break;
605 }
606
607 grid->SetColLabelAlignment( -1, vert );
608}
609
610
611void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
612{
613 wxColourDialog dlg( NULL );
614 if ( dlg.ShowModal() == wxID_OK )
615 {
616 wxColourData retData;
617 retData = dlg.GetColourData();
618 wxColour colour = retData.GetColour();
619
620 grid->SetGridLineColour( colour );
621 }
622}
623
624
0eee5283
MB
625void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
626{
4e5b5abb 627 grid->InsertRows( grid->GetGridCursorRow(), 1 );
0eee5283
MB
628}
629
630
631void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
632{
4e5b5abb 633 grid->InsertCols( grid->GetGridCursorCol(), 1 );
0eee5283
MB
634}
635
636
f445b853 637void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
0eee5283 638{
f445b853
MB
639 if ( grid->IsSelection() )
640 {
f6bcfd97
BP
641 grid->BeginBatch();
642 for ( int n = 0; n < grid->GetNumberRows(); )
5c8fc7c1
SN
643 if ( grid->IsInSelection( n , 0 ) )
644 grid->DeleteRows( n, 1 );
2f6c54eb
VZ
645 else
646 n++;
f6bcfd97 647 grid->EndBatch();
f445b853 648 }
0eee5283
MB
649}
650
651
f445b853 652void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
0eee5283 653{
f445b853
MB
654 if ( grid->IsSelection() )
655 {
f6bcfd97
BP
656 grid->BeginBatch();
657 for ( int n = 0; n < grid->GetNumberCols(); )
5c8fc7c1
SN
658 if ( grid->IsInSelection( 0 , n ) )
659 grid->DeleteCols( n, 1 );
2f6c54eb
VZ
660 else
661 n++;
f6bcfd97 662 grid->EndBatch();
f445b853 663 }
0eee5283
MB
664}
665
666
e442cc0d
MB
667void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
668{
669 grid->ClearGrid();
670}
671
043d16b2
SN
672void GridFrame::SelectCells( wxCommandEvent& WXUNUSED(ev) )
673{
674 grid->SetSelectionMode( wxGrid::wxGridSelectCells );
675}
676
677void GridFrame::SelectRows( wxCommandEvent& WXUNUSED(ev) )
678{
679 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
680}
681
682void GridFrame::SelectCols( wxCommandEvent& WXUNUSED(ev) )
683{
684 grid->SetSelectionMode( wxGrid::wxGridSelectColumns );
685}
686
ab79958a
VZ
687void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
688{
689 wxColour col = wxGetColourFromUser(this);
690 if ( col.Ok() )
758cbedf 691 {
ab79958a 692 grid->SetDefaultCellTextColour(col);
758cbedf
VZ
693 grid->Refresh();
694 }
ab79958a
VZ
695}
696
697void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
698{
699 wxColour col = wxGetColourFromUser(this);
700 if ( col.Ok() )
758cbedf 701 {
2918e2d2
SN
702 // Check the new Refresh function by passing it a rectangle
703 // which exactly fits the grid.
704 wxRect r(wxPoint(0, 0), grid->GetSize());
ab79958a 705 grid->SetDefaultCellBackgroundColour(col);
2918e2d2 706 grid->Refresh(TRUE, &r);
758cbedf 707 }
ab79958a 708}
e442cc0d 709
14e5a313
VZ
710void GridFrame::DeselectCell(wxCommandEvent& WXUNUSED(event))
711{
712 grid->DeselectCell(3, 1);
713}
714
715void GridFrame::DeselectCol(wxCommandEvent& WXUNUSED(event))
716{
717 grid->DeselectCol(2);
718}
719
720void GridFrame::DeselectRow(wxCommandEvent& WXUNUSED(event))
721{
722 grid->DeselectRow(2);
723}
724
725void GridFrame::DeselectAll(wxCommandEvent& WXUNUSED(event))
726{
727 grid->ClearSelection();
728}
729
730void GridFrame::SelectCell(wxCommandEvent& WXUNUSED(event))
731{
93521c94 732 grid->SelectBlock(3, 1, 3, 1, m_addToSel);
14e5a313
VZ
733}
734
735void GridFrame::SelectCol(wxCommandEvent& WXUNUSED(event))
736{
93521c94 737 grid->SelectCol(2, m_addToSel);
14e5a313
VZ
738}
739
740void GridFrame::SelectRow(wxCommandEvent& WXUNUSED(event))
741{
93521c94 742 grid->SelectRow(2, m_addToSel);
14e5a313
VZ
743}
744
745void GridFrame::SelectAll(wxCommandEvent& WXUNUSED(event))
746{
747 grid->SelectAll();
748}
749
93521c94
VZ
750void GridFrame::OnAddToSelectToggle(wxCommandEvent& event)
751{
752 m_addToSel = event.IsChecked();
753}
754
e442cc0d
MB
755void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
756{
757 logBuf = "";
758 if ( ev.GetRow() != -1 )
759 {
c336585e 760 logBuf << "Left click on row label " << ev.GetRow();
e442cc0d
MB
761 }
762 else if ( ev.GetCol() != -1 )
763 {
c336585e 764 logBuf << "Left click on col label " << ev.GetCol();
e442cc0d
MB
765 }
766 else
767 {
c336585e 768 logBuf << "Left click on corner label";
e442cc0d
MB
769 }
770
771 if ( ev.ShiftDown() ) logBuf << " (shift down)";
e32352cf 772 if ( ev.ControlDown() ) logBuf << " (control down)";
4693b20c 773 wxLogMessage( wxT("%s"), logBuf.c_str() );
f0102d2a 774
c336585e
MB
775 // you must call event skip if you want default grid processing
776 //
e442cc0d
MB
777 ev.Skip();
778}
779
780
781void GridFrame::OnCellLeftClick( wxGridEvent& ev )
782{
783 logBuf = "";
c336585e 784 logBuf << "Left click at row " << ev.GetRow()
e442cc0d 785 << " col " << ev.GetCol();
4693b20c 786 wxLogMessage( wxT("%s"), logBuf.c_str() );
0eee5283 787
e442cc0d
MB
788 // you must call event skip if you want default grid processing
789 // (cell highlighting etc.)
790 //
791 ev.Skip();
792}
793
794
795void GridFrame::OnRowSize( wxGridSizeEvent& ev )
796{
797 logBuf = "";
798 logBuf << "Resized row " << ev.GetRowOrCol();
4693b20c 799 wxLogMessage( wxT("%s"), logBuf.c_str() );
f0102d2a 800
e442cc0d
MB
801 ev.Skip();
802}
803
804
805void GridFrame::OnColSize( wxGridSizeEvent& ev )
806{
807 logBuf = "";
808 logBuf << "Resized col " << ev.GetRowOrCol();
4693b20c 809 wxLogMessage( wxT("%s"), logBuf.c_str() );
f0102d2a 810
e442cc0d
MB
811 ev.Skip();
812}
813
c336585e
MB
814
815void GridFrame::OnSelectCell( wxGridEvent& ev )
816{
817 logBuf = "";
5c8fc7c1
SN
818 if ( ev.Selecting() )
819 logBuf << "Selected ";
820 else
821 logBuf << "Deselected ";
822 logBuf << "cell at row " << ev.GetRow()
d95b0c2b 823 << " col " << ev.GetCol()
f41986da
MB
824 << " ( ControlDown: "<< (ev.ControlDown() ? 'T':'F')
825 << ", ShiftDown: "<< (ev.ShiftDown() ? 'T':'F')
826 << ", AltDown: "<< (ev.AltDown() ? 'T':'F')
827 << ", MetaDown: "<< (ev.MetaDown() ? 'T':'F') << " )";
4693b20c 828 wxLogMessage( wxT("%s"), logBuf.c_str() );
f0102d2a 829
c336585e
MB
830 // you must call Skip() if you want the default processing
831 // to occur in wxGrid
832 ev.Skip();
833}
834
e442cc0d
MB
835void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
836{
837 logBuf = "";
5c8fc7c1
SN
838 if ( ev.Selecting() )
839 logBuf << "Selected ";
840 else
841 logBuf << "Deselected ";
842 logBuf << "cells from row " << ev.GetTopRow()
843 << " col " << ev.GetLeftCol()
844 << " to row " << ev.GetBottomRow()
d95b0c2b 845 << " col " << ev.GetRightCol()
f41986da
MB
846 << " ( ControlDown: "<< (ev.ControlDown() ? 'T':'F')
847 << ", ShiftDown: "<< (ev.ShiftDown() ? 'T':'F')
848 << ", AltDown: "<< (ev.AltDown() ? 'T':'F')
849 << ", MetaDown: "<< (ev.MetaDown() ? 'T':'F') << " )";
4693b20c 850 wxLogMessage( wxT("%s"), logBuf.c_str() );
f0102d2a 851
e442cc0d
MB
852 ev.Skip();
853}
854
855void GridFrame::OnCellValueChanged( wxGridEvent& ev )
856{
857 logBuf = "";
858 logBuf << "Value changed for cell at"
f0102d2a 859 << " row " << ev.GetRow()
e442cc0d 860 << " col " << ev.GetCol();
f0102d2a 861
4693b20c 862 wxLogMessage( wxT("%s"), logBuf.c_str() );
f0102d2a 863
e442cc0d
MB
864 ev.Skip();
865}
866
b54ba671
VZ
867void GridFrame::OnEditorShown( wxGridEvent& ev )
868{
fe7b9ed6
VZ
869
870 if ( (ev.GetCol() == 4) &&
871 (ev.GetRow() == 0) &&
2f6c54eb
VZ
872 (wxMessageBox(_T("Are you sure you wish to edit this cell"),
873 _T("Checking"),wxYES_NO) == wxNO ) ) {
fe7b9ed6 874
2f6c54eb
VZ
875 ev.Veto();
876 return;
fe7b9ed6 877 }
2f6c54eb 878
4693b20c 879 wxLogMessage( wxT("Cell editor shown.") );
b54ba671
VZ
880
881 ev.Skip();
882}
883
884void GridFrame::OnEditorHidden( wxGridEvent& ev )
885{
671bcff5 886
fe7b9ed6
VZ
887 if ( (ev.GetCol() == 4) &&
888 (ev.GetRow() == 0) &&
2f6c54eb
VZ
889 (wxMessageBox(_T("Are you sure you wish to finish editing this cell"),
890 _T("Checking"),wxYES_NO) == wxNO ) ) {
fe7b9ed6 891
2f6c54eb
VZ
892 ev.Veto();
893 return;
fe7b9ed6
VZ
894 }
895
4693b20c 896 wxLogMessage( wxT("Cell editor hidden.") );
b54ba671
VZ
897
898 ev.Skip();
899}
e442cc0d 900
f445b853
MB
901void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
902{
903 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
904 "Michael Bedward \n"
905 "mbedward@ozemail.com.au \n\n",
906 "About",
907 wxOK );
908}
909
910
911void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
912{
913 Close( TRUE );
914}
915
35f97e95
VZ
916void GridFrame::OnBugsTable(wxCommandEvent& )
917{
918 BugsGridFrame *frame = new BugsGridFrame;
919 frame->Show(TRUE);
920}
921
671bcff5
RD
922void GridFrame::OnSmallGrid(wxCommandEvent& )
923{
924 wxFrame* frame = new wxFrame(NULL, -1, "A Small Grid",
925 wxDefaultPosition, wxSize(640, 480));
926 wxPanel* panel = new wxPanel(frame, -1);
927 wxGrid* grid = new wxGrid(panel, -1, wxPoint(10,10), wxSize(400,400),
928 wxWANTS_CHARS | wxSIMPLE_BORDER);
929 grid->CreateGrid(3,3);
930 frame->Show(TRUE);
931}
19d7140e 932
f97c9b5b
RD
933void GridFrame::OnVTable(wxCommandEvent& )
934{
1618dca7
VZ
935 static long s_sizeGrid = 10000;
936
b0c8fc35
MB
937#ifdef __WXMOTIF__
938 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
939 wxString s;
940 s << s_sizeGrid;
941 s = wxGetTextFromUser( "Size of the table to create",
942 "Size:",
943 s );
c4608a8a 944
b0c8fc35 945 s.ToLong( &s_sizeGrid );
c4608a8a 946
b0c8fc35 947#else
1618dca7
VZ
948 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
949 "Size: ",
950 "wxGridDemo question",
951 s_sizeGrid,
952 0, 32000, this);
b0c8fc35 953#endif
c4608a8a 954
1618dca7
VZ
955 if ( s_sizeGrid != -1 )
956 {
957 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
958 win->Show(TRUE);
959 }
f97c9b5b
RD
960}
961
ab79958a
VZ
962// ----------------------------------------------------------------------------
963// MyGridCellRenderer
964// ----------------------------------------------------------------------------
965
758cbedf
VZ
966// do something that the default renderer doesn't here just to show that it is
967// possible to alter the appearance of the cell beyond what the attributes
968// allow
ab79958a 969void MyGridCellRenderer::Draw(wxGrid& grid,
f97c9b5b 970 wxGridCellAttr& attr,
ab79958a
VZ
971 wxDC& dc,
972 const wxRect& rect,
973 int row, int col,
974 bool isSelected)
975{
f97c9b5b 976 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
ab79958a
VZ
977
978 dc.SetPen(*wxGREEN_PEN);
979 dc.SetBrush(*wxTRANSPARENT_BRUSH);
980 dc.DrawEllipse(rect);
981}
f97c9b5b 982
f97c9b5b 983// ----------------------------------------------------------------------------
a68c1246 984// MyGridCellAttrProvider
f97c9b5b
RD
985// ----------------------------------------------------------------------------
986
a68c1246
VZ
987MyGridCellAttrProvider::MyGridCellAttrProvider()
988{
989 m_attrForOddRows = new wxGridCellAttr;
990 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
991}
992
993MyGridCellAttrProvider::~MyGridCellAttrProvider()
994{
995 m_attrForOddRows->DecRef();
996}
997
19d7140e
VZ
998wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col,
999 wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const
a68c1246 1000{
19d7140e 1001 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
a68c1246
VZ
1002
1003 if ( row % 2 )
1004 {
1005 if ( !attr )
1006 {
1007 attr = m_attrForOddRows;
1008 attr->IncRef();
1009 }
1010 else
1011 {
1012 if ( !attr->HasBackgroundColour() )
1013 {
1014 wxGridCellAttr *attrNew = attr->Clone();
1015 attr->DecRef();
1016 attr = attrNew;
1017 attr->SetBackgroundColour(*wxLIGHT_GREY);
1018 }
1019 }
1020 }
1021
1022 return attr;
1023}
1024
1025// ============================================================================
1026// BigGridFrame and BigGridTable: Sample of a non-standard table
1027// ============================================================================
1028
1618dca7
VZ
1029BigGridFrame::BigGridFrame(long sizeGrid)
1030 : wxFrame(NULL, -1, "Plugin Virtual Table",
1031 wxDefaultPosition, wxSize(500, 450))
f97c9b5b
RD
1032{
1033 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
1618dca7 1034 m_table = new BigGridTable(sizeGrid);
a68c1246
VZ
1035
1036 // VZ: I don't understand why this slows down the display that much,
1037 // must profile it...
1038 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
1039
f97c9b5b 1040 m_grid->SetTable(m_table, TRUE);
b0c8fc35
MB
1041
1042#if defined __WXMOTIF__
1043 // MB: the grid isn't getting a sensible default size under wxMotif
1044 int cw, ch;
1045 GetClientSize( &cw, &ch );
1046 m_grid->SetSize( cw, ch );
1047#endif
f97c9b5b 1048}
35f97e95 1049
a68c1246 1050// ============================================================================
35f97e95 1051// BugsGridFrame: a "realistic" table
a68c1246
VZ
1052// ============================================================================
1053
1054// ----------------------------------------------------------------------------
1055// bugs table data
35f97e95
VZ
1056// ----------------------------------------------------------------------------
1057
816be743
VZ
1058enum Columns
1059{
1060 Col_Id,
1061 Col_Summary,
1062 Col_Severity,
1063 Col_Priority,
1064 Col_Platform,
1065 Col_Opened,
1066 Col_Max
1067};
1068
1069enum Severity
1070{
1071 Sev_Wish,
1072 Sev_Minor,
1073 Sev_Normal,
1074 Sev_Major,
1075 Sev_Critical,
1076 Sev_Max
1077};
1078
f6bcfd97 1079static const wxString severities[] =
816be743
VZ
1080{
1081 _T("wishlist"),
1082 _T("minor"),
1083 _T("normal"),
1084 _T("major"),
1085 _T("critical"),
1086};
1087
1088static struct BugsGridData
1089{
1090 int id;
9121bed2 1091 wxChar summary[80];
816be743
VZ
1092 Severity severity;
1093 int prio;
9121bed2 1094 wxChar platform[12];
816be743 1095 bool opened;
c4608a8a 1096} gs_dataBugsGrid [] =
816be743
VZ
1097{
1098 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), TRUE },
1099 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), FALSE },
1100 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), TRUE },
1101 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), FALSE },
1102};
1103
1104static const wxChar *headers[Col_Max] =
1105{
1106 _T("Id"),
1107 _T("Summary"),
1108 _T("Severity"),
1109 _T("Priority"),
1110 _T("Platform"),
1111 _T("Opened?"),
1112};
1113
a68c1246
VZ
1114// ----------------------------------------------------------------------------
1115// BugsGridTable
1116// ----------------------------------------------------------------------------
1117
816be743
VZ
1118wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
1119{
1120 switch ( col )
1121 {
1122 case Col_Id:
1123 case Col_Priority:
1124 return wxGRID_VALUE_NUMBER;;
1125
1126 case Col_Severity:
1127 // fall thorugh (TODO should be a list)
1128
1129 case Col_Summary:
c4608a8a
VZ
1130 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
1131
816be743 1132 case Col_Platform:
c4608a8a 1133 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
816be743
VZ
1134
1135 case Col_Opened:
1136 return wxGRID_VALUE_BOOL;
1137 }
1138
1139 wxFAIL_MSG(_T("unknown column"));
1140
1141 return wxEmptyString;
1142}
1143
e32352cf 1144int BugsGridTable::GetNumberRows()
816be743
VZ
1145{
1146 return WXSIZEOF(gs_dataBugsGrid);
1147}
1148
e32352cf 1149int BugsGridTable::GetNumberCols()
35f97e95 1150{
816be743
VZ
1151 return Col_Max;
1152}
1153
1154bool BugsGridTable::IsEmptyCell( int row, int col )
1155{
1156 return FALSE;
1157}
1158
1159wxString BugsGridTable::GetValue( int row, int col )
1160{
1161 const BugsGridData& gd = gs_dataBugsGrid[row];
1162
1163 switch ( col )
35f97e95 1164 {
816be743
VZ
1165 case Col_Id:
1166 case Col_Priority:
1167 case Col_Opened:
1168 wxFAIL_MSG(_T("unexpected column"));
1169 break;
1170
1171 case Col_Severity:
1172 return severities[gd.severity];
1173
1174 case Col_Summary:
1175 return gd.summary;
1176
1177 case Col_Platform:
1178 return gd.platform;
1179 }
1180
1181 return wxEmptyString;
1182}
1183
1184void BugsGridTable::SetValue( int row, int col, const wxString& value )
1185{
1186 BugsGridData& gd = gs_dataBugsGrid[row];
35f97e95 1187
816be743 1188 switch ( col )
35f97e95 1189 {
816be743
VZ
1190 case Col_Id:
1191 case Col_Priority:
1192 case Col_Opened:
1193 wxFAIL_MSG(_T("unexpected column"));
1194 break;
1195
1196 case Col_Severity:
1197 {
1198 size_t n;
1199 for ( n = 0; n < WXSIZEOF(severities); n++ )
1200 {
1201 if ( severities[n] == value )
1202 {
1203 gd.severity = (Severity)n;
1204 break;
1205 }
1206 }
1207
1208 if ( n == WXSIZEOF(severities) )
1209 {
1210 wxLogWarning(_T("Invalid severity value '%s'."),
1211 value.c_str());
1212 gd.severity = Sev_Normal;
1213 }
1214 }
ed23853b 1215 break;
816be743
VZ
1216
1217 case Col_Summary:
9121bed2 1218 wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
816be743 1219 break;
35f97e95 1220
816be743 1221 case Col_Platform:
9121bed2 1222 wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
816be743
VZ
1223 break;
1224 }
1225}
1226
1227bool BugsGridTable::CanGetValueAs( int WXUNUSED(row), int col, const wxString& typeName )
1228{
1229 if ( typeName == wxGRID_VALUE_STRING )
35f97e95 1230 {
816be743
VZ
1231 return TRUE;
1232 }
1233 else if ( typeName == wxGRID_VALUE_BOOL )
1234 {
1235 return col == Col_Opened;
1236 }
1237 else if ( typeName == wxGRID_VALUE_NUMBER )
1238 {
1239 return col == Col_Id || col == Col_Priority || col == Col_Severity;
1240 }
1241 else
35f97e95 1242 {
816be743
VZ
1243 return FALSE;
1244 }
1245}
1246
1247bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
1248{
1249 return CanGetValueAs(row, col, typeName);
1250}
1251
1252long BugsGridTable::GetValueAsLong( int row, int col )
1253{
1254 const BugsGridData& gd = gs_dataBugsGrid[row];
35f97e95 1255
816be743 1256 switch ( col )
35f97e95 1257 {
816be743
VZ
1258 case Col_Id:
1259 return gd.id;
35f97e95 1260
816be743
VZ
1261 case Col_Priority:
1262 return gd.prio;
35f97e95 1263
816be743
VZ
1264 case Col_Severity:
1265 return gd.severity;
1266
1267 default:
1268 wxFAIL_MSG(_T("unexpected column"));
1269 return -1;
1270 }
1271}
1272
1273bool BugsGridTable::GetValueAsBool( int row, int col )
1274{
1275 if ( col == Col_Opened )
1276 {
1277 return gs_dataBugsGrid[row].opened;
1278 }
1279 else
35f97e95 1280 {
816be743
VZ
1281 wxFAIL_MSG(_T("unexpected column"));
1282
1283 return FALSE;
35f97e95 1284 }
816be743 1285}
35f97e95 1286
816be743
VZ
1287void BugsGridTable::SetValueAsLong( int row, int col, long value )
1288{
1289 BugsGridData& gd = gs_dataBugsGrid[row];
1290
1291 switch ( col )
35f97e95 1292 {
816be743
VZ
1293 case Col_Priority:
1294 gd.prio = value;
1295 break;
1296
1297 default:
1298 wxFAIL_MSG(_T("unexpected column"));
35f97e95 1299 }
816be743 1300}
35f97e95 1301
816be743
VZ
1302void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1303{
1304 if ( col == Col_Opened )
1305 {
1306 gs_dataBugsGrid[row].opened = value;
1307 }
1308 else
1309 {
1310 wxFAIL_MSG(_T("unexpected column"));
1311 }
1312}
1313
1314wxString BugsGridTable::GetColLabelValue( int col )
1315{
1316 return headers[col];
1317}
1318
1319BugsGridTable::BugsGridTable()
1320{
1321}
1322
a68c1246
VZ
1323// ----------------------------------------------------------------------------
1324// BugsGridFrame
1325// ----------------------------------------------------------------------------
1326
816be743
VZ
1327BugsGridFrame::BugsGridFrame()
1328 : wxFrame(NULL, -1, "Bugs table",
1329 wxDefaultPosition, wxSize(500, 300))
1330{
1331 wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
1332 wxGridTableBase *table = new BugsGridTable();
a68c1246 1333 table->SetAttrProvider(new MyGridCellAttrProvider);
35f97e95 1334 grid->SetTable(table, TRUE);
816be743 1335
65e4e78e 1336 wxGridCellAttr *attrRO = new wxGridCellAttr,
4ee5fc9c
VZ
1337 *attrRangeEditor = new wxGridCellAttr,
1338 *attrCombo = new wxGridCellAttr;
1339
65e4e78e
VZ
1340 attrRO->SetReadOnly();
1341 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
4ee5fc9c
VZ
1342 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1343 severities));
65e4e78e
VZ
1344
1345 grid->SetColAttr(Col_Id, attrRO);
1346 grid->SetColAttr(Col_Priority, attrRangeEditor);
4ee5fc9c 1347 grid->SetColAttr(Col_Severity, attrCombo);
65e4e78e 1348
266e8367 1349 grid->SetMargins(0, 0);
b0c8fc35 1350
266e8367 1351 grid->Fit();
d10f4bf9 1352 SetClientSize(grid->GetSize());
35f97e95 1353}
19d7140e
VZ
1354
1355