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