NIREP
|
00001 00002 // Name: griddemo.cpp 00003 // Purpose: Grid control wxWidgets sample 00004 // Author: Michael Bedward 00005 // Modified by: Santiago Palacios 00006 // RCS-ID: $Id: EditRDL.cxx,v 1.7 2011/01/13 06:33:39 chengzha Exp $ 00007 // Copyright: (c) Michael Bedward, Julian Smart, Vadim Zeitlin 00008 // Licence: wxWindows license 00010 00011 // ============================================================================ 00012 // declarations 00013 // ============================================================================ 00014 00015 // ---------------------------------------------------------------------------- 00016 // headers 00017 // ---------------------------------------------------------------------------- 00018 00019 // For compilers that support precompilation, includes "wx/wx.h". 00020 #include "wx/wxprec.h" 00021 00022 #ifdef __BORLANDC__ 00023 #pragma hdrstop 00024 #endif 00025 00026 #ifndef WX_PRECOMP 00027 #include "wx/wx.h" 00028 #endif 00029 00030 #include "wx/colordlg.h" 00031 #include "wx/fontdlg.h" 00032 #include "wx/numdlg.h" 00033 00034 #include "wx/grid.h" 00035 #include "wx/generic/gridctrl.h" 00036 00037 #include "wx/dataobj.h" // For clipboard 00038 #include "wx/clipbrd.h" // For clipboard 00039 00040 #include "EditRDL.h" 00041 00042 00043 // ---------------------------------------------------------------------------- 00044 // EditRDL 00045 // ---------------------------------------------------------------------------- 00046 00047 BEGIN_EVENT_TABLE( EditRDL, wxFrame ) 00048 00049 EVT_MENU( ID_AUTOSIZECOLS, EditRDL::AutoSizeCols ) 00050 EVT_MENU( ID_SETLABEL_FONT, EditRDL::SetLabelFont ) 00051 00052 EVT_MENU( ID_INSERTROW, EditRDL::InsertRow ) 00053 EVT_MENU( ID_INSERTCOL, EditRDL::InsertCol ) 00054 EVT_MENU( ID_DELETEROW, EditRDL::DeleteSelectedRows ) 00055 EVT_MENU( ID_DELETECOL, EditRDL::DeleteSelectedCols ) 00056 EVT_MENU( ID_CLEARGRID, EditRDL::ClearGrid ) 00057 00058 EVT_MENU( wxID_ABOUT, EditRDL::About ) 00059 EVT_MENU( wxID_EXIT, EditRDL::OnQuit ) 00060 00061 EVT_MENU( ID_DESELECT_ALL, EditRDL::DeselectAll) 00062 EVT_MENU( ID_SELECT_ALL, EditRDL::SelectAll) 00063 EVT_MENU( ID_SELECT_UNSELECT, EditRDL::OnAddToSelectToggle) 00064 EVT_MENU( ID_SHOW_SELECTION, EditRDL::OnShowSelection) 00065 00066 EVT_MENU( wxID_NEW, EditRDL::OnNew) 00067 EVT_MENU( wxID_OPEN, EditRDL::OnOpen) 00068 EVT_MENU( wxID_SAVE, EditRDL::OnSave) 00069 00070 EVT_MENU( wxID_COPY, EditRDL::DoCopyToClipboard) 00071 EVT_MENU( wxID_PASTE, EditRDL::DoPasteFromClipboard) 00072 00073 EVT_GRID_LABEL_LEFT_CLICK( EditRDL::OnLabelLeftClick ) 00074 EVT_GRID_CELL_LEFT_CLICK( EditRDL::OnCellLeftClick ) 00075 EVT_GRID_ROW_SIZE( EditRDL::OnRowSize ) 00076 EVT_GRID_COL_SIZE( EditRDL::OnColSize ) 00077 EVT_GRID_SELECT_CELL( EditRDL::OnSelectCell ) 00078 EVT_GRID_RANGE_SELECT( EditRDL::OnRangeSelected ) 00079 EVT_GRID_CELL_CHANGE( EditRDL::OnCellValueChanged ) 00080 EVT_GRID_CELL_BEGIN_DRAG( EditRDL::OnCellBeginDrag ) 00081 00082 END_EVENT_TABLE() 00083 00084 00085 EditRDL::EditRDL() 00086 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("Edit Resource Description List"), 00087 wxDefaultPosition, 00088 wxDefaultSize ) 00089 { 00090 // File menu 00091 wxMenu *fileMenu = new wxMenu; 00092 00093 fileMenu->Append( wxID_NEW, _T("&New\tCtrl-N")); 00094 fileMenu->Append( wxID_OPEN, _T("&Open\tCtrl-O")); 00095 fileMenu->AppendSeparator(); 00096 00097 fileMenu->Append( wxID_SAVE, _T("&Save\tCtrl-S")); 00098 fileMenu->AppendSeparator(); 00099 00100 fileMenu->Append( wxID_EXIT, _T("E&xit\tAlt-F4") ); 00101 00102 00103 // Edit menu 00104 wxMenu *editMenu = new wxMenu; 00105 editMenu->Append( wxID_UNDO, _T("Undo\tCtrl-Z")); 00106 editMenu->Enable( wxID_UNDO, false); 00107 editMenu->Append( wxID_REDO, _T("Redo\tCtrl-Y")); 00108 editMenu->Enable( wxID_REDO, false); 00109 editMenu->AppendSeparator(); 00110 00111 editMenu->Append( wxID_CUT, _T("Cut\tCtrl-X")); 00112 editMenu->Append( wxID_COPY, _T("Copy\tCtrl-C")); 00113 editMenu->Append( wxID_PASTE, _T("Paste\tCtrl-V")); 00114 editMenu->Append( wxID_DELETE, _T("Delete\tDel")); 00115 editMenu->AppendSeparator(); 00116 00117 editMenu->Append( ID_SELECT_ALL, _T("Select all\tCtrl-A")); 00118 editMenu->Append( ID_DESELECT_ALL, _T("Deselect all")); 00119 editMenu->AppendSeparator(); 00120 00121 editMenu->Append( ID_INSERTROW, _T("Insert &row") ); 00122 editMenu->Append( ID_INSERTCOL, _T("Insert &column") ); 00123 editMenu->Append( ID_DELETEROW, _T("Delete selected ro&ws") ); 00124 editMenu->Append( ID_DELETECOL, _T("Delete selected co&ls") ); 00125 editMenu->Append( ID_CLEARGRID, _T("Cl&ear grid cell contents") ); 00126 00127 00128 // View menu 00129 wxMenu *viewMenu = new wxMenu; 00130 viewMenu->Append( ID_AUTOSIZECOLS, _T("&Auto-size cols") ); 00131 viewMenu->AppendSeparator(); 00132 00133 viewMenu->Append( ID_SETLABEL_FONT, _T("Set label fo&nt...") ); 00134 00135 // Select menu 00136 wxMenu *selectMenu = new wxMenu; 00137 selectMenu->Append( ID_SELECT_UNSELECT, _T("Add new cells to the selection"), 00138 _T("When off, old selection is deselected before ") 00139 _T("selecting the new cells"), wxITEM_CHECK ); 00140 selectMenu->Append( ID_SHOW_SELECTION, 00141 _T("&Show current selection\tCtrl-Alt-S")); 00142 00143 // Help menu 00144 wxMenu *helpMenu = new wxMenu; 00145 helpMenu->Append( wxID_ABOUT, _T("&About Edit Resource Description List") ); 00146 00147 wxMenuBar *menuBar = new wxMenuBar; 00148 menuBar->Append( fileMenu, _T("&File") ); 00149 menuBar->Append( editMenu, _T("&Edit") ); 00150 menuBar->Append( viewMenu, _T("&View") ); 00151 menuBar->Append( selectMenu, _T("&Select") ); 00152 menuBar->Append( helpMenu, _T("&Help") ); 00153 00154 SetMenuBar( menuBar ); 00155 00156 m_addToSel = false; 00157 00158 grid = new wxGrid( this, 00159 wxID_ANY, 00160 wxPoint( 0, 0 ), 00161 wxSize( 400, 300 ) ); 00162 00163 #if wxUSE_LOG 00164 int gridW = 600, gridH = 300; 00165 int logW = gridW, logH = 100; 00166 00167 logWin = new wxTextCtrl( this, 00168 wxID_ANY, 00169 wxEmptyString, 00170 wxPoint( 0, gridH + 20 ), 00171 wxSize( logW, logH ), 00172 wxTE_MULTILINE ); 00173 00174 logger = new wxLogTextCtrl( logWin ); 00175 m_logOld = wxLog::SetActiveTarget( logger ); 00176 wxLog::SetTimestamp( wxEmptyString ); 00177 #endif // wxUSE_LOG 00178 00179 // this will create a grid and, by default, an associated grid 00180 // table for strings 00181 grid->CreateGrid( 0, 0 ); 00182 grid->AppendRows(100); 00183 grid->AppendCols(8); 00184 00185 00186 // Set values of column headings 00187 grid->SetColLabelValue(0, _T("Source\nNamespace") ); 00188 grid->SetColLabelValue(1, _T("Source") ); 00189 grid->SetColLabelValue(2, _T("Target\nNamespace") ); 00190 grid->SetColLabelValue(3, _T("Target") ); 00191 grid->SetColLabelValue(4, _T("Algorithm") ); 00192 grid->SetColLabelValue(5, _T("Data Type") ); 00193 grid->SetColLabelValue(6, _T("Transform Units") ); 00194 grid->SetColLabelValue(7, _T("Filname (Use semicolons for more than one file)") ); 00195 00196 // Automatically size columns to the width of the headings 00197 //grid->AutoSizeColumns(true); 00198 grid->AutoSize(); 00199 00200 // Prevent text from going outside of cells 00201 grid->SetDefaultCellOverflow(false); 00202 00203 //grid->HandleClipboardEvent() // get clipboard event 00204 00205 // Autowrap text 00206 //grid->SetDefaultEditor(new wxGridCellAutoWrapStringEditor); 00207 grid->SetDefaultRenderer(new wxGridCellAutoWrapStringRenderer); 00208 //grid->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer); 00209 //grid->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor); 00210 00211 00212 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); 00213 topSizer->Add( grid, 00214 1, 00215 wxEXPAND ); 00216 00217 #if wxUSE_LOG 00218 topSizer->Add( logWin, 00219 0, 00220 wxEXPAND ); 00221 #endif // wxUSE_LOG 00222 00223 SetAutoLayout(true); 00224 SetSizer( topSizer ); 00225 00226 topSizer->Fit( this ); 00227 00228 Centre(); 00229 } 00230 00231 00232 EditRDL::~EditRDL() 00233 { 00234 #if wxUSE_LOG 00235 delete wxLog::SetActiveTarget(m_logOld); 00236 #endif // wxUSE_LOG 00237 } 00238 00239 00240 void EditRDL::AutoSizeCols( wxCommandEvent& WXUNUSED(ev) ) 00241 { 00242 grid->AutoSizeColumns(); 00243 grid->Refresh(); 00244 } 00245 00246 00247 00248 void EditRDL::SetLabelFont( wxCommandEvent& WXUNUSED(ev) ) 00249 { 00250 wxFont font = wxGetFontFromUser(this); 00251 if ( font.Ok() ) 00252 { 00253 grid->SetLabelFont(font); 00254 } 00255 } 00256 00257 void EditRDL::OnNew( wxCommandEvent& WXUNUSED(ev) ) 00258 { 00259 00260 } 00261 00262 void EditRDL::OnOpen( wxCommandEvent& WXUNUSED(ev) ) { 00263 wxFileDialog * filedialog = new wxFileDialog(this, wxT("Choose a Resource Description List"), wxT(""), wxT("*.*"), wxT("*.*")); 00264 if ( filedialog == NULL ) { return; } 00265 if ( filedialog->ShowModal() == wxID_CANCEL ){ delete filedialog; return;} 00266 00267 wxString path = filedialog->GetPath(); 00268 if (m_rdl->Load(std::string(path.ToAscii())) != NULL){ 00269 // FillGridFromDOM(); need to write 00270 } else { 00271 wxString msg(wxT("Error: Could not load Resource Description List ")); 00272 msg += path; 00273 wxLogError(msg); 00274 }; 00275 00276 if (filedialog){ 00277 delete filedialog; 00278 } 00279 } 00280 00281 void EditRDL::OnSave( wxCommandEvent& WXUNUSED(ev) ) { 00282 wxFileDialog * filedialog = new wxFileDialog(this, wxT("Choose a file to save Resource Description List"), wxT(""), wxT("untitled.xml"), wxT("(*.xml)|*.xml"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT, wxDefaultPosition, wxDefaultSize, wxT("Save")); 00283 if ( filedialog == NULL ) { return; } 00284 if ( filedialog->ShowModal() == wxID_CANCEL ){ delete filedialog; return;} 00285 00286 // FillDOMFromGrid(); need to write 00287 wxString path = filedialog->GetPath(); 00288 if (m_rdl->Save(std::string(path.ToAscii())) == NULL){ 00289 wxString msg(wxT("Error: Could not save Resource Description List ")); 00290 msg += path; 00291 wxLogError(msg); 00292 }; 00293 00294 if (filedialog){ 00295 delete filedialog; 00296 } 00297 } 00298 00299 00300 00301 void EditRDL::InsertRow( wxCommandEvent& WXUNUSED(ev) ) 00302 { 00303 grid->InsertRows( grid->GetGridCursorRow(), 1 ); 00304 } 00305 00306 00307 void EditRDL::InsertCol( wxCommandEvent& WXUNUSED(ev) ) 00308 { 00309 grid->InsertCols( grid->GetGridCursorCol(), 1 ); 00310 } 00311 00312 00313 void EditRDL::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) ) 00314 { 00315 if ( grid->IsSelection() ) 00316 { 00317 grid->BeginBatch(); 00318 for ( int n = 0; n < grid->GetNumberRows(); ) 00319 { 00320 if ( grid->IsInSelection( n , 0 ) ) 00321 grid->DeleteRows( n, 1 ); 00322 else 00323 n++; 00324 } 00325 grid->EndBatch(); 00326 } 00327 } 00328 00329 00330 void EditRDL::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) ) 00331 { 00332 if ( grid->IsSelection() ) 00333 { 00334 grid->BeginBatch(); 00335 for ( int n = 0; n < grid->GetNumberCols(); ) 00336 { 00337 if ( grid->IsInSelection( 0 , n ) ) 00338 grid->DeleteCols( n, 1 ); 00339 else 00340 n++; 00341 } 00342 grid->EndBatch(); 00343 } 00344 } 00345 00346 00347 void EditRDL::ClearGrid( wxCommandEvent& WXUNUSED(ev) ) 00348 { 00349 grid->ClearGrid(); 00350 } 00351 00352 00353 void EditRDL::DeselectAll(wxCommandEvent& WXUNUSED(event)) 00354 { 00355 grid->ClearSelection(); 00356 } 00357 00358 00359 void EditRDL::SelectAll(wxCommandEvent& WXUNUSED(event)) 00360 { 00361 grid->SelectAll(); 00362 } 00363 00364 void EditRDL::OnAddToSelectToggle(wxCommandEvent& event) 00365 { 00366 m_addToSel = event.IsChecked(); 00367 } 00368 00369 void EditRDL::OnLabelLeftClick( wxGridEvent& ev ) 00370 { 00371 #if wxUSE_LOG 00372 wxString logBuf; 00373 if ( ev.GetRow() != -1 ) 00374 { 00375 logBuf << _T("Left click on row label ") << ev.GetRow(); 00376 } 00377 else if ( ev.GetCol() != -1 ) 00378 { 00379 logBuf << _T("Left click on col label ") << ev.GetCol(); 00380 } 00381 else 00382 { 00383 logBuf << _T("Left click on corner label"); 00384 } 00385 00386 if ( ev.ShiftDown() ) 00387 logBuf << _T(" (shift down)"); 00388 if ( ev.ControlDown() ) 00389 logBuf << _T(" (control down)"); 00390 wxLogMessage( wxT("%s"), logBuf.c_str() ); 00391 #endif 00392 00393 // you must call event skip if you want default grid processing 00394 // 00395 ev.Skip(); 00396 } 00397 00398 00399 void EditRDL::OnCellLeftClick( wxGridEvent& ev ) 00400 { 00401 #if wxUSE_LOG 00402 wxLogMessage(_T("Left click at row %d, col %d"), ev.GetRow(), ev.GetCol()); 00403 #endif 00404 00405 // you must call event skip if you want default grid processing 00406 // (cell highlighting etc.) 00407 // 00408 ev.Skip(); 00409 } 00410 00411 00412 void EditRDL::OnRowSize( wxGridSizeEvent& ev ) 00413 { 00414 #if wxUSE_LOG 00415 wxLogMessage(_T("Resized row %d"), ev.GetRowOrCol()); 00416 #endif 00417 00418 ev.Skip(); 00419 } 00420 00421 00422 void EditRDL::OnColSize( wxGridSizeEvent& ev ) 00423 { 00424 #if wxUSE_LOG 00425 wxLogMessage(_T("Resized col %d"), ev.GetRowOrCol()); 00426 #endif 00427 00428 ev.Skip(); 00429 } 00430 00431 00432 void EditRDL::OnShowSelection(wxCommandEvent& WXUNUSED(event)) 00433 { 00434 // max number of elements to dump -- otherwise it can take too much time 00435 static const size_t countMax = 100; 00436 00437 bool rows = false; 00438 00439 switch ( grid->GetSelectionMode() ) 00440 { 00441 case wxGrid::wxGridSelectCells: 00442 { 00443 const wxGridCellCoordsArray cells(grid->GetSelectedCells()); 00444 size_t count = cells.size(); 00445 wxLogMessage(_T("%lu cells selected:"), (unsigned long)count); 00446 if ( count > countMax ) 00447 { 00448 wxLogMessage(_T("[too many selected cells, ") 00449 _T("showing only the first %lu]"), 00450 (unsigned long)countMax); 00451 count = countMax; 00452 } 00453 00454 for ( size_t n = 0; n < count; n++ ) 00455 { 00456 const wxGridCellCoords& c = cells[n]; 00457 wxLogMessage(_T(" selected cell %lu: (%d, %d)"), 00458 (unsigned long)n, c.GetCol(), c.GetRow()); 00459 } 00460 } 00461 break; 00462 00463 case wxGrid::wxGridSelectRows: 00464 rows = true; 00465 // fall through 00466 00467 case wxGrid::wxGridSelectColumns: 00468 { 00469 const wxChar *plural, *single; 00470 if ( rows ) 00471 { 00472 plural = _T("rows"); 00473 single = _T("row"); 00474 } 00475 else // columns 00476 { 00477 plural = _T("columns"); 00478 single = _T("column"); 00479 } 00480 00481 // NB: extra parentheses needed to avoid bcc 5.82 errors 00482 const wxArrayInt sels((rows ? grid->GetSelectedRows() 00483 : grid->GetSelectedCols())); 00484 size_t count = sels.size(); 00485 wxLogMessage(_T("%lu %s selected:"), 00486 (unsigned long)count, plural); 00487 if ( count > countMax ) 00488 { 00489 wxLogMessage(_T("[too many selected %s, ") 00490 _T("showing only the first %lu]"), 00491 plural, (unsigned long)countMax); 00492 count = countMax; 00493 } 00494 00495 for ( size_t n = 0; n < count; n++ ) 00496 { 00497 wxLogMessage(_T(" selected %s %lu: %d"), 00498 single, (unsigned long)n, sels[n]); 00499 } 00500 } 00501 break; 00502 00503 default: 00504 wxFAIL_MSG( _T("unknown wxGrid selection mode") ); 00505 break; 00506 } 00507 } 00508 00509 void EditRDL::OnSelectCell( wxGridEvent& ev ) 00510 { 00511 #if wxUSE_LOG 00512 wxString logBuf; 00513 if ( ev.Selecting() ) 00514 logBuf << _T("Selected "); 00515 else 00516 logBuf << _T("Deselected "); 00517 logBuf << _T("cell at row ") << ev.GetRow() 00518 << _T(" col ") << ev.GetCol() 00519 << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F') 00520 << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F') 00521 << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F') 00522 << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )"); 00523 00524 //Indicate whether this column was moved 00525 if ( ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() ) != ev.GetCol() ) 00526 logBuf << _T(" *** Column moved, current position: ") << ((wxGrid *)ev.GetEventObject())->GetColPos( ev.GetCol() ); 00527 00528 wxLogMessage( wxT("%s"), logBuf.c_str() ); 00529 #endif 00530 00531 // you must call Skip() if you want the default processing 00532 // to occur in wxGrid 00533 ev.Skip(); 00534 } 00535 00536 void EditRDL::OnRangeSelected( wxGridRangeSelectEvent& ev ) 00537 { 00538 #if wxUSE_LOG 00539 wxString logBuf; 00540 if ( ev.Selecting() ) 00541 logBuf << _T("Selected "); 00542 else 00543 logBuf << _T("Deselected "); 00544 logBuf << _T("cells from row ") << ev.GetTopRow() 00545 << _T(" col ") << ev.GetLeftCol() 00546 << _T(" to row ") << ev.GetBottomRow() 00547 << _T(" col ") << ev.GetRightCol() 00548 << _T(" ( ControlDown: ")<< (ev.ControlDown() ? 'T':'F') 00549 << _T(", ShiftDown: ")<< (ev.ShiftDown() ? 'T':'F') 00550 << _T(", AltDown: ")<< (ev.AltDown() ? 'T':'F') 00551 << _T(", MetaDown: ")<< (ev.MetaDown() ? 'T':'F') << _T(" )"); 00552 wxLogMessage( wxT("%s"), logBuf.c_str() ); 00553 #endif 00554 00555 ev.Skip(); 00556 } 00557 00558 void EditRDL::OnCellValueChanged( wxGridEvent& ev ) 00559 { 00560 int row = ev.GetRow(), 00561 col = ev.GetCol(); 00562 00563 #if wxUSE_LOG 00564 wxLogMessage(_T("Value changed for cell at row %d, col %d: now \"%s\""), 00565 row, col, grid->GetCellValue(row, col).c_str()); 00566 #endif 00567 00568 ev.Skip(); 00569 } 00570 00571 void EditRDL::OnCellBeginDrag( wxGridEvent& ev ) 00572 { 00573 #if wxUSE_LOG 00574 wxLogMessage(_T("Got request to drag cell at row %d, col %d"), 00575 ev.GetRow(), ev.GetCol()); 00576 #endif 00577 00578 ev.Skip(); 00579 } 00580 00581 00582 void EditRDL::About( wxCommandEvent& WXUNUSED(ev) ) 00583 { 00584 (void)wxMessageBox( _T("\n\nEdit Resource Description List \n\n") 00585 _T("Copyright 2010, The Unversity of Iowa."), 00586 _T("About Edit Resource Description List"), 00587 wxOK ); 00588 } 00589 00590 00591 void EditRDL::OnQuit( wxCommandEvent& WXUNUSED(ev) ) 00592 { 00593 Close( true ); 00594 } 00595 00596 void EditRDL::DoCopyToClipboard( wxCommandEvent& event ){ 00597 // On X11, we want to get the data from the primary selection instead 00598 // of the normal clipboard (which isn't normal under X11 at all). This 00599 // call has no effect under MSW. 00600 wxTheClipboard->UsePrimarySelection(); 00601 00602 const int row = grid->GetGridCursorRow(); 00603 const int col = grid->GetGridCursorCol(); 00604 00605 wxString text = grid->GetCellValue(row, col); 00606 //grid->GetCellValue(row, col).c_str() 00607 00608 if (text.IsEmpty()){ 00609 wxLogMessage( _T("No text to copy.")); 00610 return; 00611 } 00612 00613 if (!wxTheClipboard->Open()){ 00614 wxLogMessage( _T("Error opening the clipboard.")); 00615 return; 00616 } else { 00617 wxLogMessage( _T("Successfully opened the clipboard.")); 00618 } 00619 00620 // Clear the clipboard before adding to the clipboard. 00621 // Not clearing the clipboard is a common mistake. 00622 // See http://www.clipboardextender.com/?page_id=7 00623 // You’re supposed to open the clipboard, erase the clipboard, add your data 00624 // (for all formats), close the clipboard. It’s simple - open, clear, update, close. 00625 wxTheClipboard->Clear(); 00626 00627 wxTextDataObject *data = new wxTextDataObject( text ); 00628 00629 if (!wxTheClipboard->SetData( data )){ 00630 wxLogMessage( _T("Error while copying to the clipboard.")); 00631 }else{ 00632 wxLogMessage( _T("Successfully copied data to the clipboard.")); 00633 } 00634 00635 wxTheClipboard->Close(); 00636 00637 wxLogMessage( _T("Closed the clipboard.")); 00638 } 00639 00640 void EditRDL::DoPasteFromClipboard( wxCommandEvent& event ){ 00641 // On X11, we want to get the data from the primary selection instead 00642 // of the normal clipboard (which isn't normal under X11 at all). This 00643 // call has no effect under MSW. 00644 wxTheClipboard->UsePrimarySelection(); 00645 00646 const int row = grid->GetGridCursorRow(); 00647 const int col = grid->GetGridCursorCol(); 00648 00651 //const wxGridCellCoordsArray selectedCells = grid->GetSelectedCells(); 00652 //wxArrayInt temp1 = grid->GetSelectedRows(); 00653 //wxArrayInt temp2 = grid->GetSelectedCols(); 00654 //wxGridCellCoordsArray temp3 = grid->GetSelectionBlockTopLeft(); 00655 //wxGridCellCoordsArray temp4 = grid->GetSelectionBlockBottomRight(); 00656 00657 00658 00659 if (!wxTheClipboard->Open()){ 00660 wxLogMessage( _T("Error opening the clipboard.")); 00661 return; 00662 }else{ 00663 wxLogMessage( _T("Successfully opened the clipboard.")); 00664 } 00665 00666 wxTextDataObject data; 00667 00668 if (wxTheClipboard->IsSupported( data.GetFormat() )){ 00669 wxLogMessage( _T("Clipboard supports requested format.")); 00670 00671 if (wxTheClipboard->GetData( data )){ 00672 wxLogMessage( _T("Successfully retrieved data from the clipboard.")); 00673 wxLogMessage( data.GetText()); 00674 00675 // Below is a hack since GetSelectedCells, GetSelectedRows, GetSelectedCols, 00676 // GetSelectionBlockTopLeft, GetSelectionBlockBottomRight does not work 00677 // But it does work for arbitrarily selected regions when pasting a single value. 00678 if ( grid->IsSelection() ){ 00679 grid->BeginBatch(); 00680 for(int j=0; j<grid->GetNumberCols(); j++) { 00681 for(int i=0; i<grid->GetNumberRows(); i++){ 00682 if ( grid->IsInSelection(i,j) ){ 00683 grid->SetCellValue( i, j, data.GetText() ); 00684 //wxLogMessage(_T("Value changed for cell at row %d, col %d: now \"%s\""), 00685 // i, j, grid->GetCellValue(i, j).c_str()); 00686 } 00687 } 00688 } 00689 grid->EndBatch(); 00690 } else { 00691 grid->SetCellValue( row, col, data.GetText() ); 00692 } 00693 00694 }else{ 00695 wxLogMessage( _T("Error getting data from the clipboard.")); 00696 } 00697 }else{ 00698 wxLogMessage( _T("Clipboard doesn't support requested format.")); 00699 } 00700 00701 wxTheClipboard->Close(); 00702 00703 wxLogMessage( _T("Closed the clipboard.")); 00704 00705 } 00706