tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / view / tabvwshe.cxx
blobc0e2372908be15ee4b4a1499fa7d878110c386c7
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 <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>
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>
41 OUString ScTabViewShell::GetSelectionText( bool bWholeWord, bool bOnlyASample )
43 OUString aStrSelection;
45 if ( pEditShell && pEditShell.get() == GetMySubShell() )
47 aStrSelection = pEditShell->GetSelectionText( bWholeWord );
49 else
51 ScRange aRange;
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() );
63 SCCOL nCol;
64 SCROW nRow;
65 if ( aIter.GetNext( nCol, nRow ) )
67 aRange.aStart.SetCol( nCol );
68 aRange.aStart.SetRow( nRow );
69 aRange.aEnd.SetRow( nRow );
71 else
72 aRange.aEnd = aRange.aStart;
74 else
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)
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,
119 sal_uInt16 nMode )
121 SvxLinkInsertMode eMode = static_cast<SvxLinkInsertMode>(nMode);
122 bool bAsText = ( eMode != HLINK_BUTTON ); // default is now text
124 if ( bAsText )
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 );
133 else
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 );
144 else
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
158 --aSel.start.nIndex;
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();
175 int nSelInd = 1;
176 OUString sSeltext(GetSelectionText());
178 if ( !bIsEditMode )
180 if ( !SelectionEditable() )
182 // no error message (may be called from drag&drop)
183 return;
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();
201 bSelectFirst = true;
204 if ( bSelectFirst )
206 if ( pTopView )
207 pTopView->SetSelection( ESelection(0,0,0,1) );
208 if ( pTableView )
209 pTableView->SetSelection( ESelection(0,0,0,nSelInd) );
212 pHdl->DataChanging();
214 if ( pTopView )
216 pTopView->InsertField( aURLItem );
217 lcl_SelectFieldAfterInsert( *pTopView );
219 if ( pTableView )
221 pTableView->InsertField( aURLItem );
222 lcl_SelectFieldAfterInsert( *pTableView );
225 pHdl->DataChanged();
228 void ScTabViewShell::ExecSearch( SfxRequest& rReq )
230 const SfxItemSet* pReqArgs = rReq.GetArgs();
231 sal_uInt16 nSlot = rReq.GetSlot();
232 const SfxPoolItem* pItem;
234 switch ( nSlot )
236 case FID_SEARCH_NOW:
238 const SvxSearchItem* pSearchItem;
239 if ( pReqArgs &&
240 (pSearchItem = pReqArgs->GetItemIfSet(SID_SEARCH_ITEM, false)) )
242 ScGlobal::SetSearchItem( *pSearchItem );
243 SearchAndReplace( pSearchItem, true, rReq.IsAPI() );
244 rReq.Done();
247 break;
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 );
258 else
260 OSL_FAIL("SID_SEARCH_ITEM without Parameter");
262 break;
264 case FID_SEARCH:
265 case FID_REPLACE:
266 case FID_REPLACE_ALL:
267 case FID_SEARCH_ALL:
269 if (pReqArgs && SfxItemState::SET == pReqArgs->GetItemState(nSlot, false, &pItem))
271 // get search item
273 SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
275 // fill search item
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);
287 else
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 :
295 SfxCallMode::RECORD,
296 { &aSearchItem });
298 else
300 GetViewData().GetDispatcher().Execute(
301 SID_SEARCH_DLG, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD );
304 break;
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 :
313 SfxCallMode::RECORD,
314 { &aSearchItem });
316 break;
317 // case FID_SEARCH_COUNT:
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */