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