LibreOffice Module sc (master)  1
viewfun4.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <config_features.h>
21 
22 #include <memory>
23 #include <editeng/eeitem.hxx>
24 
25 #include <editeng/editobj.hxx>
26 #include <editeng/editstat.hxx>
27 #include <editeng/editview.hxx>
28 #include <editeng/flditem.hxx>
29 #include <sot/storage.hxx>
30 #include <svx/hlnkitem.hxx>
31 #include <editeng/unolingu.hxx>
32 
33 #include <sfx2/bindings.hxx>
34 #include <sfx2/dispatch.hxx>
35 #include <sfx2/docfile.hxx>
36 #include <sfx2/docfilt.hxx>
37 #include <sfx2/fcontnr.hxx>
38 #include <svtools/langtab.hxx>
39 #include <vcl/graphicfilter.hxx>
40 #include <svl/stritem.hxx>
41 #include <vcl/transfer.hxx>
42 #include <svl/urlbmk.hxx>
43 #include <svl/sharedstringpool.hxx>
44 #include <vcl/svapp.hxx>
45 #include <vcl/weld.hxx>
46 #include <avmedia/mediawindow.hxx>
47 
49 
50 #include <viewfunc.hxx>
51 #include <docsh.hxx>
52 #include <document.hxx>
53 #include <globstr.hrc>
54 #include <global.hxx>
55 #include <scresid.hxx>
56 #include <undoblk.hxx>
57 #include <undocell.hxx>
58 #include <formulacell.hxx>
59 #include <scmod.hxx>
60 #include <spelleng.hxx>
61 #include <patattr.hxx>
62 #include <tabvwsh.hxx>
63 #include <impex.hxx>
64 #include <editutil.hxx>
65 #include <editable.hxx>
66 #include <dociter.hxx>
67 #include <reffind.hxx>
68 #include <compiler.hxx>
69 #include <tokenarray.hxx>
70 #include <refupdatecontext.hxx>
71 #include <gridwin.hxx>
72 #include <refundo.hxx>
73 
74 using namespace com::sun::star;
75 
76 void ScViewFunc::PasteRTF( SCCOL nStartCol, SCROW nStartRow,
77  const css::uno::Reference< css::datatransfer::XTransferable >& rxTransferable )
78 {
79  TransferableDataHelper aDataHelper( rxTransferable );
80  if ( aDataHelper.HasFormat( SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT ) )
81  {
82  HideAllCursors();
83 
84  ScDocShell* pDocSh = GetViewData().GetDocShell();
85  ScDocument& rDoc = pDocSh->GetDocument();
86  SCTAB nTab = GetViewData().GetTabNo();
87  const bool bRecord (rDoc.IsUndoEnabled());
88 
89  const ScPatternAttr* pPattern = rDoc.GetPattern( nStartCol, nStartRow, nTab );
90  std::unique_ptr<ScTabEditEngine> pEngine(new ScTabEditEngine( *pPattern, rDoc.GetEnginePool(), &rDoc ));
91  pEngine->EnableUndo( false );
92 
93  vcl::Window* pActWin = GetActiveWin();
94  if (pActWin)
95  {
96  pEngine->SetPaperSize(Size(100000,100000));
98  EditView aEditView( pEngine.get(), aWin.get() );
99  aEditView.SetOutputArea(tools::Rectangle(0,0,100000,100000));
100 
101  // same method now for clipboard or drag&drop
102  // mba: clipboard always must contain absolute URLs (could be from alien source)
103  aEditView.InsertText( rxTransferable, OUString(), true );
104  }
105 
106  sal_Int32 nParCnt = pEngine->GetParagraphCount();
107  if (nParCnt)
108  {
109  SCROW nEndRow = nStartRow + static_cast<SCROW>(nParCnt) - 1;
110  if (nEndRow > rDoc.MaxRow())
111  nEndRow = rDoc.MaxRow();
112 
113  ScDocumentUniquePtr pUndoDoc;
114  if (bRecord)
115  {
116  pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
117  pUndoDoc->InitUndo( rDoc, nTab, nTab );
118  rDoc.CopyToDocument( nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab, InsertDeleteFlags::ALL, false, *pUndoDoc );
119  }
120 
121  SCROW nRow = nStartRow;
122 
123  // Temporarily turn off undo generation for this lot
124  bool bUndoEnabled = rDoc.IsUndoEnabled();
125  rDoc.EnableUndo( false );
126  for( sal_Int32 n = 0; n < nParCnt; n++ )
127  {
128  std::unique_ptr<EditTextObject> pObject(pEngine->CreateTextObject(n));
129  EnterData(nStartCol, nRow, nTab, *pObject, true);
130  if( ++nRow > rDoc.MaxRow() )
131  break;
132  }
133  rDoc.EnableUndo(bUndoEnabled);
134 
135  if (bRecord)
136  {
138  pRedoDoc->InitUndo( rDoc, nTab, nTab );
139  rDoc.CopyToDocument( nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab, InsertDeleteFlags::ALL|InsertDeleteFlags::NOCAPTIONS, false, *pRedoDoc );
140 
141  ScRange aMarkRange(nStartCol, nStartRow, nTab, nStartCol, nEndRow, nTab);
142  ScMarkData aDestMark(rDoc.GetSheetLimits());
143  aDestMark.SetMarkArea( aMarkRange );
144  pDocSh->GetUndoManager()->AddUndoAction(
145  std::make_unique<ScUndoPaste>( pDocSh, aMarkRange, aDestMark,
146  std::move(pUndoDoc), std::move(pRedoDoc), InsertDeleteFlags::ALL, nullptr));
147  }
148  }
149 
150  pEngine.reset();
151 
152  ShowAllCursors();
153  }
154  else
155  {
156  HideAllCursors();
157  ScDocShell* pDocSh = GetViewData().GetDocShell();
158  ScImportExport aImpEx( pDocSh->GetDocument(),
159  ScAddress( nStartCol, nStartRow, GetViewData().GetTabNo() ) );
160 
161  OUString aStr;
163  if ( aDataHelper.GetSotStorageStream( SotClipboardFormatId::RTF, xStream ) && xStream.is() )
164  // mba: clipboard always must contain absolute URLs (could be from alien source)
165  aImpEx.ImportStream( *xStream, OUString(), SotClipboardFormatId::RTF );
166  else if ( aDataHelper.GetString( SotClipboardFormatId::RTF, aStr ) )
167  aImpEx.ImportString( aStr, SotClipboardFormatId::RTF );
168  else if ( aDataHelper.GetSotStorageStream( SotClipboardFormatId::RICHTEXT, xStream ) && xStream.is() )
169  aImpEx.ImportStream( *xStream, OUString(), SotClipboardFormatId::RICHTEXT );
170  else if ( aDataHelper.GetString( SotClipboardFormatId::RICHTEXT, aStr ) )
171  aImpEx.ImportString( aStr, SotClipboardFormatId::RICHTEXT );
172 
173  AdjustRowHeight( nStartRow, aImpEx.GetRange().aEnd.Row() );
174  pDocSh->UpdateOle(GetViewData());
175  ShowAllCursors();
176  }
177 }
179 {
180  ScDocument& rDoc = GetViewData().GetDocument();
181  ScMarkData& rMark = GetViewData().GetMarkData();
182  SCTAB nTabCount = rDoc.GetTableCount();
183  bool bRecord = true;
184  if (!rDoc.IsUndoEnabled())
185  bRecord = false;
186 
187  ScRange aMarkRange;
188  rMark.MarkToSimple();
189  bool bMulti = rMark.IsMultiMarked();
190  if (bMulti)
191  rMark.GetMultiMarkArea( aMarkRange );
192  else if (rMark.IsMarked())
193  rMark.GetMarkArea( aMarkRange );
194  else
195  {
196  aMarkRange = ScRange( GetViewData().GetCurX(),
197  GetViewData().GetCurY(), GetViewData().GetTabNo() );
198  }
199  ScEditableTester aTester( rDoc, aMarkRange.aStart.Col(), aMarkRange.aStart.Row(),
200  aMarkRange.aEnd.Col(), aMarkRange.aEnd.Row(),rMark );
201  if (!aTester.IsEditable())
202  {
203  ErrorMessage(aTester.GetMessageId());
204  return;
205  }
206 
207  ScDocShell* pDocSh = GetViewData().GetDocShell();
208  bool bOk = false;
209 
210  ScDocumentUniquePtr pUndoDoc;
211  if (bRecord)
212  {
213  pUndoDoc.reset( new ScDocument( SCDOCMODE_UNDO ) );
214  SCTAB nTab = aMarkRange.aStart.Tab();
215  pUndoDoc->InitUndo( rDoc, nTab, nTab );
216 
217  if ( rMark.GetSelectCount() > 1 )
218  {
219  for (const auto& rTab : rMark)
220  if ( rTab != nTab )
221  pUndoDoc->AddUndoTab( rTab, rTab );
222  }
223  ScRange aCopyRange = aMarkRange;
224  aCopyRange.aStart.SetTab(0);
225  aCopyRange.aEnd.SetTab(nTabCount-1);
226  rDoc.CopyToDocument( aCopyRange, InsertDeleteFlags::ALL, bMulti, *pUndoDoc, &rMark );
227  }
228 
229  ScRangeListRef xRanges;
230  GetViewData().GetMultiArea( xRanges );
231  size_t nCount = xRanges->size();
232 
233  for (const SCTAB& i : rMark)
234  {
235  for (size_t j = 0; j < nCount; ++j)
236  {
237  ScRange aRange = (*xRanges)[j];
238  aRange.aStart.SetTab(i);
239  aRange.aEnd.SetTab(i);
240  ScCellIterator aIter( rDoc, aRange );
241  for (bool bHas = aIter.first(); bHas; bHas = aIter.next())
242  {
243  if (aIter.getType() != CELLTYPE_FORMULA)
244  continue;
245 
246  ScFormulaCell* pCell = aIter.getFormulaCell();
247  ScMatrixMode eMatrixMode = pCell->GetMatrixFlag();
248  if (eMatrixMode == ScMatrixMode::Reference)
249  continue;
250 
251  OUString aOld;
252  pCell->GetFormula(aOld);
253  sal_Int32 nLen = aOld.getLength();
254  if (eMatrixMode == ScMatrixMode::Formula)
255  {
256  assert(nLen >= 2 && aOld[0] == '{' && aOld[nLen-1] == '}');
257  nLen -= 2;
258  aOld = aOld.copy( 1, nLen);
259  }
260  ScRefFinder aFinder( aOld, aIter.GetPos(), rDoc, rDoc.GetAddressConvention() );
261  aFinder.ToggleRel( 0, nLen );
262  if (aFinder.GetFound())
263  {
264  ScAddress aPos = pCell->aPos;
265  const OUString& aNew = aFinder.GetText();
266  ScCompiler aComp( rDoc, aPos, rDoc.GetGrammar());
267  std::unique_ptr<ScTokenArray> pArr(aComp.CompileString(aNew));
268  ScFormulaCell* pNewCell =
269  new ScFormulaCell(
270  rDoc, aPos, *pArr, formula::FormulaGrammar::GRAM_DEFAULT, eMatrixMode);
271 
272  rDoc.SetFormulaCell(aPos, pNewCell);
273  bOk = true;
274  }
275  }
276  }
277  }
278  if (bRecord)
279  {
281  SCTAB nTab = aMarkRange.aStart.Tab();
282  pRedoDoc->InitUndo( rDoc, nTab, nTab );
283 
284  if ( rMark.GetSelectCount() > 1 )
285  {
286  for (const auto& rTab : rMark)
287  if ( rTab != nTab )
288  pRedoDoc->AddUndoTab( rTab, rTab );
289  }
290  ScRange aCopyRange = aMarkRange;
291  aCopyRange.aStart.SetTab(0);
292  aCopyRange.aEnd.SetTab(nTabCount-1);
293  rDoc.CopyToDocument( aCopyRange, InsertDeleteFlags::ALL, bMulti, *pRedoDoc, &rMark );
294 
295  pDocSh->GetUndoManager()->AddUndoAction(
296  std::make_unique<ScUndoRefConversion>( pDocSh,
297  aMarkRange, rMark, std::move(pUndoDoc), std::move(pRedoDoc), bMulti) );
298  }
299 
300  pDocSh->PostPaint( aMarkRange, PaintPartFlags::Grid );
301  pDocSh->UpdateOle(GetViewData());
302  pDocSh->SetDocumentModified();
303  CellContentChanged();
304 
305  if (!bOk)
306  ErrorMessage(STR_ERR_NOREF);
307 }
308 // Thesaurus - Undo ok
310 {
311  SCCOL nCol;
312  SCROW nRow;
313  SCTAB nTab;
314  ScDocShell* pDocSh = GetViewData().GetDocShell();
315  ScDocument& rDoc = pDocSh->GetDocument();
316  ScMarkData& rMark = GetViewData().GetMarkData();
317  ScSplitPos eWhich = GetViewData().GetActivePart();
318  EESpellState eState;
319  EditView* pEditView = nullptr;
320  std::unique_ptr<ESelection> pEditSel;
321  std::unique_ptr<ScEditEngineDefaulter> pThesaurusEngine;
322  bool bIsEditMode = GetViewData().HasEditView(eWhich);
323  bool bRecord = true;
324  if (!rDoc.IsUndoEnabled())
325  bRecord = false;
326  if (bIsEditMode) // edit mode active
327  {
328  GetViewData().GetEditView(eWhich, pEditView, nCol, nRow);
329  pEditSel.reset(new ESelection(pEditView->GetSelection()));
330  SC_MOD()->InputEnterHandler();
331  GetViewData().GetBindings().Update(); // otherwise the Sfx becomes mixed-up...
332  }
333  else
334  {
335  nCol = GetViewData().GetCurX();
336  nRow = GetViewData().GetCurY();
337  }
338  nTab = GetViewData().GetTabNo();
339 
340  ScAddress aPos(nCol, nRow, nTab);
341  ScEditableTester aTester( rDoc, nCol, nRow, nCol, nRow, rMark );
342  if (!aTester.IsEditable())
343  {
344  ErrorMessage(aTester.GetMessageId());
345  return;
346  }
347 
348  ScCellValue aOldText;
349  aOldText.assign(rDoc, aPos);
350  if (aOldText.meType != CELLTYPE_STRING && aOldText.meType != CELLTYPE_EDIT)
351  {
352  ErrorMessage(STR_THESAURUS_NO_STRING);
353  return;
354  }
355 
356  uno::Reference<linguistic2::XSpellChecker1> xSpeller = LinguMgr::GetSpellChecker();
357 
358  pThesaurusEngine.reset(new ScEditEngineDefaulter(rDoc.GetEnginePool()));
359  pThesaurusEngine->SetEditTextObjectPool( rDoc.GetEditPool() );
360  pThesaurusEngine->SetRefDevice(GetViewData().GetActiveWin());
361  pThesaurusEngine->SetSpeller(xSpeller);
362  MakeEditView(pThesaurusEngine.get(), nCol, nRow );
363  std::unique_ptr<SfxItemSet> pEditDefaults(
364  new SfxItemSet(pThesaurusEngine->GetEmptyItemSet()));
365  const ScPatternAttr* pPattern = rDoc.GetPattern(nCol, nRow, nTab);
366  if (pPattern)
367  {
368  pPattern->FillEditItemSet( pEditDefaults.get() );
369  pThesaurusEngine->SetDefaults( *pEditDefaults );
370  }
371 
372  if (aOldText.meType == CELLTYPE_EDIT)
373  pThesaurusEngine->SetTextCurrentDefaults(*aOldText.mpEditText);
374  else
375  pThesaurusEngine->SetTextCurrentDefaults(aOldText.getString(rDoc));
376 
377  pEditView = GetViewData().GetEditView(GetViewData().GetActivePart());
378  if (pEditSel)
379  pEditView->SetSelection(*pEditSel);
380  else
381  pEditView->SetSelection(ESelection(0,0,0,0));
382 
383  pThesaurusEngine->ClearModifyFlag();
384 
385  // language is now in EditEngine attributes -> no longer passed to StartThesaurus
386 
387  eState = pEditView->StartThesaurus();
388  OSL_ENSURE(eState != EESpellState::NoSpeller, "No SpellChecker");
389 
390  if (eState == EESpellState::ErrorFound) // should happen later through Wrapper!
391  {
392  LanguageType eLnge = ScViewUtil::GetEffLanguage( rDoc, ScAddress( nCol, nRow, nTab ) );
393  OUString aErr = SvtLanguageTable::GetLanguageString(eLnge) + ScResId( STR_SPELLING_NO_LANG );
394 
395  std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(GetViewData().GetDialogParent(),
396  VclMessageType::Info, VclButtonsType::Ok,
397  aErr));
398  xInfoBox->run();
399  }
400  if (pThesaurusEngine->IsModified())
401  {
402  ScCellValue aNewText;
403 
404  if (aOldText.meType == CELLTYPE_EDIT)
405  {
406  // The cell will own the text object instance.
407  std::unique_ptr<EditTextObject> pText = pThesaurusEngine->CreateTextObject();
408  auto tmp = pText.get();
409  if (rDoc.SetEditText(ScAddress(nCol,nRow,nTab), std::move(pText)))
410  aNewText.set(*tmp);
411  }
412  else
413  {
414  OUString aStr = pThesaurusEngine->GetText();
415  aNewText.set(rDoc.GetSharedStringPool().intern(aStr));
416  rDoc.SetString(nCol, nRow, nTab, aStr);
417  }
418 
419  pDocSh->SetDocumentModified();
420  if (bRecord)
421  {
422  GetViewData().GetDocShell()->GetUndoManager()->AddUndoAction(
423  std::make_unique<ScUndoThesaurus>(
424  GetViewData().GetDocShell(), nCol, nRow, nTab, aOldText, aNewText));
425  }
426  }
427 
428  KillEditView(true);
429  pDocSh->PostPaintGridAll();
430 }
431 
433 {
435  DoSheetConversion( aConvParam );
436 }
437 
439 {
440  SCCOL nCol;
441  SCROW nRow;
442  SCTAB nTab;
443  ScViewData& rViewData = GetViewData();
444  ScDocShell* pDocSh = rViewData.GetDocShell();
445  ScDocument& rDoc = pDocSh->GetDocument();
446  ScMarkData& rMark = rViewData.GetMarkData();
447  ScSplitPos eWhich = rViewData.GetActivePart();
448  EditView* pEditView = nullptr;
449  bool bIsEditMode = rViewData.HasEditView(eWhich);
450  bool bRecord = true;
451  if (!rDoc.IsUndoEnabled())
452  bRecord = false;
453  if (bIsEditMode) // edit mode active
454  {
455  rViewData.GetEditView(eWhich, pEditView, nCol, nRow);
456  SC_MOD()->InputEnterHandler();
457  }
458  else
459  {
460  nCol = rViewData.GetCurX();
461  nRow = rViewData.GetCurY();
462 
463  AlignToCursor( nCol, nRow, SC_FOLLOW_JUMP);
464  }
465  nTab = rViewData.GetTabNo();
466 
467  rMark.MarkToMulti();
468  bool bMarked = rMark.IsMultiMarked();
469  if (bMarked)
470  {
471  ScEditableTester aTester( rDoc, rMark );
472  if (!aTester.IsEditable())
473  {
474  ErrorMessage(aTester.GetMessageId());
475  return;
476  }
477  }
478 
479  ScDocumentUniquePtr pUndoDoc;
480  ScDocumentUniquePtr pRedoDoc;
481  if (bRecord)
482  {
483  pUndoDoc.reset( new ScDocument( SCDOCMODE_UNDO ) );
484  pUndoDoc->InitUndo( rDoc, nTab, nTab );
485  pRedoDoc.reset( new ScDocument( SCDOCMODE_UNDO ) );
486  pRedoDoc->InitUndo( rDoc, nTab, nTab );
487 
488  if ( rMark.GetSelectCount() > 1 )
489  {
490  for (const auto& rTab : rMark)
491  if ( rTab != nTab )
492  {
493  pUndoDoc->AddUndoTab( rTab, rTab );
494  pRedoDoc->AddUndoTab( rTab, rTab );
495  }
496  }
497  }
498 
499  // from here no return
500 
501  bool bOldEnabled = rDoc.IsIdleEnabled();
502  rDoc.EnableIdle(false); // stop online spelling
503 
504  // *** create and init the edit engine *** --------------------------------
505 
506  std::unique_ptr<ScConversionEngineBase> pEngine;
507  switch( rConvParam.GetType() )
508  {
510  pEngine.reset(new ScSpellingEngine(
511  rDoc.GetEnginePool(), rViewData, pUndoDoc.get(), pRedoDoc.get(), LinguMgr::GetSpellChecker() ));
512  break;
515  pEngine.reset(new ScTextConversionEngine(
516  rDoc.GetEnginePool(), rViewData, rConvParam, pUndoDoc.get(), pRedoDoc.get() ));
517  break;
518  default:
519  OSL_FAIL( "ScViewFunc::DoSheetConversion - unknown conversion type" );
520  }
521 
522  MakeEditView( pEngine.get(), nCol, nRow );
523  pEngine->SetRefDevice( rViewData.GetActiveWin() );
524  // simulate dummy cell:
525  pEditView = rViewData.GetEditView( rViewData.GetActivePart() );
526  rViewData.SetSpellingView( pEditView );
527  tools::Rectangle aRect( Point( 0, 0 ), Point( 0, 0 ) );
528  pEditView->SetOutputArea( aRect );
529  pEngine->SetControlWord( EEControlBits::USECHARATTRIBS );
530  pEngine->EnableUndo( false );
531  pEngine->SetPaperSize( aRect.GetSize() );
532  pEngine->SetTextCurrentDefaults( EMPTY_OUSTRING );
533 
534  // *** do the conversion *** ----------------------------------------------
535 
536  pEngine->ClearModifyFlag();
537  pEngine->ConvertAll( *pEditView );
538 
539  // *** undo/redo *** ------------------------------------------------------
540 
541  if( pEngine->IsAnyModified() )
542  {
543  if (bRecord)
544  {
545  SCCOL nNewCol = rViewData.GetCurX();
546  SCROW nNewRow = rViewData.GetCurY();
547  rViewData.GetDocShell()->GetUndoManager()->AddUndoAction(
548  std::make_unique<ScUndoConversion>(
549  pDocSh, rMark,
550  nCol, nRow, nTab, std::move(pUndoDoc),
551  nNewCol, nNewRow, nTab, std::move(pRedoDoc), rConvParam ) );
552  }
553 
555  rDoc.SetAllFormulasDirty(aCxt);
556 
557  pDocSh->SetDocumentModified();
558  }
559  else
560  {
561  pUndoDoc.reset();
562  pRedoDoc.reset();
563  }
564 
565  // *** final cleanup *** --------------------------------------------------
566 
567  rViewData.SetSpellingView( nullptr );
568  KillEditView(true);
569  pEngine.reset();
570  pDocSh->PostPaintGridAll();
571  rViewData.GetViewShell()->UpdateInputHandler();
572  rDoc.EnableIdle(bOldEnabled);
573 }
574 
575 // past from SotClipboardFormatId::FILE items
576 // is not called directly from Drop, but asynchronously -> dialogs are allowed
577 
578 bool ScViewFunc::PasteFile( const Point& rPos, const OUString& rFile, bool bLink )
579 {
581  aURL.SetSmartURL( rFile );
582  OUString aStrURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
583 
584  // is it a media URL?
585 #if HAVE_FEATURE_AVMEDIA
586  if( ::avmedia::MediaWindow::isMediaURL( aStrURL, ""/*TODO?*/ ) )
587  {
588  const SfxStringItem aMediaURLItem( SID_INSERT_AVMEDIA, aStrURL );
589  return ( nullptr != GetViewData().GetDispatcher().ExecuteList(
590  SID_INSERT_AVMEDIA, SfxCallMode::SYNCHRON,
591  { &aMediaURLItem }) );
592  }
593 #endif
594 
595  if (!bLink) // for bLink only graphics or URL
596  {
597  // 1. can I open the file?
598  std::shared_ptr<const SfxFilter> pFlt;
599 
600  // search only for its own filters, without selection box (as in ScDocumentLoader)
601  SfxFilterMatcher aMatcher( ScDocShell::Factory().GetFilterContainer()->GetName() );
602  SfxMedium aSfxMedium( aStrURL, (StreamMode::READ | StreamMode::SHARE_DENYNONE) );
603  // #i73992# GuessFilter no longer calls UseInteractionHandler.
604  // This is UI, so it can be called here.
605  aSfxMedium.UseInteractionHandler(true);
606  ErrCode nErr = aMatcher.GuessFilter( aSfxMedium, pFlt );
607 
608  if ( pFlt && !nErr )
609  {
610  // code stolen from the SFX!
611  SfxDispatcher &rDispatcher = GetViewData().GetDispatcher();
612  SfxStringItem aFileNameItem( SID_FILE_NAME, aStrURL );
613  SfxStringItem aFilterItem( SID_FILTER_NAME, pFlt->GetName() );
614  // #i69524# add target, as in SfxApplication when the Open dialog is used
615  SfxStringItem aTargetItem( SID_TARGETNAME, "_default" );
616 
617  // Open Asynchronously, because it can also happen from D&D
618  // and that is not so good for the MAC...
619  return (nullptr != rDispatcher.ExecuteList(SID_OPENDOC,
620  SfxCallMode::ASYNCHRON,
621  { &aFileNameItem, &aFilterItem, &aTargetItem}));
622  }
623  }
624 
625  // 2. can the file be inserted using the graphics filter?
626  // (as a link, since the Gallery provides it in this way)
627 
628  Graphic aGraphic;
630 
631  if (!rGraphicFilter.ImportGraphic(aGraphic, aURL,
633  {
634  if ( bLink )
635  {
636  return PasteGraphic( rPos, aGraphic, aStrURL );
637  }
638  else
639  {
640  // #i76709# if bLink isn't set, pass empty URL/filter, so a non-linked image is inserted
641  return PasteGraphic( rPos, aGraphic, EMPTY_OUSTRING );
642  }
643  }
644 
645  if (bLink) // for bLink everything, which is not graphics, as URL
646  {
647  tools::Rectangle aRect( rPos, Size(0,0) );
648  ScRange aRange = GetViewData().GetDocument().
649  GetRange( GetViewData().GetTabNo(), aRect );
650  SCCOL nPosX = aRange.aStart.Col();
651  SCROW nPosY = aRange.aStart.Row();
652 
653  InsertBookmark( aStrURL, aStrURL, nPosX, nPosY );
654  return true;
655  }
656  else
657  {
658  // 3. can the file be inserted as OLE?
659  // also non-storages, for instance sounds (#38282#)
660  uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
661 
662  //TODO/LATER: what about "bLink"?
663 
664  uno::Sequence < beans::PropertyValue > aMedium(1);
665  aMedium[0].Name = "URL";
666  aMedium[0].Value <<= aStrURL;
667 
668  comphelper::EmbeddedObjectContainer aCnt( xStorage );
669  OUString aName;
670  uno::Reference < embed::XEmbeddedObject > xObj = aCnt.InsertEmbeddedObject( aMedium, aName );
671  if( xObj.is() )
672  return PasteObject( rPos, xObj, nullptr );
673 
674  // If an OLE object can't be created, insert a URL button
675 
676  GetViewData().GetViewShell()->InsertURLButton( aStrURL, aStrURL, EMPTY_OUSTRING, &rPos );
677  return true;
678  }
679 }
680 
682  const css::uno::Reference< css::datatransfer::XTransferable >& rxTransferable,
683  SCCOL nPosX, SCROW nPosY )
684 {
685  INetBookmark aBookmark;
686  TransferableDataHelper aDataHelper( rxTransferable );
687  if ( !aDataHelper.GetINetBookmark( nFormatId, aBookmark ) )
688  return false;
689 
690  InsertBookmark( aBookmark.GetDescription(), aBookmark.GetURL(), nPosX, nPosY );
691  return true;
692 }
693 
694 void ScViewFunc::InsertBookmark( const OUString& rDescription, const OUString& rURL,
695  SCCOL nPosX, SCROW nPosY, const OUString* pTarget,
696  bool bTryReplace )
697 {
698  ScViewData& rViewData = GetViewData();
699  if ( rViewData.HasEditView( rViewData.GetActivePart() ) &&
700  nPosX >= rViewData.GetEditStartCol() && nPosX <= rViewData.GetEditEndCol() &&
701  nPosY >= rViewData.GetEditStartRow() && nPosY <= rViewData.GetEditEndRow() )
702  {
703  // insert into the cell which just got edited
704 
705  OUString aTargetFrame;
706  if (pTarget)
707  aTargetFrame = *pTarget;
708  rViewData.GetViewShell()->InsertURLField( rDescription, rURL, aTargetFrame );
709  return;
710  }
711 
712  // insert into not edited cell
713 
714  ScDocument& rDoc = GetViewData().GetDocument();
715  SCTAB nTab = GetViewData().GetTabNo();
716  ScAddress aCellPos( nPosX, nPosY, nTab );
717  EditEngine aEngine( rDoc.GetEnginePool() );
718 
719  const EditTextObject* pOld = rDoc.GetEditText(aCellPos);
720  if (pOld)
721  aEngine.SetText(*pOld);
722  else
723  {
724  OUString aOld;
725  rDoc.GetInputString(nPosX, nPosY, nTab, aOld);
726  if (!aOld.isEmpty())
727  aEngine.SetText(aOld);
728  }
729 
730  sal_Int32 nPara = aEngine.GetParagraphCount();
731  if (nPara)
732  --nPara;
733  sal_Int32 nTxtLen = aEngine.GetTextLen(nPara);
734  ESelection aInsSel( nPara, nTxtLen, nPara, nTxtLen );
735 
736  if ( bTryReplace && HasBookmarkAtCursor( nullptr ) )
737  {
738  // if called from hyperlink slot and cell contains only a URL,
739  // replace old URL with new one
740 
741  aInsSel = ESelection( 0, 0, 0, 1 ); // replace first character (field)
742  }
743 
744  SvxURLField aField( rURL, rDescription, SvxURLFormat::AppDefault );
745  if (pTarget)
746  aField.SetTargetFrame(*pTarget);
747  aEngine.QuickInsertField( SvxFieldItem( aField, EE_FEATURE_FIELD ), aInsSel );
748 
749  std::unique_ptr<EditTextObject> pData(aEngine.CreateTextObject());
750  EnterData(nPosX, nPosY, nTab, *pData);
751 }
752 
754 {
755  ScAddress aPos( GetViewData().GetCurX(), GetViewData().GetCurY(), GetViewData().GetTabNo() );
756  ScDocument& rDoc = GetViewData().GetDocShell()->GetDocument();
757 
758  const EditTextObject* pData = rDoc.GetEditText(aPos);
759  if (!pData)
760  return false;
761 
762  if (!pData->IsFieldObject())
763  // not a field object.
764  return false;
765 
766  const SvxFieldItem* pFieldItem = pData->GetField();
767  if (!pFieldItem)
768  // doesn't have a field item.
769  return false;
770 
771  const SvxFieldData* pField = pFieldItem->GetField();
772  if (!pField)
773  // doesn't have a field item data.
774  return false;
775 
776  if (pField->GetClassId() != css::text::textfield::Type::URL)
777  // not a URL field.
778  return false;
779 
780  if (pContent)
781  {
782  const SvxURLField* pURLField = static_cast<const SvxURLField*>(pField);
783  pContent->SetName( pURLField->GetRepresentation() );
784  pContent->SetURL( pURLField->GetURL() );
785  pContent->SetTargetFrame( pURLField->GetTargetFrame() );
786  }
787  return true;
788 }
789 
790 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScMarkData & GetMarkData()
Definition: viewdata.cxx:3046
void SetURL(const OUString &rURL)
bool is() const
void set(double fValue)
Definition: cellvalue.cxx:295
EESpellState StartThesaurus()
void EnableIdle(bool bDo)
Definition: document.hxx:2115
const SfxPoolItem * ExecuteList(sal_uInt16 nSlot, SfxCallMode nCall, std::initializer_list< SfxPoolItem const * > args, std::initializer_list< SfxPoolItem const * > internalargs=std::initializer_list< SfxPoolItem const * >())
void SetTargetFrame(const OUString &rFrm)
URL aURL
void MarkToSimple()
Definition: markdata.cxx:237
CellType meType
Definition: cellvalue.hxx:38
ScDocShell * GetDocShell() const
Definition: viewdata.hxx:354
ScAddress aStart
Definition: address.hxx:500
const SvxFieldItem * GetField() const
bool GetSotStorageStream(SotClipboardFormatId nFormat, tools::SvRef< SotTempStream > &rStreamRef)
SharedString intern(const OUString &rStr)
SC_DLLPUBLIC svl::SharedStringPool & GetSharedStringPool()
Definition: documen2.cxx:563
SC_DLLPUBLIC void GetInputString(SCCOL nCol, SCROW nRow, SCTAB nTab, OUString &rString)
Definition: document.cxx:3538
todo: It should be possible to have MarkArrays for each table, in order to enable "search all" across...
Definition: markdata.hxx:43
ErrCode ImportGraphic(Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat=GRFILTER_FORMAT_DONTKNOW, sal_uInt16 *pDeterminedFormat=nullptr, GraphicFilterImportFlags nImportFlags=GraphicFilterImportFlags::NONE)
#define EMPTY_OUSTRING
Definition: global.hxx:216
bool PasteBookmark(SotClipboardFormatId nFormatId, const css::uno::Reference< css::datatransfer::XTransferable > &rxTransferable, SCCOL nPosX, SCROW nPosY)
Definition: viewfun4.cxx:681
Sheet / outlining (grouping) information.
SCROW Row() const
Definition: address.hxx:262
void PostPaintGridAll()
Definition: docsh3.cxx:180
void MarkToMulti()
Definition: markdata.cxx:224
void DoSheetConversion(const ScConversionParam &rParam)
Generic implementation of sheet conversion functions.
Definition: viewfun4.cxx:438
std::unique_ptr< ContentProperties > pData
void GetFormula(OUString &rFormula, const formula::FormulaGrammar::Grammar=formula::FormulaGrammar::GRAM_DEFAULT, const ScInterpreterContext *pContext=nullptr) const
SCROW GetCurY() const
Definition: viewdata.hxx:402
#define LANGUAGE_KOREAN
sal_Int64 n
void SetDocumentModified()
Definition: docsh.cxx:2820
void SetSpellingView(EditView *pSpView)
Definition: viewdata.hxx:632
bool HasEditView(ScSplitPos eWhich) const
Definition: viewdata.hxx:564
SCTAB GetTabNo() const
Definition: viewdata.hxx:395
ErrCode GuessFilter(SfxMedium &rMedium, std::shared_ptr< const SfxFilter > &, SfxFilterFlags nMust=SfxFilterFlags::IMPORT, SfxFilterFlags nDont=SFX_FILTER_NOTINSTALLED) const
ScAddress aEnd
Definition: address.hxx:501
ScSplitPos GetActivePart() const
Definition: viewdata.hxx:398
void ToggleRel(sal_Int32 nStartPos, sal_Int32 nEndPos)
Definition: reffind.cxx:234
SC_DLLPUBLIC formula::FormulaGrammar::Grammar GetGrammar() const
Definition: document.hxx:982
bool GetINetBookmark(SotClipboardFormatId nFormat, INetBookmark &rBmk)
EmbeddedObjectRef * pObject
bool HasFormat(SotClipboardFormatId nFormat) const
const OUString & GetTargetFrame() const
SC_DLLPUBLIC formula::FormulaGrammar::AddressConvention GetAddressConvention() const
Definition: documen3.cxx:488
ScFormulaCell * getFormulaCell()
Definition: dociter.hxx:239
SCROW GetEditEndRow() const
Definition: viewdata.hxx:590
ScSplitPos
Definition: viewdata.hxx:45
CellType getType() const
Definition: dociter.hxx:236
void GetMarkArea(ScRange &rRange) const
Definition: markdata.cxx:112
virtual SfxUndoManager * GetUndoManager() override
Definition: docsh.cxx:2806
Store arbitrary cell value of any kind.
Definition: cellvalue.hxx:36
void SetAllFormulasDirty(const sc::SetFormulaDirtyContext &rCxt)
Definition: document.cxx:3833
OUString getString(const ScDocument &rDoc) const
Definition: cellvalue.cxx:494
Edit engine for text conversion.
Definition: spelleng.hxx:128
void SetTargetFrame(const OUString &rTarget)
Reference< XInputStream > xStream
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:870
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:312
SCTAB GetSelectCount() const
Definition: markdata.cxx:195
bool IsMultiMarked() const
Definition: markdata.hxx:83
int nCount
void CopyToDocument(SCCOL nCol1, SCROW nRow1, SCTAB nTab1, SCCOL nCol2, SCROW nRow2, SCTAB nTab2, InsertDeleteFlags nFlags, bool bMarked, ScDocument &rDestDoc, const ScMarkData *pMarks=nullptr, bool bColRowFlags=true)
Definition: document.cxx:2074
bool HasBookmarkAtCursor(SvxHyperlinkItem *pContent)
Definition: viewfun4.cxx:753
Walk through all cells in an area.
Definition: dociter.hxx:208
void UpdateInputHandler(bool bForce=false, bool bStopEditing=true)
Definition: tabvwsha.cxx:653
SCTAB Tab() const
Definition: address.hxx:271
SotClipboardFormatId
ScAddress aPos
SC_DLLPUBLIC const ScPatternAttr * GetPattern(SCCOL nCol, SCROW nRow, SCTAB nTab) const
Definition: document.cxx:4733
bool PasteFile(const Point &, const OUString &, bool bLink)
Definition: viewfun4.cxx:578
ScTabViewShell * GetViewShell() const
Definition: viewdata.hxx:357
SC_DLLPUBLIC bool SetEditText(const ScAddress &rPos, std::unique_ptr< EditTextObject > pEditText)
This method manages the lifecycle of the passed edit text object.
Definition: document.cxx:3392
const char * GetMessageId() const
Definition: editable.cxx:152
void SetMarkArea(const ScRange &rRange)
Definition: markdata.cxx:97
SC_DLLPUBLIC bool SetString(SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString &rString, const ScSetStringParam *pParam=nullptr)
Definition: document.cxx:3361
void SetTab(SCTAB nTabP)
Definition: address.hxx:283
Edit engine for spell checking.
Definition: spelleng.hxx:96
virtual void AddUndoAction(std::unique_ptr< SfxUndoAction > pAction, bool bTryMerg=false)
static css::uno::Reference< css::embed::XStorage > GetTemporaryStorage(const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
static css::uno::Reference< css::linguistic2::XSpellChecker1 > GetSpellChecker()
bool IsMarked() const
Definition: markdata.hxx:82
int i
std::unique_ptr< ScDocument, o3tl::default_delete< ScDocument > > ScDocumentUniquePtr
Definition: document.hxx:2610
sal_Int16 SCCOL
Definition: types.hxx:22
void UseInteractionHandler(bool)
bool GetString(SotClipboardFormatId nFormat, OUString &rStr)
#define SC_MOD()
Definition: scmod.hxx:250
void InsertBookmark(const OUString &rDescription, const OUString &rURL, SCCOL nPosX, SCROW nPosY, const OUString *pTarget=nullptr, bool bTryReplace=false)
Definition: viewfun4.cxx:694
Parameters for conversion.
Definition: spellparam.hxx:33
ScSheetLimits & GetSheetLimits() const
Definition: document.hxx:871
SC_DLLPUBLIC SfxItemPool * GetEnginePool() const
Definition: documen2.cxx:449
size_t size() const
Definition: rangelst.hxx:90
OUString ScResId(const char *pId)
Definition: scdll.cxx:89
ScGridWindow * GetActiveWin()
Definition: viewdata.cxx:3062
const OUString & GetDescription() const
#define GRFILTER_FORMAT_DONTKNOW
const OUString & GetRepresentation() const
bool IsFieldObject() const
SC_DLLPUBLIC const EditTextObject * GetEditText(const ScAddress &rPos) const
Definition: document.cxx:3609
bool IsEditable() const
Definition: editable.hxx:84
void InsertURLField(const OUString &rName, const OUString &rURL, const OUString &rTarget)
Definition: tabvwshe.cxx:162
void SetName(const OUString &rName)
static bool isMediaURL(const OUString &rURL, const OUString &rReferer, bool bDeep=false, Size *pPreferredSizePixel=nullptr)
void PostPaint(SCCOL nStartCol, SCROW nStartRow, SCTAB nStartTab, SCCOL nEndCol, SCROW nEndRow, SCTAB nEndTab, PaintPartFlags nPart, sal_uInt16 nExtFlags=0)
Definition: docsh3.cxx:98
ScConversionType GetType() const
Definition: spellparam.hxx:55
Size GetSize() const
SCCOL Col() const
Definition: address.hxx:267
const OUString & GetURL() const
virtual sal_Int32 GetClassId() const
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
const ScAddress & GetPos() const
Definition: dociter.hxx:234
void PasteRTF(SCCOL nCol, SCROW nStartRow, const css::uno::Reference< css::datatransfer::XTransferable > &rxTransferable)
Definition: viewfun4.cxx:76
sal_Int32 SCROW
Definition: types.hxx:18
void UpdateOle(const ScViewData &rViewData, bool bSnapSize=false)
Definition: docsh6.cxx:152
SC_DLLPUBLIC ScFormulaCell * SetFormulaCell(const ScAddress &rPos, ScFormulaCell *pCell)
Set formula cell, and transfer its ownership to the document.
Definition: documen2.cxx:1081
SfxDispatcher * GetDispatcher()
EESpellState
SCCOL GetEditStartCol() const
Definition: viewdata.hxx:587
void assign(const ScDocument &rDoc, const ScAddress &rPos)
Take cell value from specified position in specified document.
Definition: cellvalue.cxx:330
OUString aName
SCCOL GetEditEndCol() const
Definition: viewdata.hxx:589
const OUString & GetURL() const
const ScDocument & GetDocument() const
Definition: docsh.hxx:217
Hangul-Hanja converter.
Definition: spellparam.hxx:29
ScMatrixMode
EditTextObject * mpEditText
Definition: cellvalue.hxx:42
SC_DLLPUBLIC SfxItemPool * GetEditPool() const
Definition: documen2.cxx:444
void DoRefConversion()
Definition: viewfun4.cxx:178
static OUString GetLanguageString(const LanguageType eType)
bool InsertEmbeddedObject(const css::uno::Reference< css::embed::XEmbeddedObject > &, OUString &)
void SetSelection(const ESelection &rNewSel)
void SetOutputArea(const tools::Rectangle &rRect)
static GraphicFilter & GetGraphicFilter()
reference_type * get() const
bool IsUndoEnabled() const
Definition: document.hxx:1528
void DoHangulHanjaConversion()
Definition: viewfun4.cxx:432
ESelection GetSelection() const
constexpr sal_uInt16 EE_FEATURE_FIELD(EE_FEATURE_NOTCONV+1)
void GetMultiMarkArea(ScRange &rRange) const
Definition: markdata.cxx:117
static LanguageType GetEffLanguage(ScDocument &rDoc, const ScAddress &rPos)
Definition: viewutil.cxx:69
SC_DLLPUBLIC void EnableUndo(bool bVal)
Definition: document.cxx:6463
ScMatrixMode GetMatrixFlag() const
void DoThesaurus()
Definition: viewfun4.cxx:309
bool IsIdleEnabled() const
Definition: document.hxx:2114
aStr
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
void GetEditView(ScSplitPos eWhich, EditView *&rViewPtr, SCCOL &rCol, SCROW &rRow)
Definition: viewdata.cxx:2219
sal_Int16 SCTAB
Definition: types.hxx:23
bool SetSmartURL(OUString const &rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)
SCCOL GetCurX() const
Definition: viewdata.hxx:401
SCROW GetEditStartRow() const
Definition: viewdata.hxx:588
const SvxFieldData * GetField() const
void FillEditItemSet(SfxItemSet *pEditSet, const SfxItemSet *pCondSet=nullptr) const
Converts all Calc items contained in the own item set to edit engine items and puts them into pEditSe...
Definition: patattr.cxx:776