Update ooo320-m1
[ooovba.git] / sc / source / ui / namedlg / namedlg.cxx
blobd8c674162ce093e00167da8844f741cec6902439
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: namedlg.cxx,v $
10 * $Revision: 1.11 $
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"
34 // System - Includes ---------------------------------------------------------
38 // INCLUDE -------------------------------------------------------------------
40 #include "global.hxx"
41 #include "reffact.hxx"
42 #include "document.hxx"
43 #include "docfunc.hxx"
44 #include "scresid.hxx"
45 #include "globstr.hrc"
46 #include "namedlg.hrc"
48 #define _NAMEDLG_CXX
49 #include "namedlg.hxx"
50 #undef _NAMEDLG_CXX
51 #include <vcl/msgbox.hxx>
55 // defines -------------------------------------------------------------------
57 #define ABS_SREF SCA_VALID \
58 | SCA_COL_ABSOLUTE | SCA_ROW_ABSOLUTE | SCA_TAB_ABSOLUTE
59 #define ABS_DREF ABS_SREF \
60 | SCA_COL2_ABSOLUTE | SCA_ROW2_ABSOLUTE | SCA_TAB2_ABSOLUTE
61 #define ABS_SREF3D ABS_SREF | SCA_TAB_3D
62 #define ABS_DREF3D ABS_DREF | SCA_TAB_3D
65 //============================================================================
66 // Hilfsklasse: Merken der aktuellen Bereichsoptionen,
67 // wenn ein Name in der ComboBox gefunden wird.
69 struct SaveData
71 SaveData()
72 : bCriteria(FALSE),bPrintArea(FALSE),
73 bColHeader(FALSE),bRowHeader(FALSE),
74 bDirty(FALSE) {}
76 void Clear()
78 aStrSymbol.Erase();
79 bCriteria = bPrintArea =
80 bColHeader = bRowHeader = FALSE;
81 bDirty = TRUE;
84 String aStrSymbol;
85 BOOL bCriteria:1;
86 BOOL bPrintArea:1;
87 BOOL bColHeader:1;
88 BOOL bRowHeader:1;
89 BOOL bDirty:1;
92 static SaveData* pSaveObj = NULL;
94 #define SAVE_DATA() \
95 pSaveObj->aStrSymbol = aEdAssign.GetText(); \
96 pSaveObj->bCriteria = aBtnCriteria.IsChecked(); \
97 pSaveObj->bPrintArea = aBtnPrintArea.IsChecked(); \
98 pSaveObj->bColHeader = aBtnColHeader.IsChecked(); \
99 pSaveObj->bRowHeader = aBtnRowHeader.IsChecked(); \
100 pSaveObj->bDirty = TRUE;
102 #define RESTORE_DATA() \
103 if ( pSaveObj->bDirty ) \
105 aEdAssign.SetText( pSaveObj->aStrSymbol ); \
106 aBtnCriteria.Check( pSaveObj->bCriteria ); \
107 aBtnPrintArea.Check( pSaveObj->bPrintArea ); \
108 aBtnColHeader.Check( pSaveObj->bColHeader ); \
109 aBtnRowHeader.Check( pSaveObj->bRowHeader ); \
110 pSaveObj->bDirty = FALSE; \
113 #define ERRORBOX(s) ErrorBox(this,WinBits(WB_OK|WB_DEF_OK),s).Execute();
116 //============================================================================
117 // class ScNameDlg
119 //----------------------------------------------------------------------------
121 ScNameDlg::ScNameDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent,
122 ScViewData* ptrViewData,
123 const ScAddress& aCursorPos )
125 : ScAnyRefDlg ( pB, pCW, pParent, RID_SCDLG_NAMES ),
127 aFlName ( this, ScResId( FL_NAME ) ),
128 aEdName ( this, ScResId( ED_NAME ) ),
130 aFlAssign ( this, ScResId( FL_ASSIGN ) ),
131 aEdAssign ( this, this, ScResId( ED_ASSIGN ) ),
132 aRbAssign ( this, ScResId( RB_ASSIGN ), &aEdAssign, this ),
134 aFlType ( this, ScResId( FL_TYPE ) ),
135 aBtnPrintArea ( this, ScResId( BTN_PRINTAREA ) ),
136 aBtnColHeader ( this, ScResId( BTN_COLHEADER ) ),
137 aBtnCriteria ( this, ScResId( BTN_CRITERIA ) ),
138 aBtnRowHeader ( this, ScResId( BTN_ROWHEADER ) ),
140 aBtnOk ( this, ScResId( BTN_OK ) ),
141 aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
142 aBtnHelp ( this, ScResId( BTN_HELP ) ),
143 aBtnAdd ( this, ScResId( BTN_ADD ) ),
144 aBtnRemove ( this, ScResId( BTN_REMOVE ) ),
145 aBtnMore ( this, ScResId( BTN_MORE ) ),
147 bSaved (FALSE),
148 aStrAdd ( ScResId( STR_ADD ) ),
149 aStrModify ( ScResId( STR_MODIFY ) ),
150 errMsgInvalidSym( ScResId( STR_INVALIDSYMBOL ) ),
152 pViewData ( ptrViewData ),
153 pDoc ( ptrViewData->GetDocument() ),
154 aLocalRangeName ( *(pDoc->GetRangeName()) ),
155 theCursorPos ( aCursorPos ) // zum Berechnen der Referenzen
157 pSaveObj = new SaveData;
158 Init();
159 FreeResource();
163 //----------------------------------------------------------------------------
165 __EXPORT ScNameDlg::~ScNameDlg()
167 DELETEZ( pSaveObj );
171 //----------------------------------------------------------------------------
173 void __EXPORT ScNameDlg::Init()
175 String aAreaStr;
176 ScRange aRange;
178 DBG_ASSERT( pViewData && pDoc, "ViewData oder Document nicht gefunden!" );
180 aBtnOk.SetClickHdl ( LINK( this, ScNameDlg, OkBtnHdl ) );
181 aBtnCancel.SetClickHdl ( LINK( this, ScNameDlg, CancelBtnHdl ) );
182 aBtnAdd.SetClickHdl ( LINK( this, ScNameDlg, AddBtnHdl ) );
183 aBtnRemove.SetClickHdl ( LINK( this, ScNameDlg, RemoveBtnHdl ) );
184 aEdAssign.SetGetFocusHdl( LINK( this, ScNameDlg, AssignGetFocusHdl ) );
185 aEdAssign.SetModifyHdl ( LINK( this, ScNameDlg, EdModifyHdl ) );
186 aEdName.SetModifyHdl ( LINK( this, ScNameDlg, EdModifyHdl ) );
187 aEdName.SetSelectHdl ( LINK( this, ScNameDlg, NameSelectHdl ) );
189 aBtnCriteria .Hide();
190 aBtnPrintArea.Hide();
191 aBtnColHeader.Hide();
192 aBtnRowHeader.Hide();
194 aBtnMore.AddWindow( &aFlType );
195 aBtnMore.AddWindow( &aBtnCriteria );
196 aBtnMore.AddWindow( &aBtnPrintArea );
197 aBtnMore.AddWindow( &aBtnColHeader );
198 aBtnMore.AddWindow( &aBtnRowHeader );
200 UpdateNames();
202 pViewData->GetSimpleArea( aRange );
203 aRange.Format( aAreaStr, ABS_DREF3D, pDoc,
204 ScAddress::Details(pDoc->GetAddressConvention(), 0, 0) );
206 theCurSel = Selection( 0, SELECTION_MAX );
207 aEdAssign.GrabFocus();
208 aEdAssign.SetText( aAreaStr );
209 aEdAssign.SetSelection( theCurSel );
210 aEdName.GrabFocus();
212 aBtnAdd.Disable();
213 aBtnRemove.Disable();
214 if ( aEdName.GetEntryCount() > 0 )
215 aBtnAdd.SetText( aStrAdd );
216 UpdateChecks();
217 EdModifyHdl( 0 );
219 bSaved=TRUE;
220 SAVE_DATA()
222 //@BugID 54702
223 //SFX_APPWINDOW->Disable(FALSE); //! allgemeine Methode im ScAnyRefDlg
226 //----------------------------------------------------------------------------
227 BOOL ScNameDlg::IsRefInputMode() const
229 return aEdAssign.IsEnabled();
232 void ScNameDlg::RefInputDone( BOOL bForced)
234 ScAnyRefDlg::RefInputDone(bForced);
235 EdModifyHdl(&aEdAssign);
237 //----------------------------------------------------------------------------
238 // Uebergabe eines mit der Maus selektierten Tabellenbereiches, der dann als
239 // neue Selektion im Referenz-Edit angezeigt wird.
242 void ScNameDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
244 if ( aEdAssign.IsEnabled() )
246 if ( rRef.aStart != rRef.aEnd )
247 RefInputStart(&aEdAssign);
248 String aRefStr;
249 rRef.Format( aRefStr, ABS_DREF3D, pDocP,
250 ScAddress::Details(pDocP->GetAddressConvention(), 0, 0) );
251 aEdAssign.SetRefString( aRefStr );
256 //----------------------------------------------------------------------------
257 BOOL __EXPORT ScNameDlg::Close()
259 return DoClose( ScNameDlgWrapper::GetChildWindowId() );
263 //----------------------------------------------------------------------------
265 void ScNameDlg::SetActive()
267 aEdAssign.GrabFocus();
268 RefInputDone();
272 //----------------------------------------------------------------------------
274 void __EXPORT ScNameDlg::UpdateChecks()
276 USHORT nCurPos=0;
278 if(aLocalRangeName.SearchName( aEdName.GetText(), nCurPos))
280 ScRangeData* pData=(ScRangeData*)(aLocalRangeName.At( nCurPos ));
281 aBtnCriteria .Check( pData->HasType( RT_CRITERIA ) );
282 aBtnPrintArea.Check( pData->HasType( RT_PRINTAREA ) );
283 aBtnColHeader.Check( pData->HasType( RT_COLHEADER ) );
284 aBtnRowHeader.Check( pData->HasType( RT_ROWHEADER ) );
287 // Falls Edit-Feld leer ist: Typ-CheckBoxen deaktivieren:
289 if ( aEdName.GetText().Len() != 0 )
291 if ( !aFlType.IsEnabled() )
293 aFlType .Enable();
294 aBtnCriteria .Enable();
295 aBtnPrintArea.Enable();
296 aBtnColHeader.Enable();
297 aBtnRowHeader.Enable();
298 aFlAssign .Enable();
299 aEdAssign .Enable();
300 aRbAssign .Enable();
303 else if ( aFlType.IsEnabled() )
305 aFlType .Disable();
306 aBtnCriteria .Disable();
307 aBtnPrintArea.Disable();
308 aBtnColHeader.Disable();
309 aBtnRowHeader.Disable();
310 aFlAssign .Disable();
311 aEdAssign .Disable();
312 aRbAssign .Disable();
317 //----------------------------------------------------------------------------
319 void __EXPORT ScNameDlg::UpdateNames()
321 USHORT nRangeCount = aLocalRangeName.GetCount();
323 aEdName.SetUpdateMode( FALSE );
324 //-----------------------------------------------------------
325 USHORT nNamePos = aEdName.GetTopEntry();
326 aEdName.Clear();
328 aEdAssign.SetText( EMPTY_STRING );
330 if ( nRangeCount > 0 )
332 ScRangeData* pRangeData = NULL;
333 String aString;
335 for ( USHORT i=0; i<nRangeCount; i++ )
337 pRangeData = (ScRangeData*)(aLocalRangeName.At( i ));
338 if ( pRangeData )
340 if ( !pRangeData->HasType( RT_DATABASE )
341 && !pRangeData->HasType( RT_SHARED ) )
343 pRangeData->GetName( aString );
344 aEdName.InsertEntry( aString );
349 else
351 aBtnAdd.SetText( aStrAdd );
352 aBtnAdd.Disable();
353 aBtnRemove.Disable();
355 //-----------------------------------------------------------
356 aEdName.SetUpdateMode( TRUE );
357 aEdName.SetTopEntry(nNamePos);
358 aEdName.Invalidate();
362 //----------------------------------------------------------------------------
364 void __EXPORT ScNameDlg::CalcCurTableAssign( String& aAssign, USHORT nCurPos )
366 ScRangeData* pRangeData = (ScRangeData*)(aLocalRangeName.At( nCurPos ));
368 if ( pRangeData )
370 rtl::OUStringBuffer sBuffer;
371 pRangeData->UpdateSymbol( sBuffer, theCursorPos );
372 aAssign = sBuffer;
374 else
376 aAssign.Erase();
381 //----------------------------------------------------------------------------
382 // Handler:
383 // ========
385 IMPL_LINK( ScNameDlg, OkBtnHdl, void *, EMPTYARG )
387 if ( aBtnAdd.IsEnabled() )
388 AddBtnHdl( 0 );
390 if ( !aBtnAdd.IsEnabled() && !aBtnRemove.IsEnabled() )
392 ScDocShell* pDocSh = pViewData->GetDocShell();
393 ScDocFunc aFunc(*pDocSh);
394 aFunc.ModifyRangeNames( aLocalRangeName, FALSE );
395 Close();
397 return 0;
401 //----------------------------------------------------------------------------
403 IMPL_LINK_INLINE_START( ScNameDlg, CancelBtnHdl, void *, EMPTYARG )
405 Close();
406 return 0;
408 IMPL_LINK_INLINE_END( ScNameDlg, CancelBtnHdl, void *, EMPTYARG )
411 //----------------------------------------------------------------------------
413 IMPL_LINK( ScNameDlg, AddBtnHdl, void *, EMPTYARG )
415 BOOL bAdded = FALSE;
416 String aNewEntry = aEdName.GetText();
417 USHORT nNamePos = aEdName.GetTopEntry();
418 aNewEntry.EraseLeadingChars( ' ' );
419 aNewEntry.EraseTrailingChars( ' ' );
421 if ( aNewEntry.Len() > 0 )
423 if ( ScRangeData::IsNameValid( aNewEntry, pDoc ) )
425 if ( pDoc )
427 ScRangeData* pNewEntry = NULL;
428 RangeType nType = RT_NAME;
429 USHORT nFoundAt = 0;
430 String theSymbol = aEdAssign.GetText();
431 String aStrPos;
432 String aStrArea;
434 pNewEntry = new ScRangeData( pDoc,
435 aNewEntry,
436 theSymbol,
437 theCursorPos,
438 nType );
439 if (pNewEntry)
441 nType = nType
442 | (aBtnRowHeader .IsChecked() ? RT_ROWHEADER : RangeType(0))
443 | (aBtnColHeader .IsChecked() ? RT_COLHEADER : RangeType(0))
444 | (aBtnPrintArea .IsChecked() ? RT_PRINTAREA : RangeType(0))
445 | (aBtnCriteria .IsChecked() ? RT_CRITERIA : RangeType(0));
446 pNewEntry->AddType(nType);
449 // theSymbol gueltig?
450 // (= konnte theSymbol im ScRangeData-Ctor
451 // in ein Token-Array uebersetzt werden?)
452 if ( 0 == pNewEntry->GetErrCode() )
454 // Eintrag bereits vorhanden? Dann vorher entfernen (=Aendern)
455 if ( aLocalRangeName.SearchName( aNewEntry, nFoundAt ) )
456 { // alten Index uebernehmen
457 pNewEntry->SetIndex(
458 ((ScRangeData*)(aLocalRangeName.At(nFoundAt)))->GetIndex() );
459 aLocalRangeName.AtFree( nFoundAt );
461 else
462 pSaveObj->Clear();
464 if ( !aLocalRangeName.Insert( pNewEntry ) )
465 delete pNewEntry;
467 UpdateNames();
468 bSaved=FALSE;
469 RESTORE_DATA()
470 aEdName.SetText(EMPTY_STRING);
471 aEdName.GrabFocus();
472 UpdateChecks();
473 aBtnAdd.SetText( aStrAdd );
474 aBtnAdd.Disable();
475 aBtnRemove.Disable();
477 //@BugID 54702 raus mit dem Sch.
478 //SFX_APPWINDOW->Disable(FALSE); //! allgemeine Methode im ScAnyRefDlg
480 bAdded = TRUE;
482 else // theSymbol ungueltig
484 delete pNewEntry;
485 ERRORBOX( errMsgInvalidSym );
486 theCurSel = Selection( 0, SELECTION_MAX );
487 aEdAssign.GrabFocus();
491 else
493 ERRORBOX( ScGlobal::GetRscString(STR_INVALIDNAME) );
494 aEdName.SetSelection( Selection( 0, SELECTION_MAX ) );
495 aEdName.GrabFocus();
499 aEdName.SetTopEntry(nNamePos);
500 return bAdded;
504 //----------------------------------------------------------------------------
506 IMPL_LINK( ScNameDlg, RemoveBtnHdl, void *, EMPTYARG )
508 USHORT nRemoveAt = 0;
509 const String aStrEntry = aEdName.GetText();
511 if ( aLocalRangeName.SearchName( aStrEntry, nRemoveAt ) )
513 String aStrDelMsg = ScGlobal::GetRscString( STR_QUERY_DELENTRY );
514 String aMsg = aStrDelMsg.GetToken( 0, '#' );
516 aMsg += aStrEntry;
517 aMsg += aStrDelMsg.GetToken( 1, '#' );
519 if ( RET_YES ==
520 QueryBox( this, WinBits( WB_YES_NO | WB_DEF_YES ), aMsg ).Execute() )
522 aLocalRangeName.AtFree( nRemoveAt );
523 UpdateNames();
524 UpdateChecks();
525 bSaved=FALSE;
526 RESTORE_DATA()
527 theCurSel = Selection( 0, SELECTION_MAX );
528 aBtnAdd.SetText( aStrAdd );
529 aBtnAdd.Disable();
530 aBtnRemove.Disable();
533 return 0;
537 //----------------------------------------------------------------------------
539 IMPL_LINK( ScNameDlg, NameSelectHdl, void *, EMPTYARG )
541 USHORT nAtPos;
543 if ( aLocalRangeName.SearchName( aEdName.GetText(), nAtPos ) )
545 String aSymbol;
546 ScRangeData* pData = (ScRangeData*)(aLocalRangeName.At( nAtPos ));
548 if ( pData )
550 pData->GetSymbol( aSymbol );
551 CalcCurTableAssign( aSymbol, nAtPos );
552 aEdAssign.SetText( aSymbol );
553 aBtnAdd.SetText( aStrModify );
554 theCurSel = Selection( 0, SELECTION_MAX );
557 UpdateChecks();
558 return 0;
562 //----------------------------------------------------------------------------
564 IMPL_LINK( ScNameDlg, EdModifyHdl, Edit *, pEd )
566 String theName = aEdName.GetText();
567 String theSymbol = aEdAssign.GetText();
568 BOOL bNameFound = (COMBOBOX_ENTRY_NOTFOUND
569 != aEdName.GetEntryPos( theName ));
571 if ( pEd == &aEdName )
573 if ( theName.Len() == 0 )
575 if ( aBtnAdd.GetText() != aStrAdd )
576 aBtnAdd.SetText( aStrAdd );
577 aBtnAdd.Disable();
578 aBtnRemove.Disable();
579 aFlAssign.Disable();
580 aEdAssign.Disable();
581 aRbAssign.Disable();
582 //@BugID 54702 raus mit dem Sch.
583 //SFX_APPWINDOW->Disable(FALSE); //! allgemeine Methode im ScAnyRefDlg
585 else
587 if ( bNameFound )
589 if ( aBtnAdd.GetText() != aStrModify )
590 aBtnAdd.SetText( aStrModify );
592 aBtnRemove.Enable();
594 if(!bSaved)
596 bSaved=TRUE;
597 SAVE_DATA()
599 NameSelectHdl( 0 );
601 else
603 if ( aBtnAdd.GetText() != aStrAdd )
604 aBtnAdd.SetText( aStrAdd );
605 aBtnRemove.Disable();
607 bSaved=FALSE;
608 RESTORE_DATA()
610 theSymbol = aEdAssign.GetText();
612 if ( theSymbol.Len() > 0 )
613 aBtnAdd.Enable();
614 else
615 aBtnAdd.Disable();
617 aFlAssign.Enable();
618 aEdAssign.Enable();
619 aRbAssign.Enable();
620 //@BugID 54702 raus mit dem Sch.
621 //SFX_APPWINDOW->Enable();
623 UpdateChecks();
624 theCurSel = Selection( 0, SELECTION_MAX );
626 else if ( pEd == &aEdAssign )
628 if ( (theName.Len()>0) && (theSymbol.Len()>0) )
630 aBtnAdd.Enable();
631 if ( bNameFound )
632 aBtnRemove.Enable();
634 else
636 aBtnAdd.Disable();
637 aBtnRemove.Disable();
640 return 0;
643 //------------------------------------------------------------------------
645 IMPL_LINK_INLINE_START( ScNameDlg, AssignGetFocusHdl, void *, EMPTYARG )
647 EdModifyHdl( &aEdAssign );
648 return 0;
650 IMPL_LINK_INLINE_END( ScNameDlg, AssignGetFocusHdl, void *, EMPTYARG )