sync master with lastest vba changes
[ooovba.git] / sc / source / ui / miscdlgs / tabopdlg.cxx
blobcde93914b024e6cf70fdae75ca49c82f246b4f84
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: tabopdlg.cxx,v $
10 * $Revision: 1.12 $
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 "scitems.hxx"
39 #include <sfx2/dispatch.hxx>
40 #include <vcl/msgbox.hxx>
42 #include "uiitems.hxx"
43 #include "global.hxx"
44 #include "document.hxx"
45 #include "scresid.hxx"
46 #include "sc.hrc"
47 #include "reffact.hxx"
48 #include "tabopdlg.hrc"
50 #define _TABOPDLG_CXX
51 #include "tabopdlg.hxx"
54 //============================================================================
55 // class ScTabOpDlg
56 //----------------------------------------------------------------------------
58 ScTabOpDlg::ScTabOpDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent,
59 ScDocument* pDocument,
60 const ScRefAddress& rCursorPos )
62 : ScAnyRefDlg ( pB, pCW, pParent, RID_SCDLG_TABOP ),
64 aFlVariables ( this, ScResId( FL_VARIABLES ) ),
65 aFtFormulaRange ( this, ScResId( FT_FORMULARANGE ) ),
66 aEdFormulaRange ( this, this, ScResId( ED_FORMULARANGE ) ),
67 aRBFormulaRange ( this, ScResId( RB_FORMULARANGE ), &aEdFormulaRange, this ),
68 aFtRowCell ( this, ScResId( FT_ROWCELL ) ),
69 aEdRowCell ( this, this, ScResId( ED_ROWCELL ) ),
70 aRBRowCell ( this, ScResId( RB_ROWCELL ), &aEdRowCell, this ),
71 aFtColCell ( this, ScResId( FT_COLCELL ) ),
72 aEdColCell ( this, this, ScResId( ED_COLCELL ) ),
73 aRBColCell ( this, ScResId( RB_COLCELL ), &aEdColCell, this ),
74 aBtnOk ( this, ScResId( BTN_OK ) ),
75 aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
76 aBtnHelp ( this, ScResId( BTN_HELP ) ),
78 theFormulaCell ( rCursorPos ),
79 pDoc ( pDocument ),
80 nCurTab ( theFormulaCell.Tab() ),
81 pEdActive ( NULL ),
82 bDlgLostFocus ( FALSE ),
83 errMsgNoFormula ( ScResId( STR_NOFORMULA ) ),
84 errMsgNoColRow ( ScResId( STR_NOCOLROW ) ),
85 errMsgWrongFormula ( ScResId( STR_WRONGFORMULA ) ),
86 errMsgWrongRowCol ( ScResId( STR_WRONGROWCOL ) ),
87 errMsgNoColFormula ( ScResId( STR_NOCOLFORMULA ) ),
88 errMsgNoRowFormula ( ScResId( STR_NOROWFORMULA ) )
90 Init();
91 FreeResource();
94 //----------------------------------------------------------------------------
96 __EXPORT ScTabOpDlg::~ScTabOpDlg()
98 Hide();
101 //----------------------------------------------------------------------------
103 void __EXPORT ScTabOpDlg::Init()
105 aBtnOk. SetClickHdl ( LINK( this, ScTabOpDlg, BtnHdl ) );
106 aBtnCancel. SetClickHdl ( LINK( this, ScTabOpDlg, BtnHdl ) );
108 Link aLink = LINK( this, ScTabOpDlg, GetFocusHdl );
109 aEdFormulaRange.SetGetFocusHdl( aLink );
110 aRBFormulaRange.SetGetFocusHdl( aLink );
111 aEdRowCell. SetGetFocusHdl( aLink );
112 aRBRowCell. SetGetFocusHdl( aLink );
113 aEdColCell. SetGetFocusHdl( aLink );
114 aRBColCell. SetGetFocusHdl( aLink );
116 aLink = LINK( this, ScTabOpDlg, LoseFocusHdl );
117 aEdFormulaRange.SetLoseFocusHdl( aLink );
118 aRBFormulaRange.SetLoseFocusHdl( aLink );
119 aEdRowCell. SetLoseFocusHdl( aLink );
120 aRBRowCell. SetLoseFocusHdl( aLink );
121 aEdColCell. SetLoseFocusHdl( aLink );
122 aRBColCell. SetLoseFocusHdl( aLink );
124 aEdFormulaRange.GrabFocus();
125 pEdActive = &aEdFormulaRange;
127 //@BugID 54702 Enablen/Disablen nur noch in Basisklasse
128 //SFX_APPWINDOW->Enable();
131 //----------------------------------------------------------------------------
133 BOOL __EXPORT ScTabOpDlg::Close()
135 return DoClose( ScTabOpDlgWrapper::GetChildWindowId() );
138 //----------------------------------------------------------------------------
140 void ScTabOpDlg::SetActive()
142 if ( bDlgLostFocus )
144 bDlgLostFocus = FALSE;
145 if( pEdActive )
146 pEdActive->GrabFocus();
148 else
149 GrabFocus();
151 RefInputDone();
154 //----------------------------------------------------------------------------
156 void ScTabOpDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
158 if ( pEdActive )
160 ScAddress::Details aDetails(pDocP->GetAddressConvention(), 0, 0);
162 if ( rRef.aStart != rRef.aEnd )
163 RefInputStart(pEdActive);
165 String aStr;
166 USHORT nFmt = ( rRef.aStart.Tab() == nCurTab )
167 ? SCR_ABS
168 : SCR_ABS_3D;
170 if ( pEdActive == &aEdFormulaRange )
172 theFormulaCell.Set( rRef.aStart, false, false, false);
173 theFormulaEnd.Set( rRef.aEnd, false, false, false);
174 rRef.Format( aStr, nFmt, pDocP, aDetails );
176 else if ( pEdActive == &aEdRowCell )
178 theRowCell.Set( rRef.aStart, false, false, false);
179 rRef.aStart.Format( aStr, nFmt, pDocP, aDetails );
181 else if ( pEdActive == &aEdColCell )
183 theColCell.Set( rRef.aStart, false, false, false);
184 rRef.aStart.Format( aStr, nFmt, pDocP, aDetails );
187 pEdActive->SetRefString( aStr );
191 //----------------------------------------------------------------------------
193 void ScTabOpDlg::RaiseError( ScTabOpErr eError )
195 const String* pMsg = &errMsgNoFormula;
196 Edit* pEd = &aEdFormulaRange;
198 switch ( eError )
200 case TABOPERR_NOFORMULA:
201 pMsg = &errMsgNoFormula;
202 pEd = &aEdFormulaRange;
203 break;
205 case TABOPERR_NOCOLROW:
206 pMsg = &errMsgNoColRow;
207 pEd = &aEdRowCell;
208 break;
210 case TABOPERR_WRONGFORMULA:
211 pMsg = &errMsgWrongFormula;
212 pEd = &aEdFormulaRange;
213 break;
215 case TABOPERR_WRONGROW:
216 pMsg = &errMsgWrongRowCol;
217 pEd = &aEdRowCell;
218 break;
220 case TABOPERR_NOCOLFORMULA:
221 pMsg = &errMsgNoColFormula;
222 pEd = &aEdFormulaRange;
223 break;
225 case TABOPERR_WRONGCOL:
226 pMsg = &errMsgWrongRowCol;
227 pEd = &aEdColCell;
228 break;
230 case TABOPERR_NOROWFORMULA:
231 pMsg = &errMsgNoRowFormula;
232 pEd = &aEdFormulaRange;
233 break;
236 ErrorBox( this, WinBits( WB_OK_CANCEL | WB_DEF_OK), *pMsg ).Execute();
237 pEd->GrabFocus();
240 //----------------------------------------------------------------------------
242 BOOL lcl_Parse( const String& rString, ScDocument* pDoc, SCTAB nCurTab,
243 ScRefAddress& rStart, ScRefAddress& rEnd )
245 BOOL bRet = FALSE;
246 const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
247 if ( rString.Search(':') != STRING_NOTFOUND )
248 bRet = ConvertDoubleRef( pDoc, rString, nCurTab, rStart, rEnd, eConv );
249 else
251 bRet = ConvertSingleRef( pDoc, rString, nCurTab, rStart, eConv );
252 rEnd = rStart;
254 return bRet;
257 //----------------------------------------------------------------------------
258 // Handler:
260 IMPL_LINK( ScTabOpDlg, BtnHdl, PushButton*, pBtn )
262 if ( pBtn == &aBtnOk )
264 BYTE nMode = 3;
265 USHORT nError = 0;
267 // Zu ueberpruefen:
268 // 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen?
269 // 2. IstFormelRang Zeile bei leerer Zeile bzw. Spalte bei leerer Spalte
270 // bzw. Einfachreferenz bei beidem?
271 // 3. Ist mindestens Zeile oder Spalte und Formel voll?
273 if (aEdFormulaRange.GetText().Len() == 0)
274 nError = TABOPERR_NOFORMULA;
275 else if (aEdRowCell.GetText().Len() == 0 &&
276 aEdColCell.GetText().Len() == 0)
277 nError = TABOPERR_NOCOLROW;
278 else if ( !lcl_Parse( aEdFormulaRange.GetText(), pDoc, nCurTab,
279 theFormulaCell, theFormulaEnd ) )
280 nError = TABOPERR_WRONGFORMULA;
281 else
283 const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
284 if (aEdRowCell.GetText().Len() > 0)
286 if (!ConvertSingleRef( pDoc, aEdRowCell.GetText(), nCurTab,
287 theRowCell, eConv ))
288 nError = TABOPERR_WRONGROW;
289 else
291 if (aEdColCell.GetText().Len() == 0 &&
292 theFormulaCell.Col() != theFormulaEnd.Col())
293 nError = TABOPERR_NOCOLFORMULA;
294 else
295 nMode = 1;
298 if (aEdColCell.GetText().Len() > 0)
300 if (!ConvertSingleRef( pDoc, aEdColCell.GetText(), nCurTab,
301 theColCell, eConv ))
302 nError = TABOPERR_WRONGCOL;
303 else
305 if (nMode == 1) // beides
307 nMode = 2;
308 ConvertSingleRef( pDoc, aEdFormulaRange.GetText(), nCurTab,
309 theFormulaCell, eConv );
311 else if (theFormulaCell.Row() != theFormulaEnd.Row())
312 nError = TABOPERR_NOROWFORMULA;
313 else
314 nMode = 0;
319 if (nError)
320 RaiseError( (ScTabOpErr) nError );
321 else
323 ScTabOpParam aOutParam( theFormulaCell,
324 theFormulaEnd,
325 theRowCell,
326 theColCell,
327 nMode );
328 ScTabOpItem aOutItem( SID_TABOP, &aOutParam );
330 SetDispatcherLock( FALSE );
331 SwitchToDocument();
332 GetBindings().GetDispatcher()->Execute( SID_TABOP,
333 SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD,
334 &aOutItem, 0L, 0L );
335 Close();
338 else if ( pBtn == &aBtnCancel )
339 Close();
341 return 0;
344 //----------------------------------------------------------------------------
346 IMPL_LINK( ScTabOpDlg, GetFocusHdl, Control*, pCtrl )
348 if( (pCtrl == (Control*)&aEdFormulaRange) || (pCtrl == (Control*)&aRBFormulaRange) )
349 pEdActive = &aEdFormulaRange;
350 else if( (pCtrl == (Control*)&aEdRowCell) || (pCtrl == (Control*)&aRBRowCell) )
351 pEdActive = &aEdRowCell;
352 else if( (pCtrl == (Control*)&aEdColCell) || (pCtrl == (Control*)&aRBColCell) )
353 pEdActive = &aEdColCell;
354 else
355 pEdActive = NULL;
357 if( pEdActive )
358 pEdActive->SetSelection( Selection( 0, SELECTION_MAX ) );
360 return 0;
363 //----------------------------------------------------------------------------
365 IMPL_LINK( ScTabOpDlg, LoseFocusHdl, Control*, EMPTYARG )
367 bDlgLostFocus = !IsActive();
368 return 0;