1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
39 #include "dociter.hxx"
40 #include "inputhdl.hxx"
41 #include <svx/srchdlg.hxx>
42 #include "document.hxx"
43 #include <gridwin.hxx>
45 OUString
ScTabViewShell::GetSelectionText( bool bWholeWord
)
47 OUString aStrSelection
;
49 if ( pEditShell
&& pEditShell
== GetMySubShell() )
51 aStrSelection
= pEditShell
->GetSelectionText( bWholeWord
);
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() );
69 if ( aIter
.GetNext( nCol
, nRow
) )
71 aRange
.aStart
.SetCol( nCol
);
72 aRange
.aStart
.SetRow( nRow
);
73 aRange
.aEnd
.SetRow( nRow
);
76 aRange
.aEnd
= aRange
.aStart
;
80 // #i111531# with 1M rows it was necessary to limit the range
81 // to the actually used data area.
85 aRange
.GetVars( nCol1
, nRow1
, nTab1
, nCol2
, nRow2
, nTab2
);
87 pDoc
->ShrinkToUsedDataArea( bShrunk
, nTab1
, nCol1
, nRow1
, nCol2
, nRow2
, false);
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 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 aStrSelection
= aStrSelection
.replaceAll(OUString(CHAR_CR
), " ");
111 aStrSelection
= aStrSelection
.replaceAll("\t", " ");
112 aStrSelection
= comphelper::string::stripEnd(aStrSelection
, ' ');
117 return aStrSelection
;
120 void ScTabViewShell::InsertURL( const OUString
& rName
, const OUString
& rURL
, const OUString
& rTarget
,
123 SvxLinkInsertMode eMode
= (SvxLinkInsertMode
) nMode
;
124 bool bAsText
= ( eMode
!= HLINK_BUTTON
); // Default ist jetzt Text
128 if ( GetViewData().IsActive() )
130 // if the view is active, always use InsertURLField, which starts EditMode
131 // and selects the URL, so it can be changed from the URL bar / dialog
133 InsertURLField( rName
, rURL
, rTarget
);
137 // if the view is not active, InsertURLField doesn't work
138 // -> use InsertBookmark to directly manipulate cell content
139 // bTryReplace=sal_True -> if cell contains only one URL, replace it
141 SCCOL nPosX
= GetViewData().GetCurX();
142 SCROW nPosY
= GetViewData().GetCurY();
143 InsertBookmark( rName
, rURL
, nPosX
, nPosY
, &rTarget
, true );
148 SC_MOD()->InputEnterHandler();
149 InsertURLButton( rName
, rURL
, rTarget
);
153 static void lcl_SelectFieldAfterInsert( EditView
& rView
)
155 ESelection aSel
= rView
.GetSelection();
156 if ( aSel
.nStartPos
== aSel
.nEndPos
&& aSel
.nStartPos
> 0 )
158 // Cursor is behind the inserted field -> extend selection to the left
161 rView
.SetSelection( aSel
);
165 void ScTabViewShell::InsertURLField( const OUString
& rName
, const OUString
& rURL
, const OUString
& rTarget
)
167 SvxURLField
aURLField( rURL
, rName
, SVXURLFORMAT_REPR
);
168 aURLField
.SetTargetFrame( rTarget
);
169 SvxFieldItem
aURLItem( aURLField
, EE_FEATURE_FIELD
);
171 ScViewData
& rViewData
= GetViewData();
172 ScModule
* pScMod
= SC_MOD();
173 ScInputHandler
* pHdl
= pScMod
->GetInputHdl( rViewData
.GetViewShell() );
175 bool bSelectFirst
= false;
176 if ( !pScMod
->IsEditMode() )
178 if ( !SelectionEditable() )
180 // no error message (may be called from drag&drop)
184 // single url in cell is shown in the dialog and replaced
185 bSelectFirst
= HasBookmarkAtCursor( NULL
);
186 pScMod
->SetInputMode( SC_INPUT_TABLE
);
189 EditView
* pTopView
= pHdl
->GetTopView();
190 EditView
* pTableView
= pHdl
->GetTableView();
191 OSL_ENSURE( pTopView
|| pTableView
, "No EditView" );
196 pTopView
->SetSelection( ESelection(0,0,0,1) );
198 pTableView
->SetSelection( ESelection(0,0,0,1) );
201 pHdl
->DataChanging();
205 pTopView
->InsertField( aURLItem
);
206 lcl_SelectFieldAfterInsert( *pTopView
);
210 pTableView
->InsertField( aURLItem
);
211 lcl_SelectFieldAfterInsert( *pTableView
);
217 void ScTabViewShell::ExecSearch( SfxRequest
& rReq
)
219 const SfxItemSet
* pReqArgs
= rReq
.GetArgs();
220 sal_uInt16 nSlot
= rReq
.GetSlot();
221 const SfxPoolItem
* pItem
;
228 SfxItemState::SET
== pReqArgs
->GetItemState(SID_SEARCH_ITEM
, false, &pItem
) )
230 OSL_ENSURE( pItem
->ISA(SvxSearchItem
), "falsches Item" );
231 const SvxSearchItem
* pSearchItem
= static_cast<const SvxSearchItem
*>(pItem
);
233 ScGlobal::SetSearchItem( *pSearchItem
);
234 bool bSuccess
= SearchAndReplace( pSearchItem
, true, rReq
.IsAPI() );
235 const SfxChildWindow
* pChildWindow
= SfxViewFrame::Current()->GetChildWindow(
236 SvxSearchDialogWrapper::GetChildWindowId());
239 SvxSearchDialog
* pSearchDlg
= static_cast<SvxSearchDialog
*>(pChildWindow
->GetWindow());
242 ScTabView
* pTabView
= GetViewData().GetView();
245 vcl::Window
* pWin
= pTabView
->GetActiveWin();
248 pSearchDlg
->SetDocWin( pWin
);
249 pSearchDlg
->SetSrchFlag( bSuccess
);
259 case SID_SEARCH_ITEM
:
260 if (pReqArgs
&& SfxItemState::SET
==
261 pReqArgs
->GetItemState(SID_SEARCH_ITEM
, false, &pItem
))
263 // Search-Item merken
264 OSL_ENSURE( pItem
->ISA(SvxSearchItem
), "falsches Item" );
265 ScGlobal::SetSearchItem( *static_cast<const SvxSearchItem
*>(pItem
));
269 OSL_FAIL("SID_SEARCH_ITEM ohne Parameter");
274 case FID_REPLACE_ALL
:
277 if (pReqArgs
&& SfxItemState::SET
== pReqArgs
->GetItemState(nSlot
, false, &pItem
))
281 SvxSearchItem aSearchItem
= ScGlobal::GetSearchItem();
283 // SearchItem fuellen
285 aSearchItem
.SetSearchString(static_cast<const SfxStringItem
*>(pItem
)->GetValue());
286 if(SfxItemState::SET
== pReqArgs
->GetItemState(FN_PARAM_1
, false, &pItem
))
287 aSearchItem
.SetReplaceString(static_cast<const SfxStringItem
*>(pItem
)->GetValue());
289 if (nSlot
== FID_SEARCH
)
290 aSearchItem
.SetCommand(SvxSearchCmd::FIND
);
291 else if(nSlot
== FID_REPLACE
)
292 aSearchItem
.SetCommand(SvxSearchCmd::REPLACE
);
293 else if(nSlot
== FID_REPLACE_ALL
)
294 aSearchItem
.SetCommand(SvxSearchCmd::REPLACE_ALL
);
296 aSearchItem
.SetCommand(SvxSearchCmd::FIND_ALL
);
298 // Request ausfuehren (dabei wird das SearchItem gespeichert)
300 aSearchItem
.SetWhich(SID_SEARCH_ITEM
);
301 GetViewData().GetDispatcher().Execute( FID_SEARCH_NOW
,
302 rReq
.IsAPI() ? SfxCallMode::API
|SfxCallMode::SYNCHRON
:
305 const SfxChildWindow
* pChildWindow
= SfxViewFrame::Current()->GetChildWindow(
306 SvxSearchDialogWrapper::GetChildWindowId());
309 SvxSearchDialog
* pSearchDlg
= static_cast<SvxSearchDialog
*>(pChildWindow
->GetWindow());
312 ScTabView
* pTabView
= GetViewData().GetView();
315 vcl::Window
* pWin
= pTabView
->GetActiveWin();
318 pSearchDlg
->SetDocWin( pWin
);
319 pSearchDlg
->SetSrchFlag();
327 GetViewData().GetDispatcher().Execute(
328 SID_SEARCH_DLG
, SfxCallMode::ASYNCHRON
|SfxCallMode::RECORD
);
332 case FID_REPEAT_SEARCH
:
334 // nochmal mit ScGlobal::GetSearchItem()
336 SvxSearchItem aSearchItem
= ScGlobal::GetSearchItem();
337 aSearchItem
.SetWhich(SID_SEARCH_ITEM
);
338 GetViewData().GetDispatcher().Execute( FID_SEARCH_NOW
,
339 rReq
.IsAPI() ? SfxCallMode::API
|SfxCallMode::SYNCHRON
:
344 // case FID_SEARCH_COUNT:
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */