Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/miscdlgs/tabopdlg.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 <sfx2/dispatch.hxx>
21
#include <vcl/svapp.hxx>
22
#include <vcl/weld/Dialog.hxx>
23
#include <vcl/weld/MessageDialog.hxx>
24
25
#include <uiitems.hxx>
26
#include <document.hxx>
27
#include <scresid.hxx>
28
#include <sc.hrc>
29
#include <strings.hrc>
30
#include <reffact.hxx>
31
32
#include <tabopdlg.hxx>
33
34
35
ScTabOpDlg::ScTabOpDlg(SfxBindings* pB, SfxChildWindow* pCW, weld::Window* pParent,
36
                       ScDocument* pDocument,
37
                       const ScRefAddress& rCursorPos )
38
0
    : ScAnyRefDlgController(pB, pCW, pParent, u"modules/scalc/ui/multipleoperationsdialog.ui"_ustr,
39
0
                            u"MultipleOperationsDialog"_ustr)
40
0
    , theFormulaCell(rCursorPos)
41
0
    , pDoc(pDocument)
42
0
    , nCurTab(theFormulaCell.Tab())
43
0
    , bDlgLostFocus(false)
44
0
    , errMsgNoFormula(ScResId(STR_NOFORMULASPECIFIED))
45
0
    , errMsgNoColRow(ScResId(STR_NOCOLROW))
46
0
    , errMsgWrongFormula(ScResId(STR_WRONGFORMULA))
47
0
    , errMsgWrongRowCol(ScResId(STR_WRONGROWCOL))
48
0
    , errMsgNoColFormula(ScResId(STR_NOCOLFORMULA))
49
0
    , errMsgNoRowFormula(ScResId(STR_NOROWFORMULA))
50
0
    , m_pEdActive(nullptr)
51
0
    , m_xFtFormulaRange(m_xBuilder->weld_label(u"formulasft"_ustr))
52
0
    , m_xEdFormulaRange(new formula::RefEdit(m_xBuilder->weld_entry(u"formulas"_ustr)))
53
0
    , m_xRBFormulaRange(new formula::RefButton(m_xBuilder->weld_button(u"formulasref"_ustr)))
54
0
    , m_xFtRowCell(m_xBuilder->weld_label(u"rowft"_ustr))
55
0
    , m_xEdRowCell(new formula::RefEdit(m_xBuilder->weld_entry(u"row"_ustr)))
56
0
    , m_xRBRowCell(new formula::RefButton(m_xBuilder->weld_button(u"rowref"_ustr)))
57
0
    , m_xFtColCell(m_xBuilder->weld_label(u"colft"_ustr))
58
0
    , m_xEdColCell(new formula::RefEdit(m_xBuilder->weld_entry(u"col"_ustr)))
59
0
    , m_xRBColCell(new formula::RefButton(m_xBuilder->weld_button(u"colref"_ustr)))
60
0
    , m_xBtnOk(m_xBuilder->weld_button(u"ok"_ustr))
61
0
    , m_xBtnCancel(m_xBuilder->weld_button(u"cancel"_ustr))
62
0
{
63
0
    m_xEdFormulaRange->SetReferences(this, m_xFtFormulaRange.get());
64
0
    m_xRBFormulaRange->SetReferences(this, m_xEdFormulaRange.get());
65
66
0
    m_xEdRowCell->SetReferences(this, m_xFtRowCell.get());
67
0
    m_xRBRowCell->SetReferences(this, m_xEdRowCell.get());
68
69
0
    m_xEdColCell->SetReferences(this, m_xFtColCell.get());
70
0
    m_xRBColCell->SetReferences(this, m_xEdColCell.get());
71
72
0
    Init();
73
0
}
74
75
ScTabOpDlg::~ScTabOpDlg()
76
0
{
77
0
}
78
79
void ScTabOpDlg::Init()
80
0
{
81
0
    m_xBtnOk->connect_clicked( LINK( this, ScTabOpDlg, BtnHdl ) );
82
0
    m_xBtnCancel->connect_clicked( LINK( this, ScTabOpDlg, BtnHdl ) );
83
84
0
    Link<formula::RefEdit&,void> aEditLink = LINK( this, ScTabOpDlg, GetEditFocusHdl );
85
0
    m_xEdFormulaRange->SetGetFocusHdl( aEditLink );
86
0
    m_xEdRowCell->SetGetFocusHdl( aEditLink );
87
0
    m_xEdColCell->SetGetFocusHdl( aEditLink );
88
89
0
    Link<formula::RefButton&,void> aButtonLink = LINK( this, ScTabOpDlg, GetButtonFocusHdl );
90
0
    m_xRBFormulaRange->SetGetFocusHdl( aButtonLink );
91
0
    m_xRBRowCell->SetGetFocusHdl( aButtonLink );
92
0
    m_xRBColCell->SetGetFocusHdl( aButtonLink );
93
94
0
    aEditLink = LINK( this, ScTabOpDlg, LoseEditFocusHdl );
95
0
    m_xEdFormulaRange->SetLoseFocusHdl( aEditLink );
96
0
    m_xEdRowCell->SetLoseFocusHdl( aEditLink );
97
0
    m_xEdColCell->SetLoseFocusHdl( aEditLink );
98
99
0
    aButtonLink = LINK( this, ScTabOpDlg, LoseButtonFocusHdl );
100
0
    m_xRBFormulaRange->SetLoseFocusHdl( aButtonLink );
101
0
    m_xRBRowCell->SetLoseFocusHdl( aButtonLink );
102
0
    m_xRBColCell->SetLoseFocusHdl( aButtonLink );
103
104
0
    m_xEdFormulaRange->GrabFocus();
105
0
    m_pEdActive = m_xEdFormulaRange.get();
106
0
}
107
108
void ScTabOpDlg::Close()
109
0
{
110
0
    DoClose( ScTabOpDlgWrapper::GetChildWindowId() );
111
0
}
112
113
void ScTabOpDlg::SetActive()
114
0
{
115
0
    if ( bDlgLostFocus )
116
0
    {
117
0
        bDlgLostFocus = false;
118
0
        if (m_pEdActive)
119
0
            m_pEdActive->GrabFocus();
120
0
    }
121
0
    else
122
0
        m_xDialog->grab_focus();
123
124
0
    RefInputDone();
125
0
}
126
127
void ScTabOpDlg::SetReference( const ScRange& rRef, ScDocument& rDocP )
128
0
{
129
0
    if (!m_pEdActive)
130
0
        return;
131
132
0
    ScAddress::Details aDetails(rDocP.GetAddressConvention(), 0, 0);
133
134
0
    if ( rRef.aStart != rRef.aEnd )
135
0
        RefInputStart(m_pEdActive);
136
137
0
    OUString      aStr;
138
0
    ScRefFlags      nFmt = ( rRef.aStart.Tab() == nCurTab )
139
0
                            ? ScRefFlags::RANGE_ABS
140
0
                            : ScRefFlags::RANGE_ABS_3D;
141
142
0
    if (m_pEdActive == m_xEdFormulaRange.get())
143
0
    {
144
0
        theFormulaCell.Set( rRef.aStart, false, false, false);
145
0
        theFormulaEnd.Set( rRef.aEnd, false, false, false);
146
0
        aStr = rRef.Format(rDocP, nFmt, aDetails);
147
0
    }
148
0
    else if (m_pEdActive == m_xEdRowCell.get())
149
0
    {
150
0
        theRowCell.Set( rRef.aStart, false, false, false);
151
0
        aStr = rRef.aStart.Format(nFmt, &rDocP, aDetails);
152
0
    }
153
0
    else if (m_pEdActive == m_xEdColCell.get())
154
0
    {
155
0
        theColCell.Set( rRef.aStart, false, false, false);
156
0
        aStr = rRef.aStart.Format(nFmt, &rDocP, aDetails);
157
0
    }
158
159
0
    m_pEdActive->SetRefString( aStr );
160
0
}
161
162
void ScTabOpDlg::RaiseError( ScTabOpErr eError )
163
0
{
164
0
    const OUString* pMsg = &errMsgNoFormula;
165
0
    formula::RefEdit* pEd  = m_xEdFormulaRange.get();
166
167
0
    switch ( eError )
168
0
    {
169
0
        case TABOPERR_NOFORMULA:
170
0
            pMsg = &errMsgNoFormula;
171
0
            pEd  = m_xEdFormulaRange.get();
172
0
            break;
173
174
0
        case TABOPERR_NOCOLROW:
175
0
            pMsg = &errMsgNoColRow;
176
0
            pEd  = m_xEdRowCell.get();
177
0
            break;
178
179
0
        case TABOPERR_WRONGFORMULA:
180
0
            pMsg = &errMsgWrongFormula;
181
0
            pEd  = m_xEdFormulaRange.get();
182
0
            break;
183
184
0
        case TABOPERR_WRONGROW:
185
0
            pMsg = &errMsgWrongRowCol;
186
0
            pEd  = m_xEdRowCell.get();
187
0
            break;
188
189
0
        case TABOPERR_NOCOLFORMULA:
190
0
            pMsg = &errMsgNoColFormula;
191
0
            pEd  = m_xEdFormulaRange.get();
192
0
            break;
193
194
0
        case TABOPERR_WRONGCOL:
195
0
            pMsg = &errMsgWrongRowCol;
196
0
            pEd  = m_xEdColCell.get();
197
0
            break;
198
199
0
        case TABOPERR_NOROWFORMULA:
200
0
            pMsg = &errMsgNoRowFormula;
201
0
            pEd  = m_xEdFormulaRange.get();
202
0
            break;
203
0
    }
204
205
0
    std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
206
0
                                              VclMessageType::Error, VclButtonsType::OkCancel, *pMsg));
207
0
    xBox->run();
208
0
    pEd->GrabFocus();
209
0
}
210
211
static bool lcl_Parse( const OUString& rString, const ScDocument& rDoc, SCTAB nCurTab,
212
                ScRefAddress& rStart, ScRefAddress& rEnd )
213
0
{
214
0
    bool bRet = false;
215
0
    const formula::FormulaGrammar::AddressConvention eConv = rDoc.GetAddressConvention();
216
0
    if ( rString.indexOf(':') != -1 )
217
0
        bRet = ConvertDoubleRef( rDoc, rString, nCurTab, rStart, rEnd, eConv );
218
0
    else
219
0
    {
220
0
        bRet = ConvertSingleRef( rDoc, rString, nCurTab, rStart, eConv );
221
0
        rEnd = rStart;
222
0
    }
223
0
    return bRet;
224
0
}
225
226
// Handler:
227
228
IMPL_LINK(ScTabOpDlg, BtnHdl, weld::Button&, rBtn, void)
229
0
{
230
0
    if (&rBtn == m_xBtnOk.get())
231
0
    {
232
0
        ScTabOpParam::Mode eMode = ScTabOpParam::Column;
233
0
        sal_uInt16 nError = 0;
234
235
        // The following code checks:
236
        // 1. do the strings contain correct cell references / defined names?
237
        // 2. is formula range row if row is empty or column if column is empty
238
        //    or single reference if both?
239
        // 3. is at least one of row or column non-empty?
240
241
0
        if (m_xEdFormulaRange->GetText().isEmpty())
242
0
            nError = TABOPERR_NOFORMULA;
243
0
        else if (m_xEdRowCell->GetText().isEmpty() &&
244
0
                 m_xEdColCell->GetText().isEmpty())
245
0
            nError = TABOPERR_NOCOLROW;
246
0
        else if ( !lcl_Parse( m_xEdFormulaRange->GetText(), *pDoc, nCurTab,
247
0
                                theFormulaCell, theFormulaEnd ) )
248
0
            nError = TABOPERR_WRONGFORMULA;
249
0
        else
250
0
        {
251
0
            const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
252
0
            if (!m_xEdRowCell->GetText().isEmpty())
253
0
            {
254
0
                if (!ConvertSingleRef( *pDoc, m_xEdRowCell->GetText(), nCurTab,
255
0
                                       theRowCell, eConv ))
256
0
                    nError = TABOPERR_WRONGROW;
257
0
                else
258
0
                {
259
0
                    if (m_xEdColCell->GetText().isEmpty() &&
260
0
                        theFormulaCell.Col() != theFormulaEnd.Col())
261
0
                        nError = TABOPERR_NOCOLFORMULA;
262
0
                    else
263
0
                        eMode = ScTabOpParam::Row;
264
0
                }
265
0
            }
266
0
            if (!m_xEdColCell->GetText().isEmpty())
267
0
            {
268
0
                if (!ConvertSingleRef( *pDoc, m_xEdColCell->GetText(), nCurTab,
269
0
                                       theColCell, eConv ))
270
0
                    nError = TABOPERR_WRONGCOL;
271
0
                else
272
0
                {
273
0
                    if (eMode == ScTabOpParam::Row)                         // both
274
0
                    {
275
0
                        eMode = ScTabOpParam::Both;
276
0
                        ConvertSingleRef( *pDoc, m_xEdFormulaRange->GetText(), nCurTab,
277
0
                                          theFormulaCell, eConv );
278
0
                    }
279
0
                    else if (theFormulaCell.Row() != theFormulaEnd.Row())
280
0
                        nError = TABOPERR_NOROWFORMULA;
281
0
                    else
282
0
                        eMode = ScTabOpParam::Column;
283
0
                }
284
0
            }
285
0
        }
286
287
0
        if (nError)
288
0
            RaiseError( static_cast<ScTabOpErr>(nError) );
289
0
        else
290
0
        {
291
0
            ScTabOpParam aOutParam(theFormulaCell, theFormulaEnd, theRowCell, theColCell, eMode);
292
0
            ScTabOpItem  aOutItem( SID_TABOP, &aOutParam );
293
294
0
            SetDispatcherLock( false );
295
0
            SwitchToDocument();
296
0
            GetBindings().GetDispatcher()->ExecuteList(SID_TABOP,
297
0
                                      SfxCallMode::SLOT | SfxCallMode::RECORD,
298
0
                                      { &aOutItem });
299
0
            response(RET_OK);
300
0
        }
301
0
    }
302
0
    else if (&rBtn == m_xBtnCancel.get())
303
0
        response(RET_CANCEL);
304
0
}
305
306
IMPL_LINK( ScTabOpDlg, GetEditFocusHdl, formula::RefEdit&, rCtrl, void )
307
0
{
308
0
    if (&rCtrl == m_xEdFormulaRange.get())
309
0
        m_pEdActive = m_xEdFormulaRange.get();
310
0
    else if (&rCtrl == m_xEdRowCell.get())
311
0
        m_pEdActive = m_xEdRowCell.get();
312
0
    else if (&rCtrl == m_xEdColCell.get())
313
0
        m_pEdActive = m_xEdColCell.get();
314
0
    else
315
0
        m_pEdActive = nullptr;
316
317
0
    if( m_pEdActive )
318
0
        m_pEdActive->SelectAll();
319
0
}
320
321
IMPL_LINK( ScTabOpDlg, GetButtonFocusHdl, formula::RefButton&, rCtrl, void )
322
0
{
323
0
    if (&rCtrl == m_xRBFormulaRange.get())
324
0
        m_pEdActive = m_xEdFormulaRange.get();
325
0
    else if (&rCtrl == m_xRBRowCell.get())
326
0
        m_pEdActive = m_xEdRowCell.get();
327
0
    else if (&rCtrl == m_xRBColCell.get())
328
0
        m_pEdActive = m_xEdColCell.get();
329
0
    else
330
0
        m_pEdActive = nullptr;
331
332
0
    if( m_pEdActive )
333
0
        m_pEdActive->SelectAll();
334
0
}
335
336
IMPL_LINK_NOARG(ScTabOpDlg, LoseEditFocusHdl, formula::RefEdit&, void)
337
0
{
338
0
    bDlgLostFocus = !m_xDialog->has_toplevel_focus();
339
0
}
340
341
IMPL_LINK_NOARG(ScTabOpDlg, LoseButtonFocusHdl, formula::RefButton&, void)
342
0
{
343
0
    bDlgLostFocus = !m_xDialog->has_toplevel_focus();
344
0
}
345
346
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */