]> git.saurik.com Git - wxWidgets.git/blob - samples/newgrid/griddemo.cpp
1e3586dd3dc1477959a30917d1203af89cc16ad1
[wxWidgets.git] / samples / newgrid / griddemo.cpp
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 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
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
37 #include "wx/grid.h"
38
39 #include "griddemo.h"
40
41 // ----------------------------------------------------------------------------
42 // wxWin macros
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_APP( GridApp )
46
47 // ============================================================================
48 // implementation
49 // ============================================================================
50
51 // ----------------------------------------------------------------------------
52 // GridApp
53 // ----------------------------------------------------------------------------
54
55 bool GridApp::OnInit()
56 {
57 GridFrame *frame = new GridFrame;
58 frame->Show( TRUE );
59
60 return TRUE;
61 }
62
63 // ----------------------------------------------------------------------------
64 // GridFrame
65 // ----------------------------------------------------------------------------
66
67 BEGIN_EVENT_TABLE( GridFrame, wxFrame )
68 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
69 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
70 EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
71 EVT_MENU( ID_TOGGLEROWSIZING, GridFrame::ToggleRowSizing )
72 EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing )
73 EVT_MENU( ID_TOGGLEGRIDSIZING, GridFrame::ToggleGridSizing )
74 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
75 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
76 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
77 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
78 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
79 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
80 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
81 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
82 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
83 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
84 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
85 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
86
87 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
88 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
89
90 EVT_MENU( ID_ABOUT, GridFrame::About )
91 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
92 EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
93 EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable)
94
95 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
96 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
97 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
98 EVT_GRID_COL_SIZE( GridFrame::OnColSize )
99 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
100 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
101 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
102
103 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
104 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
105 END_EVENT_TABLE()
106
107
108 GridFrame::GridFrame()
109 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
110 wxDefaultPosition,
111 wxDefaultSize )
112 {
113 int gridW = 600, gridH = 300;
114 int logW = gridW, logH = 100;
115
116 wxMenu *fileMenu = new wxMenu;
117 fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V");
118 fileMenu->Append( ID_BUGS_TABLE, "&Bugs table test\tCtrl-B");
119 fileMenu->AppendSeparator();
120 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
121
122 wxMenu *viewMenu = new wxMenu;
123 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
124 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
125 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
126 viewMenu->Append( ID_TOGGLEROWSIZING, "Ro&w drag-resize", "", TRUE );
127 viewMenu->Append( ID_TOGGLECOLSIZING, "C&ol drag-resize", "", TRUE );
128 viewMenu->Append( ID_TOGGLEGRIDSIZING, "&Grid drag-resize", "", TRUE );
129
130 wxMenu *rowLabelMenu = new wxMenu;
131
132 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
133 rowLabelMenu,
134 "Change alignment of row labels" );
135
136 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
137 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
138
139 wxMenu *colLabelMenu = new wxMenu;
140
141 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
142 colLabelMenu,
143 "Change alignment of col labels" );
144
145 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
146 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
147
148 wxMenu *colMenu = new wxMenu;
149 colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
150 colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
151 colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
152 colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour" );
153 colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour" );
154
155 wxMenu *editMenu = new wxMenu;
156 editMenu->Append( ID_INSERTROW, "Insert &row" );
157 editMenu->Append( ID_INSERTCOL, "Insert &column" );
158 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
159 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
160 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
161
162 wxMenu *helpMenu = new wxMenu;
163 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
164
165 wxMenuBar *menuBar = new wxMenuBar;
166 menuBar->Append( fileMenu, "&File" );
167 menuBar->Append( viewMenu, "&View" );
168 menuBar->Append( colMenu, "&Colours" );
169 menuBar->Append( editMenu, "&Edit" );
170 menuBar->Append( helpMenu, "&Help" );
171
172 SetMenuBar( menuBar );
173
174 grid = new wxGrid( this,
175 -1,
176 wxPoint( 0, 0 ),
177 wxSize( 400, 300 ) );
178
179 logWin = new wxTextCtrl( this,
180 -1,
181 wxEmptyString,
182 wxPoint( 0, gridH + 20 ),
183 wxSize( logW, logH ),
184 wxTE_MULTILINE );
185
186 logger = new wxLogTextCtrl( logWin );
187 m_logOld = logger->SetActiveTarget( logger );
188 logger->SetTimestamp( NULL );
189
190 // this will create a grid and, by default, an associated grid
191 // table for strings
192 grid->CreateGrid( 100, 100 );
193
194 grid->SetRowSize( 0, 60 );
195 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
196
197 grid->SetCellValue( 0, 1, "Blah" );
198 grid->SetCellValue( 0, 2, "Blah" );
199 grid->SetCellValue( 0, 3, "Read only" );
200 grid->SetReadOnly( 0, 3 );
201
202 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
203
204 grid->SetRowSize( 99, 60 );
205 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
206
207 grid->SetCellValue(2, 2, "red");
208
209 grid->SetCellTextColour(2, 2, *wxRED);
210 grid->SetCellValue(3, 3, "green on grey");
211 grid->SetCellTextColour(3, 3, *wxGREEN);
212 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
213
214 grid->SetCellValue(4, 4, "a weird looking cell");
215 grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
216 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
217
218 grid->SetCellValue(3, 0, "1");
219 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
220 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
221
222 wxGridCellAttr *attr;
223 attr = new wxGridCellAttr;
224 attr->SetTextColour(*wxBLUE);
225 grid->SetColAttr(5, attr);
226 attr = new wxGridCellAttr;
227 attr->SetBackgroundColour(*wxBLUE);
228 grid->SetRowAttr(5, attr);
229
230 grid->SetCellValue(2, 4, "a wider column");
231 grid->SetColSize(4, 120);
232 grid->SetColMinimalWidth(4, 120);
233
234 grid->SetColFormatFloat(5);
235 grid->SetCellValue(0, 5, "3.1415");
236 grid->SetCellValue(1, 5, "1415");
237 grid->SetCellValue(2, 5, "12345.67890");
238
239 grid->SetColFormatFloat(6, 6, 2);
240 grid->SetCellValue(0, 6, "3.1415");
241 grid->SetCellValue(1, 6, "1415");
242 grid->SetCellValue(2, 6, "12345.67890");
243
244 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
245 topSizer->Add( grid,
246 1,
247 wxEXPAND );
248
249 topSizer->Add( logWin,
250 0,
251 wxEXPAND );
252
253 SetAutoLayout( TRUE );
254 SetSizer( topSizer );
255
256 topSizer->Fit( this );
257 topSizer->SetSizeHints( this );
258
259 Centre();
260 SetDefaults();
261 }
262
263
264 GridFrame::~GridFrame()
265 {
266 delete wxLog::SetActiveTarget(m_logOld);
267 }
268
269
270 void GridFrame::SetDefaults()
271 {
272 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
273 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
274 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
275 GetMenuBar()->Check( ID_TOGGLEROWSIZING, TRUE );
276 GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE );
277 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, TRUE );
278 }
279
280
281 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
282 {
283 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
284 {
285 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
286 }
287 else
288 {
289 grid->SetRowLabelSize( 0 );
290 }
291 }
292
293
294 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
295 {
296 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
297 {
298 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
299 }
300 else
301 {
302 grid->SetColLabelSize( 0 );
303 }
304 }
305
306
307 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
308 {
309 grid->EnableEditing(
310 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
311 }
312
313
314 void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
315 {
316 grid->EnableDragRowSize(
317 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
318 }
319
320
321 void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
322 {
323 grid->EnableDragColSize(
324 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
325 }
326
327 void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
328 {
329 grid->EnableDragGridSize(
330 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
331 }
332
333
334 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
335 {
336 wxColourDialog dlg( NULL );
337 if ( dlg.ShowModal() == wxID_OK )
338 {
339 wxColourData retData;
340 retData = dlg.GetColourData();
341 wxColour colour = retData.GetColour();
342
343 grid->SetLabelBackgroundColour( colour );
344 }
345 }
346
347
348 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
349 {
350 wxColourDialog dlg( NULL );
351 if ( dlg.ShowModal() == wxID_OK )
352 {
353 wxColourData retData;
354 retData = dlg.GetColourData();
355 wxColour colour = retData.GetColour();
356
357 grid->SetLabelTextColour( colour );
358 }
359 }
360
361
362 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
363 {
364 int horiz, vert;
365 grid->GetRowLabelAlignment( &horiz, &vert );
366
367 switch ( horiz )
368 {
369 case wxLEFT:
370 horiz = wxCENTRE;
371 break;
372
373 case wxCENTRE:
374 horiz = wxRIGHT;
375 break;
376
377 case wxRIGHT:
378 horiz = wxLEFT;
379 break;
380 }
381
382 grid->SetRowLabelAlignment( horiz, -1 );
383 }
384
385 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
386 {
387 int horiz, vert;
388 grid->GetRowLabelAlignment( &horiz, &vert );
389
390 switch ( vert )
391 {
392 case wxTOP:
393 vert = wxCENTRE;
394 break;
395
396 case wxCENTRE:
397 vert = wxBOTTOM;
398 break;
399
400 case wxBOTTOM:
401 vert = wxTOP;
402 break;
403 }
404
405 grid->SetRowLabelAlignment( -1, vert );
406 }
407
408
409 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
410 {
411 int horiz, vert;
412 grid->GetColLabelAlignment( &horiz, &vert );
413
414 switch ( horiz )
415 {
416 case wxLEFT:
417 horiz = wxCENTRE;
418 break;
419
420 case wxCENTRE:
421 horiz = wxRIGHT;
422 break;
423
424 case wxRIGHT:
425 horiz = wxLEFT;
426 break;
427 }
428
429 grid->SetColLabelAlignment( horiz, -1 );
430 }
431
432
433 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
434 {
435 int horiz, vert;
436 grid->GetColLabelAlignment( &horiz, &vert );
437
438 switch ( vert )
439 {
440 case wxTOP:
441 vert = wxCENTRE;
442 break;
443
444 case wxCENTRE:
445 vert = wxBOTTOM;
446 break;
447
448 case wxBOTTOM:
449 vert = wxTOP;
450 break;
451 }
452
453 grid->SetColLabelAlignment( -1, vert );
454 }
455
456
457 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
458 {
459 wxColourDialog dlg( NULL );
460 if ( dlg.ShowModal() == wxID_OK )
461 {
462 wxColourData retData;
463 retData = dlg.GetColourData();
464 wxColour colour = retData.GetColour();
465
466 grid->SetGridLineColour( colour );
467 }
468 }
469
470
471 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
472 {
473 grid->InsertRows( grid->GetGridCursorRow(), 1 );
474 }
475
476
477 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
478 {
479 grid->InsertCols( grid->GetGridCursorCol(), 1 );
480 }
481
482
483 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
484 {
485 #if 0
486 if ( grid->IsSelection() )
487 {
488 int topRow, bottomRow, leftCol, rightCol;
489 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
490 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
491 }
492 #endif
493 }
494
495
496 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
497 {
498 #if 0
499 if ( grid->IsSelection() )
500 {
501 int topRow, bottomRow, leftCol, rightCol;
502 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
503 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
504 }
505 #endif
506 }
507
508
509 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
510 {
511 grid->ClearGrid();
512 }
513
514 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
515 {
516 wxColour col = wxGetColourFromUser(this);
517 if ( col.Ok() )
518 {
519 grid->SetDefaultCellTextColour(col);
520 grid->Refresh();
521 }
522 }
523
524 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
525 {
526 wxColour col = wxGetColourFromUser(this);
527 if ( col.Ok() )
528 {
529 grid->SetDefaultCellBackgroundColour(col);
530 grid->Refresh();
531 }
532 }
533
534 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
535 {
536 logBuf = "";
537 if ( ev.GetRow() != -1 )
538 {
539 logBuf << "Left click on row label " << ev.GetRow();
540 }
541 else if ( ev.GetCol() != -1 )
542 {
543 logBuf << "Left click on col label " << ev.GetCol();
544 }
545 else
546 {
547 logBuf << "Left click on corner label";
548 }
549
550 if ( ev.ShiftDown() ) logBuf << " (shift down)";
551 wxLogMessage( "%s", logBuf.c_str() );
552
553 // you must call event skip if you want default grid processing
554 //
555 ev.Skip();
556 }
557
558
559 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
560 {
561 logBuf = "";
562 logBuf << "Left click at row " << ev.GetRow()
563 << " col " << ev.GetCol();
564 wxLogMessage( "%s", logBuf.c_str() );
565
566 // you must call event skip if you want default grid processing
567 // (cell highlighting etc.)
568 //
569 ev.Skip();
570 }
571
572
573 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
574 {
575 logBuf = "";
576 logBuf << "Resized row " << ev.GetRowOrCol();
577 wxLogMessage( "%s", logBuf.c_str() );
578
579 ev.Skip();
580 }
581
582
583 void GridFrame::OnColSize( wxGridSizeEvent& ev )
584 {
585 logBuf = "";
586 logBuf << "Resized col " << ev.GetRowOrCol();
587 wxLogMessage( "%s", logBuf.c_str() );
588
589 ev.Skip();
590 }
591
592
593 void GridFrame::OnSelectCell( wxGridEvent& ev )
594 {
595 logBuf = "";
596 logBuf << "Selected cell at row " << ev.GetRow()
597 << " col " << ev.GetCol();
598 wxLogMessage( "%s", logBuf.c_str() );
599
600 // you must call Skip() if you want the default processing
601 // to occur in wxGrid
602 ev.Skip();
603 }
604
605 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
606 {
607 logBuf = "";
608 logBuf << "Selected cells from row " << ev.GetTopRow()
609 << " col " << ev.GetLeftCol()
610 << " to row " << ev.GetBottomRow()
611 << " col " << ev.GetRightCol();
612
613 wxLogMessage( "%s", logBuf.c_str() );
614
615 ev.Skip();
616 }
617
618 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
619 {
620 logBuf = "";
621 logBuf << "Value changed for cell at"
622 << " row " << ev.GetRow()
623 << " col " << ev.GetCol();
624
625 wxLogMessage( "%s", logBuf.c_str() );
626
627 ev.Skip();
628 }
629
630 void GridFrame::OnEditorShown( wxGridEvent& ev )
631 {
632 wxLogMessage( "Cell editor shown." );
633
634 ev.Skip();
635 }
636
637 void GridFrame::OnEditorHidden( wxGridEvent& ev )
638 {
639 wxLogMessage( "Cell editor hidden." );
640
641 ev.Skip();
642 }
643
644 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
645 {
646 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
647 "Michael Bedward \n"
648 "mbedward@ozemail.com.au \n\n",
649 "About",
650 wxOK );
651 }
652
653
654 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
655 {
656 Close( TRUE );
657 }
658
659 void GridFrame::OnBugsTable(wxCommandEvent& )
660 {
661 BugsGridFrame *frame = new BugsGridFrame;
662 frame->Show(TRUE);
663 }
664
665 void GridFrame::OnVTable(wxCommandEvent& )
666 {
667 static long s_sizeGrid = 10000;
668
669 #ifdef __WXMOTIF__
670 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
671 wxString s;
672 s << s_sizeGrid;
673 s = wxGetTextFromUser( "Size of the table to create",
674 "Size:",
675 s );
676
677 s.ToLong( &s_sizeGrid );
678
679 #else
680 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
681 "Size: ",
682 "wxGridDemo question",
683 s_sizeGrid,
684 0, 32000, this);
685 #endif
686
687 if ( s_sizeGrid != -1 )
688 {
689 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
690 win->Show(TRUE);
691 }
692 }
693
694 // ----------------------------------------------------------------------------
695 // MyGridCellRenderer
696 // ----------------------------------------------------------------------------
697
698 // do something that the default renderer doesn't here just to show that it is
699 // possible to alter the appearance of the cell beyond what the attributes
700 // allow
701 void MyGridCellRenderer::Draw(wxGrid& grid,
702 wxGridCellAttr& attr,
703 wxDC& dc,
704 const wxRect& rect,
705 int row, int col,
706 bool isSelected)
707 {
708 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
709
710 dc.SetPen(*wxGREEN_PEN);
711 dc.SetBrush(*wxTRANSPARENT_BRUSH);
712 dc.DrawEllipse(rect);
713 }
714
715 // ----------------------------------------------------------------------------
716 // MyGridCellAttrProvider
717 // ----------------------------------------------------------------------------
718
719 MyGridCellAttrProvider::MyGridCellAttrProvider()
720 {
721 m_attrForOddRows = new wxGridCellAttr;
722 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
723 }
724
725 MyGridCellAttrProvider::~MyGridCellAttrProvider()
726 {
727 m_attrForOddRows->DecRef();
728 }
729
730 wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col) const
731 {
732 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col);
733
734 if ( row % 2 )
735 {
736 if ( !attr )
737 {
738 attr = m_attrForOddRows;
739 attr->IncRef();
740 }
741 else
742 {
743 if ( !attr->HasBackgroundColour() )
744 {
745 wxGridCellAttr *attrNew = attr->Clone();
746 attr->DecRef();
747 attr = attrNew;
748 attr->SetBackgroundColour(*wxLIGHT_GREY);
749 }
750 }
751 }
752
753 return attr;
754 }
755
756 // ============================================================================
757 // BigGridFrame and BigGridTable: Sample of a non-standard table
758 // ============================================================================
759
760 BigGridFrame::BigGridFrame(long sizeGrid)
761 : wxFrame(NULL, -1, "Plugin Virtual Table",
762 wxDefaultPosition, wxSize(500, 450))
763 {
764 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
765 m_table = new BigGridTable(sizeGrid);
766
767 // VZ: I don't understand why this slows down the display that much,
768 // must profile it...
769 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
770
771 m_grid->SetTable(m_table, TRUE);
772
773 #if defined __WXMOTIF__
774 // MB: the grid isn't getting a sensible default size under wxMotif
775 int cw, ch;
776 GetClientSize( &cw, &ch );
777 m_grid->SetSize( cw, ch );
778 #endif
779 }
780
781 // ============================================================================
782 // BugsGridFrame: a "realistic" table
783 // ============================================================================
784
785 // ----------------------------------------------------------------------------
786 // bugs table data
787 // ----------------------------------------------------------------------------
788
789 enum Columns
790 {
791 Col_Id,
792 Col_Summary,
793 Col_Severity,
794 Col_Priority,
795 Col_Platform,
796 Col_Opened,
797 Col_Max
798 };
799
800 enum Severity
801 {
802 Sev_Wish,
803 Sev_Minor,
804 Sev_Normal,
805 Sev_Major,
806 Sev_Critical,
807 Sev_Max
808 };
809
810 static const wxChar* severities[] =
811 {
812 _T("wishlist"),
813 _T("minor"),
814 _T("normal"),
815 _T("major"),
816 _T("critical"),
817 };
818
819 static struct BugsGridData
820 {
821 int id;
822 wxChar summary[80];
823 Severity severity;
824 int prio;
825 wxChar platform[12];
826 bool opened;
827 } gs_dataBugsGrid [] =
828 {
829 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), TRUE },
830 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), FALSE },
831 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), TRUE },
832 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), FALSE },
833 };
834
835 static const wxChar *headers[Col_Max] =
836 {
837 _T("Id"),
838 _T("Summary"),
839 _T("Severity"),
840 _T("Priority"),
841 _T("Platform"),
842 _T("Opened?"),
843 };
844
845 // ----------------------------------------------------------------------------
846 // BugsGridTable
847 // ----------------------------------------------------------------------------
848
849 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
850 {
851 switch ( col )
852 {
853 case Col_Id:
854 case Col_Priority:
855 return wxGRID_VALUE_NUMBER;;
856
857 case Col_Severity:
858 // fall thorugh (TODO should be a list)
859
860 case Col_Summary:
861 return wxString::Format(_T("%s:80"), wxGRID_VALUE_STRING);
862
863 case Col_Platform:
864 return wxString::Format(_T("%s:all,MSW,GTK,other"), wxGRID_VALUE_CHOICE);
865
866 case Col_Opened:
867 return wxGRID_VALUE_BOOL;
868 }
869
870 wxFAIL_MSG(_T("unknown column"));
871
872 return wxEmptyString;
873 }
874
875 long BugsGridTable::GetNumberRows()
876 {
877 return WXSIZEOF(gs_dataBugsGrid);
878 }
879
880 long BugsGridTable::GetNumberCols()
881 {
882 return Col_Max;
883 }
884
885 bool BugsGridTable::IsEmptyCell( int row, int col )
886 {
887 return FALSE;
888 }
889
890 wxString BugsGridTable::GetValue( int row, int col )
891 {
892 const BugsGridData& gd = gs_dataBugsGrid[row];
893
894 switch ( col )
895 {
896 case Col_Id:
897 case Col_Priority:
898 case Col_Opened:
899 wxFAIL_MSG(_T("unexpected column"));
900 break;
901
902 case Col_Severity:
903 return severities[gd.severity];
904
905 case Col_Summary:
906 return gd.summary;
907
908 case Col_Platform:
909 return gd.platform;
910 }
911
912 return wxEmptyString;
913 }
914
915 void BugsGridTable::SetValue( int row, int col, const wxString& value )
916 {
917 BugsGridData& gd = gs_dataBugsGrid[row];
918
919 switch ( col )
920 {
921 case Col_Id:
922 case Col_Priority:
923 case Col_Opened:
924 wxFAIL_MSG(_T("unexpected column"));
925 break;
926
927 case Col_Severity:
928 {
929 size_t n;
930 for ( n = 0; n < WXSIZEOF(severities); n++ )
931 {
932 if ( severities[n] == value )
933 {
934 gd.severity = (Severity)n;
935 break;
936 }
937 }
938
939 if ( n == WXSIZEOF(severities) )
940 {
941 wxLogWarning(_T("Invalid severity value '%s'."),
942 value.c_str());
943 gd.severity = Sev_Normal;
944 }
945 }
946 break;
947
948 case Col_Summary:
949 wxStrncpy(gd.summary, value, WXSIZEOF(gd.summary));
950 break;
951
952 case Col_Platform:
953 wxStrncpy(gd.platform, value, WXSIZEOF(gd.platform));
954 break;
955 }
956 }
957
958 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row), int col, const wxString& typeName )
959 {
960 if ( typeName == wxGRID_VALUE_STRING )
961 {
962 return TRUE;
963 }
964 else if ( typeName == wxGRID_VALUE_BOOL )
965 {
966 return col == Col_Opened;
967 }
968 else if ( typeName == wxGRID_VALUE_NUMBER )
969 {
970 return col == Col_Id || col == Col_Priority || col == Col_Severity;
971 }
972 else
973 {
974 return FALSE;
975 }
976 }
977
978 bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
979 {
980 return CanGetValueAs(row, col, typeName);
981 }
982
983 long BugsGridTable::GetValueAsLong( int row, int col )
984 {
985 const BugsGridData& gd = gs_dataBugsGrid[row];
986
987 switch ( col )
988 {
989 case Col_Id:
990 return gd.id;
991
992 case Col_Priority:
993 return gd.prio;
994
995 case Col_Severity:
996 return gd.severity;
997
998 default:
999 wxFAIL_MSG(_T("unexpected column"));
1000 return -1;
1001 }
1002 }
1003
1004 bool BugsGridTable::GetValueAsBool( int row, int col )
1005 {
1006 if ( col == Col_Opened )
1007 {
1008 return gs_dataBugsGrid[row].opened;
1009 }
1010 else
1011 {
1012 wxFAIL_MSG(_T("unexpected column"));
1013
1014 return FALSE;
1015 }
1016 }
1017
1018 void BugsGridTable::SetValueAsLong( int row, int col, long value )
1019 {
1020 BugsGridData& gd = gs_dataBugsGrid[row];
1021
1022 switch ( col )
1023 {
1024 case Col_Priority:
1025 gd.prio = value;
1026 break;
1027
1028 default:
1029 wxFAIL_MSG(_T("unexpected column"));
1030 }
1031 }
1032
1033 void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1034 {
1035 if ( col == Col_Opened )
1036 {
1037 gs_dataBugsGrid[row].opened = value;
1038 }
1039 else
1040 {
1041 wxFAIL_MSG(_T("unexpected column"));
1042 }
1043 }
1044
1045 wxString BugsGridTable::GetColLabelValue( int col )
1046 {
1047 return headers[col];
1048 }
1049
1050 BugsGridTable::BugsGridTable()
1051 {
1052 }
1053
1054 // ----------------------------------------------------------------------------
1055 // BugsGridFrame
1056 // ----------------------------------------------------------------------------
1057
1058 BugsGridFrame::BugsGridFrame()
1059 : wxFrame(NULL, -1, "Bugs table",
1060 wxDefaultPosition, wxSize(500, 300))
1061 {
1062 wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
1063 wxGridTableBase *table = new BugsGridTable();
1064 table->SetAttrProvider(new MyGridCellAttrProvider);
1065 grid->SetTable(table, TRUE);
1066
1067 wxGridCellAttr *attrRO = new wxGridCellAttr,
1068 *attrRangeEditor = new wxGridCellAttr,
1069 *attrCombo = new wxGridCellAttr;
1070
1071 attrRO->SetReadOnly();
1072 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
1073 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1074 severities));
1075
1076 grid->SetColAttr(Col_Id, attrRO);
1077 grid->SetColAttr(Col_Priority, attrRangeEditor);
1078 grid->SetColAttr(Col_Severity, attrCombo);
1079
1080 grid->SetMargins(0, 0);
1081
1082 grid->Fit();
1083 wxSize size = grid->GetSize();
1084 size.x += 10;
1085 size.y += 10;
1086 SetClientSize(size);
1087 }