1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: solvrdlg.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 //----------------------------------------------------------------------------
38 #include "rangelst.hxx"
39 #include "scitems.hxx"
40 #include <sfx2/dispatch.hxx>
41 #include <svtools/zforlist.hxx>
42 #include <vcl/msgbox.hxx>
44 #include "uiitems.hxx"
45 #include "reffact.hxx"
46 #include "document.hxx"
47 #include "scresid.hxx"
48 #include "solvrdlg.hrc"
51 #include "solvrdlg.hxx"
55 #define ERRORBOX(s) ErrorBox( this, WinBits( WB_OK | WB_DEF_OK), s ).Execute()
58 //============================================================================
60 //----------------------------------------------------------------------------
62 ScSolverDlg::ScSolverDlg( SfxBindings
* pB
, SfxChildWindow
* pCW
, Window
* pParent
,
63 ScDocument
* pDocument
,
64 ScAddress aCursorPos
)
66 : ScAnyRefDlg ( pB
, pCW
, pParent
, RID_SCDLG_SOLVER
),
68 aFlVariables ( this, ScResId( FL_VARIABLES
) ),
69 aFtFormulaCell ( this, ScResId( FT_FORMULACELL
) ),
70 aEdFormulaCell ( this, this, ScResId( ED_FORMULACELL
) ),
71 aRBFormulaCell ( this, ScResId( RB_FORMULACELL
), &aEdFormulaCell
, this ),
72 aFtTargetVal ( this, ScResId( FT_TARGETVAL
) ),
73 aEdTargetVal ( this, ScResId( ED_TARGETVAL
) ),
74 aFtVariableCell ( this, ScResId( FT_VARCELL
) ),
75 aEdVariableCell ( this, this, ScResId( ED_VARCELL
) ),
76 aRBVariableCell ( this, ScResId( RB_VARCELL
), &aEdVariableCell
, this ),
77 aBtnOk ( this, ScResId( BTN_OK
) ),
78 aBtnCancel ( this, ScResId( BTN_CANCEL
) ),
79 aBtnHelp ( this, ScResId( BTN_HELP
) ),
81 theFormulaCell ( aCursorPos
),
82 theVariableCell ( aCursorPos
),
84 nCurTab ( aCursorPos
.Tab() ),
86 bDlgLostFocus ( FALSE
),
87 errMsgInvalidVar ( ScResId( STR_INVALIDVAR
) ),
88 errMsgInvalidForm ( ScResId( STR_INVALIDFORM
) ),
89 errMsgNoFormula ( ScResId( STR_NOFORMULA
) ),
90 errMsgInvalidVal ( ScResId( STR_INVALIDVAL
) )
96 //----------------------------------------------------------------------------
98 __EXPORT
ScSolverDlg::~ScSolverDlg()
102 //----------------------------------------------------------------------------
104 void __EXPORT
ScSolverDlg::Init()
108 aBtnOk
. SetClickHdl ( LINK( this, ScSolverDlg
, BtnHdl
) );
109 aBtnCancel
. SetClickHdl ( LINK( this, ScSolverDlg
, BtnHdl
) );
111 Link aLink
= LINK( this, ScSolverDlg
, GetFocusHdl
);
112 aEdFormulaCell
. SetGetFocusHdl ( aLink
);
113 aRBFormulaCell
. SetGetFocusHdl ( aLink
);
114 aEdVariableCell
.SetGetFocusHdl ( aLink
);
115 aRBVariableCell
.SetGetFocusHdl ( aLink
);
116 aEdTargetVal
. SetGetFocusHdl ( aLink
);
118 aLink
= LINK( this, ScSolverDlg
, LoseFocusHdl
);
119 aEdFormulaCell
. SetLoseFocusHdl ( aLink
);
120 aRBFormulaCell
. SetLoseFocusHdl ( aLink
);
121 aEdVariableCell
.SetLoseFocusHdl ( aLink
);
122 aRBVariableCell
.SetLoseFocusHdl ( aLink
);
124 theFormulaCell
.Format( aStr
, SCA_ABS
, NULL
, pDoc
->GetAddressConvention() );
126 aEdFormulaCell
.SetText( aStr
);
127 aEdFormulaCell
.GrabFocus();
128 pEdActive
= &aEdFormulaCell
;
131 //----------------------------------------------------------------------------
133 BOOL __EXPORT
ScSolverDlg::Close()
135 return DoClose( ScSolverDlgWrapper::GetChildWindowId() );
138 //----------------------------------------------------------------------------
140 void ScSolverDlg::SetActive()
144 bDlgLostFocus
= FALSE
;
146 pEdActive
->GrabFocus();
155 //----------------------------------------------------------------------------
157 void ScSolverDlg::SetReference( const ScRange
& rRef
, ScDocument
* pDocP
)
161 if ( rRef
.aStart
!= rRef
.aEnd
)
162 RefInputStart(pEdActive
);
165 ScAddress aAdr
= rRef
.aStart
;
166 USHORT nFmt
= ( aAdr
.Tab() == nCurTab
)
170 aAdr
.Format( aStr
, nFmt
, pDocP
, pDocP
->GetAddressConvention() );
171 pEdActive
->SetRefString( aStr
);
173 if ( pEdActive
== &aEdFormulaCell
)
174 theFormulaCell
= aAdr
;
175 else if ( pEdActive
== &aEdVariableCell
)
176 theVariableCell
= aAdr
;
180 //----------------------------------------------------------------------------
182 void ScSolverDlg::RaiseError( ScSolverErr eError
)
186 case SOLVERR_NOFORMULA
:
187 ERRORBOX( errMsgNoFormula
);
188 aEdFormulaCell
.GrabFocus();
191 case SOLVERR_INVALID_FORMULA
:
192 ERRORBOX( errMsgInvalidForm
);
193 aEdFormulaCell
.GrabFocus();
196 case SOLVERR_INVALID_VARIABLE
:
197 ERRORBOX( errMsgInvalidVar
);
198 aEdVariableCell
.GrabFocus();
201 case SOLVERR_INVALID_TARGETVALUE
:
202 ERRORBOX( errMsgInvalidVal
);
203 aEdTargetVal
.GrabFocus();
208 //----------------------------------------------------------------------------
210 BOOL
ScSolverDlg::IsRefInputMode() const
212 return pEdActive
!= NULL
;
215 //----------------------------------------------------------------------------
217 BOOL __EXPORT
ScSolverDlg::CheckTargetValue( String
& rStrVal
)
222 return pDoc
->GetFormatTable()->IsNumberFormat( rStrVal
, n1
, n2
);
225 //----------------------------------------------------------------------------
228 IMPL_LINK( ScSolverDlg
, BtnHdl
, PushButton
*, pBtn
)
230 if ( pBtn
== &aBtnOk
)
232 theTargetValStr
= aEdTargetVal
.GetText();
235 // 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen?
236 // 2. verweist die Formel-Koordinate wirklich auf eine Formelzelle?
237 // 3. wurde ein korrekter Zielwert eingegeben
239 const formula::FormulaGrammar::AddressConvention eConv
= pDoc
->GetAddressConvention();
240 USHORT nRes1
= theFormulaCell
.Parse( aEdFormulaCell
.GetText(), pDoc
, eConv
);
241 USHORT nRes2
= theVariableCell
.Parse( aEdVariableCell
.GetText(), pDoc
, eConv
);
243 if ( SCA_VALID
== ( nRes1
& SCA_VALID
) )
245 if ( SCA_VALID
== ( nRes2
& SCA_VALID
) )
247 if ( CheckTargetValue( theTargetValStr
) )
250 pDoc
->GetCellType( theFormulaCell
.Col(),
251 theFormulaCell
.Row(),
252 theFormulaCell
.Tab(),
255 if ( CELLTYPE_FORMULA
== eType
)
257 ScSolveParam
aOutParam( theFormulaCell
,
260 ScSolveItem
aOutItem( SCITEM_SOLVEDATA
, &aOutParam
);
262 SetDispatcherLock( FALSE
);
265 GetBindings().GetDispatcher()->Execute( SID_SOLVE
,
266 SFX_CALLMODE_SLOT
| SFX_CALLMODE_RECORD
,
270 else RaiseError( SOLVERR_NOFORMULA
);
272 else RaiseError( SOLVERR_INVALID_TARGETVALUE
);
274 else RaiseError( SOLVERR_INVALID_VARIABLE
);
276 else RaiseError( SOLVERR_INVALID_FORMULA
);
278 else if ( pBtn
== &aBtnCancel
)
286 //----------------------------------------------------------------------------
288 IMPL_LINK( ScSolverDlg
, GetFocusHdl
, Control
*, pCtrl
)
293 if( (pCtrl
== (Control
*)&aEdFormulaCell
) || (pCtrl
== (Control
*)&aRBFormulaCell
) )
294 pEdit
= pEdActive
= &aEdFormulaCell
;
295 else if( (pCtrl
== (Control
*)&aEdVariableCell
) || (pCtrl
== (Control
*)&aRBVariableCell
) )
296 pEdit
= pEdActive
= &aEdVariableCell
;
297 else if( pCtrl
== (Control
*)&aEdTargetVal
)
298 pEdit
= &aEdTargetVal
;
301 pEdit
->SetSelection( Selection( 0, SELECTION_MAX
) );
306 //----------------------------------------------------------------------------
308 IMPL_LINK( ScSolverDlg
, LoseFocusHdl
, Control
*, EMPTYARG
)
310 bDlgLostFocus
= !IsActive();