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