]> git.saurik.com Git - wxWidgets.git/blame - samples/newgrid/griddemo.cpp
added an extremely simple cell attr cache (yet it catches 80% of acccesses)
[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 )
f0102d2a
VZ
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 )
e442cc0d 77 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
0eee5283
MB
78 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
79 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
f445b853
MB
80 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
81 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
e442cc0d 82 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
f0102d2a 83
ab79958a
VZ
84 EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour )
85 EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour )
86
e442cc0d
MB
87 EVT_MENU( ID_ABOUT, GridFrame::About )
88 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
89
5795d034
MB
90 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
91 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
92 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
93 EVT_GRID_COL_SIZE( GridFrame::OnColSize )
c336585e 94 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
5795d034
MB
95 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
96 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
f0102d2a 97
e442cc0d
MB
98END_EVENT_TABLE()
99
f0102d2a 100
e442cc0d
MB
101GridFrame::GridFrame()
102 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
103 wxDefaultPosition,
104 wxDefaultSize )
105{
106 int gridW = 600, gridH = 300;
107 int logW = gridW, logH = 80;
f0102d2a 108
e442cc0d 109 wxMenu *fileMenu = new wxMenu;
f0102d2a
VZ
110 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
111
e442cc0d
MB
112 wxMenu *viewMenu = new wxMenu;
113 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
114 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
f445b853 115 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
f0102d2a 116
e442cc0d
MB
117 wxMenu *rowLabelMenu = new wxMenu;
118
119 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
120 rowLabelMenu,
121 "Change alignment of row labels" );
122
123 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
f0102d2a
VZ
124 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
125
e442cc0d
MB
126 wxMenu *colLabelMenu = new wxMenu;
127
128 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
129 colLabelMenu,
130 "Change alignment of col labels" );
131
132 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
133 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
134
ab79958a
VZ
135 wxMenu *colMenu = new wxMenu;
136 colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
137 colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
138 colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
139 colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour" );
140 colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour" );
e442cc0d 141
0eee5283
MB
142 wxMenu *editMenu = new wxMenu;
143 editMenu->Append( ID_INSERTROW, "Insert &row" );
144 editMenu->Append( ID_INSERTCOL, "Insert &column" );
f445b853
MB
145 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
146 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
0eee5283 147 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
f0102d2a 148
e442cc0d
MB
149 wxMenu *helpMenu = new wxMenu;
150 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
f0102d2a 151
e442cc0d
MB
152 wxMenuBar *menuBar = new wxMenuBar;
153 menuBar->Append( fileMenu, "&File" );
154 menuBar->Append( viewMenu, "&View" );
ab79958a 155 menuBar->Append( colMenu, "&Colours" );
0eee5283 156 menuBar->Append( editMenu, "&Edit" );
e442cc0d
MB
157 menuBar->Append( helpMenu, "&Help" );
158
159 SetMenuBar( menuBar );
160
161 grid = new wxGrid( this,
162 -1,
163 wxPoint( 0, 0 ),
164 wxSize( 400, 300 ) );
f0102d2a 165
e442cc0d
MB
166 logWin = new wxTextCtrl( this,
167 -1,
168 wxEmptyString,
169 wxPoint( 0, gridH + 20 ),
170 wxSize( logW, logH ),
171 wxTE_MULTILINE );
f0102d2a 172
e442cc0d
MB
173 logger = new wxLogTextCtrl( logWin );
174 logger->SetActiveTarget( logger );
175 logger->SetTimestamp( NULL );
176
177 // this will create a grid and, by default, an associated grid
178 // table for string data
179 //
180 grid->CreateGrid( 100, 100 );
181
e442cc0d
MB
182 grid->SetRowSize( 0, 60 );
183 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
f0102d2a 184
e442cc0d
MB
185 grid->SetCellValue( 0, 1, "Blah" );
186 grid->SetCellValue( 0, 2, "Blah" );
187
188 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
189
190 grid->SetRowSize( 99, 60 );
191 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
f445b853 192
b99be8fb 193 grid->SetCellValue(2, 2, "red");
ab79958a 194
b99be8fb 195 grid->SetCellTextColour(2, 2, *wxRED);
2e9a6788 196 grid->SetCellValue(3, 3, "green on grey");
b99be8fb 197 grid->SetCellTextColour(3, 3, *wxGREEN);
2e9a6788 198 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
b99be8fb 199
ab79958a
VZ
200 grid->SetCellValue(4, 4, "a weird looking cell");
201 grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
202 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
203
e442cc0d
MB
204 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
205 topSizer->Add( grid,
206 1,
207 wxEXPAND );
f0102d2a 208
e442cc0d 209 topSizer->Add( logWin,
f0102d2a 210 0,
e442cc0d 211 wxEXPAND );
f0102d2a 212
e442cc0d
MB
213 SetAutoLayout( TRUE );
214 SetSizer( topSizer );
f0102d2a 215
e442cc0d
MB
216 topSizer->Fit( this );
217 topSizer->SetSizeHints( this );
f0102d2a 218
e442cc0d
MB
219 Centre();
220 SetDefaults();
221}
222
223
224GridFrame::~GridFrame()
225{
226}
227
f445b853 228
e442cc0d
MB
229void GridFrame::SetDefaults()
230{
231 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
232 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
f445b853 233 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
e442cc0d
MB
234}
235
236
237void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
238{
239 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
240 {
241 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
242 }
243 else
244 {
245 grid->SetRowLabelSize( 0 );
246 }
247}
248
249
250void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
251{
252 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
253 {
254 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
255 }
256 else
257 {
258 grid->SetColLabelSize( 0 );
259 }
260}
261
262
f445b853 263void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
e442cc0d 264{
f445b853
MB
265 grid->EnableEditing(
266 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
e442cc0d
MB
267}
268
269
270void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
271{
272 wxColourDialog dlg( NULL );
273 if ( dlg.ShowModal() == wxID_OK )
274 {
275 wxColourData retData;
276 retData = dlg.GetColourData();
277 wxColour colour = retData.GetColour();
278
279 grid->SetLabelBackgroundColour( colour );
280 }
281}
282
283
284void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
285{
286 wxColourDialog dlg( NULL );
287 if ( dlg.ShowModal() == wxID_OK )
288 {
289 wxColourData retData;
290 retData = dlg.GetColourData();
291 wxColour colour = retData.GetColour();
292
293 grid->SetLabelTextColour( colour );
294 }
295}
296
297
298void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
299{
300 int horiz, vert;
301 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 302
e442cc0d
MB
303 switch ( horiz )
304 {
305 case wxLEFT:
306 horiz = wxCENTRE;
307 break;
f0102d2a 308
e442cc0d
MB
309 case wxCENTRE:
310 horiz = wxRIGHT;
311 break;
312
313 case wxRIGHT:
314 horiz = wxLEFT;
315 break;
316 }
317
318 grid->SetRowLabelAlignment( horiz, -1 );
319}
320
321void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
322{
323 int horiz, vert;
324 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 325
e442cc0d
MB
326 switch ( vert )
327 {
328 case wxTOP:
329 vert = wxCENTRE;
330 break;
f0102d2a 331
e442cc0d
MB
332 case wxCENTRE:
333 vert = wxBOTTOM;
334 break;
335
336 case wxBOTTOM:
337 vert = wxTOP;
338 break;
339 }
340
341 grid->SetRowLabelAlignment( -1, vert );
342}
343
344
345void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
346{
347 int horiz, vert;
348 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 349
e442cc0d
MB
350 switch ( horiz )
351 {
352 case wxLEFT:
353 horiz = wxCENTRE;
354 break;
f0102d2a 355
e442cc0d
MB
356 case wxCENTRE:
357 horiz = wxRIGHT;
358 break;
359
360 case wxRIGHT:
361 horiz = wxLEFT;
362 break;
363 }
364
365 grid->SetColLabelAlignment( horiz, -1 );
366}
367
368
369void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
370{
371 int horiz, vert;
372 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 373
e442cc0d
MB
374 switch ( vert )
375 {
376 case wxTOP:
377 vert = wxCENTRE;
378 break;
f0102d2a 379
e442cc0d
MB
380 case wxCENTRE:
381 vert = wxBOTTOM;
382 break;
383
384 case wxBOTTOM:
385 vert = wxTOP;
386 break;
387 }
388
389 grid->SetColLabelAlignment( -1, vert );
390}
391
392
393void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
394{
395 wxColourDialog dlg( NULL );
396 if ( dlg.ShowModal() == wxID_OK )
397 {
398 wxColourData retData;
399 retData = dlg.GetColourData();
400 wxColour colour = retData.GetColour();
401
402 grid->SetGridLineColour( colour );
403 }
404}
405
406
0eee5283
MB
407void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
408{
409 grid->InsertRows( 0, 1 );
410}
411
412
413void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
414{
415 grid->InsertCols( 0, 1 );
416}
417
418
f445b853 419void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
0eee5283 420{
f445b853
MB
421 if ( grid->IsSelection() )
422 {
423 int topRow, bottomRow, leftCol, rightCol;
424 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
425 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
426 }
0eee5283
MB
427}
428
429
f445b853 430void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
0eee5283 431{
f445b853
MB
432 if ( grid->IsSelection() )
433 {
434 int topRow, bottomRow, leftCol, rightCol;
435 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
436 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
437 }
0eee5283
MB
438}
439
440
e442cc0d
MB
441void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
442{
443 grid->ClearGrid();
444}
445
ab79958a
VZ
446void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
447{
448 wxColour col = wxGetColourFromUser(this);
449 if ( col.Ok() )
450 grid->SetDefaultCellTextColour(col);
451}
452
453void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
454{
455 wxColour col = wxGetColourFromUser(this);
456 if ( col.Ok() )
457 grid->SetDefaultCellBackgroundColour(col);
458}
e442cc0d 459
e442cc0d
MB
460void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
461{
462 logBuf = "";
463 if ( ev.GetRow() != -1 )
464 {
c336585e 465 logBuf << "Left click on row label " << ev.GetRow();
e442cc0d
MB
466 }
467 else if ( ev.GetCol() != -1 )
468 {
c336585e 469 logBuf << "Left click on col label " << ev.GetCol();
e442cc0d
MB
470 }
471 else
472 {
c336585e 473 logBuf << "Left click on corner label";
e442cc0d
MB
474 }
475
476 if ( ev.ShiftDown() ) logBuf << " (shift down)";
477 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 478
c336585e
MB
479 // you must call event skip if you want default grid processing
480 //
e442cc0d
MB
481 ev.Skip();
482}
483
484
485void GridFrame::OnCellLeftClick( wxGridEvent& ev )
486{
487 logBuf = "";
c336585e 488 logBuf << "Left click at row " << ev.GetRow()
e442cc0d
MB
489 << " col " << ev.GetCol();
490 wxLogMessage( "%s", logBuf.c_str() );
0eee5283 491
e442cc0d
MB
492 // you must call event skip if you want default grid processing
493 // (cell highlighting etc.)
494 //
495 ev.Skip();
496}
497
498
499void GridFrame::OnRowSize( wxGridSizeEvent& ev )
500{
501 logBuf = "";
502 logBuf << "Resized row " << ev.GetRowOrCol();
503 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 504
e442cc0d
MB
505 ev.Skip();
506}
507
508
509void GridFrame::OnColSize( wxGridSizeEvent& ev )
510{
511 logBuf = "";
512 logBuf << "Resized col " << ev.GetRowOrCol();
513 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 514
e442cc0d
MB
515 ev.Skip();
516}
517
c336585e
MB
518
519void GridFrame::OnSelectCell( wxGridEvent& ev )
520{
521 logBuf = "";
522 logBuf << "Selected cell at row " << ev.GetRow()
523 << " col " << ev.GetCol();
524 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 525
c336585e
MB
526 // you must call Skip() if you want the default processing
527 // to occur in wxGrid
528 ev.Skip();
529}
530
e442cc0d
MB
531void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
532{
533 logBuf = "";
534 logBuf << "Selected cells from row " << ev.GetTopRow()
535 << " col " << ev.GetLeftCol()
536 << " to row " << ev.GetBottomRow()
537 << " col " << ev.GetRightCol();
f0102d2a 538
e442cc0d 539 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 540
e442cc0d
MB
541 ev.Skip();
542}
543
544void GridFrame::OnCellValueChanged( wxGridEvent& ev )
545{
546 logBuf = "";
547 logBuf << "Value changed for cell at"
f0102d2a 548 << " row " << ev.GetRow()
e442cc0d 549 << " col " << ev.GetCol();
f0102d2a 550
e442cc0d 551 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 552
e442cc0d
MB
553 ev.Skip();
554}
555
556
f445b853
MB
557void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
558{
559 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
560 "Michael Bedward \n"
561 "mbedward@ozemail.com.au \n\n",
562 "About",
563 wxOK );
564}
565
566
567void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
568{
569 Close( TRUE );
570}
571
ab79958a
VZ
572// ----------------------------------------------------------------------------
573// MyGridCellRenderer
574// ----------------------------------------------------------------------------
575
576void MyGridCellRenderer::Draw(wxGrid& grid,
577 wxDC& dc,
578 const wxRect& rect,
579 int row, int col,
580 bool isSelected)
581{
582 wxGridCellStringRenderer::Draw(grid, dc, rect, row, col, isSelected);
583
584 dc.SetPen(*wxGREEN_PEN);
585 dc.SetBrush(*wxTRANSPARENT_BRUSH);
586 dc.DrawEllipse(rect);
587}