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