Line data Source code
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 <editeng/eeitem.hxx>
22 :
23 : #include "scitems.hxx"
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/viewfrm.hxx>
30 : #include <sfx2/request.hxx>
31 : #include <sfx2/objface.hxx>
32 : #include <svl/stritem.hxx>
33 :
34 : #include "tabvwsh.hxx"
35 : #include "sc.hrc"
36 : #include "scmod.hxx"
37 : #include "impex.hxx"
38 : #include "editsh.hxx"
39 : #include "dociter.hxx"
40 : #include "inputhdl.hxx"
41 : #include "document.hxx"
42 :
43 : //==================================================================
44 :
45 0 : String ScTabViewShell::GetSelectionText( sal_Bool bWholeWord )
46 : {
47 0 : String aStrSelection;
48 :
49 0 : if ( pEditShell && pEditShell == GetMySubShell() )
50 : {
51 0 : aStrSelection = pEditShell->GetSelectionText( bWholeWord );
52 : }
53 : else
54 : {
55 0 : ScRange aRange;
56 :
57 0 : if ( GetViewData()->GetSimpleArea( aRange ) == SC_MARK_SIMPLE )
58 : {
59 0 : ScDocument* pDoc = GetViewData()->GetDocument();
60 0 : if ( bInFormatDialog && aRange.aStart.Row() != aRange.aEnd.Row() )
61 : {
62 : // Range auf eine Datenzeile begrenzen
63 : // (nur wenn der Aufruf aus einem Format-Dialog kommt)
64 0 : ScHorizontalCellIterator aIter( pDoc, aRange.aStart.Tab(),
65 0 : aRange.aStart.Col(), aRange.aStart.Row(),
66 0 : aRange.aEnd.Col(), aRange.aEnd.Row() );
67 : SCCOL nCol;
68 : SCROW nRow;
69 0 : if ( aIter.GetNext( nCol, nRow ) )
70 : {
71 0 : aRange.aStart.SetCol( nCol );
72 0 : aRange.aStart.SetRow( nRow );
73 0 : aRange.aEnd.SetRow( nRow );
74 : }
75 : else
76 0 : aRange.aEnd = aRange.aStart;
77 : }
78 : else
79 : {
80 : // #i111531# with 1M rows it was necessary to limit the range
81 : // to the actually used data area.
82 : SCCOL nCol1, nCol2;
83 : SCROW nRow1, nRow2;
84 : SCTAB nTab1, nTab2;
85 0 : aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
86 : bool bShrunk;
87 0 : pDoc->ShrinkToUsedDataArea( bShrunk, nTab1, nCol1, nRow1, nCol2, nRow2, false);
88 0 : if (bShrunk)
89 : {
90 0 : aRange.aStart.SetCol( nCol1 );
91 0 : aRange.aStart.SetRow( nRow1 );
92 0 : aRange.aEnd.SetCol( nCol2 );
93 0 : aRange.aEnd.SetRow( nRow2 );
94 : }
95 : }
96 :
97 0 : ScImportExport aObj( pDoc, aRange );
98 0 : aObj.SetFormulas( GetViewData()->GetOptions().GetOption( VOPT_FORMULAS ) );
99 0 : OUString aExportOUString;
100 0 : aObj.ExportString( aExportOUString );
101 0 : aStrSelection = convertLineEnd(aExportOUString, LINEEND_CR);
102 :
103 : // Tab/CR durch Space ersetzen, wenn fuer Dialog oder per Basic/SelectionTextExt,
104 : // oder wenn es eine einzelne Zeile ist.
105 : // Sonst mehrzeilig mit Tabs beibehalten (z.B. Mail oder Basic/SelectionText).
106 : // Fuer Mail werden die Tabs dann spaeter in (mehrere) Spaces gewandelt.
107 :
108 0 : if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
109 : {
110 : xub_StrLen nAt;
111 0 : while ( (nAt = aStrSelection.Search( CHAR_CR )) != STRING_NOTFOUND )
112 0 : aStrSelection.SetChar( nAt, ' ' );
113 0 : while ( (nAt = aStrSelection.Search( '\t' )) != STRING_NOTFOUND )
114 0 : aStrSelection.SetChar( nAt, ' ' );
115 :
116 0 : aStrSelection = comphelper::string::stripEnd(aStrSelection, ' ');
117 0 : }
118 : }
119 : }
120 :
121 0 : return aStrSelection;
122 : }
123 :
124 : //------------------------------------------------------------------------
125 :
126 0 : void ScTabViewShell::InsertURL( const String& rName, const String& rURL, const String& rTarget,
127 : sal_uInt16 nMode )
128 : {
129 0 : SvxLinkInsertMode eMode = (SvxLinkInsertMode) nMode;
130 0 : sal_Bool bAsText = ( eMode != HLINK_BUTTON ); // Default ist jetzt Text
131 :
132 0 : if ( bAsText )
133 : {
134 0 : if ( GetViewData()->IsActive() )
135 : {
136 : // if the view is active, always use InsertURLField, which starts EditMode
137 : // and selects the URL, so it can be changed from the URL bar / dialog
138 :
139 0 : InsertURLField( rName, rURL, rTarget );
140 : }
141 : else
142 : {
143 : // if the view is not active, InsertURLField doesn't work
144 : // -> use InsertBookmark to directly manipulate cell content
145 : // bTryReplace=sal_True -> if cell contains only one URL, replace it
146 :
147 0 : SCCOL nPosX = GetViewData()->GetCurX();
148 0 : SCROW nPosY = GetViewData()->GetCurY();
149 0 : InsertBookmark( rName, rURL, nPosX, nPosY, &rTarget, sal_True );
150 : }
151 : }
152 : else
153 : {
154 0 : SC_MOD()->InputEnterHandler();
155 0 : InsertURLButton( rName, rURL, rTarget );
156 : }
157 0 : }
158 :
159 : //------------------------------------------------------------------------
160 :
161 0 : static void lcl_SelectFieldAfterInsert( EditView& rView )
162 : {
163 0 : ESelection aSel = rView.GetSelection();
164 0 : if ( aSel.nStartPos == aSel.nEndPos && aSel.nStartPos > 0 )
165 : {
166 : // Cursor is behind the inserted field -> extend selection to the left
167 :
168 0 : --aSel.nStartPos;
169 0 : rView.SetSelection( aSel );
170 : }
171 0 : }
172 :
173 0 : void ScTabViewShell::InsertURLField( const String& rName, const String& rURL, const String& rTarget )
174 : {
175 0 : SvxURLField aURLField( rURL, rName, SVXURLFORMAT_REPR );
176 0 : aURLField.SetTargetFrame( rTarget );
177 0 : SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
178 :
179 0 : ScViewData* pViewData = GetViewData();
180 0 : ScModule* pScMod = SC_MOD();
181 0 : ScInputHandler* pHdl = pScMod->GetInputHdl( pViewData->GetViewShell() );
182 :
183 0 : sal_Bool bSelectFirst = false;
184 0 : if ( !pScMod->IsEditMode() )
185 : {
186 0 : if ( !SelectionEditable() )
187 : {
188 : // no error message (may be called from drag&drop)
189 0 : return;
190 : }
191 :
192 : // single url in cell is shown in the dialog and replaced
193 0 : bSelectFirst = HasBookmarkAtCursor( NULL );
194 0 : pScMod->SetInputMode( SC_INPUT_TABLE );
195 : }
196 :
197 0 : EditView* pTopView = pHdl->GetTopView();
198 0 : EditView* pTableView = pHdl->GetTableView();
199 : OSL_ENSURE( pTopView || pTableView, "No EditView" );
200 :
201 0 : if ( bSelectFirst )
202 : {
203 0 : if ( pTopView )
204 0 : pTopView->SetSelection( ESelection(0,0,0,1) );
205 0 : if ( pTableView )
206 0 : pTableView->SetSelection( ESelection(0,0,0,1) );
207 : }
208 :
209 0 : pHdl->DataChanging();
210 :
211 0 : if ( pTopView )
212 : {
213 0 : pTopView->InsertField( aURLItem );
214 0 : lcl_SelectFieldAfterInsert( *pTopView );
215 : }
216 0 : if ( pTableView )
217 : {
218 0 : pTableView->InsertField( aURLItem );
219 0 : lcl_SelectFieldAfterInsert( *pTableView );
220 : }
221 :
222 0 : pHdl->DataChanged();
223 : }
224 :
225 0 : void ScTabViewShell::ExecSearch( SfxRequest& rReq )
226 : {
227 0 : const SfxItemSet* pReqArgs = rReq.GetArgs();
228 0 : sal_uInt16 nSlot = rReq.GetSlot();
229 : const SfxPoolItem* pItem;
230 :
231 0 : switch ( nSlot )
232 : {
233 : case FID_SEARCH_NOW:
234 : {
235 0 : if ( pReqArgs &&
236 0 : SFX_ITEM_SET == pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem) )
237 : {
238 : OSL_ENSURE( pItem->ISA(SvxSearchItem), "falsches Item" );
239 0 : const SvxSearchItem* pSearchItem = (const SvxSearchItem*) pItem;
240 :
241 0 : ScGlobal::SetSearchItem( *pSearchItem );
242 0 : SearchAndReplace( pSearchItem, sal_True, rReq.IsAPI() );
243 0 : rReq.Done();
244 : }
245 : }
246 0 : break;
247 :
248 : case SID_SEARCH_ITEM:
249 0 : if (pReqArgs && SFX_ITEM_SET ==
250 0 : pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem))
251 : {
252 : // Search-Item merken
253 : OSL_ENSURE( pItem->ISA(SvxSearchItem), "falsches Item" );
254 0 : ScGlobal::SetSearchItem( *(const SvxSearchItem*) pItem );
255 : }
256 : else
257 : {
258 : OSL_FAIL("SID_SEARCH_ITEM ohne Parameter");
259 : }
260 0 : break;
261 : case FID_SEARCH:
262 : case FID_REPLACE:
263 : case FID_REPLACE_ALL:
264 : case FID_SEARCH_ALL:
265 : {
266 0 : if (pReqArgs && SFX_ITEM_SET == pReqArgs->GetItemState(nSlot, false, &pItem))
267 : {
268 : // SearchItem holen
269 :
270 0 : SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
271 :
272 : // SearchItem fuellen
273 :
274 0 : aSearchItem.SetSearchString(((SfxStringItem*)pItem)->GetValue());
275 0 : if(SFX_ITEM_SET == pReqArgs->GetItemState(FN_PARAM_1, false, &pItem))
276 0 : aSearchItem.SetReplaceString(((SfxStringItem*)pItem)->GetValue());
277 :
278 0 : if (nSlot == FID_SEARCH)
279 0 : aSearchItem.SetCommand(SVX_SEARCHCMD_FIND);
280 0 : else if(nSlot == FID_REPLACE)
281 0 : aSearchItem.SetCommand(SVX_SEARCHCMD_REPLACE);
282 0 : else if(nSlot == FID_REPLACE_ALL)
283 0 : aSearchItem.SetCommand(SVX_SEARCHCMD_REPLACE_ALL);
284 : else
285 0 : aSearchItem.SetCommand(SVX_SEARCHCMD_FIND_ALL);
286 :
287 : // Request ausfuehren (dabei wird das SearchItem gespeichert)
288 :
289 0 : aSearchItem.SetWhich(SID_SEARCH_ITEM);
290 0 : GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
291 0 : rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
292 : SFX_CALLMODE_STANDARD,
293 0 : &aSearchItem, 0L );
294 : }
295 : else
296 : {
297 0 : GetViewData()->GetDispatcher().Execute(
298 0 : SID_SEARCH_DLG, SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD );
299 : }
300 : }
301 0 : break;
302 : case FID_REPEAT_SEARCH:
303 : {
304 : // nochmal mit ScGlobal::GetSearchItem()
305 :
306 0 : SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
307 0 : aSearchItem.SetWhich(SID_SEARCH_ITEM);
308 0 : GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
309 0 : rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
310 : SFX_CALLMODE_STANDARD,
311 0 : &aSearchItem, 0L );
312 : }
313 0 : break;
314 : // case FID_SEARCH_COUNT:
315 : }
316 93 : }
317 :
318 : //--------------------------------------------------------------------
319 :
320 :
321 :
322 :
323 :
324 :
325 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|