]> git.saurik.com Git - wxWidgets.git/blob - samples/newgrid/griddemo.cpp
-fpermissive is now returned by wx-config -cflags if the library was compiled
[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 #ifdef __GNUG__
12 #pragma implementation
13 #pragma interface
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/colordlg.h"
28
29 #include "wx/grid.h"
30
31 #include "griddemo.h"
32
33 IMPLEMENT_APP( GridApp )
34
35
36
37 bool GridApp::OnInit()
38 {
39 GridFrame *frame = new GridFrame;
40 frame->Show( TRUE );
41
42 return TRUE;
43 }
44
45
46
47 BEGIN_EVENT_TABLE( GridFrame, wxFrame )
48 EVT_MENU( ID_TOGGLEROWLABELS, GridFrame::ToggleRowLabels )
49 EVT_MENU( ID_TOGGLECOLLABELS, GridFrame::ToggleColLabels )
50 EVT_MENU( ID_TOGGLECONTROLPANEL, GridFrame::ToggleControlPanel )
51 EVT_MENU( ID_TOGGLECELLEDIT, GridFrame::ToggleCellEdit )
52 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
53 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
54 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
55 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
56 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
57 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
58 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
59 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
60 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
61 EVT_MENU( ID_DELETEROW, GridFrame::DeleteRow )
62 EVT_MENU( ID_DELETECOL, GridFrame::DeleteCol )
63 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
64 EVT_MENU( ID_ABOUT, GridFrame::About )
65 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
66
67 EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
68 EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
69 EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
70 EVT_GRID_COL_SIZE( GridFrame::OnColSize )
71 EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell )
72 EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected )
73 EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
74 END_EVENT_TABLE()
75
76
77 GridFrame::GridFrame()
78 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
79 wxDefaultPosition,
80 wxDefaultSize )
81 {
82 int gridW = 600, gridH = 300;
83 int logW = gridW, logH = 80;
84
85 wxMenu *fileMenu = new wxMenu;
86 fileMenu->Append( wxID_EXIT, "E&xit" );
87
88 wxMenu *viewMenu = new wxMenu;
89 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
90 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
91 viewMenu->Append( ID_TOGGLECONTROLPANEL, "To&p controls", "", TRUE );
92 viewMenu->Append( ID_TOGGLECELLEDIT, "&In-place editing", "", TRUE );
93 viewMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
94 viewMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
95
96 wxMenu *rowLabelMenu = new wxMenu;
97
98 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
99 rowLabelMenu,
100 "Change alignment of row labels" );
101
102 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
103 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
104
105 wxMenu *colLabelMenu = new wxMenu;
106
107 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
108 colLabelMenu,
109 "Change alignment of col labels" );
110
111 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
112 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
113
114 viewMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
115
116 wxMenu *editMenu = new wxMenu;
117 editMenu->Append( ID_INSERTROW, "Insert &row" );
118 editMenu->Append( ID_INSERTCOL, "Insert &column" );
119 editMenu->Append( ID_DELETEROW, "Delete ro&w" );
120 editMenu->Append( ID_DELETECOL, "Delete co&l" );
121 editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
122
123 wxMenu *helpMenu = new wxMenu;
124 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
125
126 wxMenuBar *menuBar = new wxMenuBar;
127 menuBar->Append( fileMenu, "&File" );
128 menuBar->Append( viewMenu, "&View" );
129 menuBar->Append( editMenu, "&Edit" );
130 menuBar->Append( helpMenu, "&Help" );
131
132 SetMenuBar( menuBar );
133
134 grid = new wxGrid( this,
135 -1,
136 wxPoint( 0, 0 ),
137 wxSize( 400, 300 ) );
138
139 logWin = new wxTextCtrl( this,
140 -1,
141 wxEmptyString,
142 wxPoint( 0, gridH + 20 ),
143 wxSize( logW, logH ),
144 wxTE_MULTILINE );
145
146 logger = new wxLogTextCtrl( logWin );
147 logger->SetActiveTarget( logger );
148 logger->SetTimestamp( NULL );
149
150 // this will create a grid and, by default, an associated grid
151 // table for string data
152 //
153 grid->CreateGrid( 100, 100 );
154
155 grid->EnableTopEditControl( TRUE );
156
157 grid->SetRowSize( 0, 60 );
158 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
159
160 grid->SetCellValue( 0, 1, "Blah" );
161 grid->SetCellValue( 0, 2, "Blah" );
162
163 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
164
165 grid->SetRowSize( 99, 60 );
166 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
167
168 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
169 topSizer->Add( grid,
170 1,
171 wxEXPAND );
172
173 topSizer->Add( logWin,
174 0,
175 wxEXPAND );
176
177 SetAutoLayout( TRUE );
178 SetSizer( topSizer );
179
180 topSizer->Fit( this );
181 topSizer->SetSizeHints( this );
182
183 Centre();
184 SetDefaults();
185 }
186
187
188 GridFrame::~GridFrame()
189 {
190 }
191
192 void GridFrame::SetDefaults()
193 {
194 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
195 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
196 GetMenuBar()->Check( ID_TOGGLECONTROLPANEL, TRUE );
197 GetMenuBar()->Check( ID_TOGGLECELLEDIT, TRUE );
198 }
199
200
201 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
202 {
203 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
204 {
205 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
206 }
207 else
208 {
209 grid->SetRowLabelSize( 0 );
210 }
211 }
212
213
214 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
215 {
216 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
217 {
218 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
219 }
220 else
221 {
222 grid->SetColLabelSize( 0 );
223 }
224 }
225
226
227 void GridFrame::ToggleControlPanel( wxCommandEvent& WXUNUSED(ev) )
228 {
229 grid->EnableTopEditControl(GetMenuBar()->IsChecked(ID_TOGGLECONTROLPANEL));
230 }
231
232
233 void GridFrame::ToggleCellEdit( wxCommandEvent& WXUNUSED(ev) )
234 {
235 grid->EnableCellEditControl(
236 GetMenuBar()->IsChecked( ID_TOGGLECELLEDIT ) );
237 }
238
239
240 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
241 {
242 wxColourDialog dlg( NULL );
243 if ( dlg.ShowModal() == wxID_OK )
244 {
245 wxColourData retData;
246 retData = dlg.GetColourData();
247 wxColour colour = retData.GetColour();
248
249 grid->SetLabelBackgroundColour( colour );
250 }
251 }
252
253
254 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
255 {
256 wxColourDialog dlg( NULL );
257 if ( dlg.ShowModal() == wxID_OK )
258 {
259 wxColourData retData;
260 retData = dlg.GetColourData();
261 wxColour colour = retData.GetColour();
262
263 grid->SetLabelTextColour( colour );
264 }
265 }
266
267
268 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
269 {
270 int horiz, vert;
271 grid->GetRowLabelAlignment( &horiz, &vert );
272
273 switch ( horiz )
274 {
275 case wxLEFT:
276 horiz = wxCENTRE;
277 break;
278
279 case wxCENTRE:
280 horiz = wxRIGHT;
281 break;
282
283 case wxRIGHT:
284 horiz = wxLEFT;
285 break;
286 }
287
288 grid->SetRowLabelAlignment( horiz, -1 );
289 }
290
291 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
292 {
293 int horiz, vert;
294 grid->GetRowLabelAlignment( &horiz, &vert );
295
296 switch ( vert )
297 {
298 case wxTOP:
299 vert = wxCENTRE;
300 break;
301
302 case wxCENTRE:
303 vert = wxBOTTOM;
304 break;
305
306 case wxBOTTOM:
307 vert = wxTOP;
308 break;
309 }
310
311 grid->SetRowLabelAlignment( -1, vert );
312 }
313
314
315 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
316 {
317 int horiz, vert;
318 grid->GetColLabelAlignment( &horiz, &vert );
319
320 switch ( horiz )
321 {
322 case wxLEFT:
323 horiz = wxCENTRE;
324 break;
325
326 case wxCENTRE:
327 horiz = wxRIGHT;
328 break;
329
330 case wxRIGHT:
331 horiz = wxLEFT;
332 break;
333 }
334
335 grid->SetColLabelAlignment( horiz, -1 );
336 }
337
338
339 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
340 {
341 int horiz, vert;
342 grid->GetColLabelAlignment( &horiz, &vert );
343
344 switch ( vert )
345 {
346 case wxTOP:
347 vert = wxCENTRE;
348 break;
349
350 case wxCENTRE:
351 vert = wxBOTTOM;
352 break;
353
354 case wxBOTTOM:
355 vert = wxTOP;
356 break;
357 }
358
359 grid->SetColLabelAlignment( -1, vert );
360 }
361
362
363 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
364 {
365 wxColourDialog dlg( NULL );
366 if ( dlg.ShowModal() == wxID_OK )
367 {
368 wxColourData retData;
369 retData = dlg.GetColourData();
370 wxColour colour = retData.GetColour();
371
372 grid->SetGridLineColour( colour );
373 }
374 }
375
376
377 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
378 {
379 grid->InsertRows( 0, 1 );
380 }
381
382
383 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
384 {
385 grid->InsertCols( 0, 1 );
386 }
387
388
389 void GridFrame::DeleteRow( wxCommandEvent& WXUNUSED(ev) )
390 {
391 grid->DeleteRows( 0, 1 );
392 }
393
394
395 void GridFrame::DeleteCol( wxCommandEvent& WXUNUSED(ev) )
396 {
397 grid->DeleteCols( 0, 1 );
398 }
399
400
401 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
402 {
403 grid->ClearGrid();
404 }
405
406
407 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
408 {
409 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
410 "Michael Bedward \n"
411 "mbedward@ozemail.com.au \n\n",
412 "About",
413 wxOK );
414 }
415
416
417 void GridFrame::OnSize( wxSizeEvent& WXUNUSED(ev) )
418 {
419 if ( grid && logWin )
420 {
421 int cw, ch;
422 GetClientSize( &cw, &ch );
423
424 int gridH = ch - 90;
425 int logH = 80;
426 if ( gridH < 0 )
427 {
428 gridH = ch;
429 }
430
431 grid->SetSize( 0, 0, cw, gridH );
432 logWin->SetSize( 0, gridH + 10, cw, logH );
433 }
434 }
435
436 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
437 {
438 Close( TRUE );
439 }
440
441
442 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
443 {
444 logBuf = "";
445 if ( ev.GetRow() != -1 )
446 {
447 logBuf << "Left click on row label " << ev.GetRow();
448 }
449 else if ( ev.GetCol() != -1 )
450 {
451 logBuf << "Left click on col label " << ev.GetCol();
452 }
453 else
454 {
455 logBuf << "Left click on corner label";
456 }
457
458 if ( ev.ShiftDown() ) logBuf << " (shift down)";
459 wxLogMessage( "%s", logBuf.c_str() );
460
461 // you must call event skip if you want default grid processing
462 //
463 ev.Skip();
464 }
465
466
467 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
468 {
469 logBuf = "";
470 logBuf << "Left click at row " << ev.GetRow()
471 << " col " << ev.GetCol();
472 wxLogMessage( "%s", logBuf.c_str() );
473
474 // you must call event skip if you want default grid processing
475 // (cell highlighting etc.)
476 //
477 ev.Skip();
478 }
479
480
481 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
482 {
483 logBuf = "";
484 logBuf << "Resized row " << ev.GetRowOrCol();
485 wxLogMessage( "%s", logBuf.c_str() );
486
487 ev.Skip();
488 }
489
490
491 void GridFrame::OnColSize( wxGridSizeEvent& ev )
492 {
493 logBuf = "";
494 logBuf << "Resized col " << ev.GetRowOrCol();
495 wxLogMessage( "%s", logBuf.c_str() );
496
497 ev.Skip();
498 }
499
500
501 void GridFrame::OnSelectCell( wxGridEvent& ev )
502 {
503 logBuf = "";
504 logBuf << "Selected cell at row " << ev.GetRow()
505 << " col " << ev.GetCol();
506 wxLogMessage( "%s", logBuf.c_str() );
507
508 // you must call Skip() if you want the default processing
509 // to occur in wxGrid
510 ev.Skip();
511 }
512
513 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
514 {
515 logBuf = "";
516 logBuf << "Selected cells from row " << ev.GetTopRow()
517 << " col " << ev.GetLeftCol()
518 << " to row " << ev.GetBottomRow()
519 << " col " << ev.GetRightCol();
520
521 wxLogMessage( "%s", logBuf.c_str() );
522
523 ev.Skip();
524 }
525
526 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
527 {
528 logBuf = "";
529 logBuf << "Value changed for cell at"
530 << " row " << ev.GetRow()
531 << " col " << ev.GetCol();
532
533 wxLogMessage( "%s", logBuf.c_str() );
534
535 ev.Skip();
536 }
537
538