Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / sc / source / ui / view / tabvwshe.cxx
blobee01c6bde6e7ea4d87fd23005a939c2e50a89010
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>
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>
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"
43 //==================================================================
45 String ScTabViewShell::GetSelectionText( sal_Bool bWholeWord )
47 String aStrSelection;
49 if ( pEditShell && pEditShell == GetMySubShell() )
51 aStrSelection = pEditShell->GetSelectionText( bWholeWord );
53 else
55 ScRange aRange;
57 if ( GetViewData()->GetSimpleArea( aRange ) == SC_MARK_SIMPLE )
59 ScDocument* pDoc = GetViewData()->GetDocument();
60 if ( bInFormatDialog && aRange.aStart.Row() != aRange.aEnd.Row() )
62 // Range auf eine Datenzeile begrenzen
63 // (nur wenn der Aufruf aus einem Format-Dialog kommt)
64 ScHorizontalCellIterator aIter( pDoc, aRange.aStart.Tab(),
65 aRange.aStart.Col(), aRange.aStart.Row(),
66 aRange.aEnd.Col(), aRange.aEnd.Row() );
67 SCCOL nCol;
68 SCROW nRow;
69 if ( aIter.GetNext( nCol, nRow ) )
71 aRange.aStart.SetCol( nCol );
72 aRange.aStart.SetRow( nRow );
73 aRange.aEnd.SetRow( nRow );
75 else
76 aRange.aEnd = aRange.aStart;
78 else
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 aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
86 bool bShrunk;
87 pDoc->ShrinkToUsedDataArea( bShrunk, nTab1, nCol1, nRow1, nCol2, nRow2, false);
88 if (bShrunk)
90 aRange.aStart.SetCol( nCol1 );
91 aRange.aStart.SetRow( nRow1 );
92 aRange.aEnd.SetCol( nCol2 );
93 aRange.aEnd.SetRow( nRow2 );
97 ScImportExport aObj( pDoc, aRange );
98 aObj.SetFormulas( GetViewData()->GetOptions().GetOption( VOPT_FORMULAS ) );
99 rtl::OUString aExportOUString;
100 aObj.ExportString( aExportOUString );
101 aStrSelection = convertLineEnd(aExportOUString, LINEEND_CR);
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.
108 if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
110 xub_StrLen nAt;
111 while ( (nAt = aStrSelection.Search( CHAR_CR )) != STRING_NOTFOUND )
112 aStrSelection.SetChar( nAt, ' ' );
113 while ( (nAt = aStrSelection.Search( '\t' )) != STRING_NOTFOUND )
114 aStrSelection.SetChar( nAt, ' ' );
116 aStrSelection = comphelper::string::stripEnd(aStrSelection, ' ');
121 return aStrSelection;
124 //------------------------------------------------------------------------
126 void ScTabViewShell::InsertURL( const String& rName, const String& rURL, const String& rTarget,
127 sal_uInt16 nMode )
129 SvxLinkInsertMode eMode = (SvxLinkInsertMode) nMode;
130 sal_Bool bAsText = ( eMode != HLINK_BUTTON ); // Default ist jetzt Text
132 if ( bAsText )
134 if ( GetViewData()->IsActive() )
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
139 InsertURLField( rName, rURL, rTarget );
141 else
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
147 SCCOL nPosX = GetViewData()->GetCurX();
148 SCROW nPosY = GetViewData()->GetCurY();
149 InsertBookmark( rName, rURL, nPosX, nPosY, &rTarget, sal_True );
152 else
154 SC_MOD()->InputEnterHandler();
155 InsertURLButton( rName, rURL, rTarget );
159 //------------------------------------------------------------------------
161 // wenn CLOOKs: -> mit <editview.hxx> <flditem.hxx>in neue tabvwsh
163 static void lcl_SelectFieldAfterInsert( EditView& rView )
165 ESelection aSel = rView.GetSelection();
166 if ( aSel.nStartPos == aSel.nEndPos && aSel.nStartPos > 0 )
168 // Cursor is behind the inserted field -> extend selection to the left
170 --aSel.nStartPos;
171 rView.SetSelection( aSel );
175 void ScTabViewShell::InsertURLField( const String& rName, const String& rURL, const String& rTarget )
177 SvxURLField aURLField( rURL, rName, SVXURLFORMAT_REPR );
178 aURLField.SetTargetFrame( rTarget );
179 SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
181 ScViewData* pViewData = GetViewData();
182 ScModule* pScMod = SC_MOD();
183 ScInputHandler* pHdl = pScMod->GetInputHdl( pViewData->GetViewShell() );
185 sal_Bool bSelectFirst = false;
186 if ( !pScMod->IsEditMode() )
188 if ( !SelectionEditable() )
190 // no error message (may be called from drag&drop)
191 return;
194 // single url in cell is shown in the dialog and replaced
195 bSelectFirst = HasBookmarkAtCursor( NULL );
196 pScMod->SetInputMode( SC_INPUT_TABLE );
199 EditView* pTopView = pHdl->GetTopView();
200 EditView* pTableView = pHdl->GetTableView();
201 OSL_ENSURE( pTopView || pTableView, "No EditView" );
203 if ( bSelectFirst )
205 if ( pTopView )
206 pTopView->SetSelection( ESelection(0,0,0,1) );
207 if ( pTableView )
208 pTableView->SetSelection( ESelection(0,0,0,1) );
211 pHdl->DataChanging();
213 if ( pTopView )
215 pTopView->InsertField( aURLItem );
216 lcl_SelectFieldAfterInsert( *pTopView );
218 if ( pTableView )
220 pTableView->InsertField( aURLItem );
221 lcl_SelectFieldAfterInsert( *pTableView );
224 pHdl->DataChanged();
227 void ScTabViewShell::ExecSearch( SfxRequest& rReq )
229 const SfxItemSet* pReqArgs = rReq.GetArgs();
230 sal_uInt16 nSlot = rReq.GetSlot();
231 const SfxPoolItem* pItem;
233 switch ( nSlot )
235 case FID_SEARCH_NOW:
237 if ( pReqArgs &&
238 SFX_ITEM_SET == pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem) )
240 OSL_ENSURE( pItem->ISA(SvxSearchItem), "falsches Item" );
241 const SvxSearchItem* pSearchItem = (const SvxSearchItem*) pItem;
243 ScGlobal::SetSearchItem( *pSearchItem );
244 SearchAndReplace( pSearchItem, sal_True, rReq.IsAPI() );
245 rReq.Done();
248 break;
250 case SID_SEARCH_ITEM:
251 if (pReqArgs && SFX_ITEM_SET ==
252 pReqArgs->GetItemState(SID_SEARCH_ITEM, false, &pItem))
254 // Search-Item merken
255 OSL_ENSURE( pItem->ISA(SvxSearchItem), "falsches Item" );
256 ScGlobal::SetSearchItem( *(const SvxSearchItem*) pItem );
258 else
260 OSL_FAIL("SID_SEARCH_ITEM ohne Parameter");
262 break;
263 case FID_SEARCH:
264 case FID_REPLACE:
265 case FID_REPLACE_ALL:
266 case FID_SEARCH_ALL:
268 if (pReqArgs && SFX_ITEM_SET == pReqArgs->GetItemState(nSlot, false, &pItem))
270 // SearchItem holen
272 SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
274 // SearchItem fuellen
276 aSearchItem.SetSearchString(((SfxStringItem*)pItem)->GetValue());
277 if(SFX_ITEM_SET == pReqArgs->GetItemState(FN_PARAM_1, false, &pItem))
278 aSearchItem.SetReplaceString(((SfxStringItem*)pItem)->GetValue());
280 if (nSlot == FID_SEARCH)
281 aSearchItem.SetCommand(SVX_SEARCHCMD_FIND);
282 else if(nSlot == FID_REPLACE)
283 aSearchItem.SetCommand(SVX_SEARCHCMD_REPLACE);
284 else if(nSlot == FID_REPLACE_ALL)
285 aSearchItem.SetCommand(SVX_SEARCHCMD_REPLACE_ALL);
286 else
287 aSearchItem.SetCommand(SVX_SEARCHCMD_FIND_ALL);
289 // Request ausfuehren (dabei wird das SearchItem gespeichert)
291 aSearchItem.SetWhich(SID_SEARCH_ITEM);
292 GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
293 rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
294 SFX_CALLMODE_STANDARD,
295 &aSearchItem, 0L );
297 else
299 GetViewData()->GetDispatcher().Execute(
300 SID_SEARCH_DLG, SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD );
303 break;
304 case FID_REPEAT_SEARCH:
306 // nochmal mit ScGlobal::GetSearchItem()
308 SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
309 aSearchItem.SetWhich(SID_SEARCH_ITEM);
310 GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
311 rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
312 SFX_CALLMODE_STANDARD,
313 &aSearchItem, 0L );
315 break;
316 // case FID_SEARCH_COUNT:
320 //--------------------------------------------------------------------
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */