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