1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/string.hxx>
21 #include <vcl/msgbox.hxx>
23 #include "reffact.hxx"
24 #include "document.hxx"
25 #include "scresid.hxx"
26 #include "globstr.hrc"
27 #include "dbnamdlg.hrc"
28 #include "rangenam.hxx" // IsNameValid
29 #include "globalnames.hxx"
32 #include "dbnamdlg.hxx"
36 //============================================================================
38 #define ABS_SREF SCA_VALID \
39 | SCA_COL_ABSOLUTE | SCA_ROW_ABSOLUTE | SCA_TAB_ABSOLUTE
40 #define ABS_DREF ABS_SREF \
41 | SCA_COL2_ABSOLUTE | SCA_ROW2_ABSOLUTE | SCA_TAB2_ABSOLUTE
42 #define ABS_DREF3D ABS_DREF | SCA_TAB_3D
44 //----------------------------------------------------------------------------
48 static DBSaveData
* pSaveObj
= NULL
;
50 #define ERRORBOX(s) ErrorBox(this,WinBits(WB_OK|WB_DEF_OK),s).Execute()
52 //============================================================================
58 DBSaveData( Edit
& rEd
, CheckBox
& rHdr
, CheckBox
& rSize
, CheckBox
& rFmt
,
59 CheckBox
& rStrip
, ScRange
& rArea
)
61 rBtnHeader(rHdr
), rBtnSize(rSize
), rBtnFormat(rFmt
), rBtnStrip(rStrip
),
63 bHeader(false), bSize(false), bFormat(false), bDirty(false) {}
85 //----------------------------------------------------------------------------
87 void DBSaveData::Save()
90 aStr
= rEdAssign
.GetText();
91 bHeader
= rBtnHeader
.IsChecked();
92 bSize
= rBtnSize
.IsChecked();
93 bFormat
= rBtnFormat
.IsChecked();
94 bStrip
= rBtnStrip
.IsChecked();
99 //----------------------------------------------------------------------------
101 void DBSaveData::Restore()
106 rEdAssign
.SetText( aStr
);
107 rBtnHeader
.Check ( bHeader
);
108 rBtnSize
.Check ( bSize
);
109 rBtnFormat
.Check ( bFormat
);
110 rBtnStrip
.Check ( bStrip
);
116 //============================================================================
119 //----------------------------------------------------------------------------
121 ScDbNameDlg::ScDbNameDlg( SfxBindings
* pB
, SfxChildWindow
* pCW
, Window
* pParent
,
122 ScViewData
* ptrViewData
)
124 : ScAnyRefDlg ( pB
, pCW
, pParent
, RID_SCDLG_DBNAMES
),
126 aFlName ( this, ScResId( FL_NAME
) ),
127 aEdName ( this, ScResId( ED_NAME
) ),
129 aFlAssign ( this, ScResId( FL_ASSIGN
) ),
130 aEdAssign ( this, this, &aFlAssign
, ScResId( ED_DBAREA
) ),
131 aRbAssign ( this, ScResId( RB_DBAREA
), &aEdAssign
, this ),
133 aFlOptions ( this, ScResId( FL_OPTIONS
) ),
134 aBtnHeader ( this, ScResId( BTN_HEADER
) ),
135 aBtnDoSize ( this, ScResId( BTN_SIZE
) ),
136 aBtnKeepFmt ( this, ScResId( BTN_FORMAT
) ),
137 aBtnStripData ( this, ScResId( BTN_STRIPDATA
) ),
138 aFTSource ( this, ScResId( FT_SOURCE
) ),
139 aFTOperations ( this, ScResId( FT_OPERATIONS
) ),
141 aBtnOk ( this, ScResId( BTN_OK
) ),
142 aBtnCancel ( this, ScResId( BTN_CANCEL
) ),
143 aBtnHelp ( this, ScResId( BTN_HELP
) ),
144 aBtnAdd ( this, ScResId( BTN_ADD
) ),
145 aBtnRemove ( this, ScResId( BTN_REMOVE
) ),
146 aBtnMore ( this, ScResId( BTN_MORE
) ),
148 aStrAdd ( ScResId( STR_ADD
) ),
149 aStrModify ( ScResId( STR_MODIFY
) ),
150 aStrInvalid ( ScResId( STR_DB_INVALID
) ),
152 pViewData ( ptrViewData
),
153 pDoc ( ptrViewData
->GetDocument() ),
154 bRefInputMode ( false ),
155 aAddrDetails ( pDoc
->GetAddressConvention(), 0, 0 ),
156 aLocalDbCol ( *(pDoc
->GetDBCollection()) )
158 // WB_NOLABEL can't be set in resource...
159 aFTSource
.SetStyle( aFTSource
.GetStyle() | WB_NOLABEL
);
160 aFTOperations
.SetStyle( aFTOperations
.GetStyle() | WB_NOLABEL
);
162 // damit die Strings in der Resource bei den FixedTexten bleiben koennen:
163 aStrSource
= aFTSource
.GetText();
164 aStrOperations
= aFTOperations
.GetText();
166 pSaveObj
= new DBSaveData( aEdAssign
, aBtnHeader
,
167 aBtnDoSize
, aBtnKeepFmt
, aBtnStripData
, theCurArea
);
170 aRbAssign
.SetAccessibleRelationMemberOf(&aFlAssign
);
174 //----------------------------------------------------------------------------
176 ScDbNameDlg::~ScDbNameDlg()
182 //----------------------------------------------------------------------------
184 void ScDbNameDlg::Init()
186 aBtnHeader
.Check( sal_True
); // Default: mit Spaltenkoepfen
188 aBtnMore
.AddWindow( &aFlOptions
);
189 aBtnMore
.AddWindow( &aBtnHeader
);
190 aBtnMore
.AddWindow( &aBtnDoSize
);
191 aBtnMore
.AddWindow( &aBtnKeepFmt
);
192 aBtnMore
.AddWindow( &aBtnStripData
);
193 aBtnMore
.AddWindow( &aFTSource
);
194 aBtnMore
.AddWindow( &aFTOperations
);
196 aBtnOk
.SetClickHdl ( LINK( this, ScDbNameDlg
, OkBtnHdl
) );
197 aBtnCancel
.SetClickHdl ( LINK( this, ScDbNameDlg
, CancelBtnHdl
) );
198 aBtnAdd
.SetClickHdl ( LINK( this, ScDbNameDlg
, AddBtnHdl
) );
199 aBtnRemove
.SetClickHdl ( LINK( this, ScDbNameDlg
, RemoveBtnHdl
) );
200 aEdName
.SetModifyHdl ( LINK( this, ScDbNameDlg
, NameModifyHdl
) );
201 aEdAssign
.SetModifyHdl ( LINK( this, ScDbNameDlg
, AssModifyHdl
) );
206 if ( pViewData
&& pDoc
)
215 ScDBCollection
* pDBColl
= pDoc
->GetDBCollection();
216 ScDBData
* pDBData
= NULL
;
218 pViewData
->GetSimpleArea( nStartCol
, nStartRow
, nStartTab
,
219 nEndCol
, nEndRow
, nEndTab
);
221 theCurArea
= ScRange( ScAddress( nStartCol
, nStartRow
, nStartTab
),
222 ScAddress( nEndCol
, nEndRow
, nEndTab
) );
224 theCurArea
.Format( theAreaStr
, ABS_DREF3D
, pDoc
, aAddrDetails
);
228 // Feststellen, ob definierter DB-Bereich markiert wurde:
229 pDBData
= pDBColl
->GetDBAtCursor( nStartCol
, nStartRow
, nStartTab
, sal_True
);
232 ScAddress
& rStart
= theCurArea
.aStart
;
233 ScAddress
& rEnd
= theCurArea
.aEnd
;
240 pDBData
->GetArea( nTab
, nCol1
, nRow1
, nCol2
, nRow2
);
242 if ( (rStart
.Tab() == nTab
)
243 && (rStart
.Col() == nCol1
) && (rStart
.Row() == nRow1
)
244 && (rEnd
.Col() == nCol2
) && (rEnd
.Row() == nRow2
) )
246 OUString aDBName
= pDBData
->GetName();
247 if ( aDBName
!= STR_DB_LOCAL_NONAME
)
248 aEdName
.SetText(aDBName
);
250 aBtnHeader
.Check( pDBData
->HasHeader() );
251 aBtnDoSize
.Check( pDBData
->IsDoSize() );
252 aBtnKeepFmt
.Check( pDBData
->IsKeepFmt() );
253 aBtnStripData
.Check( pDBData
->IsStripData() );
254 SetInfoStrings( pDBData
);
260 aEdAssign
.SetText( theAreaStr
);
268 void ScDbNameDlg::SetInfoStrings( const ScDBData
* pDBData
)
271 aBuf
.append(aStrSource
);
274 aBuf
.append(sal_Unicode(' '));
275 aBuf
.append(pDBData
->GetSourceString());
277 aFTSource
.SetText(aBuf
.makeStringAndClear());
279 aBuf
.append(aStrOperations
);
282 aBuf
.append(sal_Unicode(' '));
283 aBuf
.append(pDBData
->GetOperations());
285 aFTOperations
.SetText(aBuf
.makeStringAndClear());
288 //----------------------------------------------------------------------------
289 // Uebergabe eines mit der Maus selektierten Tabellenbereiches, der dann als
290 // neue Selektion im Referenz-Fenster angezeigt wird.
292 void ScDbNameDlg::SetReference( const ScRange
& rRef
, ScDocument
* pDocP
)
294 if ( aEdAssign
.IsEnabled() )
296 if ( rRef
.aStart
!= rRef
.aEnd
)
297 RefInputStart( &aEdAssign
);
302 theCurArea
.Format( aRefStr
, ABS_DREF3D
, pDocP
, aAddrDetails
);
303 aEdAssign
.SetRefString( aRefStr
);
306 aBtnKeepFmt
.Enable();
307 aBtnStripData
.Enable();
309 aFTOperations
.Enable();
317 //----------------------------------------------------------------------------
319 sal_Bool
ScDbNameDlg::Close()
321 return DoClose( ScDbNameDlgWrapper::GetChildWindowId() );
324 //------------------------------------------------------------------------
326 void ScDbNameDlg::SetActive()
328 aEdAssign
.GrabFocus();
330 // kein NameModifyHdl, weil sonst Bereiche nicht geaendert werden koennen
331 // (nach dem Aufziehen der Referenz wuerde der alte Inhalt wieder angezeigt)
332 // (der ausgewaehlte DB-Name hat sich auch nicht veraendert)
337 //------------------------------------------------------------------------
339 void ScDbNameDlg::UpdateNames()
341 typedef ScDBCollection::NamedDBs DBsType
;
343 const DBsType
& rDBs
= aLocalDbCol
.getNamedDBs();
345 aEdName
.SetUpdateMode( false );
346 //-----------------------------------------------------------
348 aEdAssign
.SetText( EMPTY_STRING
);
352 DBsType::const_iterator itr
= rDBs
.begin(), itrEnd
= rDBs
.end();
353 for (; itr
!= itrEnd
; ++itr
)
354 aEdName
.InsertEntry(itr
->GetName());
358 aBtnAdd
.SetText( aStrAdd
);
360 aBtnRemove
.Disable();
362 //-----------------------------------------------------------
363 aEdName
.SetUpdateMode( sal_True
);
364 aEdName
.Invalidate();
367 //------------------------------------------------------------------------
369 void ScDbNameDlg::UpdateDBData( const String
& rStrName
)
372 const ScDBData
* pData
= aLocalDbCol
.getNamedDBs().findByUpperName(ScGlobal::pCharClass
->uppercase(rStrName
));
382 pData
->GetArea( nTab
, nColStart
, nRowStart
, nColEnd
, nRowEnd
);
383 theCurArea
= ScRange( ScAddress( nColStart
, nRowStart
, nTab
),
384 ScAddress( nColEnd
, nRowEnd
, nTab
) );
386 theCurArea
.Format( theArea
, ABS_DREF3D
, pDoc
, aAddrDetails
);
387 aEdAssign
.SetText( theArea
);
388 aBtnAdd
.SetText( aStrModify
);
389 aBtnHeader
.Check( pData
->HasHeader() );
390 aBtnDoSize
.Check( pData
->IsDoSize() );
391 aBtnKeepFmt
.Check( pData
->IsKeepFmt() );
392 aBtnStripData
.Check( pData
->IsStripData() );
393 SetInfoStrings( pData
);
396 aBtnAdd
.SetText( aStrModify
);
401 aBtnKeepFmt
.Enable();
402 aBtnStripData
.Enable();
404 aFTOperations
.Enable();
407 //------------------------------------------------------------------------
410 sal_Bool
ScDbNameDlg::IsRefInputMode() const
412 return bRefInputMode
;
415 //------------------------------------------------------------------------
419 IMPL_LINK_NOARG(ScDbNameDlg
, OkBtnHdl
)
423 // Der View die Aenderungen und die Remove-Liste uebergeben:
424 // beide werden nur als Referenz uebergeben, so dass an dieser
425 // Stelle keine Speicherleichen entstehen koennen:
427 pViewData
->GetView()->
428 NotifyCloseDbNameDlg( aLocalDbCol
, aRemoveList
);
434 //------------------------------------------------------------------------
436 IMPL_LINK_NOARG_INLINE_START(ScDbNameDlg
, CancelBtnHdl
)
441 IMPL_LINK_NOARG_INLINE_END(ScDbNameDlg
, CancelBtnHdl
)
443 //------------------------------------------------------------------------
445 IMPL_LINK_NOARG(ScDbNameDlg
, AddBtnHdl
)
447 String aNewName
= comphelper::string::strip(aEdName
.GetText(), ' ');
448 String aNewArea
= aEdAssign
.GetText();
450 if ( aNewName
.Len() > 0 && aNewArea
.Len() > 0 )
452 if ( ScRangeData::IsNameValid( aNewName
, pDoc
) && !aNewName
.EqualsAscii(STR_DB_LOCAL_NONAME
) )
454 // weil jetzt editiert werden kann, muss erst geparst werden
456 String aText
= aEdAssign
.GetText();
457 if ( aTmpRange
.ParseAny( aText
, pDoc
, aAddrDetails
) & SCA_VALID
)
459 theCurArea
= aTmpRange
;
460 ScAddress aStart
= theCurArea
.aStart
;
461 ScAddress aEnd
= theCurArea
.aEnd
;
463 ScDBData
* pOldEntry
= aLocalDbCol
.getNamedDBs().findByUpperName(ScGlobal::pCharClass
->uppercase(aNewName
));
466 // Bereich veraendern
468 pOldEntry
->MoveTo( aStart
.Tab(), aStart
.Col(), aStart
.Row(),
469 aEnd
.Col(), aEnd
.Row() );
470 pOldEntry
->SetByRow( sal_True
);
471 pOldEntry
->SetHeader( aBtnHeader
.IsChecked() );
472 pOldEntry
->SetDoSize( aBtnDoSize
.IsChecked() );
473 pOldEntry
->SetKeepFmt( aBtnKeepFmt
.IsChecked() );
474 pOldEntry
->SetStripData( aBtnStripData
.IsChecked() );
478 // neuen Bereich einfuegen
480 ScDBData
* pNewEntry
= new ScDBData( aNewName
, aStart
.Tab(),
481 aStart
.Col(), aStart
.Row(),
482 aEnd
.Col(), aEnd
.Row(),
483 sal_True
, aBtnHeader
.IsChecked() );
484 pNewEntry
->SetDoSize( aBtnDoSize
.IsChecked() );
485 pNewEntry
->SetKeepFmt( aBtnKeepFmt
.IsChecked() );
486 pNewEntry
->SetStripData( aBtnStripData
.IsChecked() );
488 aLocalDbCol
.getNamedDBs().insert(pNewEntry
);
493 aEdName
.SetText( EMPTY_STRING
);
495 aBtnAdd
.SetText( aStrAdd
);
497 aBtnRemove
.Disable();
498 aEdAssign
.SetText( EMPTY_STRING
);
499 aBtnHeader
.Check( sal_True
); // Default: mit Spaltenkoepfen
500 aBtnDoSize
.Check( false );
501 aBtnKeepFmt
.Check( false );
502 aBtnStripData
.Check( false );
503 SetInfoStrings( NULL
); // leer
504 theCurArea
= ScRange();
511 ERRORBOX( aStrInvalid
);
512 aEdAssign
.SetSelection( Selection( 0, SELECTION_MAX
) );
513 aEdAssign
.GrabFocus();
518 ERRORBOX( ScGlobal::GetRscString(STR_INVALIDNAME
) );
519 aEdName
.SetSelection( Selection( 0, SELECTION_MAX
) );
528 class FindByName
: public ::std::unary_function
<ScDBData
, bool>
530 const OUString
& mrName
;
532 FindByName(const OUString
& rName
) : mrName(rName
) {}
533 bool operator() (const ScDBData
& r
) const
535 return r
.GetName().equals(mrName
);
541 IMPL_LINK_NOARG(ScDbNameDlg
, RemoveBtnHdl
)
543 OUString aStrEntry
= aEdName
.GetText();
544 ScDBCollection::NamedDBs
& rDBs
= aLocalDbCol
.getNamedDBs();
545 ScDBCollection::NamedDBs::iterator itr
=
546 ::std::find_if(rDBs
.begin(), rDBs
.end(), FindByName(aStrEntry
));
548 if (itr
!= rDBs
.end())
550 String aStrDelMsg
= ScGlobal::GetRscString( STR_QUERY_DELENTRY
);
553 aBuf
.append(aStrDelMsg
.GetToken(0, '#'));
554 aBuf
.append(aStrEntry
);
555 aBuf
.append(aStrDelMsg
.GetToken(1, '#'));
556 QueryBox
aBox(this, WinBits(WB_YES_NO
|WB_DEF_YES
), aBuf
.makeStringAndClear());
558 if (RET_YES
== aBox
.Execute())
561 SCCOL nColStart
, nColEnd
;
562 SCROW nRowStart
, nRowEnd
;
563 itr
->GetArea( nTab
, nColStart
, nRowStart
, nColEnd
, nRowEnd
);
564 aRemoveList
.push_back(
565 ScRange( ScAddress( nColStart
, nRowStart
, nTab
),
566 ScAddress( nColEnd
, nRowEnd
, nTab
) ) );
572 aEdName
.SetText( EMPTY_STRING
);
574 aBtnAdd
.SetText( aStrAdd
);
576 aBtnRemove
.Disable();
577 aEdAssign
.SetText( EMPTY_STRING
);
578 theCurArea
= ScRange();
579 aBtnHeader
.Check( sal_True
); // Default: mit Spaltenkoepfen
580 aBtnDoSize
.Check( false );
581 aBtnKeepFmt
.Check( false );
582 aBtnStripData
.Check( false );
583 SetInfoStrings( NULL
); // leer
592 //------------------------------------------------------------------------
594 IMPL_LINK_NOARG(ScDbNameDlg
, NameModifyHdl
)
596 OUString theName
= aEdName
.GetText();
597 sal_Bool bNameFound
= (COMBOBOX_ENTRY_NOTFOUND
598 != aEdName
.GetEntryPos( theName
));
600 if ( theName
.isEmpty() )
602 if (aBtnAdd
.GetText() != aStrAdd
)
603 aBtnAdd
.SetText( aStrAdd
);
605 aBtnRemove
.Disable();
606 aFlAssign
.Disable();
607 aBtnHeader
.Disable();
608 aBtnDoSize
.Disable();
609 aBtnKeepFmt
.Disable();
610 aBtnStripData
.Disable();
611 aFTSource
.Disable();
612 aFTOperations
.Disable();
613 aEdAssign
.Disable();
614 aRbAssign
.Disable();
616 //pSaveObj->Restore();
617 //@BugID 54702 Enablen/Disablen nur noch in Basisklasse
618 //SFX_APPWINDOW->Disable(sal_False); //! allgemeine Methode im ScAnyRefDlg
619 bRefInputMode
= false;
625 if (aBtnAdd
.GetText() != aStrModify
)
626 aBtnAdd
.SetText( aStrModify
);
633 UpdateDBData( theName
);
637 if (aBtnAdd
.GetText() != aStrAdd
)
638 aBtnAdd
.SetText( aStrAdd
);
643 if ( !aEdAssign
.GetText().isEmpty() )
648 aBtnKeepFmt
.Enable();
649 aBtnStripData
.Enable();
651 aFTOperations
.Enable();
656 aBtnHeader
.Disable();
657 aBtnDoSize
.Disable();
658 aBtnKeepFmt
.Disable();
659 aBtnStripData
.Disable();
661 aFTOperations
.Disable();
663 aBtnRemove
.Disable();
670 //@BugID 54702 Enablen/Disablen nur noch in Basisklasse
671 //SFX_APPWINDOW->Enable();
672 bRefInputMode
= true;
677 //------------------------------------------------------------------------
679 IMPL_LINK_NOARG(ScDbNameDlg
, AssModifyHdl
)
681 // hier parsen fuer Save() etc.
684 String aText
= aEdAssign
.GetText();
685 if ( aTmpRange
.ParseAny( aText
, pDoc
, aAddrDetails
) & SCA_VALID
)
686 theCurArea
= aTmpRange
;
692 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */