Line data Source code
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 "rangelst.hxx"
21 : #include "scitems.hxx"
22 : #include <sfx2/dispatch.hxx>
23 : #include <svl/zforlist.hxx>
24 : #include <vcl/msgbox.hxx>
25 :
26 : #include "uiitems.hxx"
27 : #include "reffact.hxx"
28 : #include "document.hxx"
29 : #include "scresid.hxx"
30 : #include "globstr.hrc"
31 : #include "sc.hrc"
32 : #include "solvrdlg.hxx"
33 :
34 : namespace
35 : {
36 0 : void lclErrorDialog( vcl::Window* pParent, const OUString& aString )
37 : {
38 0 : ScopedVclPtrInstance<MessageDialog>::Create(pParent, aString)->Execute();
39 0 : }
40 : }
41 :
42 0 : ScSolverDlg::ScSolverDlg( SfxBindings* pB, SfxChildWindow* pCW, vcl::Window* pParent,
43 : ScDocument* pDocument,
44 : ScAddress aCursorPos )
45 :
46 : : ScAnyRefDlg(pB, pCW, pParent, "GoalSeekDialog", "modules/scalc/ui/goalseekdlg.ui")
47 : , theFormulaCell(aCursorPos)
48 : , theVariableCell(aCursorPos)
49 : , pDoc(pDocument)
50 0 : , nCurTab(aCursorPos.Tab())
51 : , pEdActive(NULL)
52 : , bDlgLostFocus(false)
53 0 : , errMsgInvalidVar(ScGlobal::GetRscString(STR_INVALIDVAR))
54 0 : , errMsgInvalidForm(ScGlobal::GetRscString(STR_INVALIDFORM))
55 0 : , errMsgNoFormula(ScGlobal::GetRscString(STR_NOFORMULA))
56 0 : , errMsgInvalidVal(ScGlobal::GetRscString(STR_INVALIDVAL))
57 : {
58 0 : get(m_pFtFormulaCell, "formulatext");
59 0 : get(m_pEdFormulaCell, "formulaedit");
60 0 : m_pEdFormulaCell->SetReferences(this, m_pFtFormulaCell);
61 0 : get(m_pRBFormulaCell, "formulabutton");
62 0 : m_pRBFormulaCell->SetReferences(this, m_pEdFormulaCell),
63 0 : get(m_pEdTargetVal, "target");
64 0 : get(m_pFtVariableCell, "vartext");
65 0 : get(m_pEdVariableCell, "varedit");
66 0 : m_pEdVariableCell->SetReferences(this, m_pFtVariableCell);
67 0 : get(m_pRBVariableCell, "varbutton");
68 0 : m_pRBVariableCell->SetReferences(this, m_pEdVariableCell);
69 0 : get(m_pBtnOk, "ok");
70 0 : get(m_pBtnCancel, "cancel");
71 0 : Init();
72 0 : }
73 :
74 0 : ScSolverDlg::~ScSolverDlg()
75 : {
76 0 : disposeOnce();
77 0 : }
78 :
79 0 : void ScSolverDlg::dispose()
80 : {
81 0 : m_pFtFormulaCell.clear();
82 0 : m_pEdFormulaCell.clear();
83 0 : m_pRBFormulaCell.clear();
84 0 : m_pEdTargetVal.clear();
85 0 : m_pFtVariableCell.clear();
86 0 : m_pEdVariableCell.clear();
87 0 : m_pRBVariableCell.clear();
88 0 : m_pBtnOk.clear();
89 0 : m_pBtnCancel.clear();
90 0 : pEdActive.clear();
91 0 : ScAnyRefDlg::dispose();
92 0 : }
93 :
94 0 : void ScSolverDlg::Init()
95 : {
96 0 : m_pBtnOk->SetClickHdl( LINK( this, ScSolverDlg, BtnHdl ) );
97 0 : m_pBtnCancel->SetClickHdl( LINK( this, ScSolverDlg, BtnHdl ) );
98 :
99 0 : Link<> aLink = LINK( this, ScSolverDlg, GetFocusHdl );
100 0 : m_pEdFormulaCell->SetGetFocusHdl( aLink );
101 0 : m_pRBFormulaCell->SetGetFocusHdl( aLink );
102 0 : m_pEdVariableCell->SetGetFocusHdl( aLink );
103 0 : m_pRBVariableCell->SetGetFocusHdl( aLink );
104 0 : m_pEdTargetVal->SetGetFocusHdl( aLink );
105 :
106 0 : aLink = LINK( this, ScSolverDlg, LoseFocusHdl );
107 0 : m_pEdFormulaCell->SetLoseFocusHdl ( aLink );
108 0 : m_pRBFormulaCell->SetLoseFocusHdl ( aLink );
109 0 : m_pEdVariableCell->SetLoseFocusHdl ( aLink );
110 0 : m_pRBVariableCell->SetLoseFocusHdl ( aLink );
111 :
112 0 : OUString aStr(theFormulaCell.Format(SCA_ABS, NULL, pDoc->GetAddressConvention()));
113 :
114 0 : m_pEdFormulaCell->SetText( aStr );
115 0 : m_pEdFormulaCell->GrabFocus();
116 0 : pEdActive = m_pEdFormulaCell;
117 0 : }
118 :
119 0 : bool ScSolverDlg::Close()
120 : {
121 0 : return DoClose( ScSolverDlgWrapper::GetChildWindowId() );
122 : }
123 :
124 0 : void ScSolverDlg::SetActive()
125 : {
126 0 : if ( bDlgLostFocus )
127 : {
128 0 : bDlgLostFocus = false;
129 0 : if( pEdActive )
130 0 : pEdActive->GrabFocus();
131 : }
132 : else
133 : {
134 0 : GrabFocus();
135 : }
136 0 : RefInputDone();
137 0 : }
138 :
139 0 : void ScSolverDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
140 : {
141 0 : if( pEdActive )
142 : {
143 0 : if ( rRef.aStart != rRef.aEnd )
144 0 : RefInputStart(pEdActive);
145 :
146 0 : ScAddress aAdr = rRef.aStart;
147 0 : sal_uInt16 nFmt = ( aAdr.Tab() == nCurTab )
148 : ? SCA_ABS
149 0 : : SCA_ABS_3D;
150 :
151 0 : OUString aStr(aAdr.Format(nFmt, pDocP, pDocP->GetAddressConvention()));
152 0 : pEdActive->SetRefString( aStr );
153 :
154 0 : if ( pEdActive == m_pEdFormulaCell )
155 0 : theFormulaCell = aAdr;
156 0 : else if ( pEdActive == m_pEdVariableCell )
157 0 : theVariableCell = aAdr;
158 : }
159 0 : }
160 :
161 0 : void ScSolverDlg::RaiseError( ScSolverErr eError )
162 : {
163 0 : switch ( eError )
164 : {
165 : case SOLVERR_NOFORMULA:
166 0 : lclErrorDialog( this, errMsgNoFormula );
167 0 : m_pEdFormulaCell->GrabFocus();
168 0 : break;
169 :
170 : case SOLVERR_INVALID_FORMULA:
171 0 : lclErrorDialog( this, errMsgInvalidForm );
172 0 : m_pEdFormulaCell->GrabFocus();
173 0 : break;
174 :
175 : case SOLVERR_INVALID_VARIABLE:
176 0 : lclErrorDialog( this, errMsgInvalidVar );
177 0 : m_pEdVariableCell->GrabFocus();
178 0 : break;
179 :
180 : case SOLVERR_INVALID_TARGETVALUE:
181 0 : lclErrorDialog( this, errMsgInvalidVal );
182 0 : m_pEdTargetVal->GrabFocus();
183 0 : break;
184 : }
185 0 : }
186 :
187 0 : bool ScSolverDlg::IsRefInputMode() const
188 : {
189 0 : return pEdActive != nullptr;
190 : }
191 :
192 0 : bool ScSolverDlg::CheckTargetValue( const OUString& rStrVal )
193 : {
194 0 : sal_uInt32 n1 = 0;
195 : double n2;
196 :
197 0 : return pDoc->GetFormatTable()->IsNumberFormat( rStrVal, n1, n2 );
198 : }
199 :
200 : // Handler:
201 :
202 0 : IMPL_LINK( ScSolverDlg, BtnHdl, PushButton*, pBtn )
203 : {
204 0 : if (pBtn == m_pBtnOk)
205 : {
206 0 : theTargetValStr = m_pEdTargetVal->GetText();
207 :
208 : // The following code checks:
209 : // 1. do the strings contain correct references / defined names?
210 : // 2. does the formula coordinate refer to a cell containing a formula?
211 : // 3. has a valid target value been entered?
212 :
213 0 : const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
214 0 : sal_uInt16 nRes1 = theFormulaCell .Parse( m_pEdFormulaCell->GetText(), pDoc, eConv );
215 0 : sal_uInt16 nRes2 = theVariableCell.Parse( m_pEdVariableCell->GetText(), pDoc, eConv );
216 :
217 0 : if ( SCA_VALID == ( nRes1 & SCA_VALID ) )
218 : {
219 0 : if ( SCA_VALID == ( nRes2 & SCA_VALID ) )
220 : {
221 0 : if ( CheckTargetValue( theTargetValStr ) )
222 : {
223 : CellType eType;
224 0 : pDoc->GetCellType( theFormulaCell.Col(),
225 : theFormulaCell.Row(),
226 0 : theFormulaCell.Tab(),
227 0 : eType );
228 :
229 0 : if ( CELLTYPE_FORMULA == eType )
230 : {
231 : ScSolveParam aOutParam( theFormulaCell,
232 : theVariableCell,
233 0 : theTargetValStr );
234 0 : ScSolveItem aOutItem( SCITEM_SOLVEDATA, &aOutParam );
235 :
236 0 : SetDispatcherLock( false );
237 :
238 0 : SwitchToDocument();
239 0 : GetBindings().GetDispatcher()->Execute( SID_SOLVE,
240 : SfxCallMode::SLOT | SfxCallMode::RECORD,
241 0 : &aOutItem, 0L, 0L );
242 0 : Close();
243 : }
244 0 : else RaiseError( SOLVERR_NOFORMULA );
245 : }
246 0 : else RaiseError( SOLVERR_INVALID_TARGETVALUE );
247 : }
248 0 : else RaiseError( SOLVERR_INVALID_VARIABLE );
249 : }
250 0 : else RaiseError( SOLVERR_INVALID_FORMULA );
251 : }
252 0 : else if (pBtn == m_pBtnCancel)
253 : {
254 0 : Close();
255 : }
256 :
257 0 : return 0;
258 : }
259 :
260 0 : IMPL_LINK( ScSolverDlg, GetFocusHdl, Control*, pCtrl )
261 : {
262 0 : Edit* pEdit = NULL;
263 0 : pEdActive = NULL;
264 :
265 0 : if( (pCtrl == static_cast<Control*>(m_pEdFormulaCell)) || (pCtrl == static_cast<Control*>(m_pRBFormulaCell)) )
266 0 : pEdit = pEdActive = m_pEdFormulaCell;
267 0 : else if( (pCtrl == static_cast<Control*>(m_pEdVariableCell)) || (pCtrl == static_cast<Control*>(m_pRBVariableCell)) )
268 0 : pEdit = pEdActive = m_pEdVariableCell;
269 0 : else if( pCtrl == static_cast<Control*>(m_pEdTargetVal) )
270 0 : pEdit = m_pEdTargetVal;
271 :
272 0 : if( pEdit )
273 0 : pEdit->SetSelection( Selection( 0, SELECTION_MAX ) );
274 :
275 0 : return 0;
276 : }
277 :
278 0 : IMPL_LINK_NOARG(ScSolverDlg, LoseFocusHdl)
279 : {
280 0 : bDlgLostFocus = !IsActive();
281 0 : return 0;
282 156 : }
283 :
284 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|