xref: /trunk/main/sc/source/ui/view/tabvwshe.cxx (revision 8e8ee8fefdac26d905672cc573c35fd0ae1f9356)
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 //IAccessibility2 Implementation 2009-----
52 #include <svx/srchdlg.hxx>
53 //-----IAccessibility2 Implementation 2009
54 #include "document.hxx"
55 
56 //==================================================================
57 
58 String __EXPORT ScTabViewShell::GetSelectionText( sal_Bool bWholeWord )
59 {
60     String aStrSelection;
61 
62     if ( pEditShell && pEditShell == GetMySubShell() )
63     {
64         aStrSelection = pEditShell->GetSelectionText( bWholeWord );
65     }
66     else
67     {
68         ScRange aRange;
69 
70         if ( GetViewData()->GetSimpleArea( aRange ) == SC_MARK_SIMPLE )
71         {
72             ScDocument* pDoc = GetViewData()->GetDocument();
73             if ( bInFormatDialog && aRange.aStart.Row() != aRange.aEnd.Row() )
74             {
75                 // Range auf eine Datenzeile begrenzen
76                 // (#48613# nur wenn der Aufruf aus einem Format-Dialog kommt)
77                 ScHorizontalCellIterator aIter( pDoc, aRange.aStart.Tab(),
78                     aRange.aStart.Col(), aRange.aStart.Row(),
79                     aRange.aEnd.Col(), aRange.aEnd.Row() );
80                 SCCOL nCol;
81                 SCROW nRow;
82                 if ( aIter.GetNext( nCol, nRow ) )
83                 {
84                     aRange.aStart.SetCol( nCol );
85                     aRange.aStart.SetRow( nRow );
86                     aRange.aEnd.SetRow( nRow );
87                 }
88                 else
89                     aRange.aEnd = aRange.aStart;
90             }
91             else
92             {
93                 // #i111531# with 1M rows it was necessary to limit the range
94                 // to the actually used data area.
95                 SCCOL nCol1, nCol2;
96                 SCROW nRow1, nRow2;
97                 SCTAB nTab1, nTab2;
98                 aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
99                 bool bShrunk;
100                 pDoc->ShrinkToUsedDataArea( bShrunk, nTab1, nCol1, nRow1, nCol2, nRow2, false);
101                 if (bShrunk)
102                 {
103                     aRange.aStart.SetCol( nCol1 );
104                     aRange.aStart.SetRow( nRow1 );
105                     aRange.aEnd.SetCol( nCol2 );
106                     aRange.aEnd.SetRow( nRow2 );
107                 }
108             }
109 
110             ScImportExport aObj( pDoc, aRange );
111             aObj.SetFormulas( GetViewData()->GetOptions().GetOption( VOPT_FORMULAS ) );
112             rtl::OUString aExportOUString;
113             aObj.ExportString( aExportOUString );
114             aStrSelection = aExportOUString;
115 
116             aStrSelection.ConvertLineEnd( LINEEND_CR );
117 
118             //  Tab/CR durch Space ersetzen, wenn fuer Dialog oder per Basic/SelectionTextExt,
119             //  oder wenn es eine einzelne Zeile ist.
120             //  Sonst mehrzeilig mit Tabs beibehalten (z.B. Mail oder Basic/SelectionText).
121             //  Fuer Mail werden die Tabs dann spaeter in (mehrere) Spaces gewandelt.
122 
123             if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
124             {
125                 xub_StrLen nAt;
126                 while (  (nAt = aStrSelection.Search( CHAR_CR )) != STRING_NOTFOUND )
127                     aStrSelection.SetChar( nAt, ' ' );
128                 while (  (nAt = aStrSelection.Search( '\t' )) != STRING_NOTFOUND )
129                     aStrSelection.SetChar( nAt, ' ' );
130 
131                 aStrSelection.EraseTrailingChars( ' ' );
132             }
133         }
134     }
135 
136     return aStrSelection;
137 }
138 
139 //------------------------------------------------------------------------
140 
141 void ScTabViewShell::InsertURL( const String& rName, const String& rURL, const String& rTarget,
142                                 sal_uInt16 nMode )
143 {
144     SvxLinkInsertMode eMode = (SvxLinkInsertMode) nMode;
145     sal_Bool bAsText = ( eMode != HLINK_BUTTON );       // Default ist jetzt Text
146 
147     if ( bAsText )
148     {
149         if ( GetViewData()->IsActive() )
150         {
151             //  if the view is active, always use InsertURLField, which starts EditMode
152             //  and selects the URL, so it can be changed from the URL bar / dialog
153 
154             InsertURLField( rName, rURL, rTarget );
155         }
156         else
157         {
158             //  #91216# if the view is not active, InsertURLField doesn't work
159             //  -> use InsertBookmark to directly manipulate cell content
160             //  bTryReplace=sal_True -> if cell contains only one URL, replace it
161 
162             SCCOL nPosX = GetViewData()->GetCurX();
163             SCROW nPosY = GetViewData()->GetCurY();
164             InsertBookmark( rName, rURL, nPosX, nPosY, &rTarget, sal_True );
165         }
166     }
167     else
168     {
169         SC_MOD()->InputEnterHandler();
170         InsertURLButton( rName, rURL, rTarget );
171     }
172 }
173 
174 //------------------------------------------------------------------------
175 
176 // wenn CLOOKs: -> mit <editview.hxx> <flditem.hxx>in neue tabvwsh
177 
178 void lcl_SelectFieldAfterInsert( EditView& rView )
179 {
180     ESelection aSel = rView.GetSelection();
181     if ( aSel.nStartPos == aSel.nEndPos && aSel.nStartPos > 0 )
182     {
183         //  Cursor is behind the inserted field -> extend selection to the left
184 
185         --aSel.nStartPos;
186         rView.SetSelection( aSel );
187     }
188 }
189 
190 void ScTabViewShell::InsertURLField( const String& rName, const String& rURL, const String& rTarget )
191 {
192     SvxURLField aURLField( rURL, rName, SVXURLFORMAT_REPR );
193     aURLField.SetTargetFrame( rTarget );
194     SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
195 
196     ScViewData*     pViewData   = GetViewData();
197     ScModule*       pScMod      = SC_MOD();
198     ScInputHandler* pHdl        = pScMod->GetInputHdl( pViewData->GetViewShell() );
199 
200     sal_Bool bSelectFirst = sal_False;
201     if ( !pScMod->IsEditMode() )
202     {
203         if ( !SelectionEditable() )
204         {
205             // no error message (may be called from drag&drop)
206             Sound::Beep();
207             return;
208         }
209 
210         // single url in cell is shown in the dialog and replaced
211         bSelectFirst = HasBookmarkAtCursor( NULL );
212         pScMod->SetInputMode( SC_INPUT_TABLE );
213     }
214 
215     EditView*       pTopView    = pHdl->GetTopView();
216     EditView*       pTableView  = pHdl->GetTableView();
217     DBG_ASSERT( pTopView || pTableView, "No EditView" );
218 
219     if ( bSelectFirst )
220     {
221         if ( pTopView )
222             pTopView->SetSelection( ESelection(0,0,0,1) );
223         if ( pTableView )
224             pTableView->SetSelection( ESelection(0,0,0,1) );
225     }
226 
227     pHdl->DataChanging();
228 
229     if ( pTopView )
230     {
231         pTopView->InsertField( aURLItem );
232         lcl_SelectFieldAfterInsert( *pTopView );
233     }
234     if ( pTableView )
235     {
236         pTableView->InsertField( aURLItem );
237         lcl_SelectFieldAfterInsert( *pTableView );
238     }
239 
240     pHdl->DataChanged();
241 }
242 
243 void ScTabViewShell::ExecSearch( SfxRequest& rReq )
244 {
245     const SfxItemSet*   pReqArgs    = rReq.GetArgs();
246     sal_uInt16              nSlot       = rReq.GetSlot();
247     const SfxPoolItem*  pItem;
248 
249     switch ( nSlot )
250     {
251         case FID_SEARCH_NOW:
252             {
253                 if ( pReqArgs &&
254                      SFX_ITEM_SET == pReqArgs->GetItemState(SID_SEARCH_ITEM, sal_False, &pItem) )
255                 {
256                     DBG_ASSERT( pItem->ISA(SvxSearchItem), "falsches Item" );
257                     const SvxSearchItem* pSearchItem = (const SvxSearchItem*) pItem;
258 
259                     ScGlobal::SetSearchItem( *pSearchItem );
260                     //IAccessibility2 Implementation 2009-----
261                     //SearchAndReplace( pSearchItem, sal_True, rReq.IsAPI() );
262                     sal_Bool bSuccess = SearchAndReplace( pSearchItem, sal_True, rReq.IsAPI() );
263                     if ( Application::IsAccessibilityEnabled() )
264                     {
265                         SvxSearchDialog* pSearchDlg =
266                             ((SvxSearchDialog*)(SfxViewFrame::Current()->GetChildWindow(
267                             SvxSearchDialogWrapper::GetChildWindowId())->GetWindow()));
268                         if( pSearchDlg )
269                         {
270                             ScTabView* pTabView = GetViewData()->GetView();
271                             if( pTabView )
272                             {
273                                 Window* pWin = pTabView->GetActiveWin();
274                                 if( pWin )
275                                 {
276                                     pSearchDlg->SetDocWin( pWin );
277                                     pSearchDlg->SetSrchFlag( bSuccess );
278                                 }
279                             }
280                         }
281                     }
282                     //-----IAccessibility2 Implementation 2009
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                     //IAccessibility2 Implementation 2009-----
335                     if ( Application::IsAccessibilityEnabled() )
336                     {
337                         SvxSearchDialog* pSearchDlg =
338                             ((SvxSearchDialog*)(SfxViewFrame::Current()->GetChildWindow(
339                             SvxSearchDialogWrapper::GetChildWindowId())->GetWindow()));
340                         if( pSearchDlg )
341                         {
342                             ScTabView* pTabView = GetViewData()->GetView();
343                             if( pTabView )
344                             {
345                                 Window* pWin = pTabView->GetActiveWin();
346                                 if( pWin )
347                                 {
348                                     pSearchDlg->SetDocWin( pWin );
349                                     pSearchDlg->SetSrchFlag();
350                                 }
351                             }
352                         }
353                     }
354                     //-----IAccessibility2 Implementation 2009
355                 }
356                 else
357                 {
358                     GetViewData()->GetDispatcher().Execute(
359                             SID_SEARCH_DLG, SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD );
360                 }
361             }
362             break;
363         case FID_REPEAT_SEARCH:
364             {
365                 //  nochmal mit ScGlobal::GetSearchItem()
366 
367                 SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
368                 aSearchItem.SetWhich(SID_SEARCH_ITEM);
369                 GetViewData()->GetDispatcher().Execute( FID_SEARCH_NOW,
370                         rReq.IsAPI() ? SFX_CALLMODE_API|SFX_CALLMODE_SYNCHRON :
371                                         SFX_CALLMODE_STANDARD,
372                         &aSearchItem, 0L );
373             }
374             break;
375 //      case FID_SEARCH_COUNT:
376     }
377 }
378 
379 //--------------------------------------------------------------------
380 
381 
382 
383 
384 
385 
386