xref: /trunk/main/sc/source/ui/view/tabvwshe.cxx (revision c5c7750d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 #include <editeng/eeitem.hxx>
31 
32 #include "scitems.hxx"
33 #include <editeng/editview.hxx>
34 #include <editeng/flditem.hxx>
35 #include <svx/hlnkitem.hxx>
36 #include <svl/srchitem.hxx>
37 #include <sfx2/dispatch.hxx>
38 #include <sfx2/viewfrm.hxx>
39 #include <sfx2/request.hxx>
40 #include <sfx2/objface.hxx>
41 #include <svl/stritem.hxx>
42 #include <vcl/sound.hxx>
43 
44 #include "tabvwsh.hxx"
45 #include "sc.hrc"
46 #include "scmod.hxx"
47 #include "impex.hxx"
48 #include "editsh.hxx"
49 #include "dociter.hxx"
50 #include "inputhdl.hxx"
51 #include <svx/srchdlg.hxx>
52 #include "document.hxx"
53 
54 //==================================================================
55 
GetSelectionText(sal_Bool bWholeWord)56 String __EXPORT ScTabViewShell::GetSelectionText( sal_Bool bWholeWord )
57 {
58 	String aStrSelection;
59 
60 	if ( pEditShell && pEditShell == GetMySubShell() )
61 	{
62 		aStrSelection = pEditShell->GetSelectionText( bWholeWord );
63 	}
64 	else
65 	{
66 		ScRange aRange;
67 
68 		if ( GetViewData()->GetSimpleArea( aRange ) == SC_MARK_SIMPLE )
69 		{
70 			ScDocument* pDoc = GetViewData()->GetDocument();
71 			if ( bInFormatDialog && aRange.aStart.Row() != aRange.aEnd.Row() )
72 			{
73 				// Range auf eine Datenzeile begrenzen
74 				// (#48613# nur wenn der Aufruf aus einem Format-Dialog kommt)
75 				ScHorizontalCellIterator aIter( pDoc, aRange.aStart.Tab(),
76 					aRange.aStart.Col(), aRange.aStart.Row(),
77 					aRange.aEnd.Col(), aRange.aEnd.Row() );
78                 SCCOL nCol;
79                 SCROW nRow;
80                 if ( aIter.GetNext( nCol, nRow ) )
81 				{
82 					aRange.aStart.SetCol( nCol );
83 					aRange.aStart.SetRow( nRow );
84 					aRange.aEnd.SetRow( nRow );
85 				}
86 				else
87 					aRange.aEnd = aRange.aStart;
88 			}
89             else
90             {
91                 // #i111531# with 1M rows it was necessary to limit the range
92                 // to the actually used data area.
93                 SCCOL nCol1, nCol2;
94                 SCROW nRow1, nRow2;
95                 SCTAB nTab1, nTab2;
96                 aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
97                 bool bShrunk;
98                 pDoc->ShrinkToUsedDataArea( bShrunk, nTab1, nCol1, nRow1, nCol2, nRow2, false);
99                 if (bShrunk)
100                 {
101                     aRange.aStart.SetCol( nCol1 );
102                     aRange.aStart.SetRow( nRow1 );
103                     aRange.aEnd.SetCol( nCol2 );
104                     aRange.aEnd.SetRow( nRow2 );
105                 }
106             }
107 
108 			ScImportExport aObj( pDoc, aRange );
109 			aObj.SetFormulas( GetViewData()->GetOptions().GetOption( VOPT_FORMULAS ) );
110 			rtl::OUString aExportOUString;
111 			aObj.ExportString( aExportOUString );
112 			aStrSelection = aExportOUString;
113 
114 			aStrSelection.ConvertLineEnd( LINEEND_CR );
115 
116 			//	Tab/CR durch Space ersetzen, wenn fuer Dialog oder per Basic/SelectionTextExt,
117 			//	oder wenn es eine einzelne Zeile ist.
118 			//	Sonst mehrzeilig mit Tabs beibehalten (z.B. Mail oder Basic/SelectionText).
119 			//	Fuer Mail werden die Tabs dann spaeter in (mehrere) Spaces gewandelt.
120 
121 			if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
122 			{
123 				xub_StrLen nAt;
124 				while (  (nAt = aStrSelection.Search( CHAR_CR )) != STRING_NOTFOUND )
125 					aStrSelection.SetChar( nAt, ' ' );
126 				while (  (nAt = aStrSelection.Search( '\t' )) != STRING_NOTFOUND )
127 					aStrSelection.SetChar( nAt, ' ' );
128 
129 				aStrSelection.EraseTrailingChars( ' ' );
130 			}
131 		}
132 	}
133 
134 	return aStrSelection;
135 }
136 
137 //------------------------------------------------------------------------
138 
InsertURL(const String & rName,const String & rURL,const String & rTarget,sal_uInt16 nMode)139 void ScTabViewShell::InsertURL( const String& rName, const String& rURL, const String& rTarget,
140 								sal_uInt16 nMode )
141 {
142 	SvxLinkInsertMode eMode = (SvxLinkInsertMode) nMode;
143 	sal_Bool bAsText = ( eMode != HLINK_BUTTON );		// Default ist jetzt 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 			//	#91216# 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, sal_True );
163 		}
164 	}
165 	else
166 	{
167 		SC_MOD()->InputEnterHandler();
168 		InsertURLButton( rName, rURL, rTarget );
169 	}
170 }
171 
172 //------------------------------------------------------------------------
173 
174 // wenn CLOOKs: -> mit <editview.hxx> <flditem.hxx>in neue tabvwsh
175 
lcl_SelectFieldAfterInsert(EditView & rView)176 void lcl_SelectFieldAfterInsert( EditView& rView )
177 {
178 	ESelection aSel = rView.GetSelection();
179 	if ( aSel.nStartPos == aSel.nEndPos && aSel.nStartPos > 0 )
180 	{
181 		//	Cursor is behind the inserted field -> extend selection to the left
182 
183 		--aSel.nStartPos;
184 		rView.SetSelection( aSel );
185 	}
186 }
187 
InsertURLField(const String & rName,const String & rURL,const String & rTarget)188 void ScTabViewShell::InsertURLField( const String& rName, const String& rURL, const String& rTarget )
189 {
190 	SvxURLField aURLField( rURL, rName, SVXURLFORMAT_REPR );
191 	aURLField.SetTargetFrame( rTarget );
192     SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
193 
194 	ScViewData*		pViewData	= GetViewData();
195 	ScModule*		pScMod		= SC_MOD();
196 	ScInputHandler*	pHdl		= pScMod->GetInputHdl( pViewData->GetViewShell() );
197 
198 	sal_Bool bSelectFirst = sal_False;
199 	if ( !pScMod->IsEditMode() )
200 	{
201 		if ( !SelectionEditable() )
202 		{
203 			// no error message (may be called from drag&drop)
204 			Sound::Beep();
205 			return;
206 		}
207 
208 		// single url in cell is shown in the dialog and replaced
209 		bSelectFirst = HasBookmarkAtCursor( NULL );
210 		pScMod->SetInputMode( SC_INPUT_TABLE );
211 	}
212 
213 	EditView*		pTopView	= pHdl->GetTopView();
214 	EditView*		pTableView	= pHdl->GetTableView();
215 	DBG_ASSERT( pTopView || pTableView, "No EditView" );
216 
217 	if ( bSelectFirst )
218 	{
219 		if ( pTopView )
220 			pTopView->SetSelection( ESelection(0,0,0,1) );
221 		if ( pTableView )
222 			pTableView->SetSelection( ESelection(0,0,0,1) );
223 	}
224 
225 	pHdl->DataChanging();
226 
227 	if ( pTopView )
228 	{
229 		pTopView->InsertField( aURLItem );
230 		lcl_SelectFieldAfterInsert( *pTopView );
231 	}
232 	if ( pTableView )
233 	{
234 		pTableView->InsertField( aURLItem );
235 		lcl_SelectFieldAfterInsert( *pTableView );
236 	}
237 
238 	pHdl->DataChanged();
239 }
240 
ExecSearch(SfxRequest & rReq)241 void ScTabViewShell::ExecSearch( SfxRequest& rReq )
242 {
243 	const SfxItemSet*	pReqArgs	= rReq.GetArgs();
244 	sal_uInt16				nSlot		= rReq.GetSlot();
245 	const SfxPoolItem*	pItem;
246 
247 	switch ( nSlot )
248 	{
249 		case FID_SEARCH_ON:
250 			bSearchJustOpened = sal_True;
251 			break;
252 
253 		case FID_SEARCH_NOW:
254 			{
255 				if ( pReqArgs &&
256 					 SFX_ITEM_SET == pReqArgs->GetItemState(SID_SEARCH_ITEM, sal_False, &pItem) )
257 				{
258 					DBG_ASSERT( pItem->ISA(SvxSearchItem), "falsches Item" );
259 					const SvxSearchItem* pSearchItem = (const SvxSearchItem*) pItem;
260 
261 					ScGlobal::SetSearchItem( *pSearchItem );
262 					//SearchAndReplace( pSearchItem, sal_True, rReq.IsAPI() );
263 					sal_Bool bSuccess = SearchAndReplace( pSearchItem, sal_True, rReq.IsAPI() );
264 					if ( Application::IsAccessibilityEnabled() )
265 					{
266 						SvxSearchDialog* pSearchDlg =
267 							((SvxSearchDialog*)(SfxViewFrame::Current()->GetChildWindow(
268 							SvxSearchDialogWrapper::GetChildWindowId())->GetWindow()));
269 						if( pSearchDlg )
270 						{
271 							ScTabView* pTabView = GetViewData()->GetView();
272 							if( pTabView )
273 							{
274 								Window* pWin = pTabView->GetActiveWin();
275 								if( pWin )
276 								{
277 									pSearchDlg->SetDocWin( pWin );
278 									pSearchDlg->SetSrchFlag( bSuccess );
279 								}
280 							}
281 						}
282 					}
283 					rReq.Done();
284 				}
285 			}
286 			break;
287 
288 		case SID_SEARCH_ITEM:
289 			if (pReqArgs && SFX_ITEM_SET ==
290 							pReqArgs->GetItemState(SID_SEARCH_ITEM, sal_False, &pItem))
291 			{
292 				//	Search-Item merken
293 				DBG_ASSERT( pItem->ISA(SvxSearchItem), "falsches Item" );
294 				ScGlobal::SetSearchItem( *(const SvxSearchItem*) pItem );
295 			}
296 			else
297 			{
298 				DBG_ERROR("SID_SEARCH_ITEM ohne Parameter");
299 			}
300 			break;
301 		case FID_SEARCH:
302 		case FID_REPLACE:
303 		case FID_REPLACE_ALL:
304 		case FID_SEARCH_ALL:
305 			{
306 				if (pReqArgs && SFX_ITEM_SET == pReqArgs->GetItemState(nSlot, sal_False, &pItem))
307 				{
308 					//	SearchItem holen
309 
310 					SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
311 
312 					//	SearchItem fuellen
313 
314 					aSearchItem.SetSearchString(((SfxStringItem*)pItem)->GetValue());
315 					if(SFX_ITEM_SET == pReqArgs->GetItemState(FN_PARAM_1, sal_False, &pItem))
316 						aSearchItem.SetReplaceString(((SfxStringItem*)pItem)->GetValue());
317 
318 					if (nSlot == FID_SEARCH)
319 						aSearchItem.SetCommand(SVX_SEARCHCMD_FIND);
320 					else if(nSlot == FID_REPLACE)
321 						aSearchItem.SetCommand(SVX_SEARCHCMD_REPLACE);
322 					else if(nSlot == FID_REPLACE_ALL)
323 						aSearchItem.SetCommand(SVX_SEARCHCMD_REPLACE_ALL);
324 					else
325 						aSearchItem.SetCommand(SVX_SEARCHCMD_FIND_ALL);
326 
327 					//	Request ausfuehren (dabei wird das SearchItem gespeichert)
328 
329 					aSearchItem.SetWhich(SID_SEARCH_ITEM);
330 					GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
331 							rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
332 											SFX_CALLMODE_STANDARD,
333 							&aSearchItem, 0L );
334 					if ( Application::IsAccessibilityEnabled() )
335 					{
336 						SvxSearchDialog* pSearchDlg =
337 							((SvxSearchDialog*)(SfxViewFrame::Current()->GetChildWindow(
338 							SvxSearchDialogWrapper::GetChildWindowId())->GetWindow()));
339 						if( pSearchDlg )
340 						{
341 							ScTabView* pTabView = GetViewData()->GetView();
342 							if( pTabView )
343 							{
344 								Window* pWin = pTabView->GetActiveWin();
345 								if( pWin )
346 								{
347 									pSearchDlg->SetDocWin( pWin );
348 									pSearchDlg->SetSrchFlag();
349 								}
350 							}
351 						}
352 					}
353 				}
354 				else
355 				{
356 					GetViewData()->GetDispatcher().Execute(
357 							SID_SEARCH_DLG, SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD );
358 				}
359 			}
360 			break;
361 		case FID_REPEAT_SEARCH:
362 			{
363 				//	nochmal mit ScGlobal::GetSearchItem()
364 
365 				SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
366 				aSearchItem.SetWhich(SID_SEARCH_ITEM);
367 				GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
368 						rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
369 										SFX_CALLMODE_STANDARD,
370 						&aSearchItem, 0L );
371 			}
372 			break;
373 //		case FID_SEARCH_COUNT:
374 	}
375 }
376 
377 //--------------------------------------------------------------------
378 
379 
380 
381 
382 
383 
384