Coverage Report

Created: 2026-02-14 09:37

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