LibreOffice Module sc (master) 1
tabvwshe.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 <comphelper/string.hxx>
21#include <comphelper/lok.hxx>
22#include <editeng/eeitem.hxx>
23#include <osl/diagnose.h>
24
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>
32
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>
41
42OUString ScTabViewShell::GetSelectionText( bool bWholeWord, bool bOnlyASample )
43{
44 OUString aStrSelection;
45
46 if ( pEditShell && pEditShell.get() == GetMySubShell() )
47 {
48 aStrSelection = pEditShell->GetSelectionText( bWholeWord );
49 }
50 else
51 {
52 ScRange aRange;
53
54 if ( GetViewData().GetSimpleArea( aRange ) == SC_MARK_SIMPLE )
55 {
57 if ( (bOnlyASample || bInFormatDialog) && aRange.aStart.Row() != aRange.aEnd.Row() )
58 {
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 ) )
67 {
68 aRange.aStart.SetCol( nCol );
69 aRange.aStart.SetRow( nRow );
70 aRange.aEnd.SetRow( nRow );
71 }
72 else
73 aRange.aEnd = aRange.aStart;
74 }
75 else
76 {
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)
86 {
87 aRange.aStart.SetCol( nCol1 );
88 aRange.aStart.SetRow( nRow1 );
89 aRange.aEnd.SetCol( nCol2 );
90 aRange.aEnd.SetRow( nRow2 );
91 }
92 }
93
94 ScImportExport aObj( rDoc, aRange );
95 // tdf#148437 - if cell contains a formula, overwrite entire content of the cell
96 aObj.SetFormulas(true);
97 OUString aExportOUString;
98 /* TODO: STRING_TSVC under some circumstances? */
99 aObj.ExportString( aExportOUString, SotClipboardFormatId::STRING );
100 aStrSelection = convertLineEnd(aExportOUString, LINEEND_CR);
101
102 // replace Tab/CR with space, if for dialog or through Basic/SelectionTextExt,
103 // or when it is a single row.
104 // Otherwise keep Tabs in multi-row (for instance mail or Basic/SelectionText).
105 // for mail the Tabs are then later changed into (multiple) spaces.
106
107 if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
108 {
109 aStrSelection = aStrSelection.replaceAll("\r", " ");
110 aStrSelection = aStrSelection.replaceAll("\t", " ");
111 aStrSelection = comphelper::string::stripEnd(aStrSelection, ' ');
112 }
113 }
114 }
115
116 return aStrSelection;
117}
118
120{
121 bool bRet = false;
122
123 if (pEditShell && pEditShell.get() == GetMySubShell())
124 {
125 bRet = pEditShell->ShouldDisableEditHyperlink();
126 }
127
128 return bRet;
129}
130
132{
133 if (pEditShell && pEditShell.get() == GetMySubShell())
134 {
135 pEditShell->EnableEditHyperlink();
136 }
137}
138
139void ScTabViewShell::InsertURL( const OUString& rName, const OUString& rURL, const OUString& rTarget,
140 sal_uInt16 nMode )
141{
142 SvxLinkInsertMode eMode = static_cast<SvxLinkInsertMode>(nMode);
143 bool bAsText = ( eMode != HLINK_BUTTON ); // default is now text
144
145 if ( bAsText )
146 {
147 if ( GetViewData().IsActive() )
148 {
149 // if the view is active, always use InsertURLField, which starts EditMode
150 // and selects the URL, so it can be changed from the URL bar / dialog
151
152 InsertURLField( rName, rURL, rTarget );
153 }
154 else
155 {
156 // if the view is not active, InsertURLField doesn't work
157 // -> use InsertBookmark to directly manipulate cell content
158 // bTryReplace=sal_True -> if cell contains only one URL, replace it
159
160 SCCOL nPosX = GetViewData().GetCurX();
161 SCROW nPosY = GetViewData().GetCurY();
162 InsertBookmark( rName, rURL, nPosX, nPosY, &rTarget, true );
163 }
164 }
165 else
166 {
167 SC_MOD()->InputEnterHandler();
168 InsertURLButton( rName, rURL, rTarget, nullptr );
169 }
170}
171
173{
174 ESelection aSel = rView.GetSelection();
175 if ( aSel.nStartPos == aSel.nEndPos && aSel.nStartPos > 0 )
176 {
177 // Cursor is behind the inserted field -> extend selection to the left
178
179 --aSel.nStartPos;
180 rView.SetSelection( aSel );
181 }
182}
183
184void ScTabViewShell::InsertURLField( const OUString& rName, const OUString& rURL, const OUString& rTarget )
185{
186 SvxURLField aURLField( rURL, rName, SvxURLFormat::Repr );
187 aURLField.SetTargetFrame( rTarget );
188 SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
189
190 ScViewData& rViewData = GetViewData();
191 ScModule* pScMod = SC_MOD();
192 ScInputHandler* pHdl = pScMod->GetInputHdl( rViewData.GetViewShell() );
193
194 bool bSelectFirst = false;
195 bool bIsEditMode = pScMod->IsEditMode();
196 int nSelInd = 1;
197 OUString sSeltext(GetSelectionText());
198
199 if ( !bIsEditMode )
200 {
201 if ( !SelectionEditable() )
202 {
203 // no error message (may be called from drag&drop)
204 return;
205 }
206
207 // single url in cell is shown in the dialog and replaced
208 bSelectFirst = HasBookmarkAtCursor( nullptr );
209 pScMod->SetInputMode( SC_INPUT_TABLE );
210 }
211
212 EditView* pTopView = pHdl->GetTopView();
213 EditView* pTableView = pHdl->GetTableView();
214 OSL_ENSURE( pTopView || pTableView, "No EditView" );
215
216 // Check if user selected a whole cell by single click, and cell has content.
217 // tdf#80043 - if true, replace the entire content of the selected cell instead of
218 // inserting a duplicate, or appending the url.
219 if (!bIsEditMode && !bSelectFirst && pTableView && !sSeltext.isEmpty())
220 {
221 nSelInd = sSeltext.getLength();
222 bSelectFirst = true;
223 }
224
225 if ( bSelectFirst )
226 {
227 if ( pTopView )
228 pTopView->SetSelection( ESelection(0,0,0,1) );
229 if ( pTableView )
230 pTableView->SetSelection( ESelection(0,0,0,nSelInd) );
231 }
232
233 pHdl->DataChanging();
234
235 if ( pTopView )
236 {
237 pTopView->InsertField( aURLItem );
238 lcl_SelectFieldAfterInsert( *pTopView );
239 }
240 if ( pTableView )
241 {
242 pTableView->InsertField( aURLItem );
243 lcl_SelectFieldAfterInsert( *pTableView );
244 }
245
246 pHdl->DataChanged();
247}
248
250{
251 const SfxItemSet* pReqArgs = rReq.GetArgs();
252 sal_uInt16 nSlot = rReq.GetSlot();
253 const SfxPoolItem* pItem;
254
255 switch ( nSlot )
256 {
257 case FID_SEARCH_NOW:
258 {
259 const SvxSearchItem* pSearchItem;
260 if ( pReqArgs &&
261 (pSearchItem = pReqArgs->GetItemIfSet(SID_SEARCH_ITEM, false)) )
262 {
263 ScGlobal::SetSearchItem( *pSearchItem );
264 SearchAndReplace( pSearchItem, true, rReq.IsAPI() );
265 rReq.Done();
266 }
267 }
268 break;
269
270 case SID_SEARCH_ITEM:
271 {
272 const SvxSearchItem* pSearchItem;
273 if (pReqArgs && (pSearchItem =
274 pReqArgs->GetItemIfSet(SID_SEARCH_ITEM, false)))
275 {
276 // remember search item
277 ScGlobal::SetSearchItem( *pSearchItem );
278 }
279 else
280 {
281 OSL_FAIL("SID_SEARCH_ITEM without Parameter");
282 }
283 break;
284 }
285 case FID_SEARCH:
286 case FID_REPLACE:
287 case FID_REPLACE_ALL:
288 case FID_SEARCH_ALL:
289 {
290 if (pReqArgs && SfxItemState::SET == pReqArgs->GetItemState(nSlot, false, &pItem))
291 {
292 // get search item
293
295
296 // fill search item
297
298 aSearchItem.SetSearchString(static_cast<const SfxStringItem*>(pItem)->GetValue());
299 if(SfxItemState::SET == pReqArgs->GetItemState(FN_PARAM_1, false, &pItem))
300 aSearchItem.SetReplaceString(static_cast<const SfxStringItem*>(pItem)->GetValue());
301
302 if (nSlot == FID_SEARCH)
303 aSearchItem.SetCommand(SvxSearchCmd::FIND);
304 else if(nSlot == FID_REPLACE)
305 aSearchItem.SetCommand(SvxSearchCmd::REPLACE);
306 else if(nSlot == FID_REPLACE_ALL)
307 aSearchItem.SetCommand(SvxSearchCmd::REPLACE_ALL);
308 else
309 aSearchItem.SetCommand(SvxSearchCmd::FIND_ALL);
310
311 // execute request (which stores the SearchItem)
312
313 aSearchItem.SetWhich(SID_SEARCH_ITEM);
314 GetViewData().GetDispatcher().ExecuteList(FID_SEARCH_NOW,
315 rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
316 SfxCallMode::RECORD,
317 { &aSearchItem });
318 }
319 else
320 {
322 SID_SEARCH_DLG, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD );
323 }
324 }
325 break;
326 case FID_REPEAT_SEARCH:
327 {
328 // once more with ScGlobal::GetSearchItem()
329
331 aSearchItem.SetWhich(SID_SEARCH_ITEM);
332 GetViewData().GetDispatcher().ExecuteList( FID_SEARCH_NOW,
333 rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
334 SfxCallMode::RECORD,
335 { &aSearchItem });
336 }
337 break;
338// case FID_SEARCH_COUNT:
339 }
340}
341
342/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ESelection GetSelection() const
void SetSelection(const ESelection &rNewSel)
void InsertField(const SvxFieldItem &rFld)
SCTAB Tab() const
Definition: address.hxx:283
void SetCol(SCCOL nColP)
Definition: address.hxx:291
SCROW Row() const
Definition: address.hxx:274
void SetRow(SCROW nRowP)
Definition: address.hxx:287
SCCOL Col() const
Definition: address.hxx:279
bool ShrinkToUsedDataArea(bool &o_bShrunk, SCTAB nTab, SCCOL &rStartCol, SCROW &rStartRow, SCCOL &rEndCol, SCROW &rEndRow, bool bColumnsOnly, bool bStickyTopRow=false, bool bStickyLeftCol=false, ScDataAreaExtras *pDataAreaExtras=nullptr) const
Shrink a range to only include used data area.
Definition: document.cxx:1049
static SC_DLLPUBLIC void SetSearchItem(const SvxSearchItem &rNew)
Definition: global.cxx:238
static SC_DLLPUBLIC const SvxSearchItem & GetSearchItem()
Definition: global.cxx:227
ScRefCellValue * GetNext(SCCOL &rCol, SCROW &rRow)
Definition: dociter.cxx:1096
void SetFormulas(bool b)
Definition: impex.hxx:135
bool ExportString(OUString &, SotClipboardFormatId)
Definition: impex.cxx:340
void DataChanged(bool bFromTopNotify=false, bool bSetModified=true)
Definition: inputhdl.cxx:2722
EditView * GetTopView()
Definition: inputhdl.hxx:239
bool DataChanging(sal_Unicode cTyped=0, bool bFromCommand=false)
Definition: inputhdl.cxx:2710
EditView * GetTableView()
Definition: inputhdl.hxx:238
ScInputHandler * GetInputHdl(ScTabViewShell *pViewSh=nullptr, bool bUseRef=true)
Input-Handler.
Definition: scmod.cxx:1355
void SetInputMode(ScInputMode eMode, const OUString *pInitText=nullptr)
Definition: scmod.cxx:1386
bool IsEditMode()
Definition: scmod.cxx:1393
void GetVars(SCCOL &nCol1, SCROW &nRow1, SCTAB &nTab1, SCCOL &nCol2, SCROW &nRow2, SCTAB &nTab2) const
Definition: address.hxx:690
ScAddress aEnd
Definition: address.hxx:498
ScAddress aStart
Definition: address.hxx:497
void ExecSearch(SfxRequest &rReq)
Definition: tabvwshe.cxx:249
void InsertURLField(const OUString &rName, const OUString &rURL, const OUString &rTarget)
Definition: tabvwshe.cxx:184
void InsertURLButton(const OUString &rName, const OUString &rURL, const OUString &rTarget, const Point *pInsPos)
Definition: tabvwshg.cxx:42
void InsertURL(const OUString &rName, const OUString &rURL, const OUString &rTarget, sal_uInt16 nMode)
Definition: tabvwshe.cxx:139
SfxShell * GetMySubShell() const
Definition: tabvwsh4.cxx:1022
bool ShouldDisableEditHyperlink() const
return true if "Edit Hyperlink" in context menu should be disabled
Definition: tabvwshe.cxx:119
virtual OUString GetSelectionText(bool bWholeWord=false, bool bOnlyASample=false) override
Definition: tabvwshe.cxx:42
void EnableEditHyperlink()
force "Edit Hyperlink" to true, with the expectation that SID_EDIT_HYPERLINK is later Invalidated to ...
Definition: tabvwshe.cxx:131
std::unique_ptr< ScEditShell > pEditShell
Definition: tabvwsh.hxx:103
bool bInFormatDialog
Definition: tabvwsh.hxx:149
ScViewData & GetViewData()
Definition: tabview.hxx:344
SfxDispatcher & GetDispatcher()
Definition: viewdata.cxx:3140
ScDocument & GetDocument() const
Definition: viewdata.hxx:380
ScTabViewShell * GetViewShell() const
Definition: viewdata.hxx:357
bool IsActive() const
Definition: viewdata.hxx:382
SCROW GetCurY() const
Definition: viewdata.hxx:402
SCCOL GetCurX() const
Definition: viewdata.hxx:401
bool SelectionEditable(bool *pOnlyNotBecauseOfMatrix=nullptr)
Definition: viewfunc.cxx:274
bool HasBookmarkAtCursor(SvxHyperlinkItem *pContent)
Definition: viewfun4.cxx:752
bool SearchAndReplace(const SvxSearchItem *pSearchItem, bool bAddUndo, bool bIsApi)
Definition: viewfun2.cxx:1942
void InsertBookmark(const OUString &rDescription, const OUString &rURL, SCCOL nPosX, SCROW nPosY, const OUString *pTarget=nullptr, bool bTryReplace=false)
Definition: viewfun4.cxx:694
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
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 * >())
const T * GetItemIfSet(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
void SetWhich(sal_uInt16 nId)
sal_uInt16 GetSlot() const
const SfxItemSet * GetArgs() const
bool IsAPI() const
void Done(bool bRemove=false)
void SetReplaceString(const OUString &rNewString)
void SetCommand(SvxSearchCmd nNewCommand)
void SetSearchString(const OUString &rNewString)
void SetTargetFrame(const OUString &rFrm)
constexpr TypedWhichId< SvxFieldItem > EE_FEATURE_FIELD(EE_FEATURE_NOTCONV+1)
FilterGroup & rTarget
@ SC_INPUT_TABLE
Definition: global.hxx:363
SvxLinkInsertMode
HLINK_BUTTON
Mode eMode
LINEEND_CR
TOOLS_DLLPUBLIC OString convertLineEnd(const OString &rIn, LineEnd eLineEnd)
OString stripEnd(const OString &rIn, char c)
const char GetValue[]
#define SC_MOD()
Definition: scmod.hxx:247
sal_Int32 nEndPos
sal_Int32 nStartPos
static void lcl_SelectFieldAfterInsert(EditView &rView)
Definition: tabvwshe.cxx:172
sal_Int16 SCTAB
Definition: types.hxx:22
sal_Int16 SCCOL
Definition: types.hxx:21
sal_Int32 SCROW
Definition: types.hxx:17
@ SC_MARK_SIMPLE
Definition: viewdata.hxx:65