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 
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>
31 
32 #include <tabvwsh.hxx>
33 #include <sc.hrc>
34 #include <scmod.hxx>
35 #include <impex.hxx>
36 #include <editsh.hxx>
37 #include <dociter.hxx>
38 #include <inputhdl.hxx>
39 #include <document.hxx>
40 
41 OUString ScTabViewShell::GetSelectionText( bool bWholeWord )
42 {
43  OUString aStrSelection;
44 
45  if ( pEditShell && pEditShell.get() == GetMySubShell() )
46  {
47  aStrSelection = pEditShell->GetSelectionText( bWholeWord );
48  }
49  else
50  {
51  ScRange aRange;
52 
53  if ( GetViewData().GetSimpleArea( aRange ) == SC_MARK_SIMPLE )
54  {
56  if ( bInFormatDialog && aRange.aStart.Row() != aRange.aEnd.Row() )
57  {
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() );
63  SCCOL nCol;
64  SCROW nRow;
65  if ( aIter.GetNext( nCol, nRow ) )
66  {
67  aRange.aStart.SetCol( nCol );
68  aRange.aStart.SetRow( nRow );
69  aRange.aEnd.SetRow( nRow );
70  }
71  else
72  aRange.aEnd = aRange.aStart;
73  }
74  else
75  {
76  // #i111531# with 1M rows it was necessary to limit the range
77  // to the actually used data area.
78  SCCOL nCol1, nCol2;
79  SCROW nRow1, nRow2;
80  SCTAB nTab1, nTab2;
81  aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
82  bool bShrunk;
83  rDoc.ShrinkToUsedDataArea( bShrunk, nTab1, nCol1, nRow1, nCol2, nRow2, false);
84  if (bShrunk)
85  {
86  aRange.aStart.SetCol( nCol1 );
87  aRange.aStart.SetRow( nRow1 );
88  aRange.aEnd.SetCol( nCol2 );
89  aRange.aEnd.SetRow( nRow2 );
90  }
91  }
92 
93  ScImportExport aObj( rDoc, aRange );
94  aObj.SetFormulas( GetViewData().GetOptions().GetOption( VOPT_FORMULAS ) );
95  OUString aExportOUString;
96  /* TODO: STRING_TSVC under some circumstances? */
97  aObj.ExportString( aExportOUString, SotClipboardFormatId::STRING );
98  aStrSelection = convertLineEnd(aExportOUString, LINEEND_CR);
99 
100  // replace Tab/CR with space, if for dialog or through Basic/SelectionTextExt,
101  // or when it is a single row.
102  // Otherwise keep Tabs in multi-row (for instance mail or Basic/SelectionText).
103  // for mail the Tabs are then later changed into (multiple) spaces.
104 
105  if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
106  {
107  aStrSelection = aStrSelection.replaceAll("\r", " ");
108  aStrSelection = aStrSelection.replaceAll("\t", " ");
109  aStrSelection = comphelper::string::stripEnd(aStrSelection, ' ');
110  }
111  }
112  }
113 
114  return aStrSelection;
115 }
116 
117 void ScTabViewShell::InsertURL( const OUString& rName, const OUString& rURL, const OUString& rTarget,
118  sal_uInt16 nMode )
119 {
120  SvxLinkInsertMode eMode = static_cast<SvxLinkInsertMode>(nMode);
121  bool bAsText = ( eMode != HLINK_BUTTON ); // default is now text
122 
123  if ( bAsText )
124  {
125  if ( GetViewData().IsActive() )
126  {
127  // if the view is active, always use InsertURLField, which starts EditMode
128  // and selects the URL, so it can be changed from the URL bar / dialog
129 
130  InsertURLField( rName, rURL, rTarget );
131  }
132  else
133  {
134  // if the view is not active, InsertURLField doesn't work
135  // -> use InsertBookmark to directly manipulate cell content
136  // bTryReplace=sal_True -> if cell contains only one URL, replace it
137 
138  SCCOL nPosX = GetViewData().GetCurX();
139  SCROW nPosY = GetViewData().GetCurY();
140  InsertBookmark( rName, rURL, nPosX, nPosY, &rTarget, true );
141  }
142  }
143  else
144  {
145  SC_MOD()->InputEnterHandler();
146  InsertURLButton( rName, rURL, rTarget, nullptr );
147  }
148 }
149 
151 {
152  ESelection aSel = rView.GetSelection();
153  if ( aSel.nStartPos == aSel.nEndPos && aSel.nStartPos > 0 )
154  {
155  // Cursor is behind the inserted field -> extend selection to the left
156 
157  --aSel.nStartPos;
158  rView.SetSelection( aSel );
159  }
160 }
161 
162 void ScTabViewShell::InsertURLField( const OUString& rName, const OUString& rURL, const OUString& rTarget )
163 {
164  SvxURLField aURLField( rURL, rName, SvxURLFormat::Repr );
165  aURLField.SetTargetFrame( rTarget );
166  SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
167 
168  ScViewData& rViewData = GetViewData();
169  ScModule* pScMod = SC_MOD();
170  ScInputHandler* pHdl = pScMod->GetInputHdl( rViewData.GetViewShell() );
171 
172  bool bSelectFirst = false;
173  bool bIsEditMode = pScMod->IsEditMode();
174  int nSelInd = 1;
175  OUString sSeltext(GetSelectionText());
176 
177  if ( !bIsEditMode )
178  {
179  if ( !SelectionEditable() )
180  {
181  // no error message (may be called from drag&drop)
182  return;
183  }
184 
185  // single url in cell is shown in the dialog and replaced
186  bSelectFirst = HasBookmarkAtCursor( nullptr );
187  pScMod->SetInputMode( SC_INPUT_TABLE );
188  }
189 
190  EditView* pTopView = pHdl->GetTopView();
191  EditView* pTableView = pHdl->GetTableView();
192  OSL_ENSURE( pTopView || pTableView, "No EditView" );
193 
194  // Check if user selected a whole cell by single click,
195  // cell has content, and user didn't change the name/text
196  // of the link something different than the content via the hyperlink dialog.
197  // If true, assign the given hyperlink to the whole content
198  // instead of inserting a duplicate, or appending the url.
199  if (comphelper::LibreOfficeKit::isActive() && !bIsEditMode && !bSelectFirst
200  && pTableView && !sSeltext.isEmpty() && sSeltext == rName)
201  {
202  nSelInd = sSeltext.getLength();
203  bSelectFirst = true;
204  }
205 
206  if ( bSelectFirst )
207  {
208  if ( pTopView )
209  pTopView->SetSelection( ESelection(0,0,0,1) );
210  if ( pTableView )
211  pTableView->SetSelection( ESelection(0,0,0,nSelInd) );
212  }
213 
214  pHdl->DataChanging();
215 
216  if ( pTopView )
217  {
218  pTopView->InsertField( aURLItem );
219  lcl_SelectFieldAfterInsert( *pTopView );
220  }
221  if ( pTableView )
222  {
223  pTableView->InsertField( aURLItem );
224  lcl_SelectFieldAfterInsert( *pTableView );
225  }
226 
227  pHdl->DataChanged();
228 }
229 
231 {
232  const SfxItemSet* pReqArgs = rReq.GetArgs();
233  sal_uInt16 nSlot = rReq.GetSlot();
234  const SfxPoolItem* pItem;
235 
236  switch ( nSlot )
237  {
238  case FID_SEARCH_NOW:
239  {
240  if ( pReqArgs &&
241  SfxItemState::SET == pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem) )
242  {
243  OSL_ENSURE( dynamic_cast<const SvxSearchItem*>( pItem) != nullptr, "wrong Item" );
244  const SvxSearchItem* pSearchItem = static_cast<const SvxSearchItem*>(pItem);
245 
246  ScGlobal::SetSearchItem( *pSearchItem );
247  SearchAndReplace( pSearchItem, true, rReq.IsAPI() );
248  rReq.Done();
249  }
250  }
251  break;
252 
253  case SID_SEARCH_ITEM:
254  if (pReqArgs && SfxItemState::SET ==
255  pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem))
256  {
257  // remember search item
258  OSL_ENSURE( dynamic_cast<const SvxSearchItem*>( pItem) != nullptr, "wrong Item" );
259  ScGlobal::SetSearchItem( *static_cast<const SvxSearchItem*>(pItem ));
260  }
261  else
262  {
263  OSL_FAIL("SID_SEARCH_ITEM without Parameter");
264  }
265  break;
266  case FID_SEARCH:
267  case FID_REPLACE:
268  case FID_REPLACE_ALL:
269  case FID_SEARCH_ALL:
270  {
271  if (pReqArgs && SfxItemState::SET == pReqArgs->GetItemState(nSlot, false, &pItem))
272  {
273  // get search item
274 
275  SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
276 
277  // fill search item
278 
279  aSearchItem.SetSearchString(static_cast<const SfxStringItem*>(pItem)->GetValue());
280  if(SfxItemState::SET == pReqArgs->GetItemState(FN_PARAM_1, false, &pItem))
281  aSearchItem.SetReplaceString(static_cast<const SfxStringItem*>(pItem)->GetValue());
282 
283  if (nSlot == FID_SEARCH)
284  aSearchItem.SetCommand(SvxSearchCmd::FIND);
285  else if(nSlot == FID_REPLACE)
286  aSearchItem.SetCommand(SvxSearchCmd::REPLACE);
287  else if(nSlot == FID_REPLACE_ALL)
288  aSearchItem.SetCommand(SvxSearchCmd::REPLACE_ALL);
289  else
290  aSearchItem.SetCommand(SvxSearchCmd::FIND_ALL);
291 
292  // execute request (which stores the SearchItem)
293 
294  aSearchItem.SetWhich(SID_SEARCH_ITEM);
295  GetViewData().GetDispatcher().ExecuteList(FID_SEARCH_NOW,
296  rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
297  SfxCallMode::RECORD,
298  { &aSearchItem });
299  }
300  else
301  {
303  SID_SEARCH_DLG, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD );
304  }
305  }
306  break;
307  case FID_REPEAT_SEARCH:
308  {
309  // once more with ScGlobal::GetSearchItem()
310 
311  SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
312  aSearchItem.SetWhich(SID_SEARCH_ITEM);
313  GetViewData().GetDispatcher().ExecuteList( FID_SEARCH_NOW,
314  rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
315  SfxCallMode::RECORD,
316  { &aSearchItem });
317  }
318  break;
319 // case FID_SEARCH_COUNT:
320  }
321 }
322 
323 /* 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:500
TOOLS_DLLPUBLIC OString convertLineEnd(const OString &rIn, LineEnd eLineEnd)
bool IsEditMode()
Definition: scmod.cxx:1342
SCROW Row() const
Definition: address.hxx:262
bool ExportString(OUString &, SotClipboardFormatId)
Definition: impex.cxx:328
std::string GetValue
bool DataChanging(sal_Unicode cTyped=0, bool bFromCommand=false)
Definition: inputhdl.cxx:2574
void SetSearchString(const OUString &rNewString)
SCROW GetCurY() const
Definition: viewdata.hxx:402
ScAddress aEnd
Definition: address.hxx:501
void Done(bool bRemove=false)
ScDocument & GetDocument() const
Definition: viewdata.hxx:380
static void lcl_SelectFieldAfterInsert(EditView &rView)
Definition: tabvwshe.cxx:150
const SfxItemSet * GetArgs() const
void SetReplaceString(const OUString &rNewString)
LINEEND_CR
EditView * GetTableView()
Definition: inputhdl.hxx:237
std::unique_ptr< ScEditShell > pEditShell
Definition: tabvwsh.hxx:100
bool SelectionEditable(bool *pOnlyNotBecauseOfMatrix=nullptr)
Definition: viewfunc.cxx:259
virtual OUString GetSelectionText(bool bWholeWord=false) override
Definition: tabvwshe.cxx:41
bool HasBookmarkAtCursor(SvxHyperlinkItem *pContent)
Definition: viewfun4.cxx:753
Mode eMode
SCTAB Tab() const
Definition: address.hxx:271
void SetRow(SCROW nRowP)
Definition: address.hxx:275
bool SearchAndReplace(const SvxSearchItem *pSearchItem, bool bAddUndo, bool bIsApi)
Definition: viewfun2.cxx:1900
static SC_DLLPUBLIC void SetSearchItem(const SvxSearchItem &rNew)
Definition: global.cxx:223
sal_Int32 nEndPos
void SetCol(SCCOL nColP)
Definition: address.hxx:279
ScViewData & GetViewData()
Definition: tabview.hxx:334
ScTabViewShell * GetViewShell() const
Definition: viewdata.hxx:357
void InsertField(const SvxFieldItem &rFld)
void InsertURL(const OUString &rName, const OUString &rURL, const OUString &rTarget, sal_uInt16 nMode)
Definition: tabvwshe.cxx:117
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) const
Shrink a range to only include used data area.
Definition: document.cxx:1069
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
void SetFormulas(bool b)
Definition: impex.hxx:133
HLINK_BUTTON
sal_Int16 SCCOL
Definition: types.hxx:22
#define SC_MOD()
Definition: scmod.hxx:250
void InsertBookmark(const OUString &rDescription, const OUString &rURL, SCCOL nPosX, SCROW nPosY, const OUString *pTarget=nullptr, bool bTryReplace=false)
Definition: viewfun4.cxx:694
SvxLinkInsertMode
void SetCommand(SvxSearchCmd nNewCommand)
bool IsActive() const
Definition: viewdata.hxx:382
bool bInFormatDialog
Definition: tabvwsh.hxx:145
void GetVars(SCCOL &nCol1, SCROW &nRow1, SCTAB &nTab1, SCCOL &nCol2, SCROW &nRow2, SCTAB &nTab2) const
Definition: address.hxx:693
void InsertURLField(const OUString &rName, const OUString &rURL, const OUString &rTarget)
Definition: tabvwshe.cxx:162
void ExecSearch(SfxRequest &rReq)
Definition: tabvwshe.cxx:230
SCCOL Col() const
Definition: address.hxx:267
void SetInputMode(ScInputMode eMode, const OUString *pInitText=nullptr)
Definition: scmod.cxx:1335
sal_Int32 SCROW
Definition: types.hxx:18
sal_uInt16 GetSlot() const
SfxShell * GetMySubShell() const
Definition: tabvwsh4.cxx:969
void SetWhich(sal_uInt16 nId)
SfxDispatcher & GetDispatcher()
Definition: viewdata.cxx:3040
void DataChanged(bool bFromTopNotify=false, bool bSetModified=true)
Definition: inputhdl.cxx:2586
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)
void InsertURLButton(const OUString &rName, const OUString &rURL, const OUString &rTarget, const Point *pInsPos)
Definition: tabvwshg.cxx:41
EditView * GetTopView()
Definition: inputhdl.hxx:238
sal_Int16 SCTAB
Definition: types.hxx:23
SCCOL GetCurX() const
Definition: viewdata.hxx:401
sal_Int32 nStartPos
static SC_DLLPUBLIC const SvxSearchItem & GetSearchItem()
Definition: global.cxx:212