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