sync master with lastest vba changes
[ooovba.git] / sc / source / ui / miscdlgs / solvrdlg.cxx
blob5f2e2cc2632f99649a584422eca00517daff4c33
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: solvrdlg.cxx,v $
10 * $Revision: 1.13 $
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"
50 #define _SOLVRDLG_CXX
51 #include "solvrdlg.hxx"
52 #undef _SOLVERDLG_CXX
55 #define ERRORBOX(s) ErrorBox( this, WinBits( WB_OK | WB_DEF_OK), s ).Execute()
58 //============================================================================
59 // class ScSolverDlg
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 ),
83 pDoc ( pDocument ),
84 nCurTab ( aCursorPos.Tab() ),
85 pEdActive ( NULL ),
86 bDlgLostFocus ( FALSE ),
87 errMsgInvalidVar ( ScResId( STR_INVALIDVAR ) ),
88 errMsgInvalidForm ( ScResId( STR_INVALIDFORM ) ),
89 errMsgNoFormula ( ScResId( STR_NOFORMULA ) ),
90 errMsgInvalidVal ( ScResId( STR_INVALIDVAL ) )
92 Init();
93 FreeResource();
96 //----------------------------------------------------------------------------
98 __EXPORT ScSolverDlg::~ScSolverDlg()
102 //----------------------------------------------------------------------------
104 void __EXPORT ScSolverDlg::Init()
106 String aStr;
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()
142 if ( bDlgLostFocus )
144 bDlgLostFocus = FALSE;
145 if( pEdActive )
146 pEdActive->GrabFocus();
148 else
150 GrabFocus();
152 RefInputDone();
155 //----------------------------------------------------------------------------
157 void ScSolverDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
159 if( pEdActive )
161 if ( rRef.aStart != rRef.aEnd )
162 RefInputStart(pEdActive);
164 String aStr;
165 ScAddress aAdr = rRef.aStart;
166 USHORT nFmt = ( aAdr.Tab() == nCurTab )
167 ? SCA_ABS
168 : SCA_ABS_3D;
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 )
184 switch ( eError )
186 case SOLVERR_NOFORMULA:
187 ERRORBOX( errMsgNoFormula );
188 aEdFormulaCell.GrabFocus();
189 break;
191 case SOLVERR_INVALID_FORMULA:
192 ERRORBOX( errMsgInvalidForm );
193 aEdFormulaCell.GrabFocus();
194 break;
196 case SOLVERR_INVALID_VARIABLE:
197 ERRORBOX( errMsgInvalidVar );
198 aEdVariableCell.GrabFocus();
199 break;
201 case SOLVERR_INVALID_TARGETVALUE:
202 ERRORBOX( errMsgInvalidVal );
203 aEdTargetVal.GrabFocus();
204 break;
208 //----------------------------------------------------------------------------
210 BOOL ScSolverDlg::IsRefInputMode() const
212 return pEdActive != NULL;
215 //----------------------------------------------------------------------------
217 BOOL __EXPORT ScSolverDlg::CheckTargetValue( String& rStrVal )
219 sal_uInt32 n1 = 0;
220 double n2;
222 return pDoc->GetFormatTable()->IsNumberFormat( rStrVal, n1, n2 );
225 //----------------------------------------------------------------------------
226 // Handler:
228 IMPL_LINK( ScSolverDlg, BtnHdl, PushButton*, pBtn )
230 if ( pBtn == &aBtnOk )
232 theTargetValStr = aEdTargetVal.GetText();
234 // Zu ueberpruefen:
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 ) )
249 CellType eType;
250 pDoc->GetCellType( theFormulaCell.Col(),
251 theFormulaCell.Row(),
252 theFormulaCell.Tab(),
253 eType );
255 if ( CELLTYPE_FORMULA == eType )
257 ScSolveParam aOutParam( theFormulaCell,
258 theVariableCell,
259 theTargetValStr );
260 ScSolveItem aOutItem( SCITEM_SOLVEDATA, &aOutParam );
262 SetDispatcherLock( FALSE );
264 SwitchToDocument();
265 GetBindings().GetDispatcher()->Execute( SID_SOLVE,
266 SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD,
267 &aOutItem, 0L, 0L );
268 Close();
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 )
280 Close();
283 return 0;
286 //----------------------------------------------------------------------------
288 IMPL_LINK( ScSolverDlg, GetFocusHdl, Control*, pCtrl )
290 Edit* pEdit = NULL;
291 pEdActive = NULL;
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;
300 if( pEdit )
301 pEdit->SetSelection( Selection( 0, SELECTION_MAX ) );
303 return 0;
306 //----------------------------------------------------------------------------
308 IMPL_LINK( ScSolverDlg, LoseFocusHdl, Control*, EMPTYARG )
310 bDlgLostFocus = !IsActive();
311 return 0;