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 : MessageDialog(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 : }
77 :
78 0 : void ScSolverDlg::Init()
79 : {
80 0 : m_pBtnOk->SetClickHdl( LINK( this, ScSolverDlg, BtnHdl ) );
81 0 : m_pBtnCancel->SetClickHdl( LINK( this, ScSolverDlg, BtnHdl ) );
82 :
83 0 : Link aLink = LINK( this, ScSolverDlg, GetFocusHdl );
84 0 : m_pEdFormulaCell->SetGetFocusHdl( aLink );
85 0 : m_pRBFormulaCell->SetGetFocusHdl( aLink );
86 0 : m_pEdVariableCell->SetGetFocusHdl( aLink );
87 0 : m_pRBVariableCell->SetGetFocusHdl( aLink );
88 0 : m_pEdTargetVal->SetGetFocusHdl( aLink );
89 :
90 0 : aLink = LINK( this, ScSolverDlg, LoseFocusHdl );
91 0 : m_pEdFormulaCell->SetLoseFocusHdl ( aLink );
92 0 : m_pRBFormulaCell->SetLoseFocusHdl ( aLink );
93 0 : m_pEdVariableCell->SetLoseFocusHdl ( aLink );
94 0 : m_pRBVariableCell->SetLoseFocusHdl ( aLink );
95 :
96 0 : OUString aStr(theFormulaCell.Format(SCA_ABS, NULL, pDoc->GetAddressConvention()));
97 :
98 0 : m_pEdFormulaCell->SetText( aStr );
99 0 : m_pEdFormulaCell->GrabFocus();
100 0 : pEdActive = m_pEdFormulaCell;
101 0 : }
102 :
103 0 : bool ScSolverDlg::Close()
104 : {
105 0 : return DoClose( ScSolverDlgWrapper::GetChildWindowId() );
106 : }
107 :
108 0 : void ScSolverDlg::SetActive()
109 : {
110 0 : if ( bDlgLostFocus )
111 : {
112 0 : bDlgLostFocus = false;
113 0 : if( pEdActive )
114 0 : pEdActive->GrabFocus();
115 : }
116 : else
117 : {
118 0 : GrabFocus();
119 : }
120 0 : RefInputDone();
121 0 : }
122 :
123 0 : void ScSolverDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
124 : {
125 0 : if( pEdActive )
126 : {
127 0 : if ( rRef.aStart != rRef.aEnd )
128 0 : RefInputStart(pEdActive);
129 :
130 0 : ScAddress aAdr = rRef.aStart;
131 0 : sal_uInt16 nFmt = ( aAdr.Tab() == nCurTab )
132 : ? SCA_ABS
133 0 : : SCA_ABS_3D;
134 :
135 0 : OUString aStr(aAdr.Format(nFmt, pDocP, pDocP->GetAddressConvention()));
136 0 : pEdActive->SetRefString( aStr );
137 :
138 0 : if ( pEdActive == m_pEdFormulaCell )
139 0 : theFormulaCell = aAdr;
140 0 : else if ( pEdActive == m_pEdVariableCell )
141 0 : theVariableCell = aAdr;
142 : }
143 0 : }
144 :
145 0 : void ScSolverDlg::RaiseError( ScSolverErr eError )
146 : {
147 0 : switch ( eError )
148 : {
149 : case SOLVERR_NOFORMULA:
150 0 : lclErrorDialog( this, errMsgNoFormula );
151 0 : m_pEdFormulaCell->GrabFocus();
152 0 : break;
153 :
154 : case SOLVERR_INVALID_FORMULA:
155 0 : lclErrorDialog( this, errMsgInvalidForm );
156 0 : m_pEdFormulaCell->GrabFocus();
157 0 : break;
158 :
159 : case SOLVERR_INVALID_VARIABLE:
160 0 : lclErrorDialog( this, errMsgInvalidVar );
161 0 : m_pEdVariableCell->GrabFocus();
162 0 : break;
163 :
164 : case SOLVERR_INVALID_TARGETVALUE:
165 0 : lclErrorDialog( this, errMsgInvalidVal );
166 0 : m_pEdTargetVal->GrabFocus();
167 0 : break;
168 : }
169 0 : }
170 :
171 0 : bool ScSolverDlg::IsRefInputMode() const
172 : {
173 0 : return pEdActive != NULL;
174 : }
175 :
176 0 : bool ScSolverDlg::CheckTargetValue( const OUString& rStrVal )
177 : {
178 0 : sal_uInt32 n1 = 0;
179 : double n2;
180 :
181 0 : return pDoc->GetFormatTable()->IsNumberFormat( rStrVal, n1, n2 );
182 : }
183 :
184 : // Handler:
185 :
186 0 : IMPL_LINK( ScSolverDlg, BtnHdl, PushButton*, pBtn )
187 : {
188 0 : if (pBtn == m_pBtnOk)
189 : {
190 0 : theTargetValStr = m_pEdTargetVal->GetText();
191 :
192 : // The following code checks:
193 : // 1. do the strings contain correct references / defined names?
194 : // 2. does the formula coordinate refer to a cell containing a formula?
195 : // 3. has a valid target value been entered?
196 :
197 0 : const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
198 0 : sal_uInt16 nRes1 = theFormulaCell .Parse( m_pEdFormulaCell->GetText(), pDoc, eConv );
199 0 : sal_uInt16 nRes2 = theVariableCell.Parse( m_pEdVariableCell->GetText(), pDoc, eConv );
200 :
201 0 : if ( SCA_VALID == ( nRes1 & SCA_VALID ) )
202 : {
203 0 : if ( SCA_VALID == ( nRes2 & SCA_VALID ) )
204 : {
205 0 : if ( CheckTargetValue( theTargetValStr ) )
206 : {
207 : CellType eType;
208 0 : pDoc->GetCellType( theFormulaCell.Col(),
209 : theFormulaCell.Row(),
210 0 : theFormulaCell.Tab(),
211 0 : eType );
212 :
213 0 : if ( CELLTYPE_FORMULA == eType )
214 : {
215 : ScSolveParam aOutParam( theFormulaCell,
216 : theVariableCell,
217 0 : theTargetValStr );
218 0 : ScSolveItem aOutItem( SCITEM_SOLVEDATA, &aOutParam );
219 :
220 0 : SetDispatcherLock( false );
221 :
222 0 : SwitchToDocument();
223 0 : GetBindings().GetDispatcher()->Execute( SID_SOLVE,
224 : SfxCallMode::SLOT | SfxCallMode::RECORD,
225 0 : &aOutItem, 0L, 0L );
226 0 : Close();
227 : }
228 0 : else RaiseError( SOLVERR_NOFORMULA );
229 : }
230 0 : else RaiseError( SOLVERR_INVALID_TARGETVALUE );
231 : }
232 0 : else RaiseError( SOLVERR_INVALID_VARIABLE );
233 : }
234 0 : else RaiseError( SOLVERR_INVALID_FORMULA );
235 : }
236 0 : else if (pBtn == m_pBtnCancel)
237 : {
238 0 : Close();
239 : }
240 :
241 0 : return 0;
242 : }
243 :
244 0 : IMPL_LINK( ScSolverDlg, GetFocusHdl, Control*, pCtrl )
245 : {
246 0 : Edit* pEdit = NULL;
247 0 : pEdActive = NULL;
248 :
249 0 : if( (pCtrl == (Control*)m_pEdFormulaCell) || (pCtrl == (Control*)m_pRBFormulaCell) )
250 0 : pEdit = pEdActive = m_pEdFormulaCell;
251 0 : else if( (pCtrl == (Control*)m_pEdVariableCell) || (pCtrl == (Control*)m_pRBVariableCell) )
252 0 : pEdit = pEdActive = m_pEdVariableCell;
253 0 : else if( pCtrl == (Control*)m_pEdTargetVal )
254 0 : pEdit = m_pEdTargetVal;
255 :
256 0 : if( pEdit )
257 0 : pEdit->SetSelection( Selection( 0, SELECTION_MAX ) );
258 :
259 0 : return 0;
260 : }
261 :
262 0 : IMPL_LINK_NOARG(ScSolverDlg, LoseFocusHdl)
263 : {
264 0 : bDlgLostFocus = !IsActive();
265 0 : return 0;
266 228 : }
267 :
268 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|