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