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