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 :
33 : #define _SOLVRDLG_CXX
34 : #include "solvrdlg.hxx"
35 : #undef _SOLVERDLG_CXX
36 :
37 :
38 : #define ERRORBOX(s) ErrorBox( this, WinBits( WB_OK | WB_DEF_OK), s ).Execute()
39 :
40 :
41 : //============================================================================
42 : // class ScSolverDlg
43 : //----------------------------------------------------------------------------
44 :
45 0 : ScSolverDlg::ScSolverDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent,
46 : ScDocument* pDocument,
47 : ScAddress aCursorPos )
48 :
49 : : ScAnyRefDlg(pB, pCW, pParent, "GoalSeekDialog", "modules/scalc/ui/goalseekdlg.ui")
50 : , theFormulaCell(aCursorPos)
51 : , theVariableCell(aCursorPos)
52 : , pDoc(pDocument)
53 0 : , nCurTab(aCursorPos.Tab())
54 : , pEdActive(NULL)
55 : , bDlgLostFocus(false)
56 0 : , errMsgInvalidVar(ScGlobal::GetRscString(STR_INVALIDVAR))
57 0 : , errMsgInvalidForm(ScGlobal::GetRscString(STR_INVALIDFORM))
58 0 : , errMsgNoFormula(ScGlobal::GetRscString(STR_NOFORMULA))
59 0 : , errMsgInvalidVal(ScGlobal::GetRscString(STR_INVALIDVAL))
60 : {
61 0 : get(m_pFtFormulaCell, "formulatext");
62 0 : get(m_pEdFormulaCell, "formulaedit");
63 0 : m_pEdFormulaCell->SetReferences(this, m_pFtFormulaCell);
64 0 : get(m_pRBFormulaCell, "formulabutton");
65 0 : m_pRBFormulaCell->SetReferences(this, m_pEdFormulaCell),
66 0 : get(m_pEdTargetVal, "target");
67 0 : get(m_pFtVariableCell, "vartext");
68 0 : get(m_pEdVariableCell, "varedit");
69 0 : m_pEdVariableCell->SetReferences(this, m_pFtVariableCell);
70 0 : get(m_pRBVariableCell, "varbutton");
71 0 : m_pRBVariableCell->SetReferences(this, m_pEdVariableCell);
72 0 : get(m_pBtnOk, "ok");
73 0 : get(m_pBtnCancel, "cancel");
74 0 : Init();
75 0 : }
76 :
77 : //----------------------------------------------------------------------------
78 :
79 0 : ScSolverDlg::~ScSolverDlg()
80 : {
81 0 : }
82 :
83 : //----------------------------------------------------------------------------
84 :
85 0 : void ScSolverDlg::Init()
86 : {
87 0 : String aStr;
88 :
89 0 : m_pBtnOk->SetClickHdl( LINK( this, ScSolverDlg, BtnHdl ) );
90 0 : m_pBtnCancel->SetClickHdl( LINK( this, ScSolverDlg, BtnHdl ) );
91 :
92 0 : Link aLink = LINK( this, ScSolverDlg, GetFocusHdl );
93 0 : m_pEdFormulaCell->SetGetFocusHdl( aLink );
94 0 : m_pRBFormulaCell->SetGetFocusHdl( aLink );
95 0 : m_pEdVariableCell->SetGetFocusHdl( aLink );
96 0 : m_pRBVariableCell->SetGetFocusHdl( aLink );
97 0 : m_pEdTargetVal->SetGetFocusHdl( aLink );
98 :
99 0 : aLink = LINK( this, ScSolverDlg, LoseFocusHdl );
100 0 : m_pEdFormulaCell->SetLoseFocusHdl ( aLink );
101 0 : m_pRBFormulaCell->SetLoseFocusHdl ( aLink );
102 0 : m_pEdVariableCell->SetLoseFocusHdl ( aLink );
103 0 : m_pRBVariableCell->SetLoseFocusHdl ( aLink );
104 :
105 0 : theFormulaCell.Format( aStr, SCA_ABS, NULL, pDoc->GetAddressConvention() );
106 :
107 0 : m_pEdFormulaCell->SetText( aStr );
108 0 : m_pEdFormulaCell->GrabFocus();
109 0 : pEdActive = m_pEdFormulaCell;
110 0 : }
111 :
112 : //----------------------------------------------------------------------------
113 :
114 0 : sal_Bool ScSolverDlg::Close()
115 : {
116 0 : return DoClose( ScSolverDlgWrapper::GetChildWindowId() );
117 : }
118 :
119 : //----------------------------------------------------------------------------
120 :
121 0 : void ScSolverDlg::SetActive()
122 : {
123 0 : if ( bDlgLostFocus )
124 : {
125 0 : bDlgLostFocus = false;
126 0 : if( pEdActive )
127 0 : pEdActive->GrabFocus();
128 : }
129 : else
130 : {
131 0 : GrabFocus();
132 : }
133 0 : RefInputDone();
134 0 : }
135 :
136 : //----------------------------------------------------------------------------
137 :
138 0 : void ScSolverDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
139 : {
140 0 : if( pEdActive )
141 : {
142 0 : if ( rRef.aStart != rRef.aEnd )
143 0 : RefInputStart(pEdActive);
144 :
145 0 : String aStr;
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 : aAdr.Format( aStr, 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 : //----------------------------------------------------------------------------
162 :
163 0 : void ScSolverDlg::RaiseError( ScSolverErr eError )
164 : {
165 0 : switch ( eError )
166 : {
167 : case SOLVERR_NOFORMULA:
168 0 : ERRORBOX( errMsgNoFormula );
169 0 : m_pEdFormulaCell->GrabFocus();
170 0 : break;
171 :
172 : case SOLVERR_INVALID_FORMULA:
173 0 : ERRORBOX( errMsgInvalidForm );
174 0 : m_pEdFormulaCell->GrabFocus();
175 0 : break;
176 :
177 : case SOLVERR_INVALID_VARIABLE:
178 0 : ERRORBOX( errMsgInvalidVar );
179 0 : m_pEdVariableCell->GrabFocus();
180 0 : break;
181 :
182 : case SOLVERR_INVALID_TARGETVALUE:
183 0 : ERRORBOX( errMsgInvalidVal );
184 0 : m_pEdTargetVal->GrabFocus();
185 0 : break;
186 : }
187 0 : }
188 :
189 : //----------------------------------------------------------------------------
190 :
191 0 : sal_Bool ScSolverDlg::IsRefInputMode() const
192 : {
193 0 : return pEdActive != NULL;
194 : }
195 :
196 : //----------------------------------------------------------------------------
197 :
198 0 : sal_Bool ScSolverDlg::CheckTargetValue( String& rStrVal )
199 : {
200 0 : sal_uInt32 n1 = 0;
201 : double n2;
202 :
203 0 : return pDoc->GetFormatTable()->IsNumberFormat( rStrVal, n1, n2 );
204 : }
205 :
206 : //----------------------------------------------------------------------------
207 : // Handler:
208 :
209 0 : IMPL_LINK( ScSolverDlg, BtnHdl, PushButton*, pBtn )
210 : {
211 0 : if (pBtn == m_pBtnOk)
212 : {
213 0 : theTargetValStr = m_pEdTargetVal->GetText();
214 :
215 : // Zu ueberpruefen:
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
219 :
220 0 : const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
221 0 : sal_uInt16 nRes1 = theFormulaCell .Parse( m_pEdFormulaCell->GetText(), pDoc, eConv );
222 0 : sal_uInt16 nRes2 = theVariableCell.Parse( m_pEdVariableCell->GetText(), pDoc, eConv );
223 :
224 0 : if ( SCA_VALID == ( nRes1 & SCA_VALID ) )
225 : {
226 0 : if ( SCA_VALID == ( nRes2 & SCA_VALID ) )
227 : {
228 0 : if ( CheckTargetValue( theTargetValStr ) )
229 : {
230 : CellType eType;
231 0 : pDoc->GetCellType( theFormulaCell.Col(),
232 : theFormulaCell.Row(),
233 0 : theFormulaCell.Tab(),
234 0 : eType );
235 :
236 0 : if ( CELLTYPE_FORMULA == eType )
237 : {
238 : ScSolveParam aOutParam( theFormulaCell,
239 : theVariableCell,
240 0 : theTargetValStr );
241 0 : ScSolveItem aOutItem( SCITEM_SOLVEDATA, &aOutParam );
242 :
243 0 : SetDispatcherLock( false );
244 :
245 0 : SwitchToDocument();
246 0 : GetBindings().GetDispatcher()->Execute( SID_SOLVE,
247 : SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD,
248 0 : &aOutItem, 0L, 0L );
249 0 : Close();
250 : }
251 0 : else RaiseError( SOLVERR_NOFORMULA );
252 : }
253 0 : else RaiseError( SOLVERR_INVALID_TARGETVALUE );
254 : }
255 0 : else RaiseError( SOLVERR_INVALID_VARIABLE );
256 : }
257 0 : else RaiseError( SOLVERR_INVALID_FORMULA );
258 : }
259 0 : else if (pBtn == m_pBtnCancel)
260 : {
261 0 : Close();
262 : }
263 :
264 0 : return 0;
265 : }
266 :
267 : //----------------------------------------------------------------------------
268 :
269 0 : IMPL_LINK( ScSolverDlg, GetFocusHdl, Control*, pCtrl )
270 : {
271 0 : Edit* pEdit = NULL;
272 0 : pEdActive = NULL;
273 :
274 0 : if( (pCtrl == (Control*)m_pEdFormulaCell) || (pCtrl == (Control*)m_pRBFormulaCell) )
275 0 : pEdit = pEdActive = m_pEdFormulaCell;
276 0 : else if( (pCtrl == (Control*)m_pEdVariableCell) || (pCtrl == (Control*)m_pRBVariableCell) )
277 0 : pEdit = pEdActive = m_pEdVariableCell;
278 0 : else if( pCtrl == (Control*)m_pEdTargetVal )
279 0 : pEdit = m_pEdTargetVal;
280 :
281 0 : if( pEdit )
282 0 : pEdit->SetSelection( Selection( 0, SELECTION_MAX ) );
283 :
284 0 : return 0;
285 : }
286 :
287 : //----------------------------------------------------------------------------
288 :
289 0 : IMPL_LINK_NOARG(ScSolverDlg, LoseFocusHdl)
290 : {
291 0 : bDlgLostFocus = !IsActive();
292 0 : return 0;
293 93 : }
294 :
295 :
296 :
297 :
298 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|