problem pasting to calc an image copied from firefox (windows)
[LibreOffice.git] / sc / source / ui / view / tabvwshe.cxx
blobab3f0c2f18eb56f9b5806fb9dec87c126f67376b
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 <comphelper/string.hxx>
21 #include <comphelper/lok.hxx>
22 #include <editeng/eeitem.hxx>
23 #include <osl/diagnose.h>
25 #include <editeng/editview.hxx>
26 #include <editeng/flditem.hxx>
27 #include <svx/hlnkitem.hxx>
28 #include <svl/srchitem.hxx>
29 #include <sfx2/dispatch.hxx>
30 #include <sfx2/request.hxx>
31 #include <svl/stritem.hxx>
33 #include <tabvwsh.hxx>
34 #include <sc.hrc>
35 #include <scmod.hxx>
36 #include <impex.hxx>
37 #include <editsh.hxx>
38 #include <dociter.hxx>
39 #include <inputhdl.hxx>
40 #include <document.hxx>
42 OUString ScTabViewShell::GetSelectionText( bool bWholeWord )
44 OUString aStrSelection;
46 if ( pEditShell && pEditShell.get() == GetMySubShell() )
48 aStrSelection = pEditShell->GetSelectionText( bWholeWord );
50 else
52 ScRange aRange;
54 if ( GetViewData().GetSimpleArea( aRange ) == SC_MARK_SIMPLE )
56 ScDocument& rDoc = GetViewData().GetDocument();
57 if ( bInFormatDialog && aRange.aStart.Row() != aRange.aEnd.Row() )
59 // limit range to one data row
60 // (only when the call comes from a format dialog)
61 ScHorizontalCellIterator aIter( rDoc, aRange.aStart.Tab(),
62 aRange.aStart.Col(), aRange.aStart.Row(),
63 aRange.aEnd.Col(), aRange.aEnd.Row() );
64 SCCOL nCol;
65 SCROW nRow;
66 if ( aIter.GetNext( nCol, nRow ) )
68 aRange.aStart.SetCol( nCol );
69 aRange.aStart.SetRow( nRow );
70 aRange.aEnd.SetRow( nRow );
72 else
73 aRange.aEnd = aRange.aStart;
75 else
77 // #i111531# with 1M rows it was necessary to limit the range
78 // to the actually used data area.
79 SCCOL nCol1, nCol2;
80 SCROW nRow1, nRow2;
81 SCTAB nTab1, nTab2;
82 aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
83 bool bShrunk;
84 rDoc.ShrinkToUsedDataArea( bShrunk, nTab1, nCol1, nRow1, nCol2, nRow2, false);
85 if (bShrunk)
87 aRange.aStart.SetCol( nCol1 );
88 aRange.aStart.SetRow( nRow1 );
89 aRange.aEnd.SetCol( nCol2 );
90 aRange.aEnd.SetRow( nRow2 );
94 ScImportExport aObj( rDoc, aRange );
95 aObj.SetFormulas( GetViewData().GetOptions().GetOption( VOPT_FORMULAS ) );
96 OUString aExportOUString;
97 /* TODO: STRING_TSVC under some circumstances? */
98 aObj.ExportString( aExportOUString, SotClipboardFormatId::STRING );
99 aStrSelection = convertLineEnd(aExportOUString, LINEEND_CR);
101 // replace Tab/CR with space, if for dialog or through Basic/SelectionTextExt,
102 // or when it is a single row.
103 // Otherwise keep Tabs in multi-row (for instance mail or Basic/SelectionText).
104 // for mail the Tabs are then later changed into (multiple) spaces.
106 if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
108 aStrSelection = aStrSelection.replaceAll("\r", " ");
109 aStrSelection = aStrSelection.replaceAll("\t", " ");
110 aStrSelection = comphelper::string::stripEnd(aStrSelection, ' ');
115 return aStrSelection;
118 bool ScTabViewShell::ShouldDisableEditHyperlink() const
120 bool bRet = false;
122 if (pEditShell && pEditShell.get() == GetMySubShell())
124 bRet = pEditShell->ShouldDisableEditHyperlink();
127 return bRet;
130 void ScTabViewShell::EnableEditHyperlink()
132 if (pEditShell && pEditShell.get() == GetMySubShell())
134 pEditShell->EnableEditHyperlink();
138 void ScTabViewShell::InsertURL( const OUString& rName, const OUString& rURL, const OUString& rTarget,
139 sal_uInt16 nMode )
141 SvxLinkInsertMode eMode = static_cast<SvxLinkInsertMode>(nMode);
142 bool bAsText = ( eMode != HLINK_BUTTON ); // default is now text
144 if ( bAsText )
146 if ( GetViewData().IsActive() )
148 // if the view is active, always use InsertURLField, which starts EditMode
149 // and selects the URL, so it can be changed from the URL bar / dialog
151 InsertURLField( rName, rURL, rTarget );
153 else
155 // if the view is not active, InsertURLField doesn't work
156 // -> use InsertBookmark to directly manipulate cell content
157 // bTryReplace=sal_True -> if cell contains only one URL, replace it
159 SCCOL nPosX = GetViewData().GetCurX();
160 SCROW nPosY = GetViewData().GetCurY();
161 InsertBookmark( rName, rURL, nPosX, nPosY, &rTarget, true );
164 else
166 SC_MOD()->InputEnterHandler();
167 InsertURLButton( rName, rURL, rTarget, nullptr );
171 static void lcl_SelectFieldAfterInsert( EditView& rView )
173 ESelection aSel = rView.GetSelection();
174 if ( aSel.nStartPos == aSel.nEndPos && aSel.nStartPos > 0 )
176 // Cursor is behind the inserted field -> extend selection to the left
178 --aSel.nStartPos;
179 rView.SetSelection( aSel );
183 void ScTabViewShell::InsertURLField( const OUString& rName, const OUString& rURL, const OUString& rTarget )
185 SvxURLField aURLField( rURL, rName, SvxURLFormat::Repr );
186 aURLField.SetTargetFrame( rTarget );
187 SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
189 ScViewData& rViewData = GetViewData();
190 ScModule* pScMod = SC_MOD();
191 ScInputHandler* pHdl = pScMod->GetInputHdl( rViewData.GetViewShell() );
193 bool bSelectFirst = false;
194 bool bIsEditMode = pScMod->IsEditMode();
195 int nSelInd = 1;
196 OUString sSeltext(GetSelectionText());
198 if ( !bIsEditMode )
200 if ( !SelectionEditable() )
202 // no error message (may be called from drag&drop)
203 return;
206 // single url in cell is shown in the dialog and replaced
207 bSelectFirst = HasBookmarkAtCursor( nullptr );
208 pScMod->SetInputMode( SC_INPUT_TABLE );
211 EditView* pTopView = pHdl->GetTopView();
212 EditView* pTableView = pHdl->GetTableView();
213 OSL_ENSURE( pTopView || pTableView, "No EditView" );
215 // Check if user selected a whole cell by single click,
216 // cell has content, and user didn't change the name/text
217 // of the link something different than the content via the hyperlink dialog.
218 // If true, assign the given hyperlink to the whole content
219 // instead of inserting a duplicate, or appending the url.
220 if (comphelper::LibreOfficeKit::isActive() && !bIsEditMode && !bSelectFirst
221 && pTableView && !sSeltext.isEmpty() && sSeltext == rName)
223 nSelInd = sSeltext.getLength();
224 bSelectFirst = true;
227 if ( bSelectFirst )
229 if ( pTopView )
230 pTopView->SetSelection( ESelection(0,0,0,1) );
231 if ( pTableView )
232 pTableView->SetSelection( ESelection(0,0,0,nSelInd) );
235 pHdl->DataChanging();
237 if ( pTopView )
239 pTopView->InsertField( aURLItem );
240 lcl_SelectFieldAfterInsert( *pTopView );
242 if ( pTableView )
244 pTableView->InsertField( aURLItem );
245 lcl_SelectFieldAfterInsert( *pTableView );
248 pHdl->DataChanged();
251 void ScTabViewShell::ExecSearch( SfxRequest& rReq )
253 const SfxItemSet* pReqArgs = rReq.GetArgs();
254 sal_uInt16 nSlot = rReq.GetSlot();
255 const SfxPoolItem* pItem;
257 switch ( nSlot )
259 case FID_SEARCH_NOW:
261 if ( pReqArgs &&
262 SfxItemState::SET == pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem) )
264 assert( dynamic_cast<const SvxSearchItem*>( pItem) && "wrong Item" );
265 const SvxSearchItem* pSearchItem = static_cast<const SvxSearchItem*>(pItem);
267 ScGlobal::SetSearchItem( *pSearchItem );
268 SearchAndReplace( pSearchItem, true, rReq.IsAPI() );
269 rReq.Done();
272 break;
274 case SID_SEARCH_ITEM:
275 if (pReqArgs && SfxItemState::SET ==
276 pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem))
278 // remember search item
279 assert( dynamic_cast<const SvxSearchItem*>( pItem) && "wrong Item" );
280 ScGlobal::SetSearchItem( *static_cast<const SvxSearchItem*>(pItem ));
282 else
284 OSL_FAIL("SID_SEARCH_ITEM without Parameter");
286 break;
287 case FID_SEARCH:
288 case FID_REPLACE:
289 case FID_REPLACE_ALL:
290 case FID_SEARCH_ALL:
292 if (pReqArgs && SfxItemState::SET == pReqArgs->GetItemState(nSlot, false, &pItem))
294 // get search item
296 SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
298 // fill search item
300 aSearchItem.SetSearchString(static_cast<const SfxStringItem*>(pItem)->GetValue());
301 if(SfxItemState::SET == pReqArgs->GetItemState(FN_PARAM_1, false, &pItem))
302 aSearchItem.SetReplaceString(static_cast<const SfxStringItem*>(pItem)->GetValue());
304 if (nSlot == FID_SEARCH)
305 aSearchItem.SetCommand(SvxSearchCmd::FIND);
306 else if(nSlot == FID_REPLACE)
307 aSearchItem.SetCommand(SvxSearchCmd::REPLACE);
308 else if(nSlot == FID_REPLACE_ALL)
309 aSearchItem.SetCommand(SvxSearchCmd::REPLACE_ALL);
310 else
311 aSearchItem.SetCommand(SvxSearchCmd::FIND_ALL);
313 // execute request (which stores the SearchItem)
315 aSearchItem.SetWhich(SID_SEARCH_ITEM);
316 GetViewData().GetDispatcher().ExecuteList(FID_SEARCH_NOW,
317 rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
318 SfxCallMode::RECORD,
319 { &aSearchItem });
321 else
323 GetViewData().GetDispatcher().Execute(
324 SID_SEARCH_DLG, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD );
327 break;
328 case FID_REPEAT_SEARCH:
330 // once more with ScGlobal::GetSearchItem()
332 SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
333 aSearchItem.SetWhich(SID_SEARCH_ITEM);
334 GetViewData().GetDispatcher().ExecuteList( FID_SEARCH_NOW,
335 rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
336 SfxCallMode::RECORD,
337 { &aSearchItem });
339 break;
340 // case FID_SEARCH_COUNT:
344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */