]> git.saurik.com Git - wxWidgets.git/blob - samples/newgrid/griddemo.cpp
fixed setting more than one attr for a cell
[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_TOGGLEEDIT, GridFrame::ToggleEditing )
51 EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
52 EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
53 EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment )
54 EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment )
55 EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment )
56 EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment )
57 EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour )
58 EVT_MENU( ID_INSERTROW, GridFrame::InsertRow )
59 EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol )
60 EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows )
61 EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols )
62 EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid )
63
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
75 END_EVENT_TABLE()
76
77
78 GridFrame::GridFrame()
79 : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo",
80 wxDefaultPosition,
81 wxDefaultSize )
82 {
83 int gridW = 600, gridH = 300;
84 int logW = gridW, logH = 80;
85
86 wxMenu *fileMenu = new wxMenu;
87 fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" );
88
89 wxMenu *viewMenu = new wxMenu;
90 viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE );
91 viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE );
92 viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", 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 selected ro&ws" );
120 editMenu->Append( ID_DELETECOL, "Delete selected co&ls" );
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->SetRowSize( 0, 60 );
156 grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" );
157
158 grid->SetCellValue( 0, 1, "Blah" );
159 grid->SetCellValue( 0, 2, "Blah" );
160
161 grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" );
162
163 grid->SetRowSize( 99, 60 );
164 grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" );
165
166 grid->SetCellValue(2, 2, "red");
167 grid->SetCellTextColour(2, 2, *wxRED);
168 grid->SetCellValue(3, 3, "green on grey");
169 grid->SetCellTextColour(3, 3, *wxGREEN);
170 grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY);
171
172 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
173 topSizer->Add( grid,
174 1,
175 wxEXPAND );
176
177 topSizer->Add( logWin,
178 0,
179 wxEXPAND );
180
181 SetAutoLayout( TRUE );
182 SetSizer( topSizer );
183
184 topSizer->Fit( this );
185 topSizer->SetSizeHints( this );
186
187 Centre();
188 SetDefaults();
189 }
190
191
192 GridFrame::~GridFrame()
193 {
194 }
195
196
197 void GridFrame::SetDefaults()
198 {
199 GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE );
200 GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE );
201 GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE );
202 }
203
204
205 void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) )
206 {
207 if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) )
208 {
209 grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() );
210 }
211 else
212 {
213 grid->SetRowLabelSize( 0 );
214 }
215 }
216
217
218 void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) )
219 {
220 if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) )
221 {
222 grid->SetColLabelSize( grid->GetDefaultColLabelSize() );
223 }
224 else
225 {
226 grid->SetColLabelSize( 0 );
227 }
228 }
229
230
231 void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) )
232 {
233 grid->EnableEditing(
234 GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) );
235 }
236
237
238 void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) )
239 {
240 wxColourDialog dlg( NULL );
241 if ( dlg.ShowModal() == wxID_OK )
242 {
243 wxColourData retData;
244 retData = dlg.GetColourData();
245 wxColour colour = retData.GetColour();
246
247 grid->SetLabelBackgroundColour( colour );
248 }
249 }
250
251
252 void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) )
253 {
254 wxColourDialog dlg( NULL );
255 if ( dlg.ShowModal() == wxID_OK )
256 {
257 wxColourData retData;
258 retData = dlg.GetColourData();
259 wxColour colour = retData.GetColour();
260
261 grid->SetLabelTextColour( colour );
262 }
263 }
264
265
266 void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
267 {
268 int horiz, vert;
269 grid->GetRowLabelAlignment( &horiz, &vert );
270
271 switch ( horiz )
272 {
273 case wxLEFT:
274 horiz = wxCENTRE;
275 break;
276
277 case wxCENTRE:
278 horiz = wxRIGHT;
279 break;
280
281 case wxRIGHT:
282 horiz = wxLEFT;
283 break;
284 }
285
286 grid->SetRowLabelAlignment( horiz, -1 );
287 }
288
289 void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
290 {
291 int horiz, vert;
292 grid->GetRowLabelAlignment( &horiz, &vert );
293
294 switch ( vert )
295 {
296 case wxTOP:
297 vert = wxCENTRE;
298 break;
299
300 case wxCENTRE:
301 vert = wxBOTTOM;
302 break;
303
304 case wxBOTTOM:
305 vert = wxTOP;
306 break;
307 }
308
309 grid->SetRowLabelAlignment( -1, vert );
310 }
311
312
313 void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) )
314 {
315 int horiz, vert;
316 grid->GetColLabelAlignment( &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->SetColLabelAlignment( horiz, -1 );
334 }
335
336
337 void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) )
338 {
339 int horiz, vert;
340 grid->GetColLabelAlignment( &horiz, &vert );
341
342 switch ( vert )
343 {
344 case wxTOP:
345 vert = wxCENTRE;
346 break;
347
348 case wxCENTRE:
349 vert = wxBOTTOM;
350 break;
351
352 case wxBOTTOM:
353 vert = wxTOP;
354 break;
355 }
356
357 grid->SetColLabelAlignment( -1, vert );
358 }
359
360
361 void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) )
362 {
363 wxColourDialog dlg( NULL );
364 if ( dlg.ShowModal() == wxID_OK )
365 {
366 wxColourData retData;
367 retData = dlg.GetColourData();
368 wxColour colour = retData.GetColour();
369
370 grid->SetGridLineColour( colour );
371 }
372 }
373
374
375 void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) )
376 {
377 grid->InsertRows( 0, 1 );
378 }
379
380
381 void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) )
382 {
383 grid->InsertCols( 0, 1 );
384 }
385
386
387 void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) )
388 {
389 if ( grid->IsSelection() )
390 {
391 int topRow, bottomRow, leftCol, rightCol;
392 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
393 grid->DeleteRows( topRow, bottomRow - topRow + 1 );
394 }
395 }
396
397
398 void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) )
399 {
400 if ( grid->IsSelection() )
401 {
402 int topRow, bottomRow, leftCol, rightCol;
403 grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol );
404 grid->DeleteCols( leftCol, rightCol - leftCol + 1 );
405 }
406 }
407
408
409 void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) )
410 {
411 grid->ClearGrid();
412 }
413
414
415 void GridFrame::OnLabelLeftClick( wxGridEvent& ev )
416 {
417 logBuf = "";
418 if ( ev.GetRow() != -1 )
419 {
420 logBuf << "Left click on row label " << ev.GetRow();
421 }
422 else if ( ev.GetCol() != -1 )
423 {
424 logBuf << "Left click on col label " << ev.GetCol();
425 }
426 else
427 {
428 logBuf << "Left click on corner label";
429 }
430
431 if ( ev.ShiftDown() ) logBuf << " (shift down)";
432 wxLogMessage( "%s", logBuf.c_str() );
433
434 // you must call event skip if you want default grid processing
435 //
436 ev.Skip();
437 }
438
439
440 void GridFrame::OnCellLeftClick( wxGridEvent& ev )
441 {
442 logBuf = "";
443 logBuf << "Left click at row " << ev.GetRow()
444 << " col " << ev.GetCol();
445 wxLogMessage( "%s", logBuf.c_str() );
446
447 // you must call event skip if you want default grid processing
448 // (cell highlighting etc.)
449 //
450 ev.Skip();
451 }
452
453
454 void GridFrame::OnRowSize( wxGridSizeEvent& ev )
455 {
456 logBuf = "";
457 logBuf << "Resized row " << ev.GetRowOrCol();
458 wxLogMessage( "%s", logBuf.c_str() );
459
460 ev.Skip();
461 }
462
463
464 void GridFrame::OnColSize( wxGridSizeEvent& ev )
465 {
466 logBuf = "";
467 logBuf << "Resized col " << ev.GetRowOrCol();
468 wxLogMessage( "%s", logBuf.c_str() );
469
470 ev.Skip();
471 }
472
473
474 void GridFrame::OnSelectCell( wxGridEvent& ev )
475 {
476 logBuf = "";
477 logBuf << "Selected cell at row " << ev.GetRow()
478 << " col " << ev.GetCol();
479 wxLogMessage( "%s", logBuf.c_str() );
480
481 // you must call Skip() if you want the default processing
482 // to occur in wxGrid
483 ev.Skip();
484 }
485
486 void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev )
487 {
488 logBuf = "";
489 logBuf << "Selected cells from row " << ev.GetTopRow()
490 << " col " << ev.GetLeftCol()
491 << " to row " << ev.GetBottomRow()
492 << " col " << ev.GetRightCol();
493
494 wxLogMessage( "%s", logBuf.c_str() );
495
496 ev.Skip();
497 }
498
499 void GridFrame::OnCellValueChanged( wxGridEvent& ev )
500 {
501 logBuf = "";
502 logBuf << "Value changed for cell at"
503 << " row " << ev.GetRow()
504 << " col " << ev.GetCol();
505
506 wxLogMessage( "%s", logBuf.c_str() );
507
508 ev.Skip();
509 }
510
511
512 void GridFrame::About( wxCommandEvent& WXUNUSED(ev) )
513 {
514 (void)wxMessageBox( "\n\nwxGrid demo \n\n"
515 "Michael Bedward \n"
516 "mbedward@ozemail.com.au \n\n",
517 "About",
518 wxOK );
519 }
520
521
522 void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) )
523 {
524 Close( TRUE );
525 }
526