xref: /AOO41X/main/sc/source/ui/view/tabvwshe.cxx (revision 4d7c9de063a797b8b4f3d45e3561e82ad1f8ef1f)
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_NOW:
250             {
251                 if ( pReqArgs &&
252                      SFX_ITEM_SET == pReqArgs->GetItemState(SID_SEARCH_ITEM, sal_False, &pItem) )
253                 {
254                     DBG_ASSERT( pItem->ISA(SvxSearchItem), "falsches Item" );
255                     const SvxSearchItem* pSearchItem = (const SvxSearchItem*) pItem;
256 
257                     ScGlobal::SetSearchItem( *pSearchItem );
258                     //SearchAndReplace( pSearchItem, sal_True, rReq.IsAPI() );
259                     sal_Bool bSuccess = SearchAndReplace( pSearchItem, sal_True, rReq.IsAPI() );
260                     if ( Application::IsAccessibilityEnabled() )
261                     {
262                         SvxSearchDialog* pSearchDlg =
263                             ((SvxSearchDialog*)(SfxViewFrame::Current()->GetChildWindow(
264                             SvxSearchDialogWrapper::GetChildWindowId())->GetWindow()));
265                         if( pSearchDlg )
266                         {
267                             ScTabView* pTabView = GetViewData()->GetView();
268                             if( pTabView )
269                             {
270                                 Window* pWin = pTabView->GetActiveWin();
271                                 if( pWin )
272                                 {
273                                     pSearchDlg->SetDocWin( pWin );
274                                     pSearchDlg->SetSrchFlag( bSuccess );
275                                 }
276                             }
277                         }
278                     }
279                     rReq.Done();
280                 }
281             }
282             break;
283 
284         case SID_SEARCH_ITEM:
285             if (pReqArgs && SFX_ITEM_SET ==
286                             pReqArgs->GetItemState(SID_SEARCH_ITEM, sal_False, &pItem))
287             {
288                 //  Search-Item merken
289                 DBG_ASSERT( pItem->ISA(SvxSearchItem), "falsches Item" );
290                 ScGlobal::SetSearchItem( *(const SvxSearchItem*) pItem );
291             }
292             else
293             {
294                 DBG_ERROR("SID_SEARCH_ITEM ohne Parameter");
295             }
296             break;
297         case FID_SEARCH:
298         case FID_REPLACE:
299         case FID_REPLACE_ALL:
300         case FID_SEARCH_ALL:
301             {
302                 if (pReqArgs && SFX_ITEM_SET == pReqArgs->GetItemState(nSlot, sal_False, &pItem))
303                 {
304                     //  SearchItem holen
305 
306                     SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
307 
308                     //  SearchItem fuellen
309 
310                     aSearchItem.SetSearchString(((SfxStringItem*)pItem)->GetValue());
311                     if(SFX_ITEM_SET == pReqArgs->GetItemState(FN_PARAM_1, sal_False, &pItem))
312                         aSearchItem.SetReplaceString(((SfxStringItem*)pItem)->GetValue());
313 
314                     if (nSlot == FID_SEARCH)
315                         aSearchItem.SetCommand(SVX_SEARCHCMD_FIND);
316                     else if(nSlot == FID_REPLACE)
317                         aSearchItem.SetCommand(SVX_SEARCHCMD_REPLACE);
318                     else if(nSlot == FID_REPLACE_ALL)
319                         aSearchItem.SetCommand(SVX_SEARCHCMD_REPLACE_ALL);
320                     else
321                         aSearchItem.SetCommand(SVX_SEARCHCMD_FIND_ALL);
322 
323                     //  Request ausfuehren (dabei wird das SearchItem gespeichert)
324 
325                     aSearchItem.SetWhich(SID_SEARCH_ITEM);
326                     GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
327                             rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
328                                             SFX_CALLMODE_STANDARD,
329                             &aSearchItem, 0L );
330                     if ( Application::IsAccessibilityEnabled() )
331                     {
332                         SvxSearchDialog* pSearchDlg =
333                             ((SvxSearchDialog*)(SfxViewFrame::Current()->GetChildWindow(
334                             SvxSearchDialogWrapper::GetChildWindowId())->GetWindow()));
335                         if( pSearchDlg )
336                         {
337                             ScTabView* pTabView = GetViewData()->GetView();
338                             if( pTabView )
339                             {
340                                 Window* pWin = pTabView->GetActiveWin();
341                                 if( pWin )
342                                 {
343                                     pSearchDlg->SetDocWin( pWin );
344                                     pSearchDlg->SetSrchFlag();
345                                 }
346                             }
347                         }
348                     }
349                 }
350                 else
351                 {
352                     GetViewData()->GetDispatcher().Execute(
353                             SID_SEARCH_DLG, SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD );
354                 }
355             }
356             break;
357         case FID_REPEAT_SEARCH:
358             {
359                 //  nochmal mit ScGlobal::GetSearchItem()
360 
361                 SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
362                 aSearchItem.SetWhich(SID_SEARCH_ITEM);
363                 GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
364                         rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
365                                         SFX_CALLMODE_STANDARD,
366                         &aSearchItem, 0L );
367             }
368             break;
369 //      case FID_SEARCH_COUNT:
370     }
371 }
372 
373 //--------------------------------------------------------------------
374 
375 
376 
377 
378 
379 
380