1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_sc.hxx" 26 27 28 29 //---------------------------------------------------------------------------- 30 31 #include "scitems.hxx" 32 #include <sfx2/dispatch.hxx> 33 #include <vcl/msgbox.hxx> 34 35 #include "uiitems.hxx" 36 #include "global.hxx" 37 #include "document.hxx" 38 #include "scresid.hxx" 39 #include "sc.hrc" 40 #include "reffact.hxx" 41 #include "tabopdlg.hrc" 42 43 #define _TABOPDLG_CXX 44 #include "tabopdlg.hxx" 45 46 47 //============================================================================ 48 // class ScTabOpDlg 49 //---------------------------------------------------------------------------- 50 51 ScTabOpDlg::ScTabOpDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent, 52 ScDocument* pDocument, 53 const ScRefAddress& rCursorPos ) 54 55 : ScAnyRefDlg ( pB, pCW, pParent, RID_SCDLG_TABOP ), 56 // 57 aFlVariables ( this, ScResId( FL_VARIABLES ) ), 58 aFtFormulaRange ( this, ScResId( FT_FORMULARANGE ) ), 59 aEdFormulaRange ( this, this, ScResId( ED_FORMULARANGE ) ), 60 aRBFormulaRange ( this, ScResId( RB_FORMULARANGE ), &aEdFormulaRange, this ), 61 aFtRowCell ( this, ScResId( FT_ROWCELL ) ), 62 aEdRowCell ( this, this, ScResId( ED_ROWCELL ) ), 63 aRBRowCell ( this, ScResId( RB_ROWCELL ), &aEdRowCell, this ), 64 aFtColCell ( this, ScResId( FT_COLCELL ) ), 65 aEdColCell ( this, this, ScResId( ED_COLCELL ) ), 66 aRBColCell ( this, ScResId( RB_COLCELL ), &aEdColCell, this ), 67 aBtnOk ( this, ScResId( BTN_OK ) ), 68 aBtnCancel ( this, ScResId( BTN_CANCEL ) ), 69 aBtnHelp ( this, ScResId( BTN_HELP ) ), 70 // 71 theFormulaCell ( rCursorPos ), 72 pDoc ( pDocument ), 73 nCurTab ( theFormulaCell.Tab() ), 74 pEdActive ( NULL ), 75 bDlgLostFocus ( sal_False ), 76 errMsgNoFormula ( ScResId( STR_NOFORMULA ) ), 77 errMsgNoColRow ( ScResId( STR_NOCOLROW ) ), 78 errMsgWrongFormula ( ScResId( STR_WRONGFORMULA ) ), 79 errMsgWrongRowCol ( ScResId( STR_WRONGROWCOL ) ), 80 errMsgNoColFormula ( ScResId( STR_NOCOLFORMULA ) ), 81 errMsgNoRowFormula ( ScResId( STR_NOROWFORMULA ) ) 82 { 83 Init(); 84 FreeResource(); 85 } 86 87 //---------------------------------------------------------------------------- 88 89 __EXPORT ScTabOpDlg::~ScTabOpDlg() 90 { 91 Hide(); 92 } 93 94 //---------------------------------------------------------------------------- 95 96 void __EXPORT ScTabOpDlg::Init() 97 { 98 aBtnOk. SetClickHdl ( LINK( this, ScTabOpDlg, BtnHdl ) ); 99 aBtnCancel. SetClickHdl ( LINK( this, ScTabOpDlg, BtnHdl ) ); 100 101 Link aLink = LINK( this, ScTabOpDlg, GetFocusHdl ); 102 aEdFormulaRange.SetGetFocusHdl( aLink ); 103 aRBFormulaRange.SetGetFocusHdl( aLink ); 104 aEdRowCell. SetGetFocusHdl( aLink ); 105 aRBRowCell. SetGetFocusHdl( aLink ); 106 aEdColCell. SetGetFocusHdl( aLink ); 107 aRBColCell. SetGetFocusHdl( aLink ); 108 109 aLink = LINK( this, ScTabOpDlg, LoseFocusHdl ); 110 aEdFormulaRange.SetLoseFocusHdl( aLink ); 111 aRBFormulaRange.SetLoseFocusHdl( aLink ); 112 aEdRowCell. SetLoseFocusHdl( aLink ); 113 aRBRowCell. SetLoseFocusHdl( aLink ); 114 aEdColCell. SetLoseFocusHdl( aLink ); 115 aRBColCell. SetLoseFocusHdl( aLink ); 116 117 aEdFormulaRange.GrabFocus(); 118 pEdActive = &aEdFormulaRange; 119 120 //@BugID 54702 Enablen/Disablen nur noch in Basisklasse 121 //SFX_APPWINDOW->Enable(); 122 } 123 124 //---------------------------------------------------------------------------- 125 126 sal_Bool __EXPORT ScTabOpDlg::Close() 127 { 128 return DoClose( ScTabOpDlgWrapper::GetChildWindowId() ); 129 } 130 131 //---------------------------------------------------------------------------- 132 133 void ScTabOpDlg::SetActive() 134 { 135 if ( bDlgLostFocus ) 136 { 137 bDlgLostFocus = sal_False; 138 if( pEdActive ) 139 pEdActive->GrabFocus(); 140 } 141 else 142 GrabFocus(); 143 144 RefInputDone(); 145 } 146 147 //---------------------------------------------------------------------------- 148 149 void ScTabOpDlg::SetReference( const ScRange& rRef, ScDocument* pDocP ) 150 { 151 if ( pEdActive ) 152 { 153 ScAddress::Details aDetails(pDocP->GetAddressConvention(), 0, 0); 154 155 if ( rRef.aStart != rRef.aEnd ) 156 RefInputStart(pEdActive); 157 158 String aStr; 159 sal_uInt16 nFmt = ( rRef.aStart.Tab() == nCurTab ) 160 ? SCR_ABS 161 : SCR_ABS_3D; 162 163 if ( pEdActive == &aEdFormulaRange ) 164 { 165 theFormulaCell.Set( rRef.aStart, false, false, false); 166 theFormulaEnd.Set( rRef.aEnd, false, false, false); 167 rRef.Format( aStr, nFmt, pDocP, aDetails ); 168 } 169 else if ( pEdActive == &aEdRowCell ) 170 { 171 theRowCell.Set( rRef.aStart, false, false, false); 172 rRef.aStart.Format( aStr, nFmt, pDocP, aDetails ); 173 } 174 else if ( pEdActive == &aEdColCell ) 175 { 176 theColCell.Set( rRef.aStart, false, false, false); 177 rRef.aStart.Format( aStr, nFmt, pDocP, aDetails ); 178 } 179 180 pEdActive->SetRefString( aStr ); 181 } 182 } 183 184 //---------------------------------------------------------------------------- 185 186 void ScTabOpDlg::RaiseError( ScTabOpErr eError ) 187 { 188 const String* pMsg = &errMsgNoFormula; 189 Edit* pEd = &aEdFormulaRange; 190 191 switch ( eError ) 192 { 193 case TABOPERR_NOFORMULA: 194 pMsg = &errMsgNoFormula; 195 pEd = &aEdFormulaRange; 196 break; 197 198 case TABOPERR_NOCOLROW: 199 pMsg = &errMsgNoColRow; 200 pEd = &aEdRowCell; 201 break; 202 203 case TABOPERR_WRONGFORMULA: 204 pMsg = &errMsgWrongFormula; 205 pEd = &aEdFormulaRange; 206 break; 207 208 case TABOPERR_WRONGROW: 209 pMsg = &errMsgWrongRowCol; 210 pEd = &aEdRowCell; 211 break; 212 213 case TABOPERR_NOCOLFORMULA: 214 pMsg = &errMsgNoColFormula; 215 pEd = &aEdFormulaRange; 216 break; 217 218 case TABOPERR_WRONGCOL: 219 pMsg = &errMsgWrongRowCol; 220 pEd = &aEdColCell; 221 break; 222 223 case TABOPERR_NOROWFORMULA: 224 pMsg = &errMsgNoRowFormula; 225 pEd = &aEdFormulaRange; 226 break; 227 } 228 229 ErrorBox( this, WinBits( WB_OK_CANCEL | WB_DEF_OK), *pMsg ).Execute(); 230 pEd->GrabFocus(); 231 } 232 233 //---------------------------------------------------------------------------- 234 235 sal_Bool lcl_Parse( const String& rString, ScDocument* pDoc, SCTAB nCurTab, 236 ScRefAddress& rStart, ScRefAddress& rEnd ) 237 { 238 sal_Bool bRet = sal_False; 239 const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention(); 240 if ( rString.Search(':') != STRING_NOTFOUND ) 241 bRet = ConvertDoubleRef( pDoc, rString, nCurTab, rStart, rEnd, eConv ); 242 else 243 { 244 bRet = ConvertSingleRef( pDoc, rString, nCurTab, rStart, eConv ); 245 rEnd = rStart; 246 } 247 return bRet; 248 } 249 250 //---------------------------------------------------------------------------- 251 // Handler: 252 253 IMPL_LINK( ScTabOpDlg, BtnHdl, PushButton*, pBtn ) 254 { 255 if ( pBtn == &aBtnOk ) 256 { 257 sal_uInt8 nMode = 3; 258 sal_uInt16 nError = 0; 259 260 // Zu ueberpruefen: 261 // 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen? 262 // 2. IstFormelRang Zeile bei leerer Zeile bzw. Spalte bei leerer Spalte 263 // bzw. Einfachreferenz bei beidem? 264 // 3. Ist mindestens Zeile oder Spalte und Formel voll? 265 266 if (aEdFormulaRange.GetText().Len() == 0) 267 nError = TABOPERR_NOFORMULA; 268 else if (aEdRowCell.GetText().Len() == 0 && 269 aEdColCell.GetText().Len() == 0) 270 nError = TABOPERR_NOCOLROW; 271 else if ( !lcl_Parse( aEdFormulaRange.GetText(), pDoc, nCurTab, 272 theFormulaCell, theFormulaEnd ) ) 273 nError = TABOPERR_WRONGFORMULA; 274 else 275 { 276 const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention(); 277 if (aEdRowCell.GetText().Len() > 0) 278 { 279 if (!ConvertSingleRef( pDoc, aEdRowCell.GetText(), nCurTab, 280 theRowCell, eConv )) 281 nError = TABOPERR_WRONGROW; 282 else 283 { 284 if (aEdColCell.GetText().Len() == 0 && 285 theFormulaCell.Col() != theFormulaEnd.Col()) 286 nError = TABOPERR_NOCOLFORMULA; 287 else 288 nMode = 1; 289 } 290 } 291 if (aEdColCell.GetText().Len() > 0) 292 { 293 if (!ConvertSingleRef( pDoc, aEdColCell.GetText(), nCurTab, 294 theColCell, eConv )) 295 nError = TABOPERR_WRONGCOL; 296 else 297 { 298 if (nMode == 1) // beides 299 { 300 nMode = 2; 301 ConvertSingleRef( pDoc, aEdFormulaRange.GetText(), nCurTab, 302 theFormulaCell, eConv ); 303 } 304 else if (theFormulaCell.Row() != theFormulaEnd.Row()) 305 nError = TABOPERR_NOROWFORMULA; 306 else 307 nMode = 0; 308 } 309 } 310 } 311 312 if (nError) 313 RaiseError( (ScTabOpErr) nError ); 314 else 315 { 316 ScTabOpParam aOutParam( theFormulaCell, 317 theFormulaEnd, 318 theRowCell, 319 theColCell, 320 nMode ); 321 ScTabOpItem aOutItem( SID_TABOP, &aOutParam ); 322 323 SetDispatcherLock( sal_False ); 324 SwitchToDocument(); 325 GetBindings().GetDispatcher()->Execute( SID_TABOP, 326 SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD, 327 &aOutItem, 0L, 0L ); 328 Close(); 329 } 330 } 331 else if ( pBtn == &aBtnCancel ) 332 Close(); 333 334 return 0; 335 } 336 337 //---------------------------------------------------------------------------- 338 339 IMPL_LINK( ScTabOpDlg, GetFocusHdl, Control*, pCtrl ) 340 { 341 if( (pCtrl == (Control*)&aEdFormulaRange) || (pCtrl == (Control*)&aRBFormulaRange) ) 342 pEdActive = &aEdFormulaRange; 343 else if( (pCtrl == (Control*)&aEdRowCell) || (pCtrl == (Control*)&aRBRowCell) ) 344 pEdActive = &aEdRowCell; 345 else if( (pCtrl == (Control*)&aEdColCell) || (pCtrl == (Control*)&aRBColCell) ) 346 pEdActive = &aEdColCell; 347 else 348 pEdActive = NULL; 349 350 if( pEdActive ) 351 pEdActive->SetSelection( Selection( 0, SELECTION_MAX ) ); 352 353 return 0; 354 } 355 356 //---------------------------------------------------------------------------- 357 358 IMPL_LINK( ScTabOpDlg, LoseFocusHdl, Control*, EMPTYARG ) 359 { 360 bDlgLostFocus = !IsActive(); 361 return 0; 362 } 363 364 365 366 367 368