1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "scitems.hxx"
21 #include <sfx2/dispatch.hxx>
22 #include <vcl/msgbox.hxx>
24 #include "uiitems.hxx"
26 #include "document.hxx"
27 #include "scresid.hxx"
29 #include "reffact.hxx"
30 #include "tabopdlg.hrc"
33 #include "tabopdlg.hxx"
36 //============================================================================
38 //----------------------------------------------------------------------------
40 ScTabOpDlg::ScTabOpDlg( SfxBindings
* pB
, SfxChildWindow
* pCW
, Window
* pParent
,
41 ScDocument
* pDocument
,
42 const ScRefAddress
& rCursorPos
)
44 : ScAnyRefDlg ( pB
, pCW
, pParent
, RID_SCDLG_TABOP
),
46 aFlVariables ( this, ScResId( FL_VARIABLES
) ),
47 aFtFormulaRange ( this, ScResId( FT_FORMULARANGE
) ),
48 aEdFormulaRange ( this, this, &aFtFormulaRange
, ScResId( ED_FORMULARANGE
) ),
49 aRBFormulaRange ( this, ScResId( RB_FORMULARANGE
), &aEdFormulaRange
, this ),
50 aFtRowCell ( this, ScResId( FT_ROWCELL
) ),
51 aEdRowCell ( this, this, &aFtRowCell
, ScResId( ED_ROWCELL
) ),
52 aRBRowCell ( this, ScResId( RB_ROWCELL
), &aEdRowCell
, this ),
53 aFtColCell ( this, ScResId( FT_COLCELL
) ),
54 aEdColCell ( this, this, &aFtColCell
, ScResId( ED_COLCELL
) ),
55 aRBColCell ( this, ScResId( RB_COLCELL
), &aEdColCell
, this ),
56 aBtnOk ( this, ScResId( BTN_OK
) ),
57 aBtnCancel ( this, ScResId( BTN_CANCEL
) ),
58 aBtnHelp ( this, ScResId( BTN_HELP
) ),
60 theFormulaCell ( rCursorPos
),
62 nCurTab ( theFormulaCell
.Tab() ),
64 bDlgLostFocus ( false ),
65 errMsgNoFormula ( ScResId( STR_NOFORMULA
) ),
66 errMsgNoColRow ( ScResId( STR_NOCOLROW
) ),
67 errMsgWrongFormula ( ScResId( STR_WRONGFORMULA
) ),
68 errMsgWrongRowCol ( ScResId( STR_WRONGROWCOL
) ),
69 errMsgNoColFormula ( ScResId( STR_NOCOLFORMULA
) ),
70 errMsgNoRowFormula ( ScResId( STR_NOROWFORMULA
) )
76 //----------------------------------------------------------------------------
78 ScTabOpDlg::~ScTabOpDlg()
83 //----------------------------------------------------------------------------
85 void ScTabOpDlg::Init()
87 aBtnOk
. SetClickHdl ( LINK( this, ScTabOpDlg
, BtnHdl
) );
88 aBtnCancel
. SetClickHdl ( LINK( this, ScTabOpDlg
, BtnHdl
) );
90 Link aLink
= LINK( this, ScTabOpDlg
, GetFocusHdl
);
91 aEdFormulaRange
.SetGetFocusHdl( aLink
);
92 aRBFormulaRange
.SetGetFocusHdl( aLink
);
93 aEdRowCell
. SetGetFocusHdl( aLink
);
94 aRBRowCell
. SetGetFocusHdl( aLink
);
95 aEdColCell
. SetGetFocusHdl( aLink
);
96 aRBColCell
. SetGetFocusHdl( aLink
);
98 aLink
= LINK( this, ScTabOpDlg
, LoseFocusHdl
);
99 aEdFormulaRange
.SetLoseFocusHdl( aLink
);
100 aRBFormulaRange
.SetLoseFocusHdl( aLink
);
101 aEdRowCell
. SetLoseFocusHdl( aLink
);
102 aRBRowCell
. SetLoseFocusHdl( aLink
);
103 aEdColCell
. SetLoseFocusHdl( aLink
);
104 aRBColCell
. SetLoseFocusHdl( aLink
);
106 aEdFormulaRange
.GrabFocus();
107 pEdActive
= &aEdFormulaRange
;
109 //@BugID 54702 Enablen/Disablen nur noch in Basisklasse
110 //SFX_APPWINDOW->Enable();
113 //----------------------------------------------------------------------------
115 sal_Bool
ScTabOpDlg::Close()
117 return DoClose( ScTabOpDlgWrapper::GetChildWindowId() );
120 //----------------------------------------------------------------------------
122 void ScTabOpDlg::SetActive()
126 bDlgLostFocus
= false;
128 pEdActive
->GrabFocus();
136 //----------------------------------------------------------------------------
138 void ScTabOpDlg::SetReference( const ScRange
& rRef
, ScDocument
* pDocP
)
142 ScAddress::Details
aDetails(pDocP
->GetAddressConvention(), 0, 0);
144 if ( rRef
.aStart
!= rRef
.aEnd
)
145 RefInputStart(pEdActive
);
148 sal_uInt16 nFmt
= ( rRef
.aStart
.Tab() == nCurTab
)
152 if ( pEdActive
== &aEdFormulaRange
)
154 theFormulaCell
.Set( rRef
.aStart
, false, false, false);
155 theFormulaEnd
.Set( rRef
.aEnd
, false, false, false);
156 aStr
= rRef
.Format(nFmt
, pDocP
, aDetails
);
158 else if ( pEdActive
== &aEdRowCell
)
160 theRowCell
.Set( rRef
.aStart
, false, false, false);
161 aStr
= rRef
.aStart
.Format(nFmt
, pDocP
, aDetails
);
163 else if ( pEdActive
== &aEdColCell
)
165 theColCell
.Set( rRef
.aStart
, false, false, false);
166 aStr
= rRef
.aStart
.Format(nFmt
, pDocP
, aDetails
);
169 pEdActive
->SetRefString( aStr
);
173 //----------------------------------------------------------------------------
175 void ScTabOpDlg::RaiseError( ScTabOpErr eError
)
177 const OUString
* pMsg
= &errMsgNoFormula
;
178 Edit
* pEd
= &aEdFormulaRange
;
182 case TABOPERR_NOFORMULA
:
183 pMsg
= &errMsgNoFormula
;
184 pEd
= &aEdFormulaRange
;
187 case TABOPERR_NOCOLROW
:
188 pMsg
= &errMsgNoColRow
;
192 case TABOPERR_WRONGFORMULA
:
193 pMsg
= &errMsgWrongFormula
;
194 pEd
= &aEdFormulaRange
;
197 case TABOPERR_WRONGROW
:
198 pMsg
= &errMsgWrongRowCol
;
202 case TABOPERR_NOCOLFORMULA
:
203 pMsg
= &errMsgNoColFormula
;
204 pEd
= &aEdFormulaRange
;
207 case TABOPERR_WRONGCOL
:
208 pMsg
= &errMsgWrongRowCol
;
212 case TABOPERR_NOROWFORMULA
:
213 pMsg
= &errMsgNoRowFormula
;
214 pEd
= &aEdFormulaRange
;
218 ErrorBox( this, WinBits( WB_OK_CANCEL
| WB_DEF_OK
), *pMsg
).Execute();
222 //----------------------------------------------------------------------------
224 static sal_Bool
lcl_Parse( const OUString
& rString
, ScDocument
* pDoc
, SCTAB nCurTab
,
225 ScRefAddress
& rStart
, ScRefAddress
& rEnd
)
227 sal_Bool bRet
= false;
228 const formula::FormulaGrammar::AddressConvention eConv
= pDoc
->GetAddressConvention();
229 if ( rString
.indexOf(':') != -1 )
230 bRet
= ConvertDoubleRef( pDoc
, rString
, nCurTab
, rStart
, rEnd
, eConv
);
233 bRet
= ConvertSingleRef( pDoc
, rString
, nCurTab
, rStart
, eConv
);
239 //----------------------------------------------------------------------------
242 IMPL_LINK( ScTabOpDlg
, BtnHdl
, PushButton
*, pBtn
)
244 if ( pBtn
== &aBtnOk
)
246 ScTabOpParam::Mode eMode
= ScTabOpParam::Column
;
247 sal_uInt16 nError
= 0;
250 // 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen?
251 // 2. IstFormelRang Zeile bei leerer Zeile bzw. Spalte bei leerer Spalte
252 // bzw. Einfachreferenz bei beidem?
253 // 3. Ist mindestens Zeile oder Spalte und Formel voll?
255 if (aEdFormulaRange
.GetText().isEmpty())
256 nError
= TABOPERR_NOFORMULA
;
257 else if (aEdRowCell
.GetText().isEmpty() &&
258 aEdColCell
.GetText().isEmpty())
259 nError
= TABOPERR_NOCOLROW
;
260 else if ( !lcl_Parse( aEdFormulaRange
.GetText(), pDoc
, nCurTab
,
261 theFormulaCell
, theFormulaEnd
) )
262 nError
= TABOPERR_WRONGFORMULA
;
265 const formula::FormulaGrammar::AddressConvention eConv
= pDoc
->GetAddressConvention();
266 if (!aEdRowCell
.GetText().isEmpty())
268 if (!ConvertSingleRef( pDoc
, aEdRowCell
.GetText(), nCurTab
,
270 nError
= TABOPERR_WRONGROW
;
273 if (aEdColCell
.GetText().isEmpty() &&
274 theFormulaCell
.Col() != theFormulaEnd
.Col())
275 nError
= TABOPERR_NOCOLFORMULA
;
277 eMode
= ScTabOpParam::Row
;
280 if (!aEdColCell
.GetText().isEmpty())
282 if (!ConvertSingleRef( pDoc
, aEdColCell
.GetText(), nCurTab
,
284 nError
= TABOPERR_WRONGCOL
;
287 if (eMode
== ScTabOpParam::Row
) // beides
289 eMode
= ScTabOpParam::Both
;
290 ConvertSingleRef( pDoc
, aEdFormulaRange
.GetText(), nCurTab
,
291 theFormulaCell
, eConv
);
293 else if (theFormulaCell
.Row() != theFormulaEnd
.Row())
294 nError
= TABOPERR_NOROWFORMULA
;
296 eMode
= ScTabOpParam::Column
;
302 RaiseError( (ScTabOpErr
) nError
);
305 ScTabOpParam
aOutParam(theFormulaCell
, theFormulaEnd
, theRowCell
, theColCell
, eMode
);
306 ScTabOpItem
aOutItem( SID_TABOP
, &aOutParam
);
308 SetDispatcherLock( false );
310 GetBindings().GetDispatcher()->Execute( SID_TABOP
,
311 SFX_CALLMODE_SLOT
| SFX_CALLMODE_RECORD
,
316 else if ( pBtn
== &aBtnCancel
)
322 //----------------------------------------------------------------------------
324 IMPL_LINK( ScTabOpDlg
, GetFocusHdl
, Control
*, pCtrl
)
326 if( (pCtrl
== (Control
*)&aEdFormulaRange
) || (pCtrl
== (Control
*)&aRBFormulaRange
) )
327 pEdActive
= &aEdFormulaRange
;
328 else if( (pCtrl
== (Control
*)&aEdRowCell
) || (pCtrl
== (Control
*)&aRBRowCell
) )
329 pEdActive
= &aEdRowCell
;
330 else if( (pCtrl
== (Control
*)&aEdColCell
) || (pCtrl
== (Control
*)&aRBColCell
) )
331 pEdActive
= &aEdColCell
;
336 pEdActive
->SetSelection( Selection( 0, SELECTION_MAX
) );
341 //----------------------------------------------------------------------------
343 IMPL_LINK_NOARG(ScTabOpDlg
, LoseFocusHdl
)
345 bDlgLostFocus
= !IsActive();
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */