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 // System - Includes --------------------------------------------------------- 28 29 30 31 // INCLUDE ------------------------------------------------------------------- 32 33 #include "global.hxx" 34 #include "reffact.hxx" 35 #include "document.hxx" 36 #include "docfunc.hxx" 37 #include "scresid.hxx" 38 #include "globstr.hrc" 39 #include "namedlg.hrc" 40 41 #define _NAMEDLG_CXX 42 #include "namedlg.hxx" 43 #undef _NAMEDLG_CXX 44 #include <vcl/msgbox.hxx> 45 46 47 48 // defines ------------------------------------------------------------------- 49 50 #define ABS_SREF SCA_VALID \ 51 | SCA_COL_ABSOLUTE | SCA_ROW_ABSOLUTE | SCA_TAB_ABSOLUTE 52 #define ABS_DREF ABS_SREF \ 53 | SCA_COL2_ABSOLUTE | SCA_ROW2_ABSOLUTE | SCA_TAB2_ABSOLUTE 54 #define ABS_SREF3D ABS_SREF | SCA_TAB_3D 55 #define ABS_DREF3D ABS_DREF | SCA_TAB_3D 56 57 58 //============================================================================ 59 // Hilfsklasse: Merken der aktuellen Bereichsoptionen, 60 // wenn ein Name in der ComboBox gefunden wird. 61 62 struct SaveData 63 { 64 SaveData() 65 : bCriteria(sal_False),bPrintArea(sal_False), 66 bColHeader(sal_False),bRowHeader(sal_False), 67 bDirty(sal_False) {} 68 69 void Clear() 70 { 71 aStrSymbol.Erase(); 72 bCriteria = bPrintArea = 73 bColHeader = bRowHeader = sal_False; 74 bDirty = sal_True; 75 } 76 77 String aStrSymbol; 78 sal_Bool bCriteria:1; 79 sal_Bool bPrintArea:1; 80 sal_Bool bColHeader:1; 81 sal_Bool bRowHeader:1; 82 sal_Bool bDirty:1; 83 }; 84 85 static SaveData* pSaveObj = NULL; 86 87 #define SAVE_DATA() \ 88 pSaveObj->aStrSymbol = aEdAssign.GetText(); \ 89 pSaveObj->bCriteria = aBtnCriteria.IsChecked(); \ 90 pSaveObj->bPrintArea = aBtnPrintArea.IsChecked(); \ 91 pSaveObj->bColHeader = aBtnColHeader.IsChecked(); \ 92 pSaveObj->bRowHeader = aBtnRowHeader.IsChecked(); \ 93 pSaveObj->bDirty = sal_True; 94 95 #define RESTORE_DATA() \ 96 if ( pSaveObj->bDirty ) \ 97 { \ 98 aEdAssign.SetText( pSaveObj->aStrSymbol ); \ 99 aBtnCriteria.Check( pSaveObj->bCriteria ); \ 100 aBtnPrintArea.Check( pSaveObj->bPrintArea ); \ 101 aBtnColHeader.Check( pSaveObj->bColHeader ); \ 102 aBtnRowHeader.Check( pSaveObj->bRowHeader ); \ 103 pSaveObj->bDirty = sal_False; \ 104 } 105 106 #define ERRORBOX(s) ErrorBox(this,WinBits(WB_OK|WB_DEF_OK),s).Execute(); 107 108 109 //============================================================================ 110 // class ScNameDlg 111 112 //---------------------------------------------------------------------------- 113 114 ScNameDlg::ScNameDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent, 115 ScViewData* ptrViewData, 116 const ScAddress& aCursorPos ) 117 118 : ScAnyRefDlg ( pB, pCW, pParent, RID_SCDLG_NAMES ), 119 // 120 aFlName ( this, ScResId( FL_NAME ) ), 121 aEdName ( this, ScResId( ED_NAME ) ), 122 // 123 aFlAssign ( this, ScResId( FL_ASSIGN ) ), 124 aEdAssign ( this, this, ScResId( ED_ASSIGN ) ), 125 aRbAssign ( this, ScResId( RB_ASSIGN ), &aEdAssign, this ), 126 // 127 aFlType ( this, ScResId( FL_TYPE ) ), 128 aBtnPrintArea ( this, ScResId( BTN_PRINTAREA ) ), 129 aBtnColHeader ( this, ScResId( BTN_COLHEADER ) ), 130 aBtnCriteria ( this, ScResId( BTN_CRITERIA ) ), 131 aBtnRowHeader ( this, ScResId( BTN_ROWHEADER ) ), 132 // 133 aBtnOk ( this, ScResId( BTN_OK ) ), 134 aBtnCancel ( this, ScResId( BTN_CANCEL ) ), 135 aBtnHelp ( this, ScResId( BTN_HELP ) ), 136 aBtnAdd ( this, ScResId( BTN_ADD ) ), 137 aBtnRemove ( this, ScResId( BTN_REMOVE ) ), 138 aBtnMore ( this, ScResId( BTN_MORE ) ), 139 // 140 bSaved (sal_False), 141 aStrAdd ( ScResId( STR_ADD ) ), 142 aStrModify ( ScResId( STR_MODIFY ) ), 143 errMsgInvalidSym( ScResId( STR_INVALIDSYMBOL ) ), 144 // 145 pViewData ( ptrViewData ), 146 pDoc ( ptrViewData->GetDocument() ), 147 aLocalRangeName ( *(pDoc->GetRangeName()) ), 148 theCursorPos ( aCursorPos ) // zum Berechnen der Referenzen 149 { 150 pSaveObj = new SaveData; 151 Init(); 152 FreeResource(); 153 154 aRbAssign.SetAccessibleRelationMemberOf(&aFlAssign); 155 } 156 157 158 //---------------------------------------------------------------------------- 159 160 __EXPORT ScNameDlg::~ScNameDlg() 161 { 162 DELETEZ( pSaveObj ); 163 } 164 165 166 //---------------------------------------------------------------------------- 167 168 void __EXPORT ScNameDlg::Init() 169 { 170 String aAreaStr; 171 ScRange aRange; 172 173 DBG_ASSERT( pViewData && pDoc, "ViewData oder Document nicht gefunden!" ); 174 175 aBtnOk.SetClickHdl ( LINK( this, ScNameDlg, OkBtnHdl ) ); 176 aBtnCancel.SetClickHdl ( LINK( this, ScNameDlg, CancelBtnHdl ) ); 177 aBtnAdd.SetClickHdl ( LINK( this, ScNameDlg, AddBtnHdl ) ); 178 aBtnRemove.SetClickHdl ( LINK( this, ScNameDlg, RemoveBtnHdl ) ); 179 aEdAssign.SetGetFocusHdl( LINK( this, ScNameDlg, AssignGetFocusHdl ) ); 180 aEdAssign.SetModifyHdl ( LINK( this, ScNameDlg, EdModifyHdl ) ); 181 aEdName.SetModifyHdl ( LINK( this, ScNameDlg, EdModifyHdl ) ); 182 aEdName.SetSelectHdl ( LINK( this, ScNameDlg, NameSelectHdl ) ); 183 184 aBtnCriteria .Hide(); 185 aBtnPrintArea.Hide(); 186 aBtnColHeader.Hide(); 187 aBtnRowHeader.Hide(); 188 189 aBtnMore.AddWindow( &aFlType ); 190 aBtnMore.AddWindow( &aBtnCriteria ); 191 aBtnMore.AddWindow( &aBtnPrintArea ); 192 aBtnMore.AddWindow( &aBtnColHeader ); 193 aBtnMore.AddWindow( &aBtnRowHeader ); 194 195 UpdateNames(); 196 197 pViewData->GetSimpleArea( aRange ); 198 aRange.Format( aAreaStr, ABS_DREF3D, pDoc, 199 ScAddress::Details(pDoc->GetAddressConvention(), 0, 0) ); 200 201 theCurSel = Selection( 0, SELECTION_MAX ); 202 aEdAssign.GrabFocus(); 203 aEdAssign.SetText( aAreaStr ); 204 aEdAssign.SetSelection( theCurSel ); 205 aEdName.GrabFocus(); 206 207 aBtnAdd.Disable(); 208 aBtnRemove.Disable(); 209 if ( aEdName.GetEntryCount() > 0 ) 210 aBtnAdd.SetText( aStrAdd ); 211 UpdateChecks(); 212 EdModifyHdl( 0 ); 213 214 bSaved=sal_True; 215 SAVE_DATA() 216 217 //@BugID 54702 218 //SFX_APPWINDOW->Disable(sal_False); //! allgemeine Methode im ScAnyRefDlg 219 } 220 221 //---------------------------------------------------------------------------- 222 sal_Bool ScNameDlg::IsRefInputMode() const 223 { 224 return aEdAssign.IsEnabled(); 225 } 226 227 void ScNameDlg::RefInputDone( sal_Bool bForced) 228 { 229 ScAnyRefDlg::RefInputDone(bForced); 230 EdModifyHdl(&aEdAssign); 231 } 232 //---------------------------------------------------------------------------- 233 // Uebergabe eines mit der Maus selektierten Tabellenbereiches, der dann als 234 // neue Selektion im Referenz-Edit angezeigt wird. 235 236 237 void ScNameDlg::SetReference( const ScRange& rRef, ScDocument* pDocP ) 238 { 239 if ( aEdAssign.IsEnabled() ) 240 { 241 if ( rRef.aStart != rRef.aEnd ) 242 RefInputStart(&aEdAssign); 243 String aRefStr; 244 rRef.Format( aRefStr, ABS_DREF3D, pDocP, 245 ScAddress::Details(pDocP->GetAddressConvention(), 0, 0) ); 246 aEdAssign.SetRefString( aRefStr ); 247 } 248 } 249 250 251 //---------------------------------------------------------------------------- 252 sal_Bool __EXPORT ScNameDlg::Close() 253 { 254 return DoClose( ScNameDlgWrapper::GetChildWindowId() ); 255 } 256 257 258 //---------------------------------------------------------------------------- 259 260 void ScNameDlg::SetActive() 261 { 262 aEdAssign.GrabFocus(); 263 RefInputDone(); 264 } 265 266 267 //---------------------------------------------------------------------------- 268 269 void __EXPORT ScNameDlg::UpdateChecks() 270 { 271 sal_uInt16 nCurPos=0; 272 273 if(aLocalRangeName.SearchName( aEdName.GetText(), nCurPos)) 274 { 275 ScRangeData* pData=(ScRangeData*)(aLocalRangeName.At( nCurPos )); 276 aBtnCriteria .Check( pData->HasType( RT_CRITERIA ) ); 277 aBtnPrintArea.Check( pData->HasType( RT_PRINTAREA ) ); 278 aBtnColHeader.Check( pData->HasType( RT_COLHEADER ) ); 279 aBtnRowHeader.Check( pData->HasType( RT_ROWHEADER ) ); 280 } 281 282 // Falls Edit-Feld leer ist: Typ-CheckBoxen deaktivieren: 283 284 if ( aEdName.GetText().Len() != 0 ) 285 { 286 if ( !aFlType.IsEnabled() ) 287 { 288 aFlType .Enable(); 289 aBtnCriteria .Enable(); 290 aBtnPrintArea.Enable(); 291 aBtnColHeader.Enable(); 292 aBtnRowHeader.Enable(); 293 aFlAssign .Enable(); 294 aEdAssign .Enable(); 295 aRbAssign .Enable(); 296 } 297 } 298 else if ( aFlType.IsEnabled() ) 299 { 300 aFlType .Disable(); 301 aBtnCriteria .Disable(); 302 aBtnPrintArea.Disable(); 303 aBtnColHeader.Disable(); 304 aBtnRowHeader.Disable(); 305 aFlAssign .Disable(); 306 aEdAssign .Disable(); 307 aRbAssign .Disable(); 308 } 309 } 310 311 312 //---------------------------------------------------------------------------- 313 314 void __EXPORT ScNameDlg::UpdateNames() 315 { 316 sal_uInt16 nRangeCount = aLocalRangeName.GetCount(); 317 318 aEdName.SetUpdateMode( sal_False ); 319 //----------------------------------------------------------- 320 sal_uInt16 nNamePos = aEdName.GetTopEntry(); 321 aEdName.Clear(); 322 323 aEdAssign.SetText( EMPTY_STRING ); 324 325 if ( nRangeCount > 0 ) 326 { 327 ScRangeData* pRangeData = NULL; 328 String aString; 329 330 for ( sal_uInt16 i=0; i<nRangeCount; i++ ) 331 { 332 pRangeData = (ScRangeData*)(aLocalRangeName.At( i )); 333 if ( pRangeData ) 334 { 335 if ( !pRangeData->HasType( RT_DATABASE ) 336 && !pRangeData->HasType( RT_SHARED ) ) 337 { 338 pRangeData->GetName( aString ); 339 aEdName.InsertEntry( aString ); 340 } 341 } 342 } 343 } 344 else 345 { 346 aBtnAdd.SetText( aStrAdd ); 347 aBtnAdd.Disable(); 348 aBtnRemove.Disable(); 349 } 350 //----------------------------------------------------------- 351 aEdName.SetUpdateMode( sal_True ); 352 aEdName.SetTopEntry(nNamePos); 353 aEdName.Invalidate(); 354 } 355 356 357 //---------------------------------------------------------------------------- 358 359 void __EXPORT ScNameDlg::CalcCurTableAssign( String& aAssign, sal_uInt16 nCurPos ) 360 { 361 ScRangeData* pRangeData = (ScRangeData*)(aLocalRangeName.At( nCurPos )); 362 363 if ( pRangeData ) 364 { 365 rtl::OUStringBuffer sBuffer; 366 pRangeData->UpdateSymbol( sBuffer, theCursorPos ); 367 aAssign = sBuffer; 368 } 369 else 370 { 371 aAssign.Erase(); 372 } 373 } 374 375 376 //---------------------------------------------------------------------------- 377 // Handler: 378 // ======== 379 380 IMPL_LINK( ScNameDlg, OkBtnHdl, void *, EMPTYARG ) 381 { 382 if ( aBtnAdd.IsEnabled() ) 383 AddBtnHdl( 0 ); 384 385 if ( !aBtnAdd.IsEnabled() && !aBtnRemove.IsEnabled() ) 386 { 387 ScDocShell* pDocSh = pViewData->GetDocShell(); 388 ScDocFunc aFunc(*pDocSh); 389 aFunc.ModifyRangeNames( aLocalRangeName, sal_False ); 390 Close(); 391 } 392 return 0; 393 } 394 395 396 //---------------------------------------------------------------------------- 397 398 IMPL_LINK_INLINE_START( ScNameDlg, CancelBtnHdl, void *, EMPTYARG ) 399 { 400 Close(); 401 return 0; 402 } 403 IMPL_LINK_INLINE_END( ScNameDlg, CancelBtnHdl, void *, EMPTYARG ) 404 405 406 //---------------------------------------------------------------------------- 407 408 IMPL_LINK( ScNameDlg, AddBtnHdl, void *, EMPTYARG ) 409 { 410 sal_Bool bAdded = sal_False; 411 String aNewEntry = aEdName.GetText(); 412 sal_uInt16 nNamePos = aEdName.GetTopEntry(); 413 aNewEntry.EraseLeadingChars( ' ' ); 414 aNewEntry.EraseTrailingChars( ' ' ); 415 416 if ( aNewEntry.Len() > 0 ) 417 { 418 if ( ScRangeData::IsNameValid( aNewEntry, pDoc ) ) 419 { 420 if ( pDoc ) 421 { 422 ScRangeData* pNewEntry = NULL; 423 RangeType nType = RT_NAME; 424 sal_uInt16 nFoundAt = 0; 425 String theSymbol = aEdAssign.GetText(); 426 String aStrPos; 427 String aStrArea; 428 429 pNewEntry = new ScRangeData( pDoc, 430 aNewEntry, 431 theSymbol, 432 theCursorPos, 433 nType ); 434 if (pNewEntry) 435 { 436 nType = nType 437 | (aBtnRowHeader .IsChecked() ? RT_ROWHEADER : RangeType(0)) 438 | (aBtnColHeader .IsChecked() ? RT_COLHEADER : RangeType(0)) 439 | (aBtnPrintArea .IsChecked() ? RT_PRINTAREA : RangeType(0)) 440 | (aBtnCriteria .IsChecked() ? RT_CRITERIA : RangeType(0)); 441 pNewEntry->AddType(nType); 442 } 443 444 // theSymbol gueltig? 445 // (= konnte theSymbol im ScRangeData-Ctor 446 // in ein Token-Array uebersetzt werden?) 447 if ( 0 == pNewEntry->GetErrCode() ) 448 { 449 // Eintrag bereits vorhanden? Dann vorher entfernen (=Aendern) 450 if ( aLocalRangeName.SearchName( aNewEntry, nFoundAt ) ) 451 { // alten Index uebernehmen 452 pNewEntry->SetIndex( 453 ((ScRangeData*)(aLocalRangeName.At(nFoundAt)))->GetIndex() ); 454 aLocalRangeName.AtFree( nFoundAt ); 455 } 456 else 457 pSaveObj->Clear(); 458 459 if ( !aLocalRangeName.Insert( pNewEntry ) ) 460 delete pNewEntry; 461 462 UpdateNames(); 463 bSaved=sal_False; 464 RESTORE_DATA() 465 aEdName.SetText(EMPTY_STRING); 466 aEdName.GrabFocus(); 467 UpdateChecks(); 468 aBtnAdd.SetText( aStrAdd ); 469 aBtnAdd.Disable(); 470 aBtnRemove.Disable(); 471 472 //@BugID 54702 raus mit dem Sch. 473 //SFX_APPWINDOW->Disable(sal_False); //! allgemeine Methode im ScAnyRefDlg 474 475 bAdded = sal_True; 476 } 477 else // theSymbol ungueltig 478 { 479 delete pNewEntry; 480 ERRORBOX( errMsgInvalidSym ); 481 theCurSel = Selection( 0, SELECTION_MAX ); 482 aEdAssign.GrabFocus(); 483 } 484 } 485 } 486 else 487 { 488 ERRORBOX( ScGlobal::GetRscString(STR_INVALIDNAME) ); 489 aEdName.SetSelection( Selection( 0, SELECTION_MAX ) ); 490 aEdName.GrabFocus(); 491 } 492 } 493 494 aEdName.SetTopEntry(nNamePos); 495 return bAdded; 496 } 497 498 499 //---------------------------------------------------------------------------- 500 501 IMPL_LINK( ScNameDlg, RemoveBtnHdl, void *, EMPTYARG ) 502 { 503 sal_uInt16 nRemoveAt = 0; 504 const String aStrEntry = aEdName.GetText(); 505 506 if ( aLocalRangeName.SearchName( aStrEntry, nRemoveAt ) ) 507 { 508 String aStrDelMsg = ScGlobal::GetRscString( STR_QUERY_DELENTRY ); 509 String aMsg = aStrDelMsg.GetToken( 0, '#' ); 510 511 aMsg += aStrEntry; 512 aMsg += aStrDelMsg.GetToken( 1, '#' ); 513 514 if ( RET_YES == 515 QueryBox( this, WinBits( WB_YES_NO | WB_DEF_YES ), aMsg ).Execute() ) 516 { 517 aLocalRangeName.AtFree( nRemoveAt ); 518 UpdateNames(); 519 UpdateChecks(); 520 bSaved=sal_False; 521 RESTORE_DATA() 522 theCurSel = Selection( 0, SELECTION_MAX ); 523 aBtnAdd.SetText( aStrAdd ); 524 aBtnAdd.Disable(); 525 aBtnRemove.Disable(); 526 } 527 } 528 return 0; 529 } 530 531 532 //---------------------------------------------------------------------------- 533 534 IMPL_LINK( ScNameDlg, NameSelectHdl, void *, EMPTYARG ) 535 { 536 sal_uInt16 nAtPos; 537 538 if ( aLocalRangeName.SearchName( aEdName.GetText(), nAtPos ) ) 539 { 540 String aSymbol; 541 ScRangeData* pData = (ScRangeData*)(aLocalRangeName.At( nAtPos )); 542 543 if ( pData ) 544 { 545 pData->GetSymbol( aSymbol ); 546 CalcCurTableAssign( aSymbol, nAtPos ); 547 aEdAssign.SetText( aSymbol ); 548 aBtnAdd.SetText( aStrModify ); 549 theCurSel = Selection( 0, SELECTION_MAX ); 550 } 551 } 552 UpdateChecks(); 553 return 0; 554 } 555 556 557 //---------------------------------------------------------------------------- 558 559 IMPL_LINK( ScNameDlg, EdModifyHdl, Edit *, pEd ) 560 { 561 String theName = aEdName.GetText(); 562 String theSymbol = aEdAssign.GetText(); 563 sal_Bool bNameFound = (COMBOBOX_ENTRY_NOTFOUND 564 != aEdName.GetEntryPos( theName )); 565 566 if ( pEd == &aEdName ) 567 { 568 if ( theName.Len() == 0 ) 569 { 570 if ( aBtnAdd.GetText() != aStrAdd ) 571 aBtnAdd.SetText( aStrAdd ); 572 aBtnAdd.Disable(); 573 aBtnRemove.Disable(); 574 aFlAssign.Disable(); 575 aEdAssign.Disable(); 576 aRbAssign.Disable(); 577 //@BugID 54702 raus mit dem Sch. 578 //SFX_APPWINDOW->Disable(sal_False); //! allgemeine Methode im ScAnyRefDlg 579 } 580 else 581 { 582 if ( bNameFound ) 583 { 584 if ( aBtnAdd.GetText() != aStrModify ) 585 aBtnAdd.SetText( aStrModify ); 586 587 aBtnRemove.Enable(); 588 589 if(!bSaved) 590 { 591 bSaved=sal_True; 592 SAVE_DATA() 593 } 594 NameSelectHdl( 0 ); 595 } 596 else 597 { 598 if ( aBtnAdd.GetText() != aStrAdd ) 599 aBtnAdd.SetText( aStrAdd ); 600 aBtnRemove.Disable(); 601 602 bSaved=sal_False; 603 RESTORE_DATA() 604 } 605 theSymbol = aEdAssign.GetText(); 606 607 if ( theSymbol.Len() > 0 ) 608 aBtnAdd.Enable(); 609 else 610 aBtnAdd.Disable(); 611 612 aFlAssign.Enable(); 613 aEdAssign.Enable(); 614 aRbAssign.Enable(); 615 //@BugID 54702 raus mit dem Sch. 616 //SFX_APPWINDOW->Enable(); 617 } 618 UpdateChecks(); 619 theCurSel = Selection( 0, SELECTION_MAX ); 620 } 621 else if ( pEd == &aEdAssign ) 622 { 623 if ( (theName.Len()>0) && (theSymbol.Len()>0) ) 624 { 625 aBtnAdd.Enable(); 626 if ( bNameFound ) 627 aBtnRemove.Enable(); 628 } 629 else 630 { 631 aBtnAdd.Disable(); 632 aBtnRemove.Disable(); 633 } 634 } 635 return 0; 636 } 637 638 //------------------------------------------------------------------------ 639 640 IMPL_LINK_INLINE_START( ScNameDlg, AssignGetFocusHdl, void *, EMPTYARG ) 641 { 642 EdModifyHdl( &aEdAssign ); 643 return 0; 644 } 645 IMPL_LINK_INLINE_END( ScNameDlg, AssignGetFocusHdl, void *, EMPTYARG ) 646 647 648