| 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_TOGGLEROWSIZING, GridFrame::ToggleRowSizing ) |
| 72 | EVT_MENU( ID_TOGGLECOLSIZING, GridFrame::ToggleColSizing ) |
| 73 | EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour ) |
| 74 | EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour ) |
| 75 | EVT_MENU( ID_ROWLABELHORIZALIGN, GridFrame::SetRowLabelHorizAlignment ) |
| 76 | EVT_MENU( ID_ROWLABELVERTALIGN, GridFrame::SetRowLabelVertAlignment ) |
| 77 | EVT_MENU( ID_COLLABELHORIZALIGN, GridFrame::SetColLabelHorizAlignment ) |
| 78 | EVT_MENU( ID_COLLABELVERTALIGN, GridFrame::SetColLabelVertAlignment ) |
| 79 | EVT_MENU( ID_GRIDLINECOLOUR, GridFrame::SetGridLineColour ) |
| 80 | EVT_MENU( ID_INSERTROW, GridFrame::InsertRow ) |
| 81 | EVT_MENU( ID_INSERTCOL, GridFrame::InsertCol ) |
| 82 | EVT_MENU( ID_DELETEROW, GridFrame::DeleteSelectedRows ) |
| 83 | EVT_MENU( ID_DELETECOL, GridFrame::DeleteSelectedCols ) |
| 84 | EVT_MENU( ID_CLEARGRID, GridFrame::ClearGrid ) |
| 85 | |
| 86 | EVT_MENU( ID_SET_CELL_FG_COLOUR, GridFrame::SetCellFgColour ) |
| 87 | EVT_MENU( ID_SET_CELL_BG_COLOUR, GridFrame::SetCellBgColour ) |
| 88 | |
| 89 | EVT_MENU( ID_ABOUT, GridFrame::About ) |
| 90 | EVT_MENU( wxID_EXIT, GridFrame::OnQuit ) |
| 91 | EVT_MENU( ID_VTABLE, GridFrame::OnVTable) |
| 92 | EVT_MENU( ID_BUGS_TABLE, GridFrame::OnBugsTable) |
| 93 | |
| 94 | EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick ) |
| 95 | EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick ) |
| 96 | EVT_GRID_ROW_SIZE( GridFrame::OnRowSize ) |
| 97 | EVT_GRID_COL_SIZE( GridFrame::OnColSize ) |
| 98 | EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell ) |
| 99 | EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected ) |
| 100 | EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged ) |
| 101 | |
| 102 | EVT_GRID_EDITOR_SHOWN( GridFrame::OnEditorShown ) |
| 103 | EVT_GRID_EDITOR_HIDDEN( GridFrame::OnEditorHidden ) |
| 104 | END_EVENT_TABLE() |
| 105 | |
| 106 | |
| 107 | GridFrame::GridFrame() |
| 108 | : wxFrame( (wxFrame *)NULL, -1, "wxWindows grid class demo", |
| 109 | wxDefaultPosition, |
| 110 | wxDefaultSize ) |
| 111 | { |
| 112 | int gridW = 600, gridH = 300; |
| 113 | int logW = gridW, logH = 100; |
| 114 | |
| 115 | wxMenu *fileMenu = new wxMenu; |
| 116 | fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V"); |
| 117 | fileMenu->Append( ID_BUGS_TABLE, "&Bugs table test\tCtrl-B"); |
| 118 | fileMenu->AppendSeparator(); |
| 119 | fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" ); |
| 120 | |
| 121 | wxMenu *viewMenu = new wxMenu; |
| 122 | viewMenu->Append( ID_TOGGLEROWLABELS, "&Row labels", "", TRUE ); |
| 123 | viewMenu->Append( ID_TOGGLECOLLABELS, "&Col labels", "", TRUE ); |
| 124 | viewMenu->Append( ID_TOGGLEEDIT, "&Editable", "", TRUE ); |
| 125 | viewMenu->Append( ID_TOGGLEROWSIZING, "Ro&w drag-resize", "", TRUE ); |
| 126 | viewMenu->Append( ID_TOGGLECOLSIZING, "C&ol drag-resize", "", TRUE ); |
| 127 | |
| 128 | wxMenu *rowLabelMenu = new wxMenu; |
| 129 | |
| 130 | viewMenu->Append( ID_ROWLABELALIGN, "R&ow label alignment", |
| 131 | rowLabelMenu, |
| 132 | "Change alignment of row labels" ); |
| 133 | |
| 134 | rowLabelMenu->Append( ID_ROWLABELHORIZALIGN, "&Horizontal" ); |
| 135 | rowLabelMenu->Append( ID_ROWLABELVERTALIGN, "&Vertical" ); |
| 136 | |
| 137 | wxMenu *colLabelMenu = new wxMenu; |
| 138 | |
| 139 | viewMenu->Append( ID_COLLABELALIGN, "Col l&abel alignment", |
| 140 | colLabelMenu, |
| 141 | "Change alignment of col labels" ); |
| 142 | |
| 143 | colLabelMenu->Append( ID_COLLABELHORIZALIGN, "&Horizontal" ); |
| 144 | colLabelMenu->Append( ID_COLLABELVERTALIGN, "&Vertical" ); |
| 145 | |
| 146 | wxMenu *colMenu = new wxMenu; |
| 147 | colMenu->Append( ID_SETLABELCOLOUR, "Set &label colour" ); |
| 148 | colMenu->Append( ID_SETLABELTEXTCOLOUR, "Set label &text colour" ); |
| 149 | colMenu->Append( ID_GRIDLINECOLOUR, "&Grid line colour" ); |
| 150 | colMenu->Append( ID_SET_CELL_FG_COLOUR, "Set cell &foreground colour" ); |
| 151 | colMenu->Append( ID_SET_CELL_BG_COLOUR, "Set cell &background colour" ); |
| 152 | |
| 153 | wxMenu *editMenu = new wxMenu; |
| 154 | editMenu->Append( ID_INSERTROW, "Insert &row" ); |
| 155 | editMenu->Append( ID_INSERTCOL, "Insert &column" ); |
| 156 | editMenu->Append( ID_DELETEROW, "Delete selected ro&ws" ); |
| 157 | editMenu->Append( ID_DELETECOL, "Delete selected co&ls" ); |
| 158 | editMenu->Append( ID_CLEARGRID, "Cl&ear grid cell contents" ); |
| 159 | |
| 160 | wxMenu *helpMenu = new wxMenu; |
| 161 | helpMenu->Append( ID_ABOUT, "&About wxGrid demo" ); |
| 162 | |
| 163 | wxMenuBar *menuBar = new wxMenuBar; |
| 164 | menuBar->Append( fileMenu, "&File" ); |
| 165 | menuBar->Append( viewMenu, "&View" ); |
| 166 | menuBar->Append( colMenu, "&Colours" ); |
| 167 | menuBar->Append( editMenu, "&Edit" ); |
| 168 | menuBar->Append( helpMenu, "&Help" ); |
| 169 | |
| 170 | SetMenuBar( menuBar ); |
| 171 | |
| 172 | grid = new wxGrid( this, |
| 173 | -1, |
| 174 | wxPoint( 0, 0 ), |
| 175 | wxSize( 400, 300 ) ); |
| 176 | |
| 177 | logWin = new wxTextCtrl( this, |
| 178 | -1, |
| 179 | wxEmptyString, |
| 180 | wxPoint( 0, gridH + 20 ), |
| 181 | wxSize( logW, logH ), |
| 182 | wxTE_MULTILINE ); |
| 183 | |
| 184 | logger = new wxLogTextCtrl( logWin ); |
| 185 | logger->SetActiveTarget( logger ); |
| 186 | logger->SetTimestamp( NULL ); |
| 187 | |
| 188 | // this will create a grid and, by default, an associated grid |
| 189 | // table for string data |
| 190 | // |
| 191 | //grid->CreateGrid( 100, 100 ); |
| 192 | grid->SetTable(new SimpleTable(100, 100), TRUE); |
| 193 | |
| 194 | grid->SetRowSize( 0, 60 ); |
| 195 | grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" ); |
| 196 | |
| 197 | grid->SetCellValue( 0, 1, "Blah" ); |
| 198 | grid->SetCellValue( 0, 2, "Blah" ); |
| 199 | grid->SetCellValue( 0, 3, "Read only" ); |
| 200 | grid->SetReadOnly( 0, 3 ); |
| 201 | |
| 202 | grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" ); |
| 203 | |
| 204 | grid->SetRowSize( 99, 60 ); |
| 205 | grid->SetCellValue( 99, 99, "Ctrl+End\nwill go to\nthis cell" ); |
| 206 | |
| 207 | grid->SetCellValue(2, 2, "red"); |
| 208 | |
| 209 | grid->SetCellTextColour(2, 2, *wxRED); |
| 210 | grid->SetCellValue(3, 3, "green on grey"); |
| 211 | grid->SetCellTextColour(3, 3, *wxGREEN); |
| 212 | grid->SetCellBackgroundColour(3, 3, *wxLIGHT_GREY); |
| 213 | |
| 214 | grid->SetCellValue(4, 4, "a weird looking cell"); |
| 215 | grid->SetCellAlignment(4, 4, wxCENTRE, wxCENTRE); |
| 216 | grid->SetCellRenderer(4, 4, new MyGridCellRenderer); |
| 217 | |
| 218 | grid->SetCellValue(3, 0, "1"); |
| 219 | grid->SetCellRenderer(3, 0, new wxGridCellBoolRenderer); |
| 220 | grid->SetCellEditor(3, 0, new wxGridCellBoolEditor); |
| 221 | |
| 222 | wxGridCellAttr *attr; |
| 223 | attr = new wxGridCellAttr; |
| 224 | attr->SetTextColour(*wxBLUE); |
| 225 | grid->SetColAttr(5, attr); |
| 226 | attr = new wxGridCellAttr; |
| 227 | attr->SetBackgroundColour(*wxBLUE); |
| 228 | grid->SetRowAttr(5, attr); |
| 229 | |
| 230 | grid->SetCellValue(2, 4, "a wider column"); |
| 231 | grid->SetColSize(4, 120); |
| 232 | grid->SetColMinimalWidth(4, 120); |
| 233 | |
| 234 | wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); |
| 235 | topSizer->Add( grid, |
| 236 | 1, |
| 237 | wxEXPAND ); |
| 238 | |
| 239 | topSizer->Add( logWin, |
| 240 | 0, |
| 241 | wxEXPAND ); |
| 242 | |
| 243 | SetAutoLayout( TRUE ); |
| 244 | SetSizer( topSizer ); |
| 245 | |
| 246 | topSizer->Fit( this ); |
| 247 | topSizer->SetSizeHints( this ); |
| 248 | |
| 249 | Centre(); |
| 250 | SetDefaults(); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | GridFrame::~GridFrame() |
| 255 | { |
| 256 | } |
| 257 | |
| 258 | |
| 259 | void GridFrame::SetDefaults() |
| 260 | { |
| 261 | GetMenuBar()->Check( ID_TOGGLEROWLABELS, TRUE ); |
| 262 | GetMenuBar()->Check( ID_TOGGLECOLLABELS, TRUE ); |
| 263 | GetMenuBar()->Check( ID_TOGGLEEDIT, TRUE ); |
| 264 | GetMenuBar()->Check( ID_TOGGLEROWSIZING, TRUE ); |
| 265 | GetMenuBar()->Check( ID_TOGGLECOLSIZING, TRUE ); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | void GridFrame::ToggleRowLabels( wxCommandEvent& WXUNUSED(ev) ) |
| 270 | { |
| 271 | if ( GetMenuBar()->IsChecked( ID_TOGGLEROWLABELS ) ) |
| 272 | { |
| 273 | grid->SetRowLabelSize( grid->GetDefaultRowLabelSize() ); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | grid->SetRowLabelSize( 0 ); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | |
| 282 | void GridFrame::ToggleColLabels( wxCommandEvent& WXUNUSED(ev) ) |
| 283 | { |
| 284 | if ( GetMenuBar()->IsChecked( ID_TOGGLECOLLABELS ) ) |
| 285 | { |
| 286 | grid->SetColLabelSize( grid->GetDefaultColLabelSize() ); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | grid->SetColLabelSize( 0 ); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | void GridFrame::ToggleEditing( wxCommandEvent& WXUNUSED(ev) ) |
| 296 | { |
| 297 | grid->EnableEditing( |
| 298 | GetMenuBar()->IsChecked( ID_TOGGLEEDIT ) ); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | void GridFrame::ToggleRowSizing( wxCommandEvent& WXUNUSED(ev) ) |
| 303 | { |
| 304 | grid->EnableDragRowSize( |
| 305 | GetMenuBar()->IsChecked( ID_TOGGLEROWSIZING ) ); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | void GridFrame::ToggleColSizing( wxCommandEvent& WXUNUSED(ev) ) |
| 310 | { |
| 311 | grid->EnableDragColSize( |
| 312 | GetMenuBar()->IsChecked( ID_TOGGLECOLSIZING ) ); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | void GridFrame::SetLabelColour( wxCommandEvent& WXUNUSED(ev) ) |
| 317 | { |
| 318 | wxColourDialog dlg( NULL ); |
| 319 | if ( dlg.ShowModal() == wxID_OK ) |
| 320 | { |
| 321 | wxColourData retData; |
| 322 | retData = dlg.GetColourData(); |
| 323 | wxColour colour = retData.GetColour(); |
| 324 | |
| 325 | grid->SetLabelBackgroundColour( colour ); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | void GridFrame::SetLabelTextColour( wxCommandEvent& WXUNUSED(ev) ) |
| 331 | { |
| 332 | wxColourDialog dlg( NULL ); |
| 333 | if ( dlg.ShowModal() == wxID_OK ) |
| 334 | { |
| 335 | wxColourData retData; |
| 336 | retData = dlg.GetColourData(); |
| 337 | wxColour colour = retData.GetColour(); |
| 338 | |
| 339 | grid->SetLabelTextColour( colour ); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | void GridFrame::SetRowLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) ) |
| 345 | { |
| 346 | int horiz, vert; |
| 347 | grid->GetRowLabelAlignment( &horiz, &vert ); |
| 348 | |
| 349 | switch ( horiz ) |
| 350 | { |
| 351 | case wxLEFT: |
| 352 | horiz = wxCENTRE; |
| 353 | break; |
| 354 | |
| 355 | case wxCENTRE: |
| 356 | horiz = wxRIGHT; |
| 357 | break; |
| 358 | |
| 359 | case wxRIGHT: |
| 360 | horiz = wxLEFT; |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | grid->SetRowLabelAlignment( horiz, -1 ); |
| 365 | } |
| 366 | |
| 367 | void GridFrame::SetRowLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) ) |
| 368 | { |
| 369 | int horiz, vert; |
| 370 | grid->GetRowLabelAlignment( &horiz, &vert ); |
| 371 | |
| 372 | switch ( vert ) |
| 373 | { |
| 374 | case wxTOP: |
| 375 | vert = wxCENTRE; |
| 376 | break; |
| 377 | |
| 378 | case wxCENTRE: |
| 379 | vert = wxBOTTOM; |
| 380 | break; |
| 381 | |
| 382 | case wxBOTTOM: |
| 383 | vert = wxTOP; |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | grid->SetRowLabelAlignment( -1, vert ); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | void GridFrame::SetColLabelHorizAlignment( wxCommandEvent& WXUNUSED(ev) ) |
| 392 | { |
| 393 | int horiz, vert; |
| 394 | grid->GetColLabelAlignment( &horiz, &vert ); |
| 395 | |
| 396 | switch ( horiz ) |
| 397 | { |
| 398 | case wxLEFT: |
| 399 | horiz = wxCENTRE; |
| 400 | break; |
| 401 | |
| 402 | case wxCENTRE: |
| 403 | horiz = wxRIGHT; |
| 404 | break; |
| 405 | |
| 406 | case wxRIGHT: |
| 407 | horiz = wxLEFT; |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | grid->SetColLabelAlignment( horiz, -1 ); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | void GridFrame::SetColLabelVertAlignment( wxCommandEvent& WXUNUSED(ev) ) |
| 416 | { |
| 417 | int horiz, vert; |
| 418 | grid->GetColLabelAlignment( &horiz, &vert ); |
| 419 | |
| 420 | switch ( vert ) |
| 421 | { |
| 422 | case wxTOP: |
| 423 | vert = wxCENTRE; |
| 424 | break; |
| 425 | |
| 426 | case wxCENTRE: |
| 427 | vert = wxBOTTOM; |
| 428 | break; |
| 429 | |
| 430 | case wxBOTTOM: |
| 431 | vert = wxTOP; |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | grid->SetColLabelAlignment( -1, vert ); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | void GridFrame::SetGridLineColour( wxCommandEvent& WXUNUSED(ev) ) |
| 440 | { |
| 441 | wxColourDialog dlg( NULL ); |
| 442 | if ( dlg.ShowModal() == wxID_OK ) |
| 443 | { |
| 444 | wxColourData retData; |
| 445 | retData = dlg.GetColourData(); |
| 446 | wxColour colour = retData.GetColour(); |
| 447 | |
| 448 | grid->SetGridLineColour( colour ); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | void GridFrame::InsertRow( wxCommandEvent& WXUNUSED(ev) ) |
| 454 | { |
| 455 | grid->InsertRows( grid->GetGridCursorRow(), 1 ); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | void GridFrame::InsertCol( wxCommandEvent& WXUNUSED(ev) ) |
| 460 | { |
| 461 | grid->InsertCols( grid->GetGridCursorCol(), 1 ); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) ) |
| 466 | { |
| 467 | if ( grid->IsSelection() ) |
| 468 | { |
| 469 | int topRow, bottomRow, leftCol, rightCol; |
| 470 | grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol ); |
| 471 | grid->DeleteRows( topRow, bottomRow - topRow + 1 ); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | |
| 476 | void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) ) |
| 477 | { |
| 478 | if ( grid->IsSelection() ) |
| 479 | { |
| 480 | int topRow, bottomRow, leftCol, rightCol; |
| 481 | grid->GetSelection( &topRow, &leftCol, &bottomRow, &rightCol ); |
| 482 | grid->DeleteCols( leftCol, rightCol - leftCol + 1 ); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | |
| 487 | void GridFrame::ClearGrid( wxCommandEvent& WXUNUSED(ev) ) |
| 488 | { |
| 489 | grid->ClearGrid(); |
| 490 | } |
| 491 | |
| 492 | void GridFrame::SetCellFgColour( wxCommandEvent& WXUNUSED(ev) ) |
| 493 | { |
| 494 | wxColour col = wxGetColourFromUser(this); |
| 495 | if ( col.Ok() ) |
| 496 | { |
| 497 | grid->SetDefaultCellTextColour(col); |
| 498 | grid->Refresh(); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void GridFrame::SetCellBgColour( wxCommandEvent& WXUNUSED(ev) ) |
| 503 | { |
| 504 | wxColour col = wxGetColourFromUser(this); |
| 505 | if ( col.Ok() ) |
| 506 | { |
| 507 | grid->SetDefaultCellBackgroundColour(col); |
| 508 | grid->Refresh(); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | void GridFrame::OnLabelLeftClick( wxGridEvent& ev ) |
| 513 | { |
| 514 | logBuf = ""; |
| 515 | if ( ev.GetRow() != -1 ) |
| 516 | { |
| 517 | logBuf << "Left click on row label " << ev.GetRow(); |
| 518 | } |
| 519 | else if ( ev.GetCol() != -1 ) |
| 520 | { |
| 521 | logBuf << "Left click on col label " << ev.GetCol(); |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | logBuf << "Left click on corner label"; |
| 526 | } |
| 527 | |
| 528 | if ( ev.ShiftDown() ) logBuf << " (shift down)"; |
| 529 | wxLogMessage( "%s", logBuf.c_str() ); |
| 530 | |
| 531 | // you must call event skip if you want default grid processing |
| 532 | // |
| 533 | ev.Skip(); |
| 534 | } |
| 535 | |
| 536 | |
| 537 | void GridFrame::OnCellLeftClick( wxGridEvent& ev ) |
| 538 | { |
| 539 | logBuf = ""; |
| 540 | logBuf << "Left click at row " << ev.GetRow() |
| 541 | << " col " << ev.GetCol(); |
| 542 | wxLogMessage( "%s", logBuf.c_str() ); |
| 543 | |
| 544 | // you must call event skip if you want default grid processing |
| 545 | // (cell highlighting etc.) |
| 546 | // |
| 547 | ev.Skip(); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | void GridFrame::OnRowSize( wxGridSizeEvent& ev ) |
| 552 | { |
| 553 | logBuf = ""; |
| 554 | logBuf << "Resized row " << ev.GetRowOrCol(); |
| 555 | wxLogMessage( "%s", logBuf.c_str() ); |
| 556 | |
| 557 | ev.Skip(); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | void GridFrame::OnColSize( wxGridSizeEvent& ev ) |
| 562 | { |
| 563 | logBuf = ""; |
| 564 | logBuf << "Resized col " << ev.GetRowOrCol(); |
| 565 | wxLogMessage( "%s", logBuf.c_str() ); |
| 566 | |
| 567 | ev.Skip(); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | void GridFrame::OnSelectCell( wxGridEvent& ev ) |
| 572 | { |
| 573 | logBuf = ""; |
| 574 | logBuf << "Selected cell at row " << ev.GetRow() |
| 575 | << " col " << ev.GetCol(); |
| 576 | wxLogMessage( "%s", logBuf.c_str() ); |
| 577 | |
| 578 | // you must call Skip() if you want the default processing |
| 579 | // to occur in wxGrid |
| 580 | ev.Skip(); |
| 581 | } |
| 582 | |
| 583 | void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev ) |
| 584 | { |
| 585 | logBuf = ""; |
| 586 | logBuf << "Selected cells from row " << ev.GetTopRow() |
| 587 | << " col " << ev.GetLeftCol() |
| 588 | << " to row " << ev.GetBottomRow() |
| 589 | << " col " << ev.GetRightCol(); |
| 590 | |
| 591 | wxLogMessage( "%s", logBuf.c_str() ); |
| 592 | |
| 593 | ev.Skip(); |
| 594 | } |
| 595 | |
| 596 | void GridFrame::OnCellValueChanged( wxGridEvent& ev ) |
| 597 | { |
| 598 | logBuf = ""; |
| 599 | logBuf << "Value changed for cell at" |
| 600 | << " row " << ev.GetRow() |
| 601 | << " col " << ev.GetCol(); |
| 602 | |
| 603 | wxLogMessage( "%s", logBuf.c_str() ); |
| 604 | |
| 605 | ev.Skip(); |
| 606 | } |
| 607 | |
| 608 | void GridFrame::OnEditorShown( wxGridEvent& ev ) |
| 609 | { |
| 610 | wxLogMessage( "Cell editor shown." ); |
| 611 | |
| 612 | ev.Skip(); |
| 613 | } |
| 614 | |
| 615 | void GridFrame::OnEditorHidden( wxGridEvent& ev ) |
| 616 | { |
| 617 | wxLogMessage( "Cell editor hidden." ); |
| 618 | |
| 619 | ev.Skip(); |
| 620 | } |
| 621 | |
| 622 | void GridFrame::About( wxCommandEvent& WXUNUSED(ev) ) |
| 623 | { |
| 624 | (void)wxMessageBox( "\n\nwxGrid demo \n\n" |
| 625 | "Michael Bedward \n" |
| 626 | "mbedward@ozemail.com.au \n\n", |
| 627 | "About", |
| 628 | wxOK ); |
| 629 | } |
| 630 | |
| 631 | |
| 632 | void GridFrame::OnQuit( wxCommandEvent& WXUNUSED(ev) ) |
| 633 | { |
| 634 | Close( TRUE ); |
| 635 | } |
| 636 | |
| 637 | void GridFrame::OnBugsTable(wxCommandEvent& ) |
| 638 | { |
| 639 | BugsGridFrame *frame = new BugsGridFrame; |
| 640 | frame->Show(TRUE); |
| 641 | } |
| 642 | |
| 643 | void GridFrame::OnVTable(wxCommandEvent& ) |
| 644 | { |
| 645 | static long s_sizeGrid = 10000; |
| 646 | |
| 647 | s_sizeGrid = wxGetNumberFromUser("Size of the table to create", |
| 648 | "Size: ", |
| 649 | "wxGridDemo question", |
| 650 | s_sizeGrid, |
| 651 | 0, 32000, this); |
| 652 | if ( s_sizeGrid != -1 ) |
| 653 | { |
| 654 | BigGridFrame* win = new BigGridFrame(s_sizeGrid); |
| 655 | win->Show(TRUE); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // ---------------------------------------------------------------------------- |
| 660 | // MyGridCellRenderer |
| 661 | // ---------------------------------------------------------------------------- |
| 662 | |
| 663 | // do something that the default renderer doesn't here just to show that it is |
| 664 | // possible to alter the appearance of the cell beyond what the attributes |
| 665 | // allow |
| 666 | void MyGridCellRenderer::Draw(wxGrid& grid, |
| 667 | wxGridCellAttr& attr, |
| 668 | wxDC& dc, |
| 669 | const wxRect& rect, |
| 670 | int row, int col, |
| 671 | bool isSelected) |
| 672 | { |
| 673 | wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); |
| 674 | |
| 675 | dc.SetPen(*wxGREEN_PEN); |
| 676 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 677 | dc.DrawEllipse(rect); |
| 678 | } |
| 679 | |
| 680 | |
| 681 | // ---------------------------------------------------------------------------- |
| 682 | // BigGridFrame and BigGridTable: Sample of a non-standard table |
| 683 | // ---------------------------------------------------------------------------- |
| 684 | |
| 685 | BigGridFrame::BigGridFrame(long sizeGrid) |
| 686 | : wxFrame(NULL, -1, "Plugin Virtual Table", |
| 687 | wxDefaultPosition, wxSize(500, 450)) |
| 688 | { |
| 689 | m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize); |
| 690 | m_table = new BigGridTable(sizeGrid); |
| 691 | m_grid->SetTable(m_table, TRUE); |
| 692 | } |
| 693 | |
| 694 | // ---------------------------------------------------------------------------- |
| 695 | // BugsGridFrame: a "realistic" table |
| 696 | // ---------------------------------------------------------------------------- |
| 697 | |
| 698 | BugsGridFrame::BugsGridFrame() |
| 699 | : wxFrame(NULL, -1, "Bugs table", |
| 700 | wxDefaultPosition, wxSize(500, 300)) |
| 701 | { |
| 702 | enum Severity |
| 703 | { |
| 704 | Wish, |
| 705 | Minor, |
| 706 | Normal, |
| 707 | Major, |
| 708 | Critical |
| 709 | }; |
| 710 | |
| 711 | static const wxChar* severities[] = |
| 712 | { |
| 713 | _T("wishlist"), |
| 714 | _T("minor"), |
| 715 | _T("normal"), |
| 716 | _T("major"), |
| 717 | _T("critical"), |
| 718 | }; |
| 719 | |
| 720 | static const struct GridData |
| 721 | { |
| 722 | int id; |
| 723 | const wxChar *summary; |
| 724 | Severity severity; |
| 725 | int prio; |
| 726 | const wxChar *platform; |
| 727 | bool opened; |
| 728 | } data [] = |
| 729 | { |
| 730 | { 18, _T("foo doesn't work"), Major, 1, _T("wxMSW"), TRUE }, |
| 731 | { 27, _T("bar crashes"), Critical, 1, _T("all"), FALSE }, |
| 732 | { 45, _T("printing is slow"), Minor, 3, _T("wxMSW"), TRUE }, |
| 733 | { 68, _T("Rectangle() fails"), Normal, 1, _T("wxMSW"), FALSE }, |
| 734 | }; |
| 735 | |
| 736 | static const wxChar *headers[] = |
| 737 | { |
| 738 | _T("Id"), |
| 739 | _T("Summary"), |
| 740 | _T("Severity"), |
| 741 | _T("Priority"), |
| 742 | _T("Platform"), |
| 743 | _T("Opened?"), |
| 744 | }; |
| 745 | |
| 746 | // TODO the correct data type must be used for each column |
| 747 | |
| 748 | wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition); |
| 749 | wxGridTableBase *table = |
| 750 | new wxGridStringTable(WXSIZEOF(data), WXSIZEOF(headers)); |
| 751 | for ( size_t row = 0; row < WXSIZEOF(data); row++ ) |
| 752 | { |
| 753 | const GridData& gd = data[row]; |
| 754 | table->SetValue(row, 0, wxString::Format("%d", gd.id)); |
| 755 | table->SetValue(row, 1, gd.summary); |
| 756 | table->SetValue(row, 2, severities[gd.severity]); |
| 757 | table->SetValue(row, 3, wxString::Format("%d", gd.prio)); |
| 758 | table->SetValue(row, 4, gd.platform); |
| 759 | table->SetValue(row, 5, gd.opened ? _T("True") : wxEmptyString); |
| 760 | } |
| 761 | |
| 762 | for ( size_t col = 0; col < WXSIZEOF(headers); col++ ) |
| 763 | { |
| 764 | table->SetColLabelValue(col, headers[col]); |
| 765 | } |
| 766 | |
| 767 | grid->SetTable(table, TRUE); |
| 768 | } |