]> git.saurik.com Git - wxWidgets.git/blob - samples/newgrid/griddemo.cpp
e961046b0b3239ef03acdeeb9e5cd5edc6667952
[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 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
235 topSizer->Add( grid,
236 1,
237 wxEXPAND );
238
239 topSizer->Add( logWin,
240 0,
241 wxEXPAND );
242
243 SetAutoLayout( TRUE );
244 SetSizer( topSizer );
245
246 topSizer->Fit( this );
247 topSizer->SetSizeHints( this );
248
249 Centre();
250 SetDefaults();
251 }
252
253
254 GridFrame::~GridFrame()
255 {
256 delete wxLog::SetActiveTarget(m_logOld);
257 }
258
259
260 void GridFrame::SetDefaults()
261 {
262 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
263 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
264 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
265 GetMenuBar()->Check( ID_TOGGLEROWSIZING, TRUE );
266 GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE );
267 GetMenuBar()->Check( ID_TOGGLEGRIDSIZING, TRUE );
268 }
269
270
271 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
272 {
273 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
274 {
275 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
276 }
277 else
278 {
279 grid->SetRowLabelSize( 0 );
280 }
281 }
282
283
284 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
285 {
286 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
287 {
288 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
289 }
290 else
291 {
292 grid->SetColLabelSize( 0 );
293 }
294 }
295
296
297 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
298 {
299 grid->EnableEditing(
300 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
301 }
302
303
304 void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) )
305 {
306 grid->EnableDragRowSize(
307 GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) );
308 }
309
310
311 void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) )
312 {
313 grid->EnableDragColSize(
314 GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) );
315 }
316
317 void GridFrame::ToggleGridSizing( wxCommandEvent& WXUNUSED(ev) )
318 {
319 grid->EnableDragGridSize(
320 GetMenuBar()->IsChecked( ID_TOGGLEGRIDSIZING ) );
321 }
322
323
324 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
325 {
326 wxColourDialog dlg( NULL );
327 if ( dlg.ShowModal() == wxID_OK )
328 {
329 wxColourData retData;
330 retData = dlg.GetColourData();
331 wxColour colour = retData.GetColour();
332
333 grid->SetLabelBackgroundColour( colour );
334 }
335 }
336
337
338 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
339 {
340 wxColourDialog dlg( NULL );
341 if ( dlg.ShowModal() == wxID_OK )
342 {
343 wxColourData retData;
344 retData = dlg.GetColourData();
345 wxColour colour = retData.GetColour();
346
347 grid->SetLabelTextColour( colour );
348 }
349 }
350
351
352 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
353 {
354 int horiz, vert;
355 grid->GetRowLabelAlignment( &horiz, &vert );
356
357 switch ( horiz )
358 {
359 case wxLEFT:
360 horiz = wxCENTRE;
361 break;
362
363 case wxCENTRE:
364 horiz = wxRIGHT;
365 break;
366
367 case wxRIGHT:
368 horiz = wxLEFT;
369 break;
370 }
371
372 grid->SetRowLabelAlignment( horiz, -1 );
373 }
374
375 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
376 {
377 int horiz, vert;
378 grid->GetRowLabelAlignment( &horiz, &vert );
379
380 switch ( vert )
381 {
382 case wxTOP:
383 vert = wxCENTRE;
384 break;
385
386 case wxCENTRE:
387 vert = wxBOTTOM;
388 break;
389
390 case wxBOTTOM:
391 vert = wxTOP;
392 break;
393 }
394
395 grid->SetRowLabelAlignment( -1, vert );
396 }
397
398
399 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
400 {
401 int horiz, vert;
402 grid->GetColLabelAlignment( &horiz, &vert );
403
404 switch ( horiz )
405 {
406 case wxLEFT:
407 horiz = wxCENTRE;
408 break;
409
410 case wxCENTRE:
411 horiz = wxRIGHT;
412 break;
413
414 case wxRIGHT:
415 horiz = wxLEFT;
416 break;
417 }
418
419 grid->SetColLabelAlignment( horiz, -1 );
420 }
421
422
423 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
424 {
425 int horiz, vert;
426 grid->GetColLabelAlignment( &horiz, &vert );
427
428 switch ( vert )
429 {
430 case wxTOP:
431 vert = wxCENTRE;
432 break;
433
434 case wxCENTRE:
435 vert = wxBOTTOM;
436 break;
437
438 case wxBOTTOM:
439 vert = wxTOP;
440 break;
441 }
442
443 grid->SetColLabelAlignment( -1, vert );
444 }
445
446
447 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
448 {
449 wxColourDialog dlg( NULL );
450 if ( dlg.ShowModal() == wxID_OK )
451 {
452 wxColourData retData;
453 retData = dlg.GetColourData();
454 wxColour colour = retData.GetColour();
455
456 grid->SetGridLineColour( colour );
457 }
458 }
459
460
461 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
462 {
463 grid->InsertRows( grid->GetGridCursorRow(), 1 );
464 }
465
466
467 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
468 {
469 grid->InsertCols( grid->GetGridCursorCol(), 1 );
470 }
471
472
473 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
474 {
475 if ( grid->IsSelection() )
476 {
477 int topRow, bottomRow, leftCol, rightCol;
478 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
479 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
480 }
481 }
482
483
484 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
485 {
486 if ( grid->IsSelection() )
487 {
488 int topRow, bottomRow, leftCol, rightCol;
489 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
490 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
491 }
492 }
493
494
495 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
496 {
497 grid->ClearGrid();
498 }
499
500 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
501 {
502 wxColour col = wxGetColourFromUser(this);
503 if ( col.Ok() )
504 {
505 grid->SetDefaultCellTextColour(col);
506 grid->Refresh();
507 }
508 }
509
510 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
511 {
512 wxColour col = wxGetColourFromUser(this);
513 if ( col.Ok() )
514 {
515 grid->SetDefaultCellBackgroundColour(col);
516 grid->Refresh();
517 }
518 }
519
520 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
521 {
522 logBuf = "";
523 if ( ev.GetRow() != -1 )
524 {
525 logBuf << "Left click on row label " << ev.GetRow();
526 }
527 else if ( ev.GetCol() != -1 )
528 {
529 logBuf << "Left click on col label " << ev.GetCol();
530 }
531 else
532 {
533 logBuf << "Left click on corner label";
534 }
535
536 if ( ev.ShiftDown() ) logBuf << " (shift down)";
537 wxLogMessage( "%s", logBuf.c_str() );
538
539 // you must call event skip if you want default grid processing
540 //
541 ev.Skip();
542 }
543
544
545 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
546 {
547 logBuf = "";
548 logBuf << "Left click at row " << ev.GetRow()
549 << " col " << ev.GetCol();
550 wxLogMessage( "%s", logBuf.c_str() );
551
552 // you must call event skip if you want default grid processing
553 // (cell highlighting etc.)
554 //
555 ev.Skip();
556 }
557
558
559 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
560 {
561 logBuf = "";
562 logBuf << "Resized row " << ev.GetRowOrCol();
563 wxLogMessage( "%s", logBuf.c_str() );
564
565 ev.Skip();
566 }
567
568
569 void GridFrame::OnColSize( wxGridSizeEvent& ev )
570 {
571 logBuf = "";
572 logBuf << "Resized col " << ev.GetRowOrCol();
573 wxLogMessage( "%s", logBuf.c_str() );
574
575 ev.Skip();
576 }
577
578
579 void GridFrame::OnSelectCell( wxGridEvent& ev )
580 {
581 logBuf = "";
582 logBuf << "Selected cell at row " << ev.GetRow()
583 << " col " << ev.GetCol();
584 wxLogMessage( "%s", logBuf.c_str() );
585
586 // you must call Skip() if you want the default processing
587 // to occur in wxGrid
588 ev.Skip();
589 }
590
591 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
592 {
593 logBuf = "";
594 logBuf << "Selected cells from row " << ev.GetTopRow()
595 << " col " << ev.GetLeftCol()
596 << " to row " << ev.GetBottomRow()
597 << " col " << ev.GetRightCol();
598
599 wxLogMessage( "%s", logBuf.c_str() );
600
601 ev.Skip();
602 }
603
604 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
605 {
606 logBuf = "";
607 logBuf << "Value changed for cell at"
608 << " row " << ev.GetRow()
609 << " col " << ev.GetCol();
610
611 wxLogMessage( "%s", logBuf.c_str() );
612
613 ev.Skip();
614 }
615
616 void GridFrame::OnEditorShown( wxGridEvent& ev )
617 {
618 wxLogMessage( "Cell editor shown." );
619
620 ev.Skip();
621 }
622
623 void GridFrame::OnEditorHidden( wxGridEvent& ev )
624 {
625 wxLogMessage( "Cell editor hidden." );
626
627 ev.Skip();
628 }
629
630 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
631 {
632 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
633 "Michael Bedward \n"
634 "mbedward@ozemail.com.au \n\n",
635 "About",
636 wxOK );
637 }
638
639
640 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
641 {
642 Close( TRUE );
643 }
644
645 void GridFrame::OnBugsTable(wxCommandEvent& )
646 {
647 BugsGridFrame *frame = new BugsGridFrame;
648 frame->Show(TRUE);
649 }
650
651 void GridFrame::OnVTable(wxCommandEvent& )
652 {
653 static long s_sizeGrid = 10000;
654
655 #ifdef __WXMOTIF__
656 // MB: wxGetNumberFromUser doesn't work properly for wxMotif
657 wxString s;
658 s << s_sizeGrid;
659 s = wxGetTextFromUser( "Size of the table to create",
660 "Size:",
661 s );
662
663 s.ToLong( &s_sizeGrid );
664
665 #else
666 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
667 "Size: ",
668 "wxGridDemo question",
669 s_sizeGrid,
670 0, 32000, this);
671 #endif
672
673 if ( s_sizeGrid != -1 )
674 {
675 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
676 win->Show(TRUE);
677 }
678 }
679
680 // ----------------------------------------------------------------------------
681 // MyGridCellRenderer
682 // ----------------------------------------------------------------------------
683
684 // do something that the default renderer doesn't here just to show that it is
685 // possible to alter the appearance of the cell beyond what the attributes
686 // allow
687 void MyGridCellRenderer::Draw(wxGrid& grid,
688 wxGridCellAttr& attr,
689 wxDC& dc,
690 const wxRect& rect,
691 int row, int col,
692 bool isSelected)
693 {
694 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
695
696 dc.SetPen(*wxGREEN_PEN);
697 dc.SetBrush(*wxTRANSPARENT_BRUSH);
698 dc.DrawEllipse(rect);
699 }
700
701 // ----------------------------------------------------------------------------
702 // MyGridCellAttrProvider
703 // ----------------------------------------------------------------------------
704
705 MyGridCellAttrProvider::MyGridCellAttrProvider()
706 {
707 m_attrForOddRows = new wxGridCellAttr;
708 m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
709 }
710
711 MyGridCellAttrProvider::~MyGridCellAttrProvider()
712 {
713 m_attrForOddRows->DecRef();
714 }
715
716 wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col) const
717 {
718 wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col);
719
720 if ( row % 2 )
721 {
722 if ( !attr )
723 {
724 attr = m_attrForOddRows;
725 attr->IncRef();
726 }
727 else
728 {
729 if ( !attr->HasBackgroundColour() )
730 {
731 wxGridCellAttr *attrNew = attr->Clone();
732 attr->DecRef();
733 attr = attrNew;
734 attr->SetBackgroundColour(*wxLIGHT_GREY);
735 }
736 }
737 }
738
739 return attr;
740 }
741
742 // ============================================================================
743 // BigGridFrame and BigGridTable: Sample of a non-standard table
744 // ============================================================================
745
746 BigGridFrame::BigGridFrame(long sizeGrid)
747 : wxFrame(NULL, -1, "Plugin Virtual Table",
748 wxDefaultPosition, wxSize(500, 450))
749 {
750 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
751 m_table = new BigGridTable(sizeGrid);
752
753 // VZ: I don't understand why this slows down the display that much,
754 // must profile it...
755 //m_table->SetAttrProvider(new MyGridCellAttrProvider);
756
757 m_grid->SetTable(m_table, TRUE);
758
759 #if defined __WXMOTIF__
760 // MB: the grid isn't getting a sensible default size under wxMotif
761 int cw, ch;
762 GetClientSize( &cw, &ch );
763 m_grid->SetSize( cw, ch );
764 #endif
765 }
766
767 // ============================================================================
768 // BugsGridFrame: a "realistic" table
769 // ============================================================================
770
771 // ----------------------------------------------------------------------------
772 // bugs table data
773 // ----------------------------------------------------------------------------
774
775 enum Columns
776 {
777 Col_Id,
778 Col_Summary,
779 Col_Severity,
780 Col_Priority,
781 Col_Platform,
782 Col_Opened,
783 Col_Max
784 };
785
786 enum Severity
787 {
788 Sev_Wish,
789 Sev_Minor,
790 Sev_Normal,
791 Sev_Major,
792 Sev_Critical,
793 Sev_Max
794 };
795
796 static const wxChar* severities[] =
797 {
798 _T("wishlist"),
799 _T("minor"),
800 _T("normal"),
801 _T("major"),
802 _T("critical"),
803 };
804
805 static struct BugsGridData
806 {
807 int id;
808
809 // Borland can't deal with global wxStrings and will generate a
810 // compile time error with the initializing the gs_dataGridBugs
811 // array (below)
812 //
813 #ifndef __BORLANDC__
814 wxString summary;
815 #else
816 wxChar *summary;
817 #endif
818
819 Severity severity;
820 int prio;
821
822 #ifndef __BORLANDC__
823 wxString platform;
824 #else
825 wxChar *platform;
826 #endif
827
828 bool opened;
829 } gs_dataBugsGrid [] =
830 {
831 { 18, _T("foo doesn't work"), Sev_Major, 1, _T("wxMSW"), TRUE },
832 { 27, _T("bar crashes"), Sev_Critical, 1, _T("all"), FALSE },
833 { 45, _T("printing is slow"), Sev_Minor, 3, _T("wxMSW"), TRUE },
834 { 68, _T("Rectangle() fails"), Sev_Normal, 1, _T("wxMSW"), FALSE },
835 };
836
837 static const wxChar *headers[Col_Max] =
838 {
839 _T("Id"),
840 _T("Summary"),
841 _T("Severity"),
842 _T("Priority"),
843 _T("Platform"),
844 _T("Opened?"),
845 };
846
847 // ----------------------------------------------------------------------------
848 // BugsGridTable
849 // ----------------------------------------------------------------------------
850
851 wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
852 {
853 switch ( col )
854 {
855 case Col_Id:
856 case Col_Priority:
857 return wxGRID_VALUE_NUMBER;;
858
859 case Col_Severity:
860 // fall thorugh (TODO should be a list)
861
862 case Col_Summary:
863 case Col_Platform:
864 return wxGRID_VALUE_STRING;
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 #ifndef __BORLANDC__
950 gd.summary = value;
951 #else
952 // this generates a warning message if you are using the
953 // memory tracing code but it should be ok :MB
954 //
955 delete gd.summary;
956 gd.summary = copystring( value.c_str() );
957 #endif
958 break;
959
960 case Col_Platform:
961 #ifndef __BORLANDC__
962 gd.platform = value;
963 #else
964 // this generates a warning message if you are using the
965 // memory tracing code but it should be ok :MB
966 //
967 delete gd.platform;
968 gd.platform = copystring( value.c_str() );
969 #endif
970 break;
971 }
972 }
973
974 bool BugsGridTable::CanGetValueAs( int WXUNUSED(row), int col, const wxString& typeName )
975 {
976 if ( typeName == wxGRID_VALUE_STRING )
977 {
978 return TRUE;
979 }
980 else if ( typeName == wxGRID_VALUE_BOOL )
981 {
982 return col == Col_Opened;
983 }
984 else if ( typeName == wxGRID_VALUE_NUMBER )
985 {
986 return col == Col_Id || col == Col_Priority || col == Col_Severity;
987 }
988 else
989 {
990 return FALSE;
991 }
992 }
993
994 bool BugsGridTable::CanSetValueAs( int row, int col, const wxString& typeName )
995 {
996 return CanGetValueAs(row, col, typeName);
997 }
998
999 long BugsGridTable::GetValueAsLong( int row, int col )
1000 {
1001 const BugsGridData& gd = gs_dataBugsGrid[row];
1002
1003 switch ( col )
1004 {
1005 case Col_Id:
1006 return gd.id;
1007
1008 case Col_Priority:
1009 return gd.prio;
1010
1011 case Col_Severity:
1012 return gd.severity;
1013
1014 default:
1015 wxFAIL_MSG(_T("unexpected column"));
1016 return -1;
1017 }
1018 }
1019
1020 bool BugsGridTable::GetValueAsBool( int row, int col )
1021 {
1022 if ( col == Col_Opened )
1023 {
1024 return gs_dataBugsGrid[row].opened;
1025 }
1026 else
1027 {
1028 wxFAIL_MSG(_T("unexpected column"));
1029
1030 return FALSE;
1031 }
1032 }
1033
1034 void BugsGridTable::SetValueAsLong( int row, int col, long value )
1035 {
1036 BugsGridData& gd = gs_dataBugsGrid[row];
1037
1038 switch ( col )
1039 {
1040 case Col_Priority:
1041 gd.prio = value;
1042 break;
1043
1044 default:
1045 wxFAIL_MSG(_T("unexpected column"));
1046 }
1047 }
1048
1049 void BugsGridTable::SetValueAsBool( int row, int col, bool value )
1050 {
1051 if ( col == Col_Opened )
1052 {
1053 gs_dataBugsGrid[row].opened = value;
1054 }
1055 else
1056 {
1057 wxFAIL_MSG(_T("unexpected column"));
1058 }
1059 }
1060
1061 wxString BugsGridTable::GetColLabelValue( int col )
1062 {
1063 return headers[col];
1064 }
1065
1066 BugsGridTable::BugsGridTable()
1067 {
1068 }
1069
1070 // ----------------------------------------------------------------------------
1071 // BugsGridFrame
1072 // ----------------------------------------------------------------------------
1073
1074 BugsGridFrame::BugsGridFrame()
1075 : wxFrame(NULL, -1, "Bugs table",
1076 wxDefaultPosition, wxSize(500, 300))
1077 {
1078 wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
1079 wxGridTableBase *table = new BugsGridTable();
1080 table->SetAttrProvider(new MyGridCellAttrProvider);
1081 grid->SetTable(table, TRUE);
1082
1083 wxGridCellAttr *attrRO = new wxGridCellAttr,
1084 *attrRangeEditor = new wxGridCellAttr,
1085 *attrCombo = new wxGridCellAttr;
1086
1087 attrRO->SetReadOnly();
1088 attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5));
1089 attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities),
1090 severities));
1091
1092 grid->SetColAttr(Col_Id, attrRO);
1093 grid->SetColAttr(Col_Priority, attrRangeEditor);
1094 grid->SetColAttr(Col_Severity, attrCombo);
1095
1096 grid->SetMargins(0, 0);
1097
1098 grid->Fit();
1099 Fit();
1100 }