1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <editeng/eeitem.hxx>
22 #include <osl/diagnose.h>
24 #include <editeng/editview.hxx>
25 #include <editeng/flditem.hxx>
26 #include <svx/hlnkitem.hxx>
27 #include <svl/srchitem.hxx>
28 #include <sfx2/dispatch.hxx>
29 #include <sfx2/request.hxx>
30 #include <svl/stritem.hxx>
32 #include <tabvwsh.hxx>
37 #include <dociter.hxx>
38 #include <inputhdl.hxx>
39 #include <document.hxx>
41 OUString
ScTabViewShell::GetSelectionText( bool bWholeWord
, bool bOnlyASample
)
43 OUString aStrSelection
;
45 if ( pEditShell
&& pEditShell
.get() == GetMySubShell() )
47 aStrSelection
= pEditShell
->GetSelectionText( bWholeWord
);
53 if ( GetViewData().GetSimpleArea( aRange
) == SC_MARK_SIMPLE
)
55 ScDocument
& rDoc
= GetViewData().GetDocument();
56 if ( (bOnlyASample
|| bInFormatDialog
) && aRange
.aStart
.Row() != aRange
.aEnd
.Row() )
58 // limit range to one data row
59 // (only when the call comes from a format dialog)
60 ScHorizontalCellIterator
aIter( rDoc
, aRange
.aStart
.Tab(),
61 aRange
.aStart
.Col(), aRange
.aStart
.Row(),
62 aRange
.aEnd
.Col(), aRange
.aEnd
.Row() );
65 if ( aIter
.GetNext( nCol
, nRow
) )
67 aRange
.aStart
.SetCol( nCol
);
68 aRange
.aStart
.SetRow( nRow
);
69 aRange
.aEnd
.SetRow( nRow
);
72 aRange
.aEnd
= aRange
.aStart
;
76 // #i111531# with 1M rows it was necessary to limit the range
77 // to the actually used data area.
81 aRange
.GetVars( nCol1
, nRow1
, nTab1
, nCol2
, nRow2
, nTab2
);
83 rDoc
.ShrinkToUsedDataArea( bShrunk
, nTab1
, nCol1
, nRow1
, nCol2
, nRow2
, false);
86 aRange
.aStart
.SetCol( nCol1
);
87 aRange
.aStart
.SetRow( nRow1
);
88 aRange
.aEnd
.SetCol( nCol2
);
89 aRange
.aEnd
.SetRow( nRow2
);
93 ScImportExport
aObj( rDoc
, aRange
);
94 // tdf#148437 - if cell contains a formula, overwrite entire content of the cell
95 aObj
.SetFormulas(true);
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 void ScTabViewShell::InsertURL( const OUString
& rName
, const OUString
& rURL
, const OUString
& rTarget
,
121 SvxLinkInsertMode eMode
= static_cast<SvxLinkInsertMode
>(nMode
);
122 bool bAsText
= ( eMode
!= HLINK_BUTTON
); // default is now text
126 if ( GetViewData().IsActive() )
128 // if the view is active, always use InsertURLField, which starts EditMode
129 // and selects the URL, so it can be changed from the URL bar / dialog
131 InsertURLField( rName
, rURL
, rTarget
);
135 // if the view is not active, InsertURLField doesn't work
136 // -> use InsertBookmark to directly manipulate cell content
137 // bTryReplace=sal_True -> if cell contains only one URL, replace it
139 SCCOL nPosX
= GetViewData().GetCurX();
140 SCROW nPosY
= GetViewData().GetCurY();
141 InsertBookmark( rName
, rURL
, nPosX
, nPosY
, &rTarget
, true );
146 ScModule::get()->InputEnterHandler();
147 InsertURLButton( rName
, rURL
, rTarget
, nullptr );
151 static void lcl_SelectFieldAfterInsert( EditView
& rView
)
153 ESelection aSel
= rView
.GetSelection();
154 if (aSel
.start
.nIndex
== aSel
.end
.nIndex
&& aSel
.start
.nIndex
> 0)
156 // Cursor is behind the inserted field -> extend selection to the left
159 rView
.SetSelection( aSel
);
163 void ScTabViewShell::InsertURLField( const OUString
& rName
, const OUString
& rURL
, const OUString
& rTarget
)
165 SvxURLField
aURLField( rURL
, rName
, SvxURLFormat::Repr
);
166 aURLField
.SetTargetFrame( rTarget
);
167 SvxFieldItem
aURLItem( aURLField
, EE_FEATURE_FIELD
);
169 ScViewData
& rViewData
= GetViewData();
170 ScModule
* pScMod
= ScModule::get();
171 ScInputHandler
* pHdl
= pScMod
->GetInputHdl( rViewData
.GetViewShell() );
173 bool bSelectFirst
= false;
174 bool bIsEditMode
= pScMod
->IsEditMode();
176 OUString
sSeltext(GetSelectionText());
180 if ( !SelectionEditable() )
182 // no error message (may be called from drag&drop)
186 // single url in cell is shown in the dialog and replaced
187 bSelectFirst
= HasBookmarkAtCursor( nullptr );
188 pScMod
->SetInputMode( SC_INPUT_TABLE
);
191 EditView
* pTopView
= pHdl
->GetTopView();
192 EditView
* pTableView
= pHdl
->GetTableView();
193 OSL_ENSURE( pTopView
|| pTableView
, "No EditView" );
195 // Check if user selected a whole cell by single click, and cell has content.
196 // tdf#80043 - if true, replace the entire content of the selected cell instead of
197 // inserting a duplicate, or appending the url.
198 if (!bIsEditMode
&& !bSelectFirst
&& pTableView
&& !sSeltext
.isEmpty())
200 nSelInd
= sSeltext
.getLength();
207 pTopView
->SetSelection( ESelection(0,0,0,1) );
209 pTableView
->SetSelection( ESelection(0,0,0,nSelInd
) );
212 pHdl
->DataChanging();
216 pTopView
->InsertField( aURLItem
);
217 lcl_SelectFieldAfterInsert( *pTopView
);
221 pTableView
->InsertField( aURLItem
);
222 lcl_SelectFieldAfterInsert( *pTableView
);
228 void ScTabViewShell::ExecSearch( SfxRequest
& rReq
)
230 const SfxItemSet
* pReqArgs
= rReq
.GetArgs();
231 sal_uInt16 nSlot
= rReq
.GetSlot();
232 const SfxPoolItem
* pItem
;
238 const SvxSearchItem
* pSearchItem
;
240 (pSearchItem
= pReqArgs
->GetItemIfSet(SID_SEARCH_ITEM
, false)) )
242 ScGlobal::SetSearchItem( *pSearchItem
);
243 SearchAndReplace( pSearchItem
, true, rReq
.IsAPI() );
249 case SID_SEARCH_ITEM
:
251 const SvxSearchItem
* pSearchItem
;
252 if (pReqArgs
&& (pSearchItem
=
253 pReqArgs
->GetItemIfSet(SID_SEARCH_ITEM
, false)))
255 // remember search item
256 ScGlobal::SetSearchItem( *pSearchItem
);
260 OSL_FAIL("SID_SEARCH_ITEM without Parameter");
266 case FID_REPLACE_ALL
:
269 if (pReqArgs
&& SfxItemState::SET
== pReqArgs
->GetItemState(nSlot
, false, &pItem
))
273 SvxSearchItem aSearchItem
= ScGlobal::GetSearchItem();
277 aSearchItem
.SetSearchString(static_cast<const SfxStringItem
*>(pItem
)->GetValue());
278 if(SfxItemState::SET
== pReqArgs
->GetItemState(FN_PARAM_1
, false, &pItem
))
279 aSearchItem
.SetReplaceString(static_cast<const SfxStringItem
*>(pItem
)->GetValue());
281 if (nSlot
== FID_SEARCH
)
282 aSearchItem
.SetCommand(SvxSearchCmd::FIND
);
283 else if(nSlot
== FID_REPLACE
)
284 aSearchItem
.SetCommand(SvxSearchCmd::REPLACE
);
285 else if(nSlot
== FID_REPLACE_ALL
)
286 aSearchItem
.SetCommand(SvxSearchCmd::REPLACE_ALL
);
288 aSearchItem
.SetCommand(SvxSearchCmd::FIND_ALL
);
290 // execute request (which stores the SearchItem)
292 aSearchItem
.SetWhich(SID_SEARCH_ITEM
);
293 GetViewData().GetDispatcher().ExecuteList(FID_SEARCH_NOW
,
294 rReq
.IsAPI() ? SfxCallMode::API
|SfxCallMode::SYNCHRON
:
300 GetViewData().GetDispatcher().Execute(
301 SID_SEARCH_DLG
, SfxCallMode::ASYNCHRON
|SfxCallMode::RECORD
);
305 case FID_REPEAT_SEARCH
:
307 // once more with ScGlobal::GetSearchItem()
309 SvxSearchItem aSearchItem
= ScGlobal::GetSearchItem();
310 aSearchItem
.SetWhich(SID_SEARCH_ITEM
);
311 GetViewData().GetDispatcher().ExecuteList( FID_SEARCH_NOW
,
312 rReq
.IsAPI() ? SfxCallMode::API
|SfxCallMode::SYNCHRON
:
317 // case FID_SEARCH_COUNT:
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */