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