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 
42 OUString ScTabViewShell::GetSelectionText( bool bWholeWord )
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 ( 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  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);
100 
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.
105 
106  if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
107  {
108  aStrSelection = aStrSelection.replaceAll("\r", " ");
109  aStrSelection = aStrSelection.replaceAll("\t", " ");
110  aStrSelection = comphelper::string::stripEnd(aStrSelection, ' ');
111  }
112  }
113  }
114 
115  return aStrSelection;
116 }
117 
118 void ScTabViewShell::InsertURL( const OUString& rName, const OUString& rURL, const OUString& rTarget,
119  sal_uInt16 nMode )
120 {
121  SvxLinkInsertMode eMode = static_cast<SvxLinkInsertMode>(nMode);
122  bool bAsText = ( eMode != HLINK_BUTTON ); // default is now text
123 
124  if ( bAsText )
125  {
126  if ( GetViewData().IsActive() )
127  {
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
130 
131  InsertURLField( rName, rURL, rTarget );
132  }
133  else
134  {
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
138 
139  SCCOL nPosX = GetViewData().GetCurX();
140  SCROW nPosY = GetViewData().GetCurY();
141  InsertBookmark( rName, rURL, nPosX, nPosY, &rTarget, true );
142  }
143  }
144  else
145  {
146  SC_MOD()->InputEnterHandler();
147  InsertURLButton( rName, rURL, rTarget, nullptr );
148  }
149 }
150 
152 {
153  ESelection aSel = rView.GetSelection();
154  if ( aSel.nStartPos == aSel.nEndPos && aSel.nStartPos > 0 )
155  {
156  // Cursor is behind the inserted field -> extend selection to the left
157 
158  --aSel.nStartPos;
159  rView.SetSelection( aSel );
160  }
161 }
162 
163 void ScTabViewShell::InsertURLField( const OUString& rName, const OUString& rURL, const OUString& rTarget )
164 {
165  SvxURLField aURLField( rURL, rName, SvxURLFormat::Repr );
166  aURLField.SetTargetFrame( rTarget );
167  SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
168 
169  ScViewData& rViewData = GetViewData();
170  ScModule* pScMod = SC_MOD();
171  ScInputHandler* pHdl = pScMod->GetInputHdl( rViewData.GetViewShell() );
172 
173  bool bSelectFirst = false;
174  bool bIsEditMode = pScMod->IsEditMode();
175  int nSelInd = 1;
176  OUString sSeltext(GetSelectionText());
177 
178  if ( !bIsEditMode )
179  {
180  if ( !SelectionEditable() )
181  {
182  // no error message (may be called from drag&drop)
183  return;
184  }
185 
186  // single url in cell is shown in the dialog and replaced
187  bSelectFirst = HasBookmarkAtCursor( nullptr );
188  pScMod->SetInputMode( SC_INPUT_TABLE );
189  }
190 
191  EditView* pTopView = pHdl->GetTopView();
192  EditView* pTableView = pHdl->GetTableView();
193  OSL_ENSURE( pTopView || pTableView, "No EditView" );
194 
195  // Check if user selected a whole cell by single click,
196  // cell has content, and user didn't change the name/text
197  // of the link something different than the content via the hyperlink dialog.
198  // If true, assign the given hyperlink to the whole content
199  // instead of inserting a duplicate, or appending the url.
200  if (comphelper::LibreOfficeKit::isActive() && !bIsEditMode && !bSelectFirst
201  && pTableView && !sSeltext.isEmpty() && sSeltext == rName)
202  {
203  nSelInd = sSeltext.getLength();
204  bSelectFirst = true;
205  }
206 
207  if ( bSelectFirst )
208  {
209  if ( pTopView )
210  pTopView->SetSelection( ESelection(0,0,0,1) );
211  if ( pTableView )
212  pTableView->SetSelection( ESelection(0,0,0,nSelInd) );
213  }
214 
215  pHdl->DataChanging();
216 
217  if ( pTopView )
218  {
219  pTopView->InsertField( aURLItem );
220  lcl_SelectFieldAfterInsert( *pTopView );
221  }
222  if ( pTableView )
223  {
224  pTableView->InsertField( aURLItem );
225  lcl_SelectFieldAfterInsert( *pTableView );
226  }
227 
228  pHdl->DataChanged();
229 }
230 
232 {
233  const SfxItemSet* pReqArgs = rReq.GetArgs();
234  sal_uInt16 nSlot = rReq.GetSlot();
235  const SfxPoolItem* pItem;
236 
237  switch ( nSlot )
238  {
239  case FID_SEARCH_NOW:
240  {
241  if ( pReqArgs &&
242  SfxItemState::SET == pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem) )
243  {
244  OSL_ENSURE( dynamic_cast<const SvxSearchItem*>( pItem) != nullptr, "wrong Item" );
245  const SvxSearchItem* pSearchItem = static_cast<const SvxSearchItem*>(pItem);
246 
247  ScGlobal::SetSearchItem( *pSearchItem );
248  SearchAndReplace( pSearchItem, true, rReq.IsAPI() );
249  rReq.Done();
250  }
251  }
252  break;
253 
254  case SID_SEARCH_ITEM:
255  if (pReqArgs && SfxItemState::SET ==
256  pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem))
257  {
258  // remember search item
259  OSL_ENSURE( dynamic_cast<const SvxSearchItem*>( pItem) != nullptr, "wrong Item" );
260  ScGlobal::SetSearchItem( *static_cast<const SvxSearchItem*>(pItem ));
261  }
262  else
263  {
264  OSL_FAIL("SID_SEARCH_ITEM without Parameter");
265  }
266  break;
267  case FID_SEARCH:
268  case FID_REPLACE:
269  case FID_REPLACE_ALL:
270  case FID_SEARCH_ALL:
271  {
272  if (pReqArgs && SfxItemState::SET == pReqArgs->GetItemState(nSlot, false, &pItem))
273  {
274  // get search item
275 
276  SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
277 
278  // fill search item
279 
280  aSearchItem.SetSearchString(static_cast<const SfxStringItem*>(pItem)->GetValue());
281  if(SfxItemState::SET == pReqArgs->GetItemState(FN_PARAM_1, false, &pItem))
282  aSearchItem.SetReplaceString(static_cast<const SfxStringItem*>(pItem)->GetValue());
283 
284  if (nSlot == FID_SEARCH)
285  aSearchItem.SetCommand(SvxSearchCmd::FIND);
286  else if(nSlot == FID_REPLACE)
287  aSearchItem.SetCommand(SvxSearchCmd::REPLACE);
288  else if(nSlot == FID_REPLACE_ALL)
289  aSearchItem.SetCommand(SvxSearchCmd::REPLACE_ALL);
290  else
291  aSearchItem.SetCommand(SvxSearchCmd::FIND_ALL);
292 
293  // execute request (which stores the SearchItem)
294 
295  aSearchItem.SetWhich(SID_SEARCH_ITEM);
296  GetViewData().GetDispatcher().ExecuteList(FID_SEARCH_NOW,
297  rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
298  SfxCallMode::RECORD,
299  { &aSearchItem });
300  }
301  else
302  {
304  SID_SEARCH_DLG, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD );
305  }
306  }
307  break;
308  case FID_REPEAT_SEARCH:
309  {
310  // once more with ScGlobal::GetSearchItem()
311 
312  SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
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  break;
320 // case FID_SEARCH_COUNT:
321  }
322 }
323 
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OString stripEnd(const OString &rIn, char c)
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)
ScAddress aStart
Definition: address.hxx:499
TOOLS_DLLPUBLIC OString convertLineEnd(const OString &rIn, LineEnd eLineEnd)
bool IsEditMode()
Definition: scmod.cxx:1342
SCROW Row() const
Definition: address.hxx:261
bool ExportString(OUString &, SotClipboardFormatId)
Definition: impex.cxx:327
std::string GetValue
bool DataChanging(sal_Unicode cTyped=0, bool bFromCommand=false)
Definition: inputhdl.cxx:2590
void SetSearchString(const OUString &rNewString)
SCROW GetCurY() const
Definition: viewdata.hxx:401
ScAddress aEnd
Definition: address.hxx:500
void Done(bool bRemove=false)
ScDocument & GetDocument() const
Definition: viewdata.hxx:379
static void lcl_SelectFieldAfterInsert(EditView &rView)
Definition: tabvwshe.cxx:151
const SfxItemSet * GetArgs() const
void SetReplaceString(const OUString &rNewString)
LINEEND_CR
EditView * GetTableView()
Definition: inputhdl.hxx:236
std::unique_ptr< ScEditShell > pEditShell
Definition: tabvwsh.hxx:99
bool SelectionEditable(bool *pOnlyNotBecauseOfMatrix=nullptr)
Definition: viewfunc.cxx:260
virtual OUString GetSelectionText(bool bWholeWord=false) override
Definition: tabvwshe.cxx:42
bool HasBookmarkAtCursor(SvxHyperlinkItem *pContent)
Definition: viewfun4.cxx:755
Mode eMode
SCTAB Tab() const
Definition: address.hxx:270
void SetRow(SCROW nRowP)
Definition: address.hxx:274
bool SearchAndReplace(const SvxSearchItem *pSearchItem, bool bAddUndo, bool bIsApi)
Definition: viewfun2.cxx:1899
static SC_DLLPUBLIC void SetSearchItem(const SvxSearchItem &rNew)
Definition: global.cxx:224
sal_Int32 nEndPos
void SetCol(SCCOL nColP)
Definition: address.hxx:278
ScViewData & GetViewData()
Definition: tabview.hxx:333
ScTabViewShell * GetViewShell() const
Definition: viewdata.hxx:356
void InsertField(const SvxFieldItem &rFld)
void InsertURL(const OUString &rName, const OUString &rURL, const OUString &rTarget, sal_uInt16 nMode)
Definition: tabvwshe.cxx:118
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
void SetFormulas(bool b)
Definition: impex.hxx:132
HLINK_BUTTON
sal_Int16 SCCOL
Definition: types.hxx:21
#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
SvxLinkInsertMode
void SetCommand(SvxSearchCmd nNewCommand)
bool IsActive() const
Definition: viewdata.hxx:381
bool bInFormatDialog
Definition: tabvwsh.hxx:144
void GetVars(SCCOL &nCol1, SCROW &nRow1, SCTAB &nTab1, SCCOL &nCol2, SCROW &nRow2, SCTAB &nTab2) const
Definition: address.hxx:692
void InsertURLField(const OUString &rName, const OUString &rURL, const OUString &rTarget)
Definition: tabvwshe.cxx:163
void ExecSearch(SfxRequest &rReq)
Definition: tabvwshe.cxx:231
SCCOL Col() const
Definition: address.hxx:266
void SetInputMode(ScInputMode eMode, const OUString *pInitText=nullptr)
Definition: scmod.cxx:1335
sal_Int32 SCROW
Definition: types.hxx:17
sal_uInt16 GetSlot() const
SfxShell * GetMySubShell() const
Definition: tabvwsh4.cxx:987
void SetWhich(sal_uInt16 nId)
SfxDispatcher & GetDispatcher()
Definition: viewdata.cxx:3041
void DataChanged(bool bFromTopNotify=false, bool bSetModified=true)
Definition: inputhdl.cxx:2602
void SetSelection(const ESelection &rNewSel)
ESelection GetSelection() const
constexpr sal_uInt16 EE_FEATURE_FIELD(EE_FEATURE_NOTCONV+1)
ScInputHandler * GetInputHdl(ScTabViewShell *pViewSh=nullptr, bool bUseRef=true)
Input-Handler.
Definition: scmod.cxx:1304
bool IsAPI() const
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
bool ShrinkToUsedDataArea(bool &o_bShrunk, SCTAB nTab, SCCOL &rStartCol, SCROW &rStartRow, SCCOL &rEndCol, SCROW &rEndRow, bool bColumnsOnly, bool bStickyTopRow=false, bool bStickyLeftCol=false, bool bConsiderCellNotes=false, bool bConsiderCellDrawObjects=false, bool bConsiderCellFormats=false) const
Shrink a range to only include used data area.
Definition: document.cxx:1062
void InsertURLButton(const OUString &rName, const OUString &rURL, const OUString &rTarget, const Point *pInsPos)
Definition: tabvwshg.cxx:42
EditView * GetTopView()
Definition: inputhdl.hxx:237
sal_Int16 SCTAB
Definition: types.hxx:22
SCCOL GetCurX() const
Definition: viewdata.hxx:400
sal_Int32 nStartPos
static SC_DLLPUBLIC const SvxSearchItem & GetSearchItem()
Definition: global.cxx:213