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 "rangelst.hxx"
21 #include "scitems.hxx"
22 #include <sfx2/dispatch.hxx>
23 #include <svl/zforlist.hxx>
24 #include <vcl/msgbox.hxx>
26 #include "uiitems.hxx"
27 #include "reffact.hxx"
28 #include "document.hxx"
29 #include "scresid.hxx"
30 #include "globstr.hrc"
34 #include "solvrdlg.hxx"
38 #define ERRORBOX(s) ErrorBox( this, WinBits( WB_OK | WB_DEF_OK), s ).Execute()
41 //============================================================================
43 //----------------------------------------------------------------------------
45 ScSolverDlg::ScSolverDlg( SfxBindings
* pB
, SfxChildWindow
* pCW
, Window
* pParent
,
46 ScDocument
* pDocument
,
47 ScAddress aCursorPos
)
49 : ScAnyRefDlg(pB
, pCW
, pParent
, "GoalSeekDialog", "modules/scalc/ui/goalseekdlg.ui")
50 , theFormulaCell(aCursorPos
)
51 , theVariableCell(aCursorPos
)
53 , nCurTab(aCursorPos
.Tab())
55 , bDlgLostFocus(false)
56 , errMsgInvalidVar(ScGlobal::GetRscString(STR_INVALIDVAR
))
57 , errMsgInvalidForm(ScGlobal::GetRscString(STR_INVALIDFORM
))
58 , errMsgNoFormula(ScGlobal::GetRscString(STR_NOFORMULA
))
59 , errMsgInvalidVal(ScGlobal::GetRscString(STR_INVALIDVAL
))
61 get(m_pFtFormulaCell
, "formulatext");
62 get(m_pEdFormulaCell
, "formulaedit");
63 m_pEdFormulaCell
->SetReferences(this, m_pFtFormulaCell
);
64 get(m_pRBFormulaCell
, "formulabutton");
65 m_pRBFormulaCell
->SetReferences(this, m_pEdFormulaCell
),
66 get(m_pEdTargetVal
, "target");
67 get(m_pFtVariableCell
, "vartext");
68 get(m_pEdVariableCell
, "varedit");
69 m_pEdVariableCell
->SetReferences(this, m_pFtVariableCell
);
70 get(m_pRBVariableCell
, "varbutton");
71 m_pRBVariableCell
->SetReferences(this, m_pEdVariableCell
);
73 get(m_pBtnCancel
, "cancel");
77 //----------------------------------------------------------------------------
79 ScSolverDlg::~ScSolverDlg()
83 //----------------------------------------------------------------------------
85 void ScSolverDlg::Init()
89 m_pBtnOk
->SetClickHdl( LINK( this, ScSolverDlg
, BtnHdl
) );
90 m_pBtnCancel
->SetClickHdl( LINK( this, ScSolverDlg
, BtnHdl
) );
92 Link aLink
= LINK( this, ScSolverDlg
, GetFocusHdl
);
93 m_pEdFormulaCell
->SetGetFocusHdl( aLink
);
94 m_pRBFormulaCell
->SetGetFocusHdl( aLink
);
95 m_pEdVariableCell
->SetGetFocusHdl( aLink
);
96 m_pRBVariableCell
->SetGetFocusHdl( aLink
);
97 m_pEdTargetVal
->SetGetFocusHdl( aLink
);
99 aLink
= LINK( this, ScSolverDlg
, LoseFocusHdl
);
100 m_pEdFormulaCell
->SetLoseFocusHdl ( aLink
);
101 m_pRBFormulaCell
->SetLoseFocusHdl ( aLink
);
102 m_pEdVariableCell
->SetLoseFocusHdl ( aLink
);
103 m_pRBVariableCell
->SetLoseFocusHdl ( aLink
);
105 theFormulaCell
.Format( aStr
, SCA_ABS
, NULL
, pDoc
->GetAddressConvention() );
107 m_pEdFormulaCell
->SetText( aStr
);
108 m_pEdFormulaCell
->GrabFocus();
109 pEdActive
= m_pEdFormulaCell
;
112 //----------------------------------------------------------------------------
114 sal_Bool
ScSolverDlg::Close()
116 return DoClose( ScSolverDlgWrapper::GetChildWindowId() );
119 //----------------------------------------------------------------------------
121 void ScSolverDlg::SetActive()
125 bDlgLostFocus
= false;
127 pEdActive
->GrabFocus();
136 //----------------------------------------------------------------------------
138 void ScSolverDlg::SetReference( const ScRange
& rRef
, ScDocument
* pDocP
)
142 if ( rRef
.aStart
!= rRef
.aEnd
)
143 RefInputStart(pEdActive
);
146 ScAddress aAdr
= rRef
.aStart
;
147 sal_uInt16 nFmt
= ( aAdr
.Tab() == nCurTab
)
151 aAdr
.Format( aStr
, nFmt
, pDocP
, pDocP
->GetAddressConvention() );
152 pEdActive
->SetRefString( aStr
);
154 if ( pEdActive
== m_pEdFormulaCell
)
155 theFormulaCell
= aAdr
;
156 else if ( pEdActive
== m_pEdVariableCell
)
157 theVariableCell
= aAdr
;
161 //----------------------------------------------------------------------------
163 void ScSolverDlg::RaiseError( ScSolverErr eError
)
167 case SOLVERR_NOFORMULA
:
168 ERRORBOX( errMsgNoFormula
);
169 m_pEdFormulaCell
->GrabFocus();
172 case SOLVERR_INVALID_FORMULA
:
173 ERRORBOX( errMsgInvalidForm
);
174 m_pEdFormulaCell
->GrabFocus();
177 case SOLVERR_INVALID_VARIABLE
:
178 ERRORBOX( errMsgInvalidVar
);
179 m_pEdVariableCell
->GrabFocus();
182 case SOLVERR_INVALID_TARGETVALUE
:
183 ERRORBOX( errMsgInvalidVal
);
184 m_pEdTargetVal
->GrabFocus();
189 //----------------------------------------------------------------------------
191 sal_Bool
ScSolverDlg::IsRefInputMode() const
193 return pEdActive
!= NULL
;
196 //----------------------------------------------------------------------------
198 sal_Bool
ScSolverDlg::CheckTargetValue( String
& rStrVal
)
203 return pDoc
->GetFormatTable()->IsNumberFormat( rStrVal
, n1
, n2
);
206 //----------------------------------------------------------------------------
209 IMPL_LINK( ScSolverDlg
, BtnHdl
, PushButton
*, pBtn
)
211 if (pBtn
== m_pBtnOk
)
213 theTargetValStr
= m_pEdTargetVal
->GetText();
216 // 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen?
217 // 2. verweist die Formel-Koordinate wirklich auf eine Formelzelle?
218 // 3. wurde ein korrekter Zielwert eingegeben
220 const formula::FormulaGrammar::AddressConvention eConv
= pDoc
->GetAddressConvention();
221 sal_uInt16 nRes1
= theFormulaCell
.Parse( m_pEdFormulaCell
->GetText(), pDoc
, eConv
);
222 sal_uInt16 nRes2
= theVariableCell
.Parse( m_pEdVariableCell
->GetText(), pDoc
, eConv
);
224 if ( SCA_VALID
== ( nRes1
& SCA_VALID
) )
226 if ( SCA_VALID
== ( nRes2
& SCA_VALID
) )
228 if ( CheckTargetValue( theTargetValStr
) )
231 pDoc
->GetCellType( theFormulaCell
.Col(),
232 theFormulaCell
.Row(),
233 theFormulaCell
.Tab(),
236 if ( CELLTYPE_FORMULA
== eType
)
238 ScSolveParam
aOutParam( theFormulaCell
,
241 ScSolveItem
aOutItem( SCITEM_SOLVEDATA
, &aOutParam
);
243 SetDispatcherLock( false );
246 GetBindings().GetDispatcher()->Execute( SID_SOLVE
,
247 SFX_CALLMODE_SLOT
| SFX_CALLMODE_RECORD
,
251 else RaiseError( SOLVERR_NOFORMULA
);
253 else RaiseError( SOLVERR_INVALID_TARGETVALUE
);
255 else RaiseError( SOLVERR_INVALID_VARIABLE
);
257 else RaiseError( SOLVERR_INVALID_FORMULA
);
259 else if (pBtn
== m_pBtnCancel
)
267 //----------------------------------------------------------------------------
269 IMPL_LINK( ScSolverDlg
, GetFocusHdl
, Control
*, pCtrl
)
274 if( (pCtrl
== (Control
*)m_pEdFormulaCell
) || (pCtrl
== (Control
*)m_pRBFormulaCell
) )
275 pEdit
= pEdActive
= m_pEdFormulaCell
;
276 else if( (pCtrl
== (Control
*)m_pEdVariableCell
) || (pCtrl
== (Control
*)m_pRBVariableCell
) )
277 pEdit
= pEdActive
= m_pEdVariableCell
;
278 else if( pCtrl
== (Control
*)m_pEdTargetVal
)
279 pEdit
= m_pEdTargetVal
;
282 pEdit
->SetSelection( Selection( 0, SELECTION_MAX
) );
287 //----------------------------------------------------------------------------
289 IMPL_LINK_NOARG(ScSolverDlg
, LoseFocusHdl
)
291 bDlgLostFocus
= !IsActive();
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */