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