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