summaryrefslogtreecommitdiff
path: root/sc/source/ui/miscdlgs/tabopdlg.cxx
blob: c651e6155473e62346abf4c019087bb188bce813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "scitems.hxx"
#include <sfx2/dispatch.hxx>
#include <vcl/layout.hxx>

#include "uiitems.hxx"
#include "global.hxx"
#include "document.hxx"
#include "scresid.hxx"
#include "sc.hrc"
#include "reffact.hxx"

#include "tabopdlg.hxx"

//  class ScTabOpDlg

ScTabOpDlg::ScTabOpDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent,
                        ScDocument*         pDocument,
                        const ScRefAddress& rCursorPos )

    : ScAnyRefDlg(pB, pCW, pParent, "MultipleOperationsDialog",
        "modules/scalc/ui/multipleoperationsdialog.ui")
    , theFormulaCell(rCursorPos)
    , pDoc(pDocument)
    , nCurTab(theFormulaCell.Tab())
    , pEdActive(NULL)
    , bDlgLostFocus(false)
    , errMsgNoFormula(ScResId(STR_NOFORMULASPECIFIED))
    , errMsgNoColRow(ScResId(STR_NOCOLROW))
    , errMsgWrongFormula(ScResId(STR_WRONGFORMULA))
    , errMsgWrongRowCol(ScResId(STR_WRONGROWCOL))
    , errMsgNoColFormula(ScResId(STR_NOCOLFORMULA))
    , errMsgNoRowFormula(ScResId(STR_NOROWFORMULA))
{
    get(m_pFtFormulaRange, "formulasft");
    get(m_pEdFormulaRange, "formulas");
    m_pEdFormulaRange->SetReferences(this, m_pFtFormulaRange);
    get(m_pRBFormulaRange, "formulasref");
    m_pRBFormulaRange->SetReferences(this, m_pEdFormulaRange);

    get(m_pFtRowCell, "rowft");
    get(m_pEdRowCell, "row");
    m_pEdRowCell->SetReferences(this, m_pFtRowCell);
    get(m_pRBRowCell, "rowref");
    m_pRBRowCell->SetReferences(this, m_pEdRowCell);

    get(m_pFtColCell, "colft");
    get(m_pEdColCell, "col");
    m_pEdColCell->SetReferences(this, m_pFtColCell);
    get(m_pRBColCell, "colref");
    m_pRBColCell->SetReferences(this, m_pEdColCell);

    get(m_pBtnOk, "ok");
    get(m_pBtnCancel, "cancel");

    Init();
}

ScTabOpDlg::~ScTabOpDlg()
{
    Hide();
}

void ScTabOpDlg::Init()
{
    m_pBtnOk->SetClickHdl     ( LINK( this, ScTabOpDlg, BtnHdl ) );
    m_pBtnCancel->SetClickHdl     ( LINK( this, ScTabOpDlg, BtnHdl ) );

    Link aLink = LINK( this, ScTabOpDlg, GetFocusHdl );
    m_pEdFormulaRange->SetGetFocusHdl( aLink );
    m_pRBFormulaRange->SetGetFocusHdl( aLink );
    m_pEdRowCell->SetGetFocusHdl( aLink );
    m_pRBRowCell->SetGetFocusHdl( aLink );
    m_pEdColCell->SetGetFocusHdl( aLink );
    m_pRBColCell->SetGetFocusHdl( aLink );

    aLink = LINK( this, ScTabOpDlg, LoseFocusHdl );
    m_pEdFormulaRange->SetLoseFocusHdl( aLink );
    m_pRBFormulaRange->SetLoseFocusHdl( aLink );
    m_pEdRowCell->SetLoseFocusHdl( aLink );
    m_pRBRowCell->SetLoseFocusHdl( aLink );
    m_pEdColCell->SetLoseFocusHdl( aLink );
    m_pRBColCell->SetLoseFocusHdl( aLink );

    m_pEdFormulaRange->GrabFocus();
    pEdActive = m_pEdFormulaRange;

    //@BugID 54702 Enablen/Disablen nur noch in Basisklasse
    //SFX_APPWINDOW->Enable();
}

bool ScTabOpDlg::Close()
{
    return DoClose( ScTabOpDlgWrapper::GetChildWindowId() );
}

void ScTabOpDlg::SetActive()
{
    if ( bDlgLostFocus )
    {
        bDlgLostFocus = false;
        if( pEdActive )
            pEdActive->GrabFocus();
    }
    else
        GrabFocus();

    RefInputDone();
}

void ScTabOpDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
{
    if ( pEdActive )
    {
        ScAddress::Details aDetails(pDocP->GetAddressConvention(), 0, 0);

        if ( rRef.aStart != rRef.aEnd )
            RefInputStart(pEdActive);

        OUString      aStr;
        sal_uInt16      nFmt = ( rRef.aStart.Tab() == nCurTab )
                                ? SCR_ABS
                                : SCR_ABS_3D;

        if (pEdActive == m_pEdFormulaRange)
        {
            theFormulaCell.Set( rRef.aStart, false, false, false);
            theFormulaEnd.Set( rRef.aEnd, false, false, false);
            aStr = rRef.Format(nFmt, pDocP, aDetails);
        }
        else if ( pEdActive == m_pEdRowCell )
        {
            theRowCell.Set( rRef.aStart, false, false, false);
            aStr = rRef.aStart.Format(nFmt, pDocP, aDetails);
        }
        else if ( pEdActive == m_pEdColCell )
        {
            theColCell.Set( rRef.aStart, false, false, false);
            aStr = rRef.aStart.Format(nFmt, pDocP, aDetails);
        }

        pEdActive->SetRefString( aStr );
    }
}

void ScTabOpDlg::RaiseError( ScTabOpErr eError )
{
    const OUString* pMsg = &errMsgNoFormula;
    Edit*           pEd  = m_pEdFormulaRange;

    switch ( eError )
    {
        case TABOPERR_NOFORMULA:
            pMsg = &errMsgNoFormula;
            pEd  = m_pEdFormulaRange;
            break;

        case TABOPERR_NOCOLROW:
            pMsg = &errMsgNoColRow;
            pEd  = m_pEdRowCell;
            break;

        case TABOPERR_WRONGFORMULA:
            pMsg = &errMsgWrongFormula;
            pEd  = m_pEdFormulaRange;
            break;

        case TABOPERR_WRONGROW:
            pMsg = &errMsgWrongRowCol;
            pEd  = m_pEdRowCell;
            break;

        case TABOPERR_NOCOLFORMULA:
            pMsg = &errMsgNoColFormula;
            pEd  = m_pEdFormulaRange;
            break;

        case TABOPERR_WRONGCOL:
            pMsg = &errMsgWrongRowCol;
            pEd  = m_pEdColCell;
            break;

        case TABOPERR_NOROWFORMULA:
            pMsg = &errMsgNoRowFormula;
            pEd  = m_pEdFormulaRange;
            break;
    }

    MessageDialog(this, *pMsg, VCL_MESSAGE_ERROR, VCL_BUTTONS_OK_CANCEL).Execute();
    pEd->GrabFocus();
}

static bool lcl_Parse( const OUString& rString, ScDocument* pDoc, SCTAB nCurTab,
                ScRefAddress& rStart, ScRefAddress& rEnd )
{
    bool bRet = false;
    const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
    if ( rString.indexOf(':') != -1 )
        bRet = ConvertDoubleRef( pDoc, rString, nCurTab, rStart, rEnd, eConv );
    else
    {
        bRet = ConvertSingleRef( pDoc, rString, nCurTab, rStart, eConv );
        rEnd = rStart;
    }
    return bRet;
}

// Handler:

IMPL_LINK( ScTabOpDlg, BtnHdl, PushButton*, pBtn )
{
    if (pBtn == m_pBtnOk)
    {
        ScTabOpParam::Mode eMode = ScTabOpParam::Column;
        sal_uInt16 nError = 0;

        // Zu ueberpruefen:
        // 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen?
        // 2. IstFormelRang Zeile bei leerer Zeile bzw. Spalte bei leerer Spalte
        //    bzw. Einfachreferenz bei beidem?
        // 3. Ist mindestens Zeile oder Spalte und Formel voll?

        if (m_pEdFormulaRange->GetText().isEmpty())
            nError = TABOPERR_NOFORMULA;
        else if (m_pEdRowCell->GetText().isEmpty() &&
                 m_pEdColCell->GetText().isEmpty())
            nError = TABOPERR_NOCOLROW;
        else if ( !lcl_Parse( m_pEdFormulaRange->GetText(), pDoc, nCurTab,
                                theFormulaCell, theFormulaEnd ) )
            nError = TABOPERR_WRONGFORMULA;
        else
        {
            const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
            if (!m_pEdRowCell->GetText().isEmpty())
            {
                if (!ConvertSingleRef( pDoc, m_pEdRowCell->GetText(), nCurTab,
                                       theRowCell, eConv ))
                    nError = TABOPERR_WRONGROW;
                else
                {
                    if (m_pEdColCell->GetText().isEmpty() &&
                        theFormulaCell.Col() != theFormulaEnd.Col())
                        nError = TABOPERR_NOCOLFORMULA;
                    else
                        eMode = ScTabOpParam::Row;
                }
            }
            if (!m_pEdColCell->GetText().isEmpty())
            {
                if (!ConvertSingleRef( pDoc, m_pEdColCell->GetText(), nCurTab,
                                       theColCell, eConv ))
                    nError = TABOPERR_WRONGCOL;
                else
                {
                    if (eMode == ScTabOpParam::Row)                         // beides
                    {
                        eMode = ScTabOpParam::Both;
                        ConvertSingleRef( pDoc, m_pEdFormulaRange->GetText(), nCurTab,
                                          theFormulaCell, eConv );
                    }
                    else if (theFormulaCell.Row() != theFormulaEnd.Row())
                        nError = TABOPERR_NOROWFORMULA;
                    else
                        eMode = ScTabOpParam::Column;
                }
            }
        }

        if (nError)
            RaiseError( (ScTabOpErr) nError );
        else
        {
            ScTabOpParam aOutParam(theFormulaCell, theFormulaEnd, theRowCell, theColCell, eMode);
            ScTabOpItem  aOutItem( SID_TABOP, &aOutParam );

            SetDispatcherLock( false );
            SwitchToDocument();
            GetBindings().GetDispatcher()->Execute( SID_TABOP,
                                      SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD,
                                      &aOutItem, 0L, 0L );
            Close();
        }
    }
    else if (pBtn == m_pBtnCancel)
        Close();

    return 0;
}

IMPL_LINK( ScTabOpDlg, GetFocusHdl, Control*, pCtrl )
{
    if( (pCtrl == (Control*)m_pEdFormulaRange) || (pCtrl == (Control*)m_pRBFormulaRange) )
        pEdActive = m_pEdFormulaRange;
    else if( (pCtrl == (Control*)m_pEdRowCell) || (pCtrl == (Control*)m_pRBRowCell) )
        pEdActive = m_pEdRowCell;
    else if( (pCtrl == (Control*)m_pEdColCell) || (pCtrl == (Control*)m_pRBColCell) )
        pEdActive = m_pEdColCell;
    else
        pEdActive = NULL;

    if( pEdActive )
        pEdActive->SetSelection( Selection( 0, SELECTION_MAX ) );

    return 0;
}

IMPL_LINK_NOARG(ScTabOpDlg, LoseFocusHdl)
{
    bDlgLostFocus = !IsActive();
    return 0;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */