]> git.saurik.com Git - wxWidgets.git/blob - samples/newgrid/griddemo.cpp
212e068c3d1ecf3aba9f408f85770827925f1f84
[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
39 #include "griddemo.h"
40
41 // ----------------------------------------------------------------------------
42 // wxWin macros
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_APP( GridApp )
46
47 // ============================================================================
48 // implementation
49 // ============================================================================
50
51 // ----------------------------------------------------------------------------
52 // GridApp
53 // ----------------------------------------------------------------------------
54
55 bool GridApp::OnInit()
56 {
57 GridFrame *frame = new GridFrame;
58 frame->Show( TRUE );
59
60 return TRUE;
61 }
62
63 // ----------------------------------------------------------------------------
64 // GridFrame
65 // ----------------------------------------------------------------------------
66
67 BEGIN_EVENT_TABLE( GridFrame, wxFrame )
68 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
69 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
70 EVT_MENU( ID_TOGGLEEDIT, GridFrame::ToggleEditing )
71 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
72 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
73 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
74 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
75 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
76 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
77 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
78 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
79 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
80 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
81 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
82 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
83
84 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
85 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
86
87 EVT_MENU( ID_ABOUT, GridFrame::About )
88 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
89 EVT_MENU( ID_VTABLE, GridFrame::OnVTable)
90
91 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
92 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
93 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
94 EVT_GRID_COL_SIZE( GridFrame::OnColSize )
95 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
96 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
97 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
98
99 EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown )
100 EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden )
101 END_EVENT_TABLE()
102
103
104 GridFrame::GridFrame()
105 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
106 wxDefaultPosition,
107 wxDefaultSize )
108 {
109 int gridW = 600, gridH = 300;
110 int logW = gridW, logH = 100;
111
112 wxMenu *fileMenu = new wxMenu;
113 fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V");
114 fileMenu->AppendSeparator();
115 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
116
117 wxMenu *viewMenu = new wxMenu;
118 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
119 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
120 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
121
122 wxMenu *rowLabelMenu = new wxMenu;
123
124 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
125 rowLabelMenu,
126 "Change alignment of row labels" );
127
128 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
129 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
130
131 wxMenu *colLabelMenu = new wxMenu;
132
133 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
134 colLabelMenu,
135 "Change alignment of col labels" );
136
137 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
138 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
139
140 wxMenu *colMenu = new wxMenu;
141 colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
142 colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
143 colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
144 colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour" );
145 colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour" );
146
147 wxMenu *editMenu = new wxMenu;
148 editMenu->Append( ID_INSERTROW, "Insert &row" );
149 editMenu->Append( ID_INSERTCOL, "Insert &column" );
150 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
151 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
152 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
153
154 wxMenu *helpMenu = new wxMenu;
155 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
156
157 wxMenuBar *menuBar = new wxMenuBar;
158 menuBar->Append( fileMenu, "&File" );
159 menuBar->Append( viewMenu, "&View" );
160 menuBar->Append( colMenu, "&Colours" );
161 menuBar->Append( editMenu, "&Edit" );
162 menuBar->Append( helpMenu, "&Help" );
163
164 SetMenuBar( menuBar );
165
166 grid = new wxGrid( this,
167 -1,
168 wxPoint( 0, 0 ),
169 wxSize( 400, 300 ) );
170
171 logWin = new wxTextCtrl( this,
172 -1,
173 wxEmptyString,
174 wxPoint( 0, gridH + 20 ),
175 wxSize( logW, logH ),
176 wxTE_MULTILINE );
177
178 logger = new wxLogTextCtrl( logWin );
179 logger->SetActiveTarget( logger );
180 logger->SetTimestamp( NULL );
181
182 // this will create a grid and, by default, an associated grid
183 // table for string data
184 //
185 grid->CreateGrid( 100, 100 );
186
187 grid->SetRowSize( 0, 60 );
188 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
189
190 grid->SetCellValue( 0, 1, "Blah" );
191 grid->SetCellValue( 0, 2, "Blah" );
192 grid->SetCellValue( 0, 3, "Read only" );
193 grid->SetReadOnly( 0, 3 );
194
195 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
196
197 grid->SetRowSize( 99, 60 );
198 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
199
200 grid->SetCellValue(2, 2, "red");
201
202 grid->SetCellTextColour(2, 2, *wxRED);
203 grid->SetCellValue(3, 3, "green on grey");
204 grid->SetCellTextColour(3, 3, *wxGREEN);
205 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
206
207 grid->SetCellValue(4, 4, "a weird looking cell");
208 grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
209 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
210
211 grid->SetCellValue(3, 0, "1");
212 grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer);
213 grid->SetCellEditor(3, 0, new wxGridCellBoolEditor);
214
215 wxGridCellAttr *attr;
216 attr = new wxGridCellAttr;
217 attr->SetTextColour(*wxBLUE);
218 grid->SetColAttr(5, attr);
219 attr = new wxGridCellAttr;
220 attr->SetBackgroundColour(*wxBLUE);
221 grid->SetRowAttr(5, attr);
222
223 grid->SetCellValue(2, 4, "a wider column");
224 grid->SetColSize(4, 120);
225 grid->SetColMinimalWidth(4, 120);
226
227 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
228 topSizer->Add( grid,
229 1,
230 wxEXPAND );
231
232 topSizer->Add( logWin,
233 0,
234 wxEXPAND );
235
236 SetAutoLayout( TRUE );
237 SetSizer( topSizer );
238
239 topSizer->Fit( this );
240 topSizer->SetSizeHints( this );
241
242 Centre();
243 SetDefaults();
244 }
245
246
247 GridFrame::~GridFrame()
248 {
249 }
250
251
252 void GridFrame::SetDefaults()
253 {
254 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
255 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
256 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
257 }
258
259
260 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
261 {
262 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
263 {
264 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
265 }
266 else
267 {
268 grid->SetRowLabelSize( 0 );
269 }
270 }
271
272
273 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
274 {
275 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
276 {
277 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
278 }
279 else
280 {
281 grid->SetColLabelSize( 0 );
282 }
283 }
284
285
286 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
287 {
288 grid->EnableEditing(
289 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
290 }
291
292
293 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
294 {
295 wxColourDialog dlg( NULL );
296 if ( dlg.ShowModal() == wxID_OK )
297 {
298 wxColourData retData;
299 retData = dlg.GetColourData();
300 wxColour colour = retData.GetColour();
301
302 grid->SetLabelBackgroundColour( colour );
303 }
304 }
305
306
307 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
308 {
309 wxColourDialog dlg( NULL );
310 if ( dlg.ShowModal() == wxID_OK )
311 {
312 wxColourData retData;
313 retData = dlg.GetColourData();
314 wxColour colour = retData.GetColour();
315
316 grid->SetLabelTextColour( colour );
317 }
318 }
319
320
321 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
322 {
323 int horiz, vert;
324 grid->GetRowLabelAlignment( &horiz, &vert );
325
326 switch ( horiz )
327 {
328 case wxLEFT:
329 horiz = wxCENTRE;
330 break;
331
332 case wxCENTRE:
333 horiz = wxRIGHT;
334 break;
335
336 case wxRIGHT:
337 horiz = wxLEFT;
338 break;
339 }
340
341 grid->SetRowLabelAlignment( horiz, -1 );
342 }
343
344 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
345 {
346 int horiz, vert;
347 grid->GetRowLabelAlignment( &horiz, &vert );
348
349 switch ( vert )
350 {
351 case wxTOP:
352 vert = wxCENTRE;
353 break;
354
355 case wxCENTRE:
356 vert = wxBOTTOM;
357 break;
358
359 case wxBOTTOM:
360 vert = wxTOP;
361 break;
362 }
363
364 grid->SetRowLabelAlignment( -1, vert );
365 }
366
367
368 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
369 {
370 int horiz, vert;
371 grid->GetColLabelAlignment( &horiz, &vert );
372
373 switch ( horiz )
374 {
375 case wxLEFT:
376 horiz = wxCENTRE;
377 break;
378
379 case wxCENTRE:
380 horiz = wxRIGHT;
381 break;
382
383 case wxRIGHT:
384 horiz = wxLEFT;
385 break;
386 }
387
388 grid->SetColLabelAlignment( horiz, -1 );
389 }
390
391
392 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
393 {
394 int horiz, vert;
395 grid->GetColLabelAlignment( &horiz, &vert );
396
397 switch ( vert )
398 {
399 case wxTOP:
400 vert = wxCENTRE;
401 break;
402
403 case wxCENTRE:
404 vert = wxBOTTOM;
405 break;
406
407 case wxBOTTOM:
408 vert = wxTOP;
409 break;
410 }
411
412 grid->SetColLabelAlignment( -1, vert );
413 }
414
415
416 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
417 {
418 wxColourDialog dlg( NULL );
419 if ( dlg.ShowModal() == wxID_OK )
420 {
421 wxColourData retData;
422 retData = dlg.GetColourData();
423 wxColour colour = retData.GetColour();
424
425 grid->SetGridLineColour( colour );
426 }
427 }
428
429
430 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
431 {
432 grid->InsertRows( grid->GetGridCursorRow(), 1 );
433 }
434
435
436 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
437 {
438 grid->InsertCols( grid->GetGridCursorCol(), 1 );
439 }
440
441
442 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
443 {
444 if ( grid->IsSelection() )
445 {
446 int topRow, bottomRow, leftCol, rightCol;
447 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
448 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
449 }
450 }
451
452
453 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
454 {
455 if ( grid->IsSelection() )
456 {
457 int topRow, bottomRow, leftCol, rightCol;
458 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
459 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
460 }
461 }
462
463
464 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
465 {
466 grid->ClearGrid();
467 }
468
469 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
470 {
471 wxColour col = wxGetColourFromUser(this);
472 if ( col.Ok() )
473 {
474 grid->SetDefaultCellTextColour(col);
475 grid->Refresh();
476 }
477 }
478
479 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
480 {
481 wxColour col = wxGetColourFromUser(this);
482 if ( col.Ok() )
483 {
484 grid->SetDefaultCellBackgroundColour(col);
485 grid->Refresh();
486 }
487 }
488
489 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
490 {
491 logBuf = "";
492 if ( ev.GetRow() != -1 )
493 {
494 logBuf << "Left click on row label " << ev.GetRow();
495 }
496 else if ( ev.GetCol() != -1 )
497 {
498 logBuf << "Left click on col label " << ev.GetCol();
499 }
500 else
501 {
502 logBuf << "Left click on corner label";
503 }
504
505 if ( ev.ShiftDown() ) logBuf << " (shift down)";
506 wxLogMessage( "%s", logBuf.c_str() );
507
508 // you must call event skip if you want default grid processing
509 //
510 ev.Skip();
511 }
512
513
514 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
515 {
516 logBuf = "";
517 logBuf << "Left click at row " << ev.GetRow()
518 << " col " << ev.GetCol();
519 wxLogMessage( "%s", logBuf.c_str() );
520
521 // you must call event skip if you want default grid processing
522 // (cell highlighting etc.)
523 //
524 ev.Skip();
525 }
526
527
528 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
529 {
530 logBuf = "";
531 logBuf << "Resized row " << ev.GetRowOrCol();
532 wxLogMessage( "%s", logBuf.c_str() );
533
534 ev.Skip();
535 }
536
537
538 void GridFrame::OnColSize( wxGridSizeEvent& ev )
539 {
540 logBuf = "";
541 logBuf << "Resized col " << ev.GetRowOrCol();
542 wxLogMessage( "%s", logBuf.c_str() );
543
544 ev.Skip();
545 }
546
547
548 void GridFrame::OnSelectCell( wxGridEvent& ev )
549 {
550 logBuf = "";
551 logBuf << "Selected cell at row " << ev.GetRow()
552 << " col " << ev.GetCol();
553 wxLogMessage( "%s", logBuf.c_str() );
554
555 // you must call Skip() if you want the default processing
556 // to occur in wxGrid
557 ev.Skip();
558 }
559
560 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
561 {
562 logBuf = "";
563 logBuf << "Selected cells from row " << ev.GetTopRow()
564 << " col " << ev.GetLeftCol()
565 << " to row " << ev.GetBottomRow()
566 << " col " << ev.GetRightCol();
567
568 wxLogMessage( "%s", logBuf.c_str() );
569
570 ev.Skip();
571 }
572
573 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
574 {
575 logBuf = "";
576 logBuf << "Value changed for cell at"
577 << " row " << ev.GetRow()
578 << " col " << ev.GetCol();
579
580 wxLogMessage( "%s", logBuf.c_str() );
581
582 ev.Skip();
583 }
584
585 void GridFrame::OnEditorShown( wxGridEvent& ev )
586 {
587 wxLogMessage( "Cell editor shown." );
588
589 ev.Skip();
590 }
591
592 void GridFrame::OnEditorHidden( wxGridEvent& ev )
593 {
594 wxLogMessage( "Cell editor hidden." );
595
596 ev.Skip();
597 }
598
599 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
600 {
601 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
602 "Michael Bedward \n"
603 "mbedward@ozemail.com.au \n\n",
604 "About",
605 wxOK );
606 }
607
608
609 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
610 {
611 Close( TRUE );
612 }
613
614 void GridFrame::OnVTable(wxCommandEvent& )
615 {
616 static long s_sizeGrid = 10000;
617
618 s_sizeGrid = wxGetNumberFromUser("Size of the table to create",
619 "Size: ",
620 "wxGridDemo question",
621 s_sizeGrid,
622 0, 32000, this);
623 if ( s_sizeGrid != -1 )
624 {
625 BigGridFrame* win = new BigGridFrame(s_sizeGrid);
626 win->Show(TRUE);
627 }
628 }
629
630 // ----------------------------------------------------------------------------
631 // MyGridCellRenderer
632 // ----------------------------------------------------------------------------
633
634 // do something that the default renderer doesn't here just to show that it is
635 // possible to alter the appearance of the cell beyond what the attributes
636 // allow
637 void MyGridCellRenderer::Draw(wxGrid& grid,
638 wxGridCellAttr& attr,
639 wxDC& dc,
640 const wxRect& rect,
641 int row, int col,
642 bool isSelected)
643 {
644 wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
645
646 dc.SetPen(*wxGREEN_PEN);
647 dc.SetBrush(*wxTRANSPARENT_BRUSH);
648 dc.DrawEllipse(rect);
649 }
650
651
652 // ----------------------------------------------------------------------------
653 // BigGridFrame and BigGridTable: Sample of a non-standard table
654 // ----------------------------------------------------------------------------
655
656 BigGridFrame::BigGridFrame(long sizeGrid)
657 : wxFrame(NULL, -1, "Plugin Virtual Table",
658 wxDefaultPosition, wxSize(500, 450))
659 {
660 m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
661 m_table = new BigGridTable(sizeGrid);
662 m_grid->SetTable(m_table, TRUE);
663 }