]> git.saurik.com Git - wxWidgets.git/blame - samples/newgrid/griddemo.cpp
1. Grid cell defaults are now handled by an internal wxGridCellAttr
[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
758cbedf
VZ
204 wxGridCellAttr *attr;
205 attr = new wxGridCellAttr;
206 attr->SetTextColour(*wxBLUE);
207 grid->SetColAttr(5, attr);
208 attr = new wxGridCellAttr;
209 attr->SetBackgroundColour(*wxBLUE);
210 grid->SetRowAttr(5, attr);
211
e442cc0d
MB
212 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
213 topSizer->Add( grid,
214 1,
215 wxEXPAND );
f0102d2a 216
e442cc0d 217 topSizer->Add( logWin,
f0102d2a 218 0,
e442cc0d 219 wxEXPAND );
f0102d2a 220
e442cc0d
MB
221 SetAutoLayout( TRUE );
222 SetSizer( topSizer );
f0102d2a 223
e442cc0d
MB
224 topSizer->Fit( this );
225 topSizer->SetSizeHints( this );
f0102d2a 226
e442cc0d
MB
227 Centre();
228 SetDefaults();
229}
230
231
232GridFrame::~GridFrame()
233{
234}
235
f445b853 236
e442cc0d
MB
237void GridFrame::SetDefaults()
238{
239 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
240 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
f445b853 241 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
e442cc0d
MB
242}
243
244
245void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
246{
247 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
248 {
249 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
250 }
251 else
252 {
253 grid->SetRowLabelSize( 0 );
254 }
255}
256
257
258void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
259{
260 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
261 {
262 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
263 }
264 else
265 {
266 grid->SetColLabelSize( 0 );
267 }
268}
269
270
f445b853 271void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
e442cc0d 272{
f445b853
MB
273 grid->EnableEditing(
274 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
e442cc0d
MB
275}
276
277
278void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
279{
280 wxColourDialog dlg( NULL );
281 if ( dlg.ShowModal() == wxID_OK )
282 {
283 wxColourData retData;
284 retData = dlg.GetColourData();
285 wxColour colour = retData.GetColour();
286
287 grid->SetLabelBackgroundColour( colour );
288 }
289}
290
291
292void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
293{
294 wxColourDialog dlg( NULL );
295 if ( dlg.ShowModal() == wxID_OK )
296 {
297 wxColourData retData;
298 retData = dlg.GetColourData();
299 wxColour colour = retData.GetColour();
300
301 grid->SetLabelTextColour( colour );
302 }
303}
304
305
306void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
307{
308 int horiz, vert;
309 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 310
e442cc0d
MB
311 switch ( horiz )
312 {
313 case wxLEFT:
314 horiz = wxCENTRE;
315 break;
f0102d2a 316
e442cc0d
MB
317 case wxCENTRE:
318 horiz = wxRIGHT;
319 break;
320
321 case wxRIGHT:
322 horiz = wxLEFT;
323 break;
324 }
325
326 grid->SetRowLabelAlignment( horiz, -1 );
327}
328
329void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
330{
331 int horiz, vert;
332 grid->GetRowLabelAlignment( &horiz, &vert );
f0102d2a 333
e442cc0d
MB
334 switch ( vert )
335 {
336 case wxTOP:
337 vert = wxCENTRE;
338 break;
f0102d2a 339
e442cc0d
MB
340 case wxCENTRE:
341 vert = wxBOTTOM;
342 break;
343
344 case wxBOTTOM:
345 vert = wxTOP;
346 break;
347 }
348
349 grid->SetRowLabelAlignment( -1, vert );
350}
351
352
353void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
354{
355 int horiz, vert;
356 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 357
e442cc0d
MB
358 switch ( horiz )
359 {
360 case wxLEFT:
361 horiz = wxCENTRE;
362 break;
f0102d2a 363
e442cc0d
MB
364 case wxCENTRE:
365 horiz = wxRIGHT;
366 break;
367
368 case wxRIGHT:
369 horiz = wxLEFT;
370 break;
371 }
372
373 grid->SetColLabelAlignment( horiz, -1 );
374}
375
376
377void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
378{
379 int horiz, vert;
380 grid->GetColLabelAlignment( &horiz, &vert );
f0102d2a 381
e442cc0d
MB
382 switch ( vert )
383 {
384 case wxTOP:
385 vert = wxCENTRE;
386 break;
f0102d2a 387
e442cc0d
MB
388 case wxCENTRE:
389 vert = wxBOTTOM;
390 break;
391
392 case wxBOTTOM:
393 vert = wxTOP;
394 break;
395 }
396
397 grid->SetColLabelAlignment( -1, vert );
398}
399
400
401void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
402{
403 wxColourDialog dlg( NULL );
404 if ( dlg.ShowModal() == wxID_OK )
405 {
406 wxColourData retData;
407 retData = dlg.GetColourData();
408 wxColour colour = retData.GetColour();
409
410 grid->SetGridLineColour( colour );
411 }
412}
413
414
0eee5283
MB
415void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
416{
417 grid->InsertRows( 0, 1 );
418}
419
420
421void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
422{
423 grid->InsertCols( 0, 1 );
424}
425
426
f445b853 427void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
0eee5283 428{
f445b853
MB
429 if ( grid->IsSelection() )
430 {
431 int topRow, bottomRow, leftCol, rightCol;
432 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
433 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
434 }
0eee5283
MB
435}
436
437
f445b853 438void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
0eee5283 439{
f445b853
MB
440 if ( grid->IsSelection() )
441 {
442 int topRow, bottomRow, leftCol, rightCol;
443 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
444 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
445 }
0eee5283
MB
446}
447
448
e442cc0d
MB
449void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
450{
451 grid->ClearGrid();
452}
453
ab79958a
VZ
454void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
455{
456 wxColour col = wxGetColourFromUser(this);
457 if ( col.Ok() )
758cbedf 458 {
ab79958a 459 grid->SetDefaultCellTextColour(col);
758cbedf
VZ
460 grid->Refresh();
461 }
ab79958a
VZ
462}
463
464void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
465{
466 wxColour col = wxGetColourFromUser(this);
467 if ( col.Ok() )
758cbedf 468 {
ab79958a 469 grid->SetDefaultCellBackgroundColour(col);
758cbedf
VZ
470 grid->Refresh();
471 }
ab79958a 472}
e442cc0d 473
e442cc0d
MB
474void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
475{
476 logBuf = "";
477 if ( ev.GetRow() != -1 )
478 {
c336585e 479 logBuf << "Left click on row label " << ev.GetRow();
e442cc0d
MB
480 }
481 else if ( ev.GetCol() != -1 )
482 {
c336585e 483 logBuf << "Left click on col label " << ev.GetCol();
e442cc0d
MB
484 }
485 else
486 {
c336585e 487 logBuf << "Left click on corner label";
e442cc0d
MB
488 }
489
490 if ( ev.ShiftDown() ) logBuf << " (shift down)";
491 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 492
c336585e
MB
493 // you must call event skip if you want default grid processing
494 //
e442cc0d
MB
495 ev.Skip();
496}
497
498
499void GridFrame::OnCellLeftClick( wxGridEvent& ev )
500{
501 logBuf = "";
c336585e 502 logBuf << "Left click at row " << ev.GetRow()
e442cc0d
MB
503 << " col " << ev.GetCol();
504 wxLogMessage( "%s", logBuf.c_str() );
0eee5283 505
e442cc0d
MB
506 // you must call event skip if you want default grid processing
507 // (cell highlighting etc.)
508 //
509 ev.Skip();
510}
511
512
513void GridFrame::OnRowSize( wxGridSizeEvent& ev )
514{
515 logBuf = "";
516 logBuf << "Resized row " << ev.GetRowOrCol();
517 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 518
e442cc0d
MB
519 ev.Skip();
520}
521
522
523void GridFrame::OnColSize( wxGridSizeEvent& ev )
524{
525 logBuf = "";
526 logBuf << "Resized col " << ev.GetRowOrCol();
527 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 528
e442cc0d
MB
529 ev.Skip();
530}
531
c336585e
MB
532
533void GridFrame::OnSelectCell( wxGridEvent& ev )
534{
535 logBuf = "";
536 logBuf << "Selected cell at row " << ev.GetRow()
537 << " col " << ev.GetCol();
538 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 539
c336585e
MB
540 // you must call Skip() if you want the default processing
541 // to occur in wxGrid
542 ev.Skip();
543}
544
e442cc0d
MB
545void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
546{
547 logBuf = "";
548 logBuf << "Selected cells from row " << ev.GetTopRow()
549 << " col " << ev.GetLeftCol()
550 << " to row " << ev.GetBottomRow()
551 << " col " << ev.GetRightCol();
f0102d2a 552
e442cc0d 553 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 554
e442cc0d
MB
555 ev.Skip();
556}
557
558void GridFrame::OnCellValueChanged( wxGridEvent& ev )
559{
560 logBuf = "";
561 logBuf << "Value changed for cell at"
f0102d2a 562 << " row " << ev.GetRow()
e442cc0d 563 << " col " << ev.GetCol();
f0102d2a 564
e442cc0d 565 wxLogMessage( "%s", logBuf.c_str() );
f0102d2a 566
e442cc0d
MB
567 ev.Skip();
568}
569
570
f445b853
MB
571void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
572{
573 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
574 "Michael Bedward \n"
575 "mbedward@ozemail.com.au \n\n",
576 "About",
577 wxOK );
578}
579
580
581void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
582{
583 Close( TRUE );
584}
585
ab79958a
VZ
586// ----------------------------------------------------------------------------
587// MyGridCellRenderer
588// ----------------------------------------------------------------------------
589
758cbedf
VZ
590// do something that the default renderer doesn't here just to show that it is
591// possible to alter the appearance of the cell beyond what the attributes
592// allow
ab79958a
VZ
593void MyGridCellRenderer::Draw(wxGrid& grid,
594 wxDC& dc,
595 const wxRect& rect,
596 int row, int col,
597 bool isSelected)
598{
599 wxGridCellStringRenderer::Draw(grid, dc, rect, row, col, isSelected);
600
601 dc.SetPen(*wxGREEN_PEN);
602 dc.SetBrush(*wxTRANSPARENT_BRUSH);
603 dc.DrawEllipse(rect);
604}