]> git.saurik.com Git - wxWidgets.git/blob - samples/newgrid/griddemo.cpp
added wxGridCellRenderer
[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
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 )
94 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
95 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
96 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
97
98 END_EVENT_TABLE()
99
100
101 GridFrame::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;
108
109 wxMenu *fileMenu = new wxMenu;
110 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
111
112 wxMenu *viewMenu = new wxMenu;
113 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
114 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
115 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE );
116
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" );
124 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
125
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
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" );
141
142 wxMenu *editMenu = new wxMenu;
143 editMenu->Append( ID_INSERTROW, "Insert &row" );
144 editMenu->Append( ID_INSERTCOL, "Insert &column" );
145 editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" );
146 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
147 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
148
149 wxMenu *helpMenu = new wxMenu;
150 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
151
152 wxMenuBar *menuBar = new wxMenuBar;
153 menuBar->Append( fileMenu, "&File" );
154 menuBar->Append( viewMenu, "&View" );
155 menuBar->Append( colMenu, "&Colours" );
156 menuBar->Append( editMenu, "&Edit" );
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 ) );
165
166 logWin = new wxTextCtrl( this,
167 -1,
168 wxEmptyString,
169 wxPoint( 0, gridH + 20 ),
170 wxSize( logW, logH ),
171 wxTE_MULTILINE );
172
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
182 grid->SetRowSize( 0, 60 );
183 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
184
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" );
192
193 grid->SetCellValue(2, 2, "red");
194
195 grid->SetCellTextColour(2, 2, *wxRED);
196 grid->SetCellValue(3, 3, "green on grey");
197 grid->SetCellTextColour(3, 3, *wxGREEN);
198 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
199
200 grid->SetCellValue(4, 4, "a weird looking cell");
201 grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE);
202 grid->SetCellRenderer(4, 4, new MyGridCellRenderer);
203
204 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
205 topSizer->Add( grid,
206 1,
207 wxEXPAND );
208
209 topSizer->Add( logWin,
210 0,
211 wxEXPAND );
212
213 SetAutoLayout( TRUE );
214 SetSizer( topSizer );
215
216 topSizer->Fit( this );
217 topSizer->SetSizeHints( this );
218
219 Centre();
220 SetDefaults();
221 }
222
223
224 GridFrame::~GridFrame()
225 {
226 }
227
228
229 void GridFrame::SetDefaults()
230 {
231 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
232 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
233 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
234 }
235
236
237 void 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
250 void 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
263 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
264 {
265 grid->EnableEditing(
266 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
267 }
268
269
270 void 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
284 void 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
298 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
299 {
300 int horiz, vert;
301 grid->GetRowLabelAlignment( &horiz, &vert );
302
303 switch ( horiz )
304 {
305 case wxLEFT:
306 horiz = wxCENTRE;
307 break;
308
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
321 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
322 {
323 int horiz, vert;
324 grid->GetRowLabelAlignment( &horiz, &vert );
325
326 switch ( vert )
327 {
328 case wxTOP:
329 vert = wxCENTRE;
330 break;
331
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
345 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
346 {
347 int horiz, vert;
348 grid->GetColLabelAlignment( &horiz, &vert );
349
350 switch ( horiz )
351 {
352 case wxLEFT:
353 horiz = wxCENTRE;
354 break;
355
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
369 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
370 {
371 int horiz, vert;
372 grid->GetColLabelAlignment( &horiz, &vert );
373
374 switch ( vert )
375 {
376 case wxTOP:
377 vert = wxCENTRE;
378 break;
379
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
393 void 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
407 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
408 {
409 grid->InsertRows( 0, 1 );
410 }
411
412
413 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
414 {
415 grid->InsertCols( 0, 1 );
416 }
417
418
419 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
420 {
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 }
427 }
428
429
430 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
431 {
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 }
438 }
439
440
441 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
442 {
443 grid->ClearGrid();
444 }
445
446 void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) )
447 {
448 wxColour col = wxGetColourFromUser(this);
449 if ( col.Ok() )
450 grid->SetDefaultCellTextColour(col);
451 }
452
453 void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) )
454 {
455 wxColour col = wxGetColourFromUser(this);
456 if ( col.Ok() )
457 grid->SetDefaultCellBackgroundColour(col);
458 }
459
460 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
461 {
462 logBuf = "";
463 if ( ev.GetRow() != -1 )
464 {
465 logBuf << "Left click on row label " << ev.GetRow();
466 }
467 else if ( ev.GetCol() != -1 )
468 {
469 logBuf << "Left click on col label " << ev.GetCol();
470 }
471 else
472 {
473 logBuf << "Left click on corner label";
474 }
475
476 if ( ev.ShiftDown() ) logBuf << " (shift down)";
477 wxLogMessage( "%s", logBuf.c_str() );
478
479 // you must call event skip if you want default grid processing
480 //
481 ev.Skip();
482 }
483
484
485 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
486 {
487 logBuf = "";
488 logBuf << "Left click at row " << ev.GetRow()
489 << " col " << ev.GetCol();
490 wxLogMessage( "%s", logBuf.c_str() );
491
492 // you must call event skip if you want default grid processing
493 // (cell highlighting etc.)
494 //
495 ev.Skip();
496 }
497
498
499 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
500 {
501 logBuf = "";
502 logBuf << "Resized row " << ev.GetRowOrCol();
503 wxLogMessage( "%s", logBuf.c_str() );
504
505 ev.Skip();
506 }
507
508
509 void GridFrame::OnColSize( wxGridSizeEvent& ev )
510 {
511 logBuf = "";
512 logBuf << "Resized col " << ev.GetRowOrCol();
513 wxLogMessage( "%s", logBuf.c_str() );
514
515 ev.Skip();
516 }
517
518
519 void 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() );
525
526 // you must call Skip() if you want the default processing
527 // to occur in wxGrid
528 ev.Skip();
529 }
530
531 void 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();
538
539 wxLogMessage( "%s", logBuf.c_str() );
540
541 ev.Skip();
542 }
543
544 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
545 {
546 logBuf = "";
547 logBuf << "Value changed for cell at"
548 << " row " << ev.GetRow()
549 << " col " << ev.GetCol();
550
551 wxLogMessage( "%s", logBuf.c_str() );
552
553 ev.Skip();
554 }
555
556
557 void 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
567 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
568 {
569 Close( TRUE );
570 }
571
572 // ----------------------------------------------------------------------------
573 // MyGridCellRenderer
574 // ----------------------------------------------------------------------------
575
576 void 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 }