1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: namedlg.cxx,v $
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 -------------------------------------------------------------------
41 #include "reffact.hxx"
42 #include "document.hxx"
43 #include "docfunc.hxx"
44 #include "scresid.hxx"
45 #include "globstr.hrc"
46 #include "namedlg.hrc"
49 #include "namedlg.hxx"
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.
72 : bCriteria(FALSE
),bPrintArea(FALSE
),
73 bColHeader(FALSE
),bRowHeader(FALSE
),
79 bCriteria
= bPrintArea
=
80 bColHeader
= bRowHeader
= FALSE
;
92 static SaveData
* pSaveObj
= NULL
;
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 //============================================================================
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
) ),
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
;
163 //----------------------------------------------------------------------------
165 __EXPORT
ScNameDlg::~ScNameDlg()
171 //----------------------------------------------------------------------------
173 void __EXPORT
ScNameDlg::Init()
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
);
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
);
213 aBtnRemove
.Disable();
214 if ( aEdName
.GetEntryCount() > 0 )
215 aBtnAdd
.SetText( aStrAdd
);
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
);
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();
272 //----------------------------------------------------------------------------
274 void __EXPORT
ScNameDlg::UpdateChecks()
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() )
294 aBtnCriteria
.Enable();
295 aBtnPrintArea
.Enable();
296 aBtnColHeader
.Enable();
297 aBtnRowHeader
.Enable();
303 else if ( aFlType
.IsEnabled() )
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();
328 aEdAssign
.SetText( EMPTY_STRING
);
330 if ( nRangeCount
> 0 )
332 ScRangeData
* pRangeData
= NULL
;
335 for ( USHORT i
=0; i
<nRangeCount
; i
++ )
337 pRangeData
= (ScRangeData
*)(aLocalRangeName
.At( i
));
340 if ( !pRangeData
->HasType( RT_DATABASE
)
341 && !pRangeData
->HasType( RT_SHARED
) )
343 pRangeData
->GetName( aString
);
344 aEdName
.InsertEntry( aString
);
351 aBtnAdd
.SetText( aStrAdd
);
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
));
370 rtl::OUStringBuffer sBuffer
;
371 pRangeData
->UpdateSymbol( sBuffer
, theCursorPos
);
381 //----------------------------------------------------------------------------
385 IMPL_LINK( ScNameDlg
, OkBtnHdl
, void *, EMPTYARG
)
387 if ( aBtnAdd
.IsEnabled() )
390 if ( !aBtnAdd
.IsEnabled() && !aBtnRemove
.IsEnabled() )
392 ScDocShell
* pDocSh
= pViewData
->GetDocShell();
393 ScDocFunc
aFunc(*pDocSh
);
394 aFunc
.ModifyRangeNames( aLocalRangeName
, FALSE
);
401 //----------------------------------------------------------------------------
403 IMPL_LINK_INLINE_START( ScNameDlg
, CancelBtnHdl
, void *, EMPTYARG
)
408 IMPL_LINK_INLINE_END( ScNameDlg
, CancelBtnHdl
, void *, EMPTYARG
)
411 //----------------------------------------------------------------------------
413 IMPL_LINK( ScNameDlg
, AddBtnHdl
, void *, EMPTYARG
)
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
) )
427 ScRangeData
* pNewEntry
= NULL
;
428 RangeType nType
= RT_NAME
;
430 String theSymbol
= aEdAssign
.GetText();
434 pNewEntry
= new ScRangeData( pDoc
,
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
458 ((ScRangeData
*)(aLocalRangeName
.At(nFoundAt
)))->GetIndex() );
459 aLocalRangeName
.AtFree( nFoundAt
);
464 if ( !aLocalRangeName
.Insert( pNewEntry
) )
470 aEdName
.SetText(EMPTY_STRING
);
473 aBtnAdd
.SetText( aStrAdd
);
475 aBtnRemove
.Disable();
477 //@BugID 54702 raus mit dem Sch.
478 //SFX_APPWINDOW->Disable(FALSE); //! allgemeine Methode im ScAnyRefDlg
482 else // theSymbol ungueltig
485 ERRORBOX( errMsgInvalidSym
);
486 theCurSel
= Selection( 0, SELECTION_MAX
);
487 aEdAssign
.GrabFocus();
493 ERRORBOX( ScGlobal::GetRscString(STR_INVALIDNAME
) );
494 aEdName
.SetSelection( Selection( 0, SELECTION_MAX
) );
499 aEdName
.SetTopEntry(nNamePos
);
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, '#' );
517 aMsg
+= aStrDelMsg
.GetToken( 1, '#' );
520 QueryBox( this, WinBits( WB_YES_NO
| WB_DEF_YES
), aMsg
).Execute() )
522 aLocalRangeName
.AtFree( nRemoveAt
);
527 theCurSel
= Selection( 0, SELECTION_MAX
);
528 aBtnAdd
.SetText( aStrAdd
);
530 aBtnRemove
.Disable();
537 //----------------------------------------------------------------------------
539 IMPL_LINK( ScNameDlg
, NameSelectHdl
, void *, EMPTYARG
)
543 if ( aLocalRangeName
.SearchName( aEdName
.GetText(), nAtPos
) )
546 ScRangeData
* pData
= (ScRangeData
*)(aLocalRangeName
.At( nAtPos
));
550 pData
->GetSymbol( aSymbol
);
551 CalcCurTableAssign( aSymbol
, nAtPos
);
552 aEdAssign
.SetText( aSymbol
);
553 aBtnAdd
.SetText( aStrModify
);
554 theCurSel
= Selection( 0, SELECTION_MAX
);
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
);
578 aBtnRemove
.Disable();
582 //@BugID 54702 raus mit dem Sch.
583 //SFX_APPWINDOW->Disable(FALSE); //! allgemeine Methode im ScAnyRefDlg
589 if ( aBtnAdd
.GetText() != aStrModify
)
590 aBtnAdd
.SetText( aStrModify
);
603 if ( aBtnAdd
.GetText() != aStrAdd
)
604 aBtnAdd
.SetText( aStrAdd
);
605 aBtnRemove
.Disable();
610 theSymbol
= aEdAssign
.GetText();
612 if ( theSymbol
.Len() > 0 )
620 //@BugID 54702 raus mit dem Sch.
621 //SFX_APPWINDOW->Enable();
624 theCurSel
= Selection( 0, SELECTION_MAX
);
626 else if ( pEd
== &aEdAssign
)
628 if ( (theName
.Len()>0) && (theSymbol
.Len()>0) )
637 aBtnRemove
.Disable();
643 //------------------------------------------------------------------------
645 IMPL_LINK_INLINE_START( ScNameDlg
, AssignGetFocusHdl
, void *, EMPTYARG
)
647 EdModifyHdl( &aEdAssign
);
650 IMPL_LINK_INLINE_END( ScNameDlg
, AssignGetFocusHdl
, void *, EMPTYARG
)