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