xref: /aoo41x/main/sc/source/ui/miscdlgs/solvrdlg.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir //----------------------------------------------------------------------------
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "rangelst.hxx"
32cdf0e10cSrcweir #include "scitems.hxx"
33cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
34cdf0e10cSrcweir #include <svl/zforlist.hxx>
35cdf0e10cSrcweir #include <vcl/msgbox.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "uiitems.hxx"
38cdf0e10cSrcweir #include "reffact.hxx"
39cdf0e10cSrcweir #include "document.hxx"
40cdf0e10cSrcweir #include "scresid.hxx"
41cdf0e10cSrcweir #include "solvrdlg.hrc"
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #define _SOLVRDLG_CXX
44cdf0e10cSrcweir #include "solvrdlg.hxx"
45cdf0e10cSrcweir #undef _SOLVERDLG_CXX
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #define ERRORBOX(s)	ErrorBox( this, WinBits( WB_OK | WB_DEF_OK), s ).Execute()
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir //============================================================================
52cdf0e10cSrcweir //	class ScSolverDlg
53cdf0e10cSrcweir //----------------------------------------------------------------------------
54cdf0e10cSrcweir 
ScSolverDlg(SfxBindings * pB,SfxChildWindow * pCW,Window * pParent,ScDocument * pDocument,ScAddress aCursorPos)55cdf0e10cSrcweir ScSolverDlg::ScSolverDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent,
56cdf0e10cSrcweir 						  ScDocument* pDocument,
57cdf0e10cSrcweir 						  ScAddress	aCursorPos )
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	:	ScAnyRefDlg			( pB, pCW, pParent, RID_SCDLG_SOLVER ),
60cdf0e10cSrcweir 		//
61cdf0e10cSrcweir         aFlVariables        ( this, ScResId( FL_VARIABLES ) ),
62cdf0e10cSrcweir 		aFtFormulaCell		( this, ScResId( FT_FORMULACELL ) ),
63cdf0e10cSrcweir         aEdFormulaCell      ( this, this, ScResId( ED_FORMULACELL ) ),
64cdf0e10cSrcweir 		aRBFormulaCell		( this, ScResId( RB_FORMULACELL ), &aEdFormulaCell, this ),
65cdf0e10cSrcweir 		aFtTargetVal		( this, ScResId( FT_TARGETVAL ) ),
66cdf0e10cSrcweir 		aEdTargetVal		( this, ScResId( ED_TARGETVAL ) ),
67cdf0e10cSrcweir 		aFtVariableCell		( this, ScResId( FT_VARCELL ) ),
68cdf0e10cSrcweir         aEdVariableCell     ( this, this, ScResId( ED_VARCELL ) ),
69cdf0e10cSrcweir 		aRBVariableCell		( this, ScResId( RB_VARCELL ), &aEdVariableCell, this ),
70cdf0e10cSrcweir 		aBtnOk				( this, ScResId( BTN_OK ) ),
71cdf0e10cSrcweir 		aBtnCancel			( this, ScResId( BTN_CANCEL ) ),
72cdf0e10cSrcweir 		aBtnHelp			( this, ScResId( BTN_HELP ) ),
73cdf0e10cSrcweir 		//
74cdf0e10cSrcweir 		theFormulaCell		( aCursorPos ),
75cdf0e10cSrcweir 		theVariableCell		( aCursorPos ),
76cdf0e10cSrcweir 		pDoc				( pDocument ),
77cdf0e10cSrcweir 		nCurTab				( aCursorPos.Tab() ),
78cdf0e10cSrcweir 		pEdActive			( NULL ),
79cdf0e10cSrcweir 		bDlgLostFocus		( sal_False ),
80cdf0e10cSrcweir 		errMsgInvalidVar	( ScResId( STR_INVALIDVAR ) ),
81cdf0e10cSrcweir 		errMsgInvalidForm	( ScResId( STR_INVALIDFORM ) ),
82cdf0e10cSrcweir 		errMsgNoFormula		( ScResId( STR_NOFORMULA ) ),
83cdf0e10cSrcweir 		errMsgInvalidVal	( ScResId( STR_INVALIDVAL ) )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	Init();
86cdf0e10cSrcweir 	FreeResource();
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	aRBFormulaCell.SetAccessibleRelationMemberOf(&aFlVariables);
89cdf0e10cSrcweir 	aRBVariableCell.SetAccessibleRelationMemberOf(&aFlVariables);
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir //----------------------------------------------------------------------------
93cdf0e10cSrcweir 
~ScSolverDlg()94cdf0e10cSrcweir __EXPORT ScSolverDlg::~ScSolverDlg()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir //----------------------------------------------------------------------------
99cdf0e10cSrcweir 
Init()100cdf0e10cSrcweir void __EXPORT ScSolverDlg::Init()
101cdf0e10cSrcweir {
102cdf0e10cSrcweir 	String			aStr;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	aBtnOk.			SetClickHdl		( LINK( this, ScSolverDlg, BtnHdl ) );
105cdf0e10cSrcweir 	aBtnCancel.		SetClickHdl		( LINK( this, ScSolverDlg, BtnHdl ) );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     Link aLink = LINK( this, ScSolverDlg, GetFocusHdl );
108cdf0e10cSrcweir     aEdFormulaCell. SetGetFocusHdl  ( aLink );
109cdf0e10cSrcweir     aRBFormulaCell. SetGetFocusHdl  ( aLink );
110cdf0e10cSrcweir     aEdVariableCell.SetGetFocusHdl  ( aLink );
111cdf0e10cSrcweir     aRBVariableCell.SetGetFocusHdl  ( aLink );
112cdf0e10cSrcweir     aEdTargetVal.   SetGetFocusHdl  ( aLink );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     aLink = LINK( this, ScSolverDlg, LoseFocusHdl );
115cdf0e10cSrcweir     aEdFormulaCell. SetLoseFocusHdl ( aLink );
116cdf0e10cSrcweir     aRBFormulaCell. SetLoseFocusHdl ( aLink );
117cdf0e10cSrcweir     aEdVariableCell.SetLoseFocusHdl ( aLink );
118cdf0e10cSrcweir     aRBVariableCell.SetLoseFocusHdl ( aLink );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 	theFormulaCell.Format( aStr, SCA_ABS, NULL, pDoc->GetAddressConvention() );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	aEdFormulaCell.SetText( aStr );
123cdf0e10cSrcweir 	aEdFormulaCell.GrabFocus();
124cdf0e10cSrcweir 	pEdActive = &aEdFormulaCell;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir //----------------------------------------------------------------------------
128cdf0e10cSrcweir 
Close()129cdf0e10cSrcweir sal_Bool __EXPORT ScSolverDlg::Close()
130cdf0e10cSrcweir {
131cdf0e10cSrcweir 	return DoClose( ScSolverDlgWrapper::GetChildWindowId() );
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //----------------------------------------------------------------------------
135cdf0e10cSrcweir 
SetActive()136cdf0e10cSrcweir void ScSolverDlg::SetActive()
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     if ( bDlgLostFocus )
139cdf0e10cSrcweir 	{
140cdf0e10cSrcweir 		bDlgLostFocus = sal_False;
141cdf0e10cSrcweir         if( pEdActive )
142cdf0e10cSrcweir             pEdActive->GrabFocus();
143cdf0e10cSrcweir 	}
144cdf0e10cSrcweir 	else
145cdf0e10cSrcweir 	{
146cdf0e10cSrcweir 		GrabFocus();
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir 	RefInputDone();
149cdf0e10cSrcweir }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir //----------------------------------------------------------------------------
152cdf0e10cSrcweir 
SetReference(const ScRange & rRef,ScDocument * pDocP)153cdf0e10cSrcweir void ScSolverDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
154cdf0e10cSrcweir {
155cdf0e10cSrcweir     if( pEdActive )
156cdf0e10cSrcweir 	{
157cdf0e10cSrcweir 		if ( rRef.aStart != rRef.aEnd )
158cdf0e10cSrcweir 			RefInputStart(pEdActive);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 		String		aStr;
161cdf0e10cSrcweir 		ScAddress	aAdr = rRef.aStart;
162cdf0e10cSrcweir 		sal_uInt16	 	nFmt = ( aAdr.Tab() == nCurTab )
163cdf0e10cSrcweir 								? SCA_ABS
164cdf0e10cSrcweir 								: SCA_ABS_3D;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         aAdr.Format( aStr, nFmt, pDocP, pDocP->GetAddressConvention() );
167cdf0e10cSrcweir 		pEdActive->SetRefString( aStr );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 		if ( pEdActive == &aEdFormulaCell )
170cdf0e10cSrcweir 			theFormulaCell = aAdr;
171cdf0e10cSrcweir 		else if ( pEdActive == &aEdVariableCell )
172cdf0e10cSrcweir 			theVariableCell = aAdr;
173cdf0e10cSrcweir 	}
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir //----------------------------------------------------------------------------
177cdf0e10cSrcweir 
RaiseError(ScSolverErr eError)178cdf0e10cSrcweir void ScSolverDlg::RaiseError( ScSolverErr eError )
179cdf0e10cSrcweir {
180cdf0e10cSrcweir 	switch ( eError )
181cdf0e10cSrcweir 	{
182cdf0e10cSrcweir 		case SOLVERR_NOFORMULA:
183cdf0e10cSrcweir 			ERRORBOX( errMsgNoFormula );
184cdf0e10cSrcweir 			aEdFormulaCell.GrabFocus();
185cdf0e10cSrcweir 			break;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 		case SOLVERR_INVALID_FORMULA:
188cdf0e10cSrcweir 			ERRORBOX( errMsgInvalidForm );
189cdf0e10cSrcweir 			aEdFormulaCell.GrabFocus();
190cdf0e10cSrcweir 			break;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 		case SOLVERR_INVALID_VARIABLE:
193cdf0e10cSrcweir 			ERRORBOX( errMsgInvalidVar );
194cdf0e10cSrcweir 			aEdVariableCell.GrabFocus();
195cdf0e10cSrcweir 			break;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 		case SOLVERR_INVALID_TARGETVALUE:
198cdf0e10cSrcweir 			ERRORBOX( errMsgInvalidVal );
199cdf0e10cSrcweir 			aEdTargetVal.GrabFocus();
200cdf0e10cSrcweir 			break;
201cdf0e10cSrcweir 	}
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir //----------------------------------------------------------------------------
205cdf0e10cSrcweir 
IsRefInputMode() const206cdf0e10cSrcweir sal_Bool ScSolverDlg::IsRefInputMode() const
207cdf0e10cSrcweir {
208cdf0e10cSrcweir     return pEdActive != NULL;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir //----------------------------------------------------------------------------
212cdf0e10cSrcweir 
CheckTargetValue(String & rStrVal)213cdf0e10cSrcweir sal_Bool __EXPORT ScSolverDlg::CheckTargetValue( String& rStrVal )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	sal_uInt32 n1 = 0;
216cdf0e10cSrcweir 	double n2;
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	return pDoc->GetFormatTable()->IsNumberFormat( rStrVal, n1, n2 );
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir //----------------------------------------------------------------------------
222cdf0e10cSrcweir // Handler:
223cdf0e10cSrcweir 
IMPL_LINK(ScSolverDlg,BtnHdl,PushButton *,pBtn)224cdf0e10cSrcweir IMPL_LINK( ScSolverDlg, BtnHdl, PushButton*, pBtn )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	if ( pBtn == &aBtnOk )
227cdf0e10cSrcweir 	{
228cdf0e10cSrcweir 		theTargetValStr = aEdTargetVal.GetText();
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 		// Zu ueberpruefen:
231cdf0e10cSrcweir 		// 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen?
232cdf0e10cSrcweir 		// 2. verweist die Formel-Koordinate wirklich auf eine Formelzelle?
233cdf0e10cSrcweir 		// 3. wurde ein korrekter Zielwert eingegeben
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
236cdf0e10cSrcweir 		sal_uInt16	nRes1 = theFormulaCell .Parse( aEdFormulaCell.GetText(),  pDoc, eConv );
237cdf0e10cSrcweir 		sal_uInt16	nRes2 = theVariableCell.Parse( aEdVariableCell.GetText(), pDoc, eConv );
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 		if ( SCA_VALID == ( nRes1 & SCA_VALID ) )
240cdf0e10cSrcweir 		{
241cdf0e10cSrcweir 			if ( SCA_VALID == ( nRes2 & SCA_VALID ) )
242cdf0e10cSrcweir 			{
243cdf0e10cSrcweir 				if ( CheckTargetValue( theTargetValStr ) )
244cdf0e10cSrcweir 				{
245cdf0e10cSrcweir 					CellType eType;
246cdf0e10cSrcweir 					pDoc->GetCellType( theFormulaCell.Col(),
247cdf0e10cSrcweir 									   theFormulaCell.Row(),
248cdf0e10cSrcweir 									   theFormulaCell.Tab(),
249cdf0e10cSrcweir 									   eType );
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 					if ( CELLTYPE_FORMULA  == eType )
252cdf0e10cSrcweir 					{
253cdf0e10cSrcweir 						ScSolveParam aOutParam( theFormulaCell,
254cdf0e10cSrcweir 												theVariableCell,
255cdf0e10cSrcweir 												theTargetValStr );
256cdf0e10cSrcweir 						ScSolveItem	 aOutItem( SCITEM_SOLVEDATA, &aOutParam );
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 						SetDispatcherLock( sal_False );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 						SwitchToDocument();
261cdf0e10cSrcweir 						GetBindings().GetDispatcher()->Execute( SID_SOLVE,
262cdf0e10cSrcweir 												  SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD,
263cdf0e10cSrcweir 												  &aOutItem, 0L, 0L );
264cdf0e10cSrcweir 						Close();
265cdf0e10cSrcweir 					}
266cdf0e10cSrcweir 					else RaiseError( SOLVERR_NOFORMULA );
267cdf0e10cSrcweir 				}
268cdf0e10cSrcweir 				else RaiseError( SOLVERR_INVALID_TARGETVALUE );
269cdf0e10cSrcweir 			}
270cdf0e10cSrcweir 			else RaiseError( SOLVERR_INVALID_VARIABLE );
271cdf0e10cSrcweir 		}
272cdf0e10cSrcweir 		else RaiseError( SOLVERR_INVALID_FORMULA );
273cdf0e10cSrcweir 	}
274cdf0e10cSrcweir 	else if ( pBtn == &aBtnCancel )
275cdf0e10cSrcweir 	{
276cdf0e10cSrcweir 		Close();
277cdf0e10cSrcweir 	}
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	return 0;
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir //----------------------------------------------------------------------------
283cdf0e10cSrcweir 
IMPL_LINK(ScSolverDlg,GetFocusHdl,Control *,pCtrl)284cdf0e10cSrcweir IMPL_LINK( ScSolverDlg, GetFocusHdl, Control*, pCtrl )
285cdf0e10cSrcweir {
286cdf0e10cSrcweir     Edit* pEdit = NULL;
287cdf0e10cSrcweir     pEdActive = NULL;
288cdf0e10cSrcweir 
289cdf0e10cSrcweir     if( (pCtrl == (Control*)&aEdFormulaCell) || (pCtrl == (Control*)&aRBFormulaCell) )
290cdf0e10cSrcweir         pEdit = pEdActive = &aEdFormulaCell;
291cdf0e10cSrcweir     else if( (pCtrl == (Control*)&aEdVariableCell) || (pCtrl == (Control*)&aRBVariableCell) )
292cdf0e10cSrcweir         pEdit = pEdActive = &aEdVariableCell;
293cdf0e10cSrcweir     else if( pCtrl == (Control*)&aEdTargetVal )
294cdf0e10cSrcweir         pEdit = &aEdTargetVal;
295cdf0e10cSrcweir 
296cdf0e10cSrcweir     if( pEdit )
297cdf0e10cSrcweir         pEdit->SetSelection( Selection( 0, SELECTION_MAX ) );
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 	return 0;
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir //----------------------------------------------------------------------------
303cdf0e10cSrcweir 
IMPL_LINK(ScSolverDlg,LoseFocusHdl,Control *,EMPTYARG)304cdf0e10cSrcweir IMPL_LINK( ScSolverDlg, LoseFocusHdl, Control*, EMPTYARG )
305cdf0e10cSrcweir {
306cdf0e10cSrcweir 	bDlgLostFocus = !IsActive();
307cdf0e10cSrcweir 	return 0;
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 
313