bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / view / viewfun4.cxx
blob821937e935dd44752105bf955ea64a0ee5b871a8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "scitems.hxx"
21 #include <editeng/eeitem.hxx>
23 #include <editeng/editobj.hxx>
24 #include <editeng/editstat.hxx>
25 #include <editeng/editview.hxx>
26 #include <editeng/flditem.hxx>
27 #include <svx/hlnkitem.hxx>
28 #include <editeng/langitem.hxx>
29 #include <svx/svxerr.hxx>
30 #include <editeng/unolingu.hxx>
32 #include <sfx2/bindings.hxx>
33 #include <sfx2/dispatch.hxx>
34 #include <sfx2/docfile.hxx>
35 #include <sfx2/fcontnr.hxx>
36 #include <svtools/langtab.hxx>
37 #include <vcl/graphicfilter.hxx>
38 #include <svl/stritem.hxx>
39 #include <svtools/transfer.hxx>
40 #include <svl/urlbmk.hxx>
41 #include <vcl/msgbox.hxx>
42 #include <avmedia/mediawindow.hxx>
44 #include <comphelper/storagehelper.hxx>
45 #include <comphelper/processfactory.hxx>
47 #include "viewfunc.hxx"
48 #include "docsh.hxx"
49 #include "document.hxx"
50 #include "docpool.hxx"
51 #include "globstr.hrc"
52 #include "global.hxx"
53 #include "undoblk.hxx"
54 #include "undocell.hxx"
55 #include "formulacell.hxx"
56 #include "scmod.hxx"
57 #include "spelleng.hxx"
58 #include "patattr.hxx"
59 #include "sc.hrc"
60 #include "tabvwsh.hxx"
61 #include "impex.hxx"
62 #include "editutil.hxx"
63 #include "editable.hxx"
64 #include "dociter.hxx"
65 #include "reffind.hxx"
66 #include "compiler.hxx"
68 #include <boost/scoped_ptr.hpp>
70 using namespace com::sun::star;
72 // STATIC DATA -----------------------------------------------------------
74 sal_Bool bPasteIsDrop = false;
76 //==================================================================
78 void ScViewFunc::PasteRTF( SCCOL nStartCol, SCROW nStartRow,
79 const ::com::sun::star::uno::Reference<
80 ::com::sun::star::datatransfer::XTransferable >& rxTransferable )
82 TransferableDataHelper aDataHelper( rxTransferable );
83 if ( aDataHelper.HasFormat( SOT_FORMATSTR_ID_EDITENGINE ) )
85 HideAllCursors();
88 ScDocShell* pDocSh = GetViewData()->GetDocShell();
89 ScDocument* pDoc = pDocSh->GetDocument();
90 SCTAB nTab = GetViewData()->GetTabNo();
91 const sal_Bool bRecord (pDoc->IsUndoEnabled());
93 const ScPatternAttr* pPattern = pDoc->GetPattern( nStartCol, nStartRow, nTab );
94 ScTabEditEngine* pEngine = new ScTabEditEngine( *pPattern, pDoc->GetEnginePool() );
95 pEngine->EnableUndo( false );
97 Window* pActWin = GetActiveWin();
98 if (pActWin)
100 pEngine->SetPaperSize(Size(100000,100000));
101 Window aWin( pActWin );
102 EditView aEditView( pEngine, &aWin );
103 aEditView.SetOutputArea(Rectangle(0,0,100000,100000));
105 // same method now for clipboard or drag&drop
106 // mba: clipboard always must contain absolute URLs (could be from alien source)
107 aEditView.InsertText( rxTransferable, String(), sal_True );
110 sal_Int32 nParCnt = pEngine->GetParagraphCount();
111 if (nParCnt)
113 SCROW nEndRow = nStartRow + static_cast<SCROW>(nParCnt) - 1;
114 if (nEndRow > MAXROW)
115 nEndRow = MAXROW;
117 ScDocument* pUndoDoc = NULL;
118 if (bRecord)
120 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
121 pUndoDoc->InitUndo( pDoc, nTab, nTab );
122 pDoc->CopyToDocument( nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab, IDF_ALL, false, pUndoDoc );
125 SCROW nRow = nStartRow;
127 // Temporarily turn off undo generation for this lot
128 bool bUndoEnabled = pDoc->IsUndoEnabled();
129 pDoc->EnableUndo( false );
130 for( sal_Int32 n = 0; n < nParCnt; n++ )
132 boost::scoped_ptr<EditTextObject> pObject(pEngine->CreateTextObject(n));
133 EnterData(nStartCol, nRow, nTab, *pObject, true);
134 if( ++nRow > MAXROW )
135 break;
137 pDoc->EnableUndo(bUndoEnabled);
139 if (bRecord)
141 ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
142 pRedoDoc->InitUndo( pDoc, nTab, nTab );
143 pDoc->CopyToDocument( nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab, IDF_ALL|IDF_NOCAPTIONS, false, pRedoDoc );
145 ScRange aMarkRange(nStartCol, nStartRow, nTab, nStartCol, nEndRow, nTab);
146 ScMarkData aDestMark;
147 aDestMark.SetMarkArea( aMarkRange );
148 pDocSh->GetUndoManager()->AddUndoAction(
149 new ScUndoPaste( pDocSh, aMarkRange, aDestMark,
150 pUndoDoc, pRedoDoc, IDF_ALL, NULL));
154 delete pEngine;
156 ShowAllCursors();
158 else
160 HideAllCursors();
161 ScDocShell* pDocSh = GetViewData()->GetDocShell();
162 ScImportExport aImpEx( pDocSh->GetDocument(),
163 ScAddress( nStartCol, nStartRow, GetViewData()->GetTabNo() ) );
165 OUString aStr;
166 SotStorageStreamRef xStream;
167 if ( aDataHelper.GetSotStorageStream( SOT_FORMAT_RTF, xStream ) && xStream.Is() )
168 // mba: clipboard always must contain absolute URLs (could be from alien source)
169 aImpEx.ImportStream( *xStream, String(), SOT_FORMAT_RTF );
170 else if ( aDataHelper.GetString( SOT_FORMAT_RTF, aStr ) )
171 aImpEx.ImportString( aStr, SOT_FORMAT_RTF );
173 AdjustRowHeight( nStartRow, aImpEx.GetRange().aEnd.Row() );
174 pDocSh->UpdateOle(GetViewData());
175 ShowAllCursors();
178 void ScViewFunc::DoRefConversion( sal_Bool bRecord )
180 ScDocument* pDoc = GetViewData()->GetDocument();
181 ScMarkData& rMark = GetViewData()->GetMarkData();
182 SCTAB nTabCount = pDoc->GetTableCount();
183 if (bRecord && !pDoc->IsUndoEnabled())
184 bRecord = false;
186 ScRange aMarkRange;
187 rMark.MarkToSimple();
188 sal_Bool bMulti = rMark.IsMultiMarked();
189 if (bMulti)
190 rMark.GetMultiMarkArea( aMarkRange );
191 else if (rMark.IsMarked())
192 rMark.GetMarkArea( aMarkRange );
193 else
195 aMarkRange = ScRange( GetViewData()->GetCurX(),
196 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
198 ScEditableTester aTester( pDoc, aMarkRange.aStart.Col(), aMarkRange.aStart.Row(),
199 aMarkRange.aEnd.Col(), aMarkRange.aEnd.Row(),rMark );
200 if (!aTester.IsEditable())
202 ErrorMessage(aTester.GetMessageId());
203 return;
206 ScDocShell* pDocSh = GetViewData()->GetDocShell();
207 sal_Bool bOk = false;
209 ScDocument* pUndoDoc = NULL;
210 if (bRecord)
212 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
213 SCTAB nTab = aMarkRange.aStart.Tab();
214 pUndoDoc->InitUndo( pDoc, nTab, nTab );
216 if ( rMark.GetSelectCount() > 1 )
218 ScMarkData::iterator itr = rMark.begin(), itrEnd = rMark.end();
219 for (; itr != itrEnd; ++itr)
220 if ( *itr != nTab )
221 pUndoDoc->AddUndoTab( *itr, *itr );
223 ScRange aCopyRange = aMarkRange;
224 aCopyRange.aStart.SetTab(0);
225 aCopyRange.aEnd.SetTab(nTabCount-1);
226 pDoc->CopyToDocument( aCopyRange, IDF_ALL, bMulti, pUndoDoc, &rMark );
229 ScRangeListRef xRanges;
230 GetViewData()->GetMultiArea( xRanges );
231 size_t nCount = xRanges->size();
233 ScMarkData::iterator itr = rMark.begin(), itrEnd = rMark.end();
234 for (; itr != itrEnd; ++itr)
236 SCTAB i = *itr;
237 for (size_t j = 0; j < nCount; ++j)
239 ScRange aRange = *(*xRanges)[j];
240 aRange.aStart.SetTab(i);
241 aRange.aEnd.SetTab(i);
242 ScCellIterator aIter( pDoc, aRange );
243 for (bool bHas = aIter.first(); bHas; bHas = aIter.next())
245 if (aIter.getType() != CELLTYPE_FORMULA)
246 continue;
248 ScFormulaCell* pCell = aIter.getFormulaCell();
249 OUString aOld;
250 pCell->GetFormula(aOld);
251 sal_Int32 nLen = aOld.getLength();
252 ScRefFinder aFinder( aOld, aIter.GetPos(), pDoc, pDoc->GetAddressConvention() );
253 aFinder.ToggleRel( 0, nLen );
254 if (aFinder.GetFound())
256 ScAddress aPos = pCell->aPos;
257 String aNew = aFinder.GetText();
258 ScCompiler aComp( pDoc, aPos);
259 aComp.SetGrammar(pDoc->GetGrammar());
260 ScTokenArray* pArr = aComp.CompileString( aNew );
261 ScFormulaCell* pNewCell = new ScFormulaCell( pDoc, aPos,
262 pArr,formula::FormulaGrammar::GRAM_DEFAULT, MM_NONE );
263 pDoc->SetFormulaCell(aPos, pNewCell);
264 bOk = true;
269 if (bRecord)
271 ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
272 SCTAB nTab = aMarkRange.aStart.Tab();
273 pRedoDoc->InitUndo( pDoc, nTab, nTab );
275 if ( rMark.GetSelectCount() > 1 )
277 itr = rMark.begin();
278 for (; itr != itrEnd; ++itr)
279 if ( *itr != nTab )
280 pRedoDoc->AddUndoTab( *itr, *itr );
282 ScRange aCopyRange = aMarkRange;
283 aCopyRange.aStart.SetTab(0);
284 aCopyRange.aEnd.SetTab(nTabCount-1);
285 pDoc->CopyToDocument( aCopyRange, IDF_ALL, bMulti, pRedoDoc, &rMark );
287 pDocSh->GetUndoManager()->AddUndoAction(
288 new ScUndoRefConversion( pDocSh,
289 aMarkRange, rMark, pUndoDoc, pRedoDoc, bMulti, IDF_ALL) );
292 pDocSh->PostPaint( aMarkRange, PAINT_GRID );
293 pDocSh->UpdateOle(GetViewData());
294 pDocSh->SetDocumentModified();
295 CellContentChanged();
297 if (!bOk)
298 ErrorMessage(STR_ERR_NOREF);
300 // Thesaurus - Undo ok
301 void ScViewFunc::DoThesaurus( sal_Bool bRecord )
303 SCCOL nCol;
304 SCROW nRow;
305 SCTAB nTab;
306 ScDocShell* pDocSh = GetViewData()->GetDocShell();
307 ScDocument* pDoc = pDocSh->GetDocument();
308 ScMarkData& rMark = GetViewData()->GetMarkData();
309 ScSplitPos eWhich = GetViewData()->GetActivePart();
310 EESpellState eState;
311 String sOldText, sNewString;
312 EditTextObject* pOldTObj = NULL;
313 const EditTextObject* pTObject = NULL;
314 EditView* pEditView = NULL;
315 boost::scoped_ptr<ESelection> pEditSel;
316 ScEditEngineDefaulter* pThesaurusEngine;
317 sal_Bool bIsEditMode = GetViewData()->HasEditView(eWhich);
318 if (bRecord && !pDoc->IsUndoEnabled())
319 bRecord = false;
320 if (bIsEditMode) // Edit-Mode aktiv
322 GetViewData()->GetEditView(eWhich, pEditView, nCol, nRow);
323 pEditSel.reset(new ESelection(pEditView->GetSelection()));
324 SC_MOD()->InputEnterHandler();
325 GetViewData()->GetBindings().Update(); // sonst kommt der Sfx durcheinander...
327 else
329 nCol = GetViewData()->GetCurX();
330 nRow = GetViewData()->GetCurY();
332 nTab = GetViewData()->GetTabNo();
334 ScAddress aPos(nCol, nRow, nTab);
335 ScEditableTester aTester( pDoc, nCol, nRow, nCol, nRow, rMark );
336 if (!aTester.IsEditable())
338 ErrorMessage(aTester.GetMessageId());
339 return;
342 CellType eCellType = pDoc->GetCellType(aPos);
343 if (eCellType != CELLTYPE_STRING && eCellType != CELLTYPE_EDIT)
345 ErrorMessage(STR_THESAURUS_NO_STRING);
346 return;
349 uno::Reference<linguistic2::XSpellChecker1> xSpeller = LinguMgr::GetSpellChecker();
351 pThesaurusEngine = new ScEditEngineDefaulter( pDoc->GetEnginePool() );
352 pThesaurusEngine->SetEditTextObjectPool( pDoc->GetEditPool() );
353 pThesaurusEngine->SetRefDevice(GetViewData()->GetActiveWin());
354 pThesaurusEngine->SetSpeller(xSpeller);
355 MakeEditView(pThesaurusEngine, nCol, nRow );
356 const ScPatternAttr* pPattern = NULL;
357 SfxItemSet* pEditDefaults = new SfxItemSet(pThesaurusEngine->GetEmptyItemSet());
358 pPattern = pDoc->GetPattern(nCol, nRow, nTab);
359 if (pPattern)
361 pPattern->FillEditItemSet( pEditDefaults );
362 pThesaurusEngine->SetDefaults( *pEditDefaults );
365 if (eCellType == CELLTYPE_STRING)
367 sOldText = pDoc->GetString(aPos);
368 pThesaurusEngine->SetText(sOldText);
370 else if (eCellType == CELLTYPE_EDIT)
372 pTObject = pDoc->GetEditText(aPos);
373 if (pTObject)
375 pOldTObj = pTObject->Clone();
376 pThesaurusEngine->SetText(*pTObject);
379 else
381 OSL_FAIL("DoThesaurus: Keine String oder Editzelle");
383 pEditView = GetViewData()->GetEditView(GetViewData()->GetActivePart());
384 if (pEditSel)
385 pEditView->SetSelection(*pEditSel);
386 else
387 pEditView->SetSelection(ESelection(0,0,0,0));
389 pThesaurusEngine->ClearModifyFlag();
391 // language is now in EditEngine attributes -> no longer passed to StartThesaurus
393 eState = pEditView->StartThesaurus();
394 OSL_ENSURE(eState != EE_SPELL_NOSPELLER, "No SpellChecker");
396 if (eState == EE_SPELL_ERRORFOUND) // sollte spaeter durch Wrapper geschehen!
398 LanguageType eLnge = ScViewUtil::GetEffLanguage( pDoc, ScAddress( nCol, nRow, nTab ) );
399 SvtLanguageTable aLangTab;
400 String aErr = aLangTab.GetString(eLnge);
401 aErr += ScGlobal::GetRscString( STR_SPELLING_NO_LANG );
402 InfoBox aBox( GetViewData()->GetDialogParent(), aErr );
403 aBox.Execute();
405 if (pThesaurusEngine->IsModified())
407 EditTextObject* pNewTObj = NULL;
408 if (pTObject)
410 // The cell will own the text object instance.
411 pDoc->SetEditText(
412 ScAddress(nCol,nRow,nTab), pThesaurusEngine->CreateTextObject());
414 else
416 sNewString = pThesaurusEngine->GetText();
417 pDoc->SetString(nCol, nRow, nTab, sNewString);
419 // erack! it's broadcasted
420 // pDoc->SetDirty();
421 pDocSh->SetDocumentModified();
422 if (bRecord)
424 GetViewData()->GetDocShell()->GetUndoManager()->AddUndoAction(
425 new ScUndoThesaurus( GetViewData()->GetDocShell(),
426 nCol, nRow, nTab,
427 sOldText, pOldTObj, sNewString, pNewTObj));
429 delete pNewTObj;
431 KillEditView(sal_True);
432 delete pEditDefaults;
433 delete pThesaurusEngine;
434 delete pOldTObj;
435 pDocSh->PostPaintGridAll();
438 void ScViewFunc::DoHangulHanjaConversion( sal_Bool bRecord )
440 ScConversionParam aConvParam( SC_CONVERSION_HANGULHANJA, LANGUAGE_KOREAN, 0, true );
441 DoSheetConversion( aConvParam, bRecord );
444 void ScViewFunc::DoSheetConversion( const ScConversionParam& rConvParam, sal_Bool bRecord )
446 SCCOL nCol;
447 SCROW nRow;
448 SCTAB nTab;
449 ScViewData& rViewData = *GetViewData();
450 ScDocShell* pDocSh = rViewData.GetDocShell();
451 ScDocument* pDoc = pDocSh->GetDocument();
452 ScMarkData& rMark = rViewData.GetMarkData();
453 ScSplitPos eWhich = rViewData.GetActivePart();
454 EditView* pEditView = NULL;
455 sal_Bool bIsEditMode = rViewData.HasEditView(eWhich);
456 if (bRecord && !pDoc->IsUndoEnabled())
457 bRecord = false;
458 if (bIsEditMode) // Edit-Mode aktiv
460 rViewData.GetEditView(eWhich, pEditView, nCol, nRow);
461 SC_MOD()->InputEnterHandler();
463 else
465 nCol = rViewData.GetCurX();
466 nRow = rViewData.GetCurY();
468 AlignToCursor( nCol, nRow, SC_FOLLOW_JUMP);
470 nTab = rViewData.GetTabNo();
472 rMark.MarkToMulti();
473 sal_Bool bMarked = rMark.IsMultiMarked();
474 if (bMarked)
476 ScEditableTester aTester( pDoc, rMark );
477 if (!aTester.IsEditable())
479 ErrorMessage(aTester.GetMessageId());
480 return;
484 ScDocument* pUndoDoc = NULL;
485 ScDocument* pRedoDoc = NULL;
486 if (bRecord)
488 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
489 pUndoDoc->InitUndo( pDoc, nTab, nTab );
490 pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
491 pRedoDoc->InitUndo( pDoc, nTab, nTab );
493 if ( rMark.GetSelectCount() > 1 )
495 ScMarkData::iterator itr = rMark.begin(), itrEnd = rMark.end();
496 for (; itr != itrEnd; ++itr)
497 if ( *itr != nTab )
499 pUndoDoc->AddUndoTab( *itr, *itr );
500 pRedoDoc->AddUndoTab( *itr, *itr );
505 // ab hier kein return mehr
507 bool bOldEnabled = pDoc->IsIdleEnabled();
508 pDoc->EnableIdle(false); // stop online spelling
510 // *** create and init the edit engine *** --------------------------------
512 ScConversionEngineBase* pEngine = NULL;
513 switch( rConvParam.GetType() )
515 case SC_CONVERSION_SPELLCHECK:
516 pEngine = new ScSpellingEngine(
517 pDoc->GetEnginePool(), rViewData, pUndoDoc, pRedoDoc, LinguMgr::GetSpellChecker() );
518 break;
519 case SC_CONVERSION_HANGULHANJA:
520 case SC_CONVERSION_CHINESE_TRANSL:
521 pEngine = new ScTextConversionEngine(
522 pDoc->GetEnginePool(), rViewData, rConvParam, pUndoDoc, pRedoDoc );
523 break;
524 default:
525 OSL_FAIL( "ScViewFunc::DoSheetConversion - unknown conversion type" );
528 MakeEditView( pEngine, nCol, nRow );
529 pEngine->SetRefDevice( rViewData.GetActiveWin() );
530 // dummy Zelle simulieren:
531 pEditView = rViewData.GetEditView( rViewData.GetActivePart() );
532 rViewData.SetSpellingView( pEditView );
533 Rectangle aRect( Point( 0, 0 ), Point( 0, 0 ) );
534 pEditView->SetOutputArea( aRect );
535 pEngine->SetControlWord( EE_CNTRL_USECHARATTRIBS );
536 pEngine->EnableUndo( false );
537 pEngine->SetPaperSize( aRect.GetSize() );
538 pEngine->SetText( EMPTY_STRING );
540 // *** do the conversion *** ----------------------------------------------
542 pEngine->ClearModifyFlag();
543 pEngine->ConvertAll( *pEditView );
545 // *** undo/redo *** ------------------------------------------------------
547 if( pEngine->IsAnyModified() )
549 if (bRecord)
551 SCCOL nNewCol = rViewData.GetCurX();
552 SCROW nNewRow = rViewData.GetCurY();
553 rViewData.GetDocShell()->GetUndoManager()->AddUndoAction(
554 new ScUndoConversion(
555 pDocSh, rMark,
556 nCol, nRow, nTab, pUndoDoc,
557 nNewCol, nNewRow, nTab, pRedoDoc, rConvParam ) );
559 pDoc->SetDirty();
560 pDocSh->SetDocumentModified();
562 else
564 delete pUndoDoc;
565 delete pRedoDoc;
568 // *** final cleanup *** --------------------------------------------------
570 rViewData.SetSpellingView( NULL );
571 KillEditView(sal_True);
572 delete pEngine;
573 pDocSh->PostPaintGridAll();
574 rViewData.GetViewShell()->UpdateInputHandler();
575 pDoc->EnableIdle(bOldEnabled);
578 // Pasten von FORMAT_FILE-Items
579 // wird nicht direkt aus Drop aufgerufen, sondern asynchron -> Dialoge sind erlaubt
581 sal_Bool ScViewFunc::PasteFile( const Point& rPos, const String& rFile, sal_Bool bLink )
583 INetURLObject aURL;
584 aURL.SetSmartURL( rFile );
585 String aStrURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
587 // is it a media URL?
588 if( ::avmedia::MediaWindow::isMediaURL( aStrURL ) )
590 const SfxStringItem aMediaURLItem( SID_INSERT_AVMEDIA, aStrURL );
591 return sal_Bool( 0 != GetViewData()->GetDispatcher().Execute(
592 SID_INSERT_AVMEDIA, SFX_CALLMODE_SYNCHRON,
593 &aMediaURLItem, 0L ) );
596 if (!bLink) // bei bLink nur Grafik oder URL
598 // 1. Kann ich die Datei oeffnen?
599 const SfxFilter* pFlt = NULL;
601 // nur nach eigenen Filtern suchen, ohne Auswahlbox (wie in ScDocumentLoader)
602 SfxFilterMatcher aMatcher( ScDocShell::Factory().GetFilterContainer()->GetName() );
603 SfxMedium aSfxMedium( aStrURL, (STREAM_READ | STREAM_SHARE_DENYNONE) );
604 // #i73992# GuessFilter no longer calls UseInteractionHandler.
605 // This is UI, so it can be called here.
606 aSfxMedium.UseInteractionHandler(sal_True);
607 ErrCode nErr = aMatcher.GuessFilter( aSfxMedium, &pFlt );
609 if ( pFlt && !nErr )
611 // Code aus dem SFX geklaut!
612 SfxDispatcher &rDispatcher = GetViewData()->GetDispatcher();
613 SfxStringItem aFileNameItem( SID_FILE_NAME, aStrURL );
614 SfxStringItem aFilterItem( SID_FILTER_NAME, pFlt->GetName() );
615 // #i69524# add target, as in SfxApplication when the Open dialog is used
616 SfxStringItem aTargetItem( SID_TARGETNAME, OUString("_default") );
618 // Asynchron oeffnen, kann naemlich auch aus D&D heraus passieren
619 // und das bekommt dem MAC nicht so gut ...
620 return sal_Bool( 0 != rDispatcher.Execute( SID_OPENDOC,
621 SFX_CALLMODE_ASYNCHRON, &aFileNameItem, &aFilterItem, &aTargetItem, 0L) );
625 // 2. Kann die Datei ueber die Grafik-Filter eingefuegt werden?
626 // (als Link, weil Gallery das so anbietet)
628 sal_uInt16 nFilterFormat;
629 Graphic aGraphic;
630 GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
633 if (!rGraphicFilter.ImportGraphic(aGraphic, aURL,
634 GRFILTER_FORMAT_DONTKNOW, &nFilterFormat ))
636 if ( bLink )
638 String aFltName = rGraphicFilter.GetImportFormatName(nFilterFormat);
639 return PasteGraphic( rPos, aGraphic, aStrURL, aFltName );
641 else
643 // #i76709# if bLink isn't set, pass empty URL/filter, so a non-linked image is inserted
644 return PasteGraphic( rPos, aGraphic, EMPTY_STRING, EMPTY_STRING );
648 if (bLink) // bei bLink alles, was nicht Grafik ist, als URL
650 Rectangle aRect( rPos, Size(0,0) );
651 ScRange aRange = GetViewData()->GetDocument()->
652 GetRange( GetViewData()->GetTabNo(), aRect );
653 SCCOL nPosX = aRange.aStart.Col();
654 SCROW nPosY = aRange.aStart.Row();
656 InsertBookmark( aStrURL, aStrURL, nPosX, nPosY );
657 return sal_True;
659 else
661 // 3. Kann die Datei als OLE eingefuegt werden?
662 // auch nicht-Storages, z.B. Sounds (#38282#)
663 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
665 //TODO/LATER: what about "bLink"?
667 uno::Sequence < beans::PropertyValue > aMedium(1);
668 aMedium[0].Name = OUString( "URL" );
669 aMedium[0].Value <<= OUString( aStrURL );
671 comphelper::EmbeddedObjectContainer aCnt( xStorage );
672 OUString aName;
673 uno::Reference < embed::XEmbeddedObject > xObj = aCnt.InsertEmbeddedObject( aMedium, aName );
674 if( xObj.is() )
675 return PasteObject( rPos, xObj );
677 // If an OLE object can't be created, insert a URL button
679 GetViewData()->GetViewShell()->InsertURLButton( aStrURL, aStrURL, EMPTY_STRING, &rPos );
680 return sal_True;
684 sal_Bool ScViewFunc::PasteBookmark( sal_uLong nFormatId,
685 const ::com::sun::star::uno::Reference<
686 ::com::sun::star::datatransfer::XTransferable >& rxTransferable,
687 SCCOL nPosX, SCROW nPosY )
689 INetBookmark aBookmark;
690 TransferableDataHelper aDataHelper( rxTransferable );
691 if ( !aDataHelper.GetINetBookmark( nFormatId, aBookmark ) )
692 return false;
694 InsertBookmark( aBookmark.GetDescription(), aBookmark.GetURL(), nPosX, nPosY );
695 return sal_True;
698 void ScViewFunc::InsertBookmark( const String& rDescription, const String& rURL,
699 SCCOL nPosX, SCROW nPosY, const String* pTarget,
700 sal_Bool bTryReplace )
702 ScViewData* pViewData = GetViewData();
703 if ( pViewData->HasEditView( pViewData->GetActivePart() ) &&
704 nPosX >= pViewData->GetEditStartCol() && nPosX <= pViewData->GetEditEndCol() &&
705 nPosY >= pViewData->GetEditStartRow() && nPosY <= pViewData->GetEditEndRow() )
707 // in die gerade editierte Zelle einfuegen
709 String aTargetFrame;
710 if (pTarget)
711 aTargetFrame = *pTarget;
712 pViewData->GetViewShell()->InsertURLField( rDescription, rURL, aTargetFrame );
713 return;
716 // in nicht editierte Zelle einfuegen
718 ScDocument* pDoc = GetViewData()->GetDocument();
719 SCTAB nTab = GetViewData()->GetTabNo();
720 ScAddress aCellPos( nPosX, nPosY, nTab );
721 EditEngine aEngine( pDoc->GetEnginePool() );
723 const EditTextObject* pOld = pDoc->GetEditText(aCellPos);
724 if (pOld)
725 aEngine.SetText(*pOld);
726 else
728 OUString aOld;
729 pDoc->GetInputString(nPosX, nPosY, nTab, aOld);
730 if (!aOld.isEmpty())
731 aEngine.SetText(aOld);
734 sal_Int32 nPara = aEngine.GetParagraphCount();
735 if (nPara)
736 --nPara;
737 xub_StrLen nTxtLen = aEngine.GetTextLen(nPara);
738 ESelection aInsSel( nPara, nTxtLen, nPara, nTxtLen );
740 if ( bTryReplace && HasBookmarkAtCursor( NULL ) )
742 // if called from hyperlink slot and cell contains only a URL,
743 // replace old URL with new one
745 aInsSel = ESelection( 0, 0, 0, 1 ); // replace first character (field)
748 SvxURLField aField( rURL, rDescription, SVXURLFORMAT_APPDEFAULT );
749 if (pTarget)
750 aField.SetTargetFrame(*pTarget);
751 aEngine.QuickInsertField( SvxFieldItem( aField, EE_FEATURE_FIELD ), aInsSel );
753 boost::scoped_ptr<EditTextObject> pData(aEngine.CreateTextObject());
754 EnterData(nPosX, nPosY, nTab, *pData);
757 bool ScViewFunc::HasBookmarkAtCursor( SvxHyperlinkItem* pContent )
759 ScAddress aPos( GetViewData()->GetCurX(), GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
760 ScDocument* pDoc = GetViewData()->GetDocShell()->GetDocument();
762 const EditTextObject* pData = pDoc->GetEditText(aPos);
763 if (!pData)
764 return false;
766 if (!pData->IsFieldObject())
767 // not a field object.
768 return false;
770 const SvxFieldItem* pFieldItem = pData->GetField();
771 if (!pFieldItem)
772 // doesn't have a field item.
773 return false;
775 const SvxFieldData* pField = pFieldItem->GetField();
776 if (!pField)
777 // doesn't have a field item data.
778 return false;
780 if (pField->GetClassId() != com::sun::star::text::textfield::Type::URL)
781 // not a URL field.
782 return false;
784 if (pContent)
786 const SvxURLField* pURLField = static_cast<const SvxURLField*>(pField);
787 pContent->SetName( pURLField->GetRepresentation() );
788 pContent->SetURL( pURLField->GetURL() );
789 pContent->SetTargetFrame( pURLField->GetTargetFrame() );
791 return true;
795 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */