Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/view/tabvwshe.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#include <comphelper/string.hxx>
21
#include <editeng/eeitem.hxx>
22
#include <osl/diagnose.h>
23
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/request.hxx>
30
#include <svl/stritem.hxx>
31
32
#include <tabvwsh.hxx>
33
#include <sc.hrc>
34
#include <scmod.hxx>
35
#include <impex.hxx>
36
#include <editsh.hxx>
37
#include <dociter.hxx>
38
#include <inputhdl.hxx>
39
#include <document.hxx>
40
41
OUString ScTabViewShell::GetSelectionText( bool bWholeWord, bool bOnlyASample )
42
0
{
43
0
    OUString aStrSelection;
44
45
0
    if ( pEditShell && pEditShell.get() == GetMySubShell() )
46
0
    {
47
0
        aStrSelection = pEditShell->GetSelectionText( bWholeWord );
48
0
    }
49
0
    else
50
0
    {
51
0
        ScRange aRange;
52
53
0
        if ( GetViewData().GetSimpleArea( aRange ) == SC_MARK_SIMPLE )
54
0
        {
55
0
            ScDocument& rDoc = GetViewData().GetDocument();
56
0
            if ( (bOnlyASample || bInFormatDialog) && aRange.aStart.Row() != aRange.aEnd.Row() )
57
0
            {
58
                // limit range to one data row
59
                // (only when  the call comes from a format dialog)
60
0
                ScHorizontalCellIterator aIter( rDoc, aRange.aStart.Tab(),
61
0
                    aRange.aStart.Col(), aRange.aStart.Row(),
62
0
                    aRange.aEnd.Col(), aRange.aEnd.Row() );
63
0
                SCCOL nCol;
64
0
                SCROW nRow;
65
0
                if ( aIter.GetNext( nCol, nRow ) )
66
0
                {
67
0
                    aRange.aStart.SetCol( nCol );
68
0
                    aRange.aStart.SetRow( nRow );
69
0
                    aRange.aEnd.SetRow( nRow );
70
0
                }
71
0
                else
72
0
                    aRange.aEnd = aRange.aStart;
73
0
            }
74
0
            else
75
0
            {
76
                // #i111531# with 1M rows it was necessary to limit the range
77
                // to the actually used data area.
78
0
                SCCOL nCol1, nCol2;
79
0
                SCROW nRow1, nRow2;
80
0
                SCTAB nTab1, nTab2;
81
0
                aRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
82
0
                bool bShrunk;
83
0
                rDoc.ShrinkToUsedDataArea( bShrunk, nTab1, nCol1, nRow1, nCol2, nRow2, false);
84
0
                if (bShrunk)
85
0
                {
86
0
                    aRange.aStart.SetCol( nCol1 );
87
0
                    aRange.aStart.SetRow( nRow1 );
88
0
                    aRange.aEnd.SetCol( nCol2 );
89
0
                    aRange.aEnd.SetRow( nRow2 );
90
0
                }
91
0
            }
92
93
0
            ScImportExport aObj( rDoc, aRange );
94
            // tdf#148437 - if cell contains a formula, overwrite entire content of the cell
95
0
            aObj.SetFormulas(true);
96
0
            OUString aExportOUString;
97
            /* TODO: STRING_TSVC under some circumstances? */
98
0
            aObj.ExportString( aExportOUString, SotClipboardFormatId::STRING );
99
0
            aStrSelection = convertLineEnd(aExportOUString, LINEEND_CR);
100
101
            // replace Tab/CR with space, if for dialog or through Basic/SelectionTextExt,
102
            // or when it is a single row.
103
            // Otherwise keep Tabs in multi-row (for instance mail or Basic/SelectionText).
104
            // for mail the Tabs are then later changed into (multiple) spaces.
105
106
0
            if ( bInFormatDialog || bWholeWord || aRange.aEnd.Row() == aRange.aStart.Row() )
107
0
            {
108
0
                aStrSelection = aStrSelection.replaceAll("\r", " ");
109
0
                aStrSelection = aStrSelection.replaceAll("\t", " ");
110
0
                aStrSelection = comphelper::string::stripEnd(aStrSelection, ' ');
111
0
            }
112
0
        }
113
0
    }
114
115
0
    return aStrSelection;
116
0
}
117
118
void ScTabViewShell::InsertURL( const OUString& rName, const OUString& rURL, const OUString& rTarget,
119
                                sal_uInt16 nMode )
120
0
{
121
0
    SvxLinkInsertMode eMode = static_cast<SvxLinkInsertMode>(nMode);
122
0
    bool bAsText = ( eMode != HLINK_BUTTON );       // default is now text
123
124
0
    if ( bAsText )
125
0
    {
126
0
        if ( GetViewData().IsActive() )
127
0
        {
128
            //  if the view is active, always use InsertURLField, which starts EditMode
129
            //  and selects the URL, so it can be changed from the URL bar / dialog
130
131
0
            InsertURLField( rName, rURL, rTarget );
132
0
        }
133
0
        else
134
0
        {
135
            //  if the view is not active, InsertURLField doesn't work
136
            //  -> use InsertBookmark to directly manipulate cell content
137
            //  bTryReplace=sal_True -> if cell contains only one URL, replace it
138
139
0
            SCCOL nPosX = GetViewData().GetCurX();
140
0
            SCROW nPosY = GetViewData().GetCurY();
141
0
            InsertBookmark( rName, rURL, nPosX, nPosY, &rTarget, true );
142
0
        }
143
0
    }
144
0
    else
145
0
    {
146
0
        ScModule::get()->InputEnterHandler();
147
0
        InsertURLButton( rName, rURL, rTarget, nullptr );
148
0
    }
149
0
}
150
151
static void lcl_SelectFieldAfterInsert( EditView& rView )
152
0
{
153
0
    ESelection aSel = rView.GetSelection();
154
0
    if (aSel.start.nIndex == aSel.end.nIndex && aSel.start.nIndex > 0)
155
0
    {
156
        //  Cursor is behind the inserted field -> extend selection to the left
157
158
0
        --aSel.start.nIndex;
159
0
        rView.SetSelection( aSel );
160
0
    }
161
0
}
162
163
void ScTabViewShell::InsertURLField( const OUString& rName, const OUString& rURL, const OUString& rTarget )
164
0
{
165
0
    SvxURLField aURLField( rURL, rName, SvxURLFormat::Repr );
166
0
    aURLField.SetTargetFrame( rTarget );
167
0
    SvxFieldItem aURLItem( aURLField, EE_FEATURE_FIELD );
168
169
0
    ScViewData&     rViewData   = GetViewData();
170
0
    ScModule*       pScMod      = ScModule::get();
171
0
    ScInputHandler* pHdl        = pScMod->GetInputHdl( rViewData.GetViewShell() );
172
173
0
    bool bSelectFirst = false;
174
0
    bool bIsEditMode = pScMod->IsEditMode();
175
0
    int nSelInd = 1;
176
0
    bool bHasText = !GetSelectionText().isEmpty();
177
178
0
    if ( !bIsEditMode )
179
0
    {
180
0
        if ( !SelectionEditable() )
181
0
        {
182
            // no error message (may be called from drag&drop)
183
0
            return;
184
0
        }
185
186
        // single url in cell is shown in the dialog and replaced
187
0
        bSelectFirst = HasBookmarkAtCursor( nullptr );
188
0
        pScMod->SetInputMode( SC_INPUT_TABLE );
189
0
    }
190
191
0
    EditView*       pTopView    = pHdl->GetTopView();
192
0
    EditView*       pTableView  = pHdl->GetTableView();
193
0
    OSL_ENSURE( pTopView || pTableView, "No EditView" );
194
195
    // Check if user selected a whole cell by single click, and cell has content.
196
    // tdf#80043 - if true, replace the entire content of the selected cell instead of
197
    // inserting a duplicate, or appending the url.
198
0
    if (!bIsEditMode && !bSelectFirst && pTableView && bHasText)
199
0
    {
200
        // Using GetSelectionText() length is not safe here in case of cell content has trailing spaces
201
0
        nSelInd = EE_TEXTPOS_MAX;
202
0
        bSelectFirst = true;
203
0
    }
204
205
0
    if ( bSelectFirst )
206
0
    {
207
0
        if ( pTopView )
208
0
            pTopView->SetSelection( ESelection(0,0,0,1) );
209
0
        if ( pTableView )
210
0
            pTableView->SetSelection( ESelection(0,0,0,nSelInd) );
211
0
    }
212
213
0
    pHdl->DataChanging();
214
215
0
    if ( pTopView )
216
0
    {
217
0
        pTopView->InsertField( aURLItem );
218
0
        lcl_SelectFieldAfterInsert( *pTopView );
219
0
    }
220
0
    if ( pTableView )
221
0
    {
222
0
        pTableView->InsertField( aURLItem );
223
0
        lcl_SelectFieldAfterInsert( *pTableView );
224
0
    }
225
226
0
    pHdl->DataChanged();
227
0
}
228
229
void ScTabViewShell::ExecSearch( SfxRequest& rReq )
230
0
{
231
0
    const SfxItemSet*   pReqArgs    = rReq.GetArgs();
232
0
    sal_uInt16              nSlot       = rReq.GetSlot();
233
0
    const SfxPoolItem*  pItem;
234
235
0
    switch ( nSlot )
236
0
    {
237
0
        case FID_SEARCH_NOW:
238
0
            {
239
0
                const SvxSearchItem* pSearchItem;
240
0
                if ( pReqArgs &&
241
0
                     (pSearchItem = pReqArgs->GetItemIfSet(SID_SEARCH_ITEM, false)) )
242
0
                {
243
0
                    ScGlobal::SetSearchItem( *pSearchItem );
244
0
                    SearchAndReplace( pSearchItem, true, rReq.IsAPI() );
245
0
                    rReq.Done();
246
0
                }
247
0
            }
248
0
            break;
249
250
0
        case SID_SEARCH_ITEM:
251
0
        {
252
0
            const SvxSearchItem* pSearchItem;
253
0
            if (pReqArgs && (pSearchItem =
254
0
                            pReqArgs->GetItemIfSet(SID_SEARCH_ITEM, false)))
255
0
            {
256
                // remember search item
257
0
                ScGlobal::SetSearchItem( *pSearchItem );
258
0
            }
259
0
            else
260
0
            {
261
0
                OSL_FAIL("SID_SEARCH_ITEM without Parameter");
262
0
            }
263
0
            break;
264
0
        }
265
0
        case FID_SEARCH:
266
0
        case FID_REPLACE:
267
0
        case FID_REPLACE_ALL:
268
0
        case FID_SEARCH_ALL:
269
0
            {
270
0
                if (pReqArgs && SfxItemState::SET == pReqArgs->GetItemState(nSlot, false, &pItem))
271
0
                {
272
                    // get search item
273
274
0
                    SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
275
276
                    // fill search item
277
278
0
                    aSearchItem.SetSearchString(static_cast<const SfxStringItem*>(pItem)->GetValue());
279
0
                    if(SfxItemState::SET == pReqArgs->GetItemState(FN_PARAM_1, false, &pItem))
280
0
                        aSearchItem.SetReplaceString(static_cast<const SfxStringItem*>(pItem)->GetValue());
281
282
0
                    if (nSlot == FID_SEARCH)
283
0
                        aSearchItem.SetCommand(SvxSearchCmd::FIND);
284
0
                    else if(nSlot == FID_REPLACE)
285
0
                        aSearchItem.SetCommand(SvxSearchCmd::REPLACE);
286
0
                    else if(nSlot == FID_REPLACE_ALL)
287
0
                        aSearchItem.SetCommand(SvxSearchCmd::REPLACE_ALL);
288
0
                    else
289
0
                        aSearchItem.SetCommand(SvxSearchCmd::FIND_ALL);
290
291
                    // execute request (which stores the SearchItem)
292
293
0
                    aSearchItem.SetWhich(SID_SEARCH_ITEM);
294
0
                    GetViewData().GetDispatcher().ExecuteList(FID_SEARCH_NOW,
295
0
                            rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
296
0
                                            SfxCallMode::RECORD,
297
0
                            { &aSearchItem });
298
0
                }
299
0
                else
300
0
                {
301
0
                    GetViewData().GetDispatcher().Execute(
302
0
                            SID_SEARCH_DLG, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD );
303
0
                }
304
0
            }
305
0
            break;
306
0
        case FID_REPEAT_SEARCH:
307
0
            {
308
                // once more with ScGlobal::GetSearchItem()
309
310
0
                SvxSearchItem aSearchItem = ScGlobal::GetSearchItem();
311
0
                aSearchItem.SetWhich(SID_SEARCH_ITEM);
312
0
                GetViewData().GetDispatcher().ExecuteList( FID_SEARCH_NOW,
313
0
                        rReq.IsAPI() ? SfxCallMode::API|SfxCallMode::SYNCHRON :
314
0
                                        SfxCallMode::RECORD,
315
0
                        { &aSearchItem });
316
0
            }
317
0
            break;
318
//      case FID_SEARCH_COUNT:
319
0
    }
320
0
}
321
322
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */