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