Coverage Report

Created: 2025-07-07 10:01

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