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