]> git.saurik.com Git - wxWidgets.git/blob - samples/newgrid/griddemo.cpp
66ccfe56443edfa7dbf9c1b73e8375c6a1eb5f6a
[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_CLEARGRID, GridFrame::ClearGrid )
60 EVT_MENU( ID_ABOUT, GridFrame::About )
61 EVT_MENU( wxID_EXIT, GridFrame::OnQuit )
62
63 EVT_WXGRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
64 EVT_WXGRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
65 EVT_WXGRID_ROW_SIZE( GridFrame::OnRowSize )
66 EVT_WXGRID_COL_SIZE( GridFrame::OnColSize )
67 EVT_WXGRID_RANGE_SELECT( GridFrame::OnRangeSelected )
68 EVT_WXGRID_CELL_CHANGE( GridFrame::OnCellValueChanged )
69 END_EVENT_TABLE()
70
71
72 GridFrame::GridFrame()
73 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
74 wxDefaultPosition,
75 wxDefaultSize )
76 {
77 int gridW = 600, gridH = 300;
78 int logW = gridW, logH = 80;
79
80 wxMenu *fileMenu = new wxMenu;
81 fileMenu->Append( wxID_EXIT, "E&xit" );
82
83 wxMenu *viewMenu = new wxMenu;
84 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
85 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
86 viewMenu->Append( ID_TOGGLECONTROLPANEL, "To&p controls", "", TRUE );
87 viewMenu->Append( ID_TOGGLECELLEDIT, "&In-place editing", "", TRUE );
88 viewMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" );
89 viewMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" );
90
91 wxMenu *rowLabelMenu = new wxMenu;
92
93 viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment",
94 rowLabelMenu,
95 "Change alignment of row labels" );
96
97 rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" );
98 rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" );
99
100 wxMenu *colLabelMenu = new wxMenu;
101
102 viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment",
103 colLabelMenu,
104 "Change alignment of col labels" );
105
106 colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" );
107 colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" );
108
109 viewMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" );
110 viewMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" );
111
112 wxMenu *helpMenu = new wxMenu;
113 helpMenu->Append( ID_ABOUT, "&About wxGrid demo" );
114
115 wxMenuBar *menuBar = new wxMenuBar;
116 menuBar->Append( fileMenu, "&File" );
117 menuBar->Append( viewMenu, "&View" );
118 menuBar->Append( helpMenu, "&Help" );
119
120 SetMenuBar( menuBar );
121
122 grid = new wxGrid( this,
123 -1,
124 wxPoint( 0, 0 ),
125 wxSize( 400, 300 ) );
126
127 logWin = new wxTextCtrl( this,
128 -1,
129 wxEmptyString,
130 wxPoint( 0, gridH + 20 ),
131 wxSize( logW, logH ),
132 wxTE_MULTILINE );
133
134 logger = new wxLogTextCtrl( logWin );
135 logger->SetActiveTarget( logger );
136 logger->SetTimestamp( NULL );
137
138 // this will create a grid and, by default, an associated grid
139 // table for string data
140 //
141 grid->CreateGrid( 100, 100 );
142
143 grid->EnableTopEditControl( TRUE );
144
145 grid->SetRowSize( 0, 60 );
146 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
147
148 grid->SetCellValue( 0, 1, "Blah" );
149 grid->SetCellValue( 0, 2, "Blah" );
150
151 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
152
153 grid->SetRowSize( 99, 60 );
154 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
155
156 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
157 topSizer->Add( grid,
158 1,
159 wxEXPAND );
160
161 topSizer->Add( logWin,
162 0,
163 wxEXPAND );
164
165 SetAutoLayout( TRUE );
166 SetSizer( topSizer );
167
168 topSizer->Fit( this );
169 topSizer->SetSizeHints( this );
170
171 Centre();
172 SetDefaults();
173 }
174
175
176 GridFrame::~GridFrame()
177 {
178 }
179
180 void GridFrame::SetDefaults()
181 {
182 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
183 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
184 GetMenuBar()->Check( ID_TOGGLECONTROLPANEL, TRUE );
185 GetMenuBar()->Check( ID_TOGGLECELLEDIT, TRUE );
186 }
187
188
189 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
190 {
191 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
192 {
193 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
194 }
195 else
196 {
197 grid->SetRowLabelSize( 0 );
198 }
199 }
200
201
202 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
203 {
204 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
205 {
206 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
207 }
208 else
209 {
210 grid->SetColLabelSize( 0 );
211 }
212 }
213
214
215 void GridFrame::ToggleControlPanel( wxCommandEvent& WXUNUSED(ev) )
216 {
217 grid->EnableTopEditControl(GetMenuBar()->IsChecked(ID_TOGGLECONTROLPANEL));
218 }
219
220
221 void GridFrame::ToggleCellEdit( wxCommandEvent& WXUNUSED(ev) )
222 {
223 grid->EnableCellEditControl(
224 GetMenuBar()->IsChecked( ID_TOGGLECELLEDIT ) );
225 }
226
227
228 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
229 {
230 wxColourDialog dlg( NULL );
231 if ( dlg.ShowModal() == wxID_OK )
232 {
233 wxColourData retData;
234 retData = dlg.GetColourData();
235 wxColour colour = retData.GetColour();
236
237 grid->SetLabelBackgroundColour( colour );
238 }
239 }
240
241
242 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
243 {
244 wxColourDialog dlg( NULL );
245 if ( dlg.ShowModal() == wxID_OK )
246 {
247 wxColourData retData;
248 retData = dlg.GetColourData();
249 wxColour colour = retData.GetColour();
250
251 grid->SetLabelTextColour( colour );
252 }
253 }
254
255
256 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
257 {
258 int horiz, vert;
259 grid->GetRowLabelAlignment( &horiz, &vert );
260
261 switch ( horiz )
262 {
263 case wxLEFT:
264 horiz = wxCENTRE;
265 break;
266
267 case wxCENTRE:
268 horiz = wxRIGHT;
269 break;
270
271 case wxRIGHT:
272 horiz = wxLEFT;
273 break;
274 }
275
276 grid->SetRowLabelAlignment( horiz, -1 );
277 }
278
279 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
280 {
281 int horiz, vert;
282 grid->GetRowLabelAlignment( &horiz, &vert );
283
284 switch ( vert )
285 {
286 case wxTOP:
287 vert = wxCENTRE;
288 break;
289
290 case wxCENTRE:
291 vert = wxBOTTOM;
292 break;
293
294 case wxBOTTOM:
295 vert = wxTOP;
296 break;
297 }
298
299 grid->SetRowLabelAlignment( -1, vert );
300 }
301
302
303 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
304 {
305 int horiz, vert;
306 grid->GetColLabelAlignment( &horiz, &vert );
307
308 switch ( horiz )
309 {
310 case wxLEFT:
311 horiz = wxCENTRE;
312 break;
313
314 case wxCENTRE:
315 horiz = wxRIGHT;
316 break;
317
318 case wxRIGHT:
319 horiz = wxLEFT;
320 break;
321 }
322
323 grid->SetColLabelAlignment( horiz, -1 );
324 }
325
326
327 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
328 {
329 int horiz, vert;
330 grid->GetColLabelAlignment( &horiz, &vert );
331
332 switch ( vert )
333 {
334 case wxTOP:
335 vert = wxCENTRE;
336 break;
337
338 case wxCENTRE:
339 vert = wxBOTTOM;
340 break;
341
342 case wxBOTTOM:
343 vert = wxTOP;
344 break;
345 }
346
347 grid->SetColLabelAlignment( -1, vert );
348 }
349
350
351 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
352 {
353 wxColourDialog dlg( NULL );
354 if ( dlg.ShowModal() == wxID_OK )
355 {
356 wxColourData retData;
357 retData = dlg.GetColourData();
358 wxColour colour = retData.GetColour();
359
360 grid->SetGridLineColour( colour );
361 }
362 }
363
364
365 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
366 {
367 grid->ClearGrid();
368 }
369
370
371 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
372 {
373 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
374 "Michael Bedward \n"
375 "mbedward@ozemail.com.au \n\n",
376 "About",
377 wxOK );
378 }
379
380
381 void GridFrame::OnSize( wxSizeEvent& WXUNUSED(ev) )
382 {
383 if ( grid && logWin )
384 {
385 int cw, ch;
386 GetClientSize( &cw, &ch );
387
388 int gridH = ch - 90;
389 int logH = 80;
390 if ( gridH < 0 )
391 {
392 gridH = ch;
393 }
394
395 grid->SetSize( 0, 0, cw, gridH );
396 logWin->SetSize( 0, gridH + 10, cw, logH );
397 }
398 }
399
400 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
401 {
402 Close( TRUE );
403 }
404
405
406 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
407 {
408 logBuf = "";
409 if ( ev.GetRow() != -1 )
410 {
411 logBuf << "row label " << ev.GetRow();
412 }
413 else if ( ev.GetCol() != -1 )
414 {
415 logBuf << "col label " << ev.GetCol();
416 }
417 else
418 {
419 logBuf << "corner label";
420 }
421
422 if ( ev.ShiftDown() ) logBuf << " (shift down)";
423 wxLogMessage( "%s", logBuf.c_str() );
424
425 ev.Skip();
426 }
427
428
429 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
430 {
431 logBuf = "";
432 logBuf << "Cell at row " << ev.GetRow()
433 << " col " << ev.GetCol();
434 wxLogMessage( "%s", logBuf.c_str() );
435
436 // you must call event skip if you want default grid processing
437 // (cell highlighting etc.)
438 //
439 ev.Skip();
440 }
441
442
443 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
444 {
445 logBuf = "";
446 logBuf << "Resized row " << ev.GetRowOrCol();
447 wxLogMessage( "%s", logBuf.c_str() );
448
449 ev.Skip();
450 }
451
452
453 void GridFrame::OnColSize( wxGridSizeEvent& ev )
454 {
455 logBuf = "";
456 logBuf << "Resized col " << ev.GetRowOrCol();
457 wxLogMessage( "%s", logBuf.c_str() );
458
459 ev.Skip();
460 }
461
462 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
463 {
464 logBuf = "";
465 logBuf << "Selected cells from row " << ev.GetTopRow()
466 << " col " << ev.GetLeftCol()
467 << " to row " << ev.GetBottomRow()
468 << " col " << ev.GetRightCol();
469
470 wxLogMessage( "%s", logBuf.c_str() );
471
472 ev.Skip();
473 }
474
475 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
476 {
477 logBuf = "";
478 logBuf << "Value changed for cell at"
479 << " row " << ev.GetRow()
480 << " col " << ev.GetCol();
481
482 wxLogMessage( "%s", logBuf.c_str() );
483
484 ev.Skip();
485 }
486
487