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