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: dbnamdlg.cxx,v $
10 * $Revision: 1.9.30.2 $
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 <vcl/msgbox.hxx>
42 #include "reffact.hxx"
43 #include "document.hxx"
44 #include "scresid.hxx"
45 #include "globstr.hrc"
46 #include "dbnamdlg.hrc"
47 #include "rangenam.hxx" // IsNameValid
50 #include "dbnamdlg.hxx"
54 //============================================================================
56 #define ABS_SREF SCA_VALID \
57 | SCA_COL_ABSOLUTE | SCA_ROW_ABSOLUTE | SCA_TAB_ABSOLUTE
58 #define ABS_DREF ABS_SREF \
59 | SCA_COL2_ABSOLUTE | SCA_ROW2_ABSOLUTE | SCA_TAB2_ABSOLUTE
60 #define ABS_SREF3D ABS_SREF | SCA_TAB_3D
61 #define ABS_DREF3D ABS_DREF | SCA_TAB_3D
63 //----------------------------------------------------------------------------
67 static DBSaveData
* pSaveObj
= NULL
;
69 #define ERRORBOX(s) ErrorBox(this,WinBits(WB_OK|WB_DEF_OK),s).Execute()
70 #define QUERYBOX(m) QueryBox(this,WinBits(WB_YES_NO|WB_DEF_YES),m).Execute()
72 //============================================================================
78 DBSaveData( Edit
& rEd
, CheckBox
& rHdr
, CheckBox
& rSize
, CheckBox
& rFmt
,
79 CheckBox
& rStrip
, ScRange
& rArea
)
81 rBtnHeader(rHdr
), rBtnSize(rSize
), rBtnFormat(rFmt
), rBtnStrip(rStrip
),
83 bHeader(FALSE
), bSize(FALSE
), bFormat(FALSE
), bDirty(FALSE
) {}
105 //----------------------------------------------------------------------------
107 void DBSaveData::Save()
110 aStr
= rEdAssign
.GetText();
111 bHeader
= rBtnHeader
.IsChecked();
112 bSize
= rBtnSize
.IsChecked();
113 bFormat
= rBtnFormat
.IsChecked();
114 bStrip
= rBtnStrip
.IsChecked();
119 //----------------------------------------------------------------------------
121 void DBSaveData::Restore()
126 rEdAssign
.SetText( aStr
);
127 rBtnHeader
.Check ( bHeader
);
128 rBtnSize
.Check ( bSize
);
129 rBtnFormat
.Check ( bFormat
);
130 rBtnStrip
.Check ( bStrip
);
136 //============================================================================
139 //----------------------------------------------------------------------------
141 ScDbNameDlg::ScDbNameDlg( SfxBindings
* pB
, SfxChildWindow
* pCW
, Window
* pParent
,
142 ScViewData
* ptrViewData
)
144 : ScAnyRefDlg ( pB
, pCW
, pParent
, RID_SCDLG_DBNAMES
),
146 aFlName ( this, ScResId( FL_NAME
) ),
147 aEdName ( this, ScResId( ED_NAME
) ),
149 aFlAssign ( this, ScResId( FL_ASSIGN
) ),
150 aEdAssign ( this, this, ScResId( ED_DBAREA
) ),
151 aRbAssign ( this, ScResId( RB_DBAREA
), &aEdAssign
, this ),
153 aFlOptions ( this, ScResId( FL_OPTIONS
) ),
154 aBtnHeader ( this, ScResId( BTN_HEADER
) ),
155 aBtnDoSize ( this, ScResId( BTN_SIZE
) ),
156 aBtnKeepFmt ( this, ScResId( BTN_FORMAT
) ),
157 aBtnStripData ( this, ScResId( BTN_STRIPDATA
) ),
158 aFTSource ( this, ScResId( FT_SOURCE
) ),
159 aFTOperations ( this, ScResId( FT_OPERATIONS
) ),
161 aBtnOk ( this, ScResId( BTN_OK
) ),
162 aBtnCancel ( this, ScResId( BTN_CANCEL
) ),
163 aBtnHelp ( this, ScResId( BTN_HELP
) ),
164 aBtnAdd ( this, ScResId( BTN_ADD
) ),
165 aBtnRemove ( this, ScResId( BTN_REMOVE
) ),
166 aBtnMore ( this, ScResId( BTN_MORE
) ),
168 aStrAdd ( ScResId( STR_ADD
) ),
169 aStrModify ( ScResId( STR_MODIFY
) ),
170 aStrNoName ( ScGlobal::GetRscString(STR_DB_NONAME
) ),
171 aStrInvalid ( ScResId( STR_DB_INVALID
) ),
173 pViewData ( ptrViewData
),
174 pDoc ( ptrViewData
->GetDocument() ),
175 bRefInputMode ( FALSE
),
176 aAddrDetails ( pDoc
->GetAddressConvention(), 0, 0 ),
177 aLocalDbCol ( *(pDoc
->GetDBCollection()) )
179 // WB_NOLABEL can't be set in resource...
180 aFTSource
.SetStyle( aFTSource
.GetStyle() | WB_NOLABEL
);
181 aFTOperations
.SetStyle( aFTOperations
.GetStyle() | WB_NOLABEL
);
183 // damit die Strings in der Resource bei den FixedTexten bleiben koennen:
184 aStrSource
= aFTSource
.GetText();
185 aStrOperations
= aFTOperations
.GetText();
187 pSaveObj
= new DBSaveData( aEdAssign
, aBtnHeader
,
188 aBtnDoSize
, aBtnKeepFmt
, aBtnStripData
, theCurArea
);
194 //----------------------------------------------------------------------------
196 __EXPORT
ScDbNameDlg::~ScDbNameDlg()
200 ScRange
* pEntry
= (ScRange
*)aRemoveList
.First();
203 aRemoveList
.Remove( pEntry
);
205 pEntry
= (ScRange
*)aRemoveList
.Next();
210 //----------------------------------------------------------------------------
212 void ScDbNameDlg::Init()
214 aBtnHeader
.Check( TRUE
); // Default: mit Spaltenkoepfen
216 aBtnMore
.AddWindow( &aFlOptions
);
217 aBtnMore
.AddWindow( &aBtnHeader
);
218 aBtnMore
.AddWindow( &aBtnDoSize
);
219 aBtnMore
.AddWindow( &aBtnKeepFmt
);
220 aBtnMore
.AddWindow( &aBtnStripData
);
221 aBtnMore
.AddWindow( &aFTSource
);
222 aBtnMore
.AddWindow( &aFTOperations
);
232 aBtnOk
.SetClickHdl ( LINK( this, ScDbNameDlg
, OkBtnHdl
) );
233 aBtnCancel
.SetClickHdl ( LINK( this, ScDbNameDlg
, CancelBtnHdl
) );
234 aBtnAdd
.SetClickHdl ( LINK( this, ScDbNameDlg
, AddBtnHdl
) );
235 aBtnRemove
.SetClickHdl ( LINK( this, ScDbNameDlg
, RemoveBtnHdl
) );
236 aEdName
.SetModifyHdl ( LINK( this, ScDbNameDlg
, NameModifyHdl
) );
237 aEdAssign
.SetModifyHdl ( LINK( this, ScDbNameDlg
, AssModifyHdl
) );
240 if ( pViewData
&& pDoc
)
242 ScDBCollection
* pDBColl
= pDoc
->GetDBCollection();
243 ScDBData
* pDBData
= NULL
;
245 pViewData
->GetSimpleArea( nStartCol
, nStartRow
, nStartTab
,
246 nEndCol
, nEndRow
, nEndTab
);
248 theCurArea
= ScRange( ScAddress( nStartCol
, nStartRow
, nStartTab
),
249 ScAddress( nEndCol
, nEndRow
, nEndTab
) );
251 theCurArea
.Format( theAreaStr
, ABS_DREF3D
, pDoc
, aAddrDetails
);
255 // Feststellen, ob definierter DB-Bereich markiert wurde:
256 pDBData
= pDBColl
->GetDBAtCursor( nStartCol
, nStartRow
, nStartTab
, TRUE
);
260 ScAddress
& rStart
= theCurArea
.aStart
;
261 ScAddress
& rEnd
= theCurArea
.aEnd
;
268 pDBData
->GetArea( nTab
, nCol1
, nRow1
, nCol2
, nRow2
);
270 if ( (rStart
.Tab() == nTab
)
271 && (rStart
.Col() == nCol1
) && (rStart
.Row() == nRow1
)
272 && (rEnd
.Col() == nCol2
) && (rEnd
.Row() == nRow2
) )
274 pDBData
->GetName( theDbName
);
275 if ( theDbName
!= aStrNoName
)
276 aEdName
.SetText( theDbName
);
278 aEdName
.SetText( EMPTY_STRING
);
279 aBtnHeader
.Check( pDBData
->HasHeader() );
280 aBtnDoSize
.Check( pDBData
->IsDoSize() );
281 aBtnKeepFmt
.Check( pDBData
->IsKeepFmt() );
282 aBtnStripData
.Check( pDBData
->IsStripData() );
283 SetInfoStrings( pDBData
);
289 aEdAssign
.SetText( theAreaStr
);
297 void ScDbNameDlg::SetInfoStrings( const ScDBData
* pDBData
)
299 String aSource
= aStrSource
;
303 aSource
+= pDBData
->GetSourceString();
305 aFTSource
.SetText( aSource
);
307 String aOper
= aStrOperations
;
311 aOper
+= pDBData
->GetOperations();
313 aFTOperations
.SetText( aOper
);
316 //----------------------------------------------------------------------------
317 // Uebergabe eines mit der Maus selektierten Tabellenbereiches, der dann als
318 // neue Selektion im Referenz-Fenster angezeigt wird.
320 void ScDbNameDlg::SetReference( const ScRange
& rRef
, ScDocument
* pDocP
)
322 if ( aEdAssign
.IsEnabled() )
324 if ( rRef
.aStart
!= rRef
.aEnd
)
325 RefInputStart( &aEdAssign
);
330 theCurArea
.Format( aRefStr
, ABS_DREF3D
, pDocP
, aAddrDetails
);
331 aEdAssign
.SetRefString( aRefStr
);
334 aBtnKeepFmt
.Enable();
335 aBtnStripData
.Enable();
337 aFTOperations
.Enable();
345 //----------------------------------------------------------------------------
347 BOOL __EXPORT
ScDbNameDlg::Close()
349 return DoClose( ScDbNameDlgWrapper::GetChildWindowId() );
352 //------------------------------------------------------------------------
354 void ScDbNameDlg::SetActive()
356 aEdAssign
.GrabFocus();
358 // kein NameModifyHdl, weil sonst Bereiche nicht geaendert werden koennen
359 // (nach dem Aufziehen der Referenz wuerde der alte Inhalt wieder angezeigt)
360 // (der ausgewaehlte DB-Name hat sich auch nicht veraendert)
365 //------------------------------------------------------------------------
367 void ScDbNameDlg::UpdateNames()
369 USHORT nNameCount
= aLocalDbCol
.GetCount();
371 aEdName
.SetUpdateMode( FALSE
);
372 //-----------------------------------------------------------
374 aEdAssign
.SetText( EMPTY_STRING
);
376 if ( nNameCount
> 0 )
378 ScDBData
* pDbData
= NULL
;
381 for ( USHORT i
=0; i
<nNameCount
; i
++ )
383 pDbData
= (ScDBData
*)(aLocalDbCol
.At( i
));
386 pDbData
->GetName( aString
);
387 if ( aString
!= aStrNoName
)
388 aEdName
.InsertEntry( aString
);
394 aBtnAdd
.SetText( aStrAdd
);
396 aBtnRemove
.Disable();
398 //-----------------------------------------------------------
399 aEdName
.SetUpdateMode( TRUE
);
400 aEdName
.Invalidate();
403 //------------------------------------------------------------------------
405 void ScDbNameDlg::UpdateDBData( const String
& rStrName
)
411 aLocalDbCol
.SearchName( rStrName
, nAt
);
412 pData
= (ScDBData
*)(aLocalDbCol
.At( nAt
));
422 pData
->GetArea( nTab
, nColStart
, nRowStart
, nColEnd
, nRowEnd
);
423 theCurArea
= ScRange( ScAddress( nColStart
, nRowStart
, nTab
),
424 ScAddress( nColEnd
, nRowEnd
, nTab
) );
425 theCurArea
.Format( theArea
, ABS_DREF3D
, pDoc
, aAddrDetails
);
426 aEdAssign
.SetText( theArea
);
427 aBtnAdd
.SetText( aStrModify
);
428 aBtnHeader
.Check( pData
->HasHeader() );
429 aBtnDoSize
.Check( pData
->IsDoSize() );
430 aBtnKeepFmt
.Check( pData
->IsKeepFmt() );
431 aBtnStripData
.Check( pData
->IsStripData() );
432 SetInfoStrings( pData
);
435 aBtnAdd
.SetText( aStrModify
);
440 aBtnKeepFmt
.Enable();
441 aBtnStripData
.Enable();
443 aFTOperations
.Enable();
446 //------------------------------------------------------------------------
449 BOOL
ScDbNameDlg::IsRefInputMode() const
451 return bRefInputMode
;
454 //------------------------------------------------------------------------
458 IMPL_LINK( ScDbNameDlg
, OkBtnHdl
, void *, EMPTYARG
)
462 // Der View die Aenderungen und die Remove-Liste uebergeben:
463 // beide werden nur als Referenz uebergeben, so dass an dieser
464 // Stelle keine Speicherleichen entstehen koennen:
466 pViewData
->GetView()->
467 NotifyCloseDbNameDlg( aLocalDbCol
, aRemoveList
);
473 //------------------------------------------------------------------------
475 IMPL_LINK_INLINE_START( ScDbNameDlg
, CancelBtnHdl
, void *, EMPTYARG
)
480 IMPL_LINK_INLINE_END( ScDbNameDlg
, CancelBtnHdl
, void *, EMPTYARG
)
482 //------------------------------------------------------------------------
484 IMPL_LINK( ScDbNameDlg
, AddBtnHdl
, void *, EMPTYARG
)
486 String aNewName
= aEdName
.GetText();
487 String aNewArea
= aEdAssign
.GetText();
489 aNewName
.EraseLeadingChars( ' ' );
490 aNewName
.EraseTrailingChars( ' ' );
492 if ( aNewName
.Len() > 0 && aNewArea
.Len() > 0 )
494 if ( ScRangeData::IsNameValid( aNewName
, pDoc
) )
496 // weil jetzt editiert werden kann, muss erst geparst werden
498 String aText
= aEdAssign
.GetText();
499 if ( aTmpRange
.ParseAny( aText
, pDoc
, aAddrDetails
) & SCA_VALID
)
501 theCurArea
= aTmpRange
;
502 ScAddress aStart
= theCurArea
.aStart
;
503 ScAddress aEnd
= theCurArea
.aEnd
;
505 ScDBData
* pOldEntry
= NULL
;
507 if ( aLocalDbCol
.SearchName( aNewName
, nFoundAt
) )
508 pOldEntry
= aLocalDbCol
[nFoundAt
];
511 // Bereich veraendern
513 pOldEntry
->MoveTo( aStart
.Tab(), aStart
.Col(), aStart
.Row(),
514 aEnd
.Col(), aEnd
.Row() );
515 pOldEntry
->SetByRow( TRUE
);
516 pOldEntry
->SetHeader( aBtnHeader
.IsChecked() );
517 pOldEntry
->SetDoSize( aBtnDoSize
.IsChecked() );
518 pOldEntry
->SetKeepFmt( aBtnKeepFmt
.IsChecked() );
519 pOldEntry
->SetStripData( aBtnStripData
.IsChecked() );
523 // neuen Bereich einfuegen
525 ScDBData
* pNewEntry
= new ScDBData( aNewName
, aStart
.Tab(),
526 aStart
.Col(), aStart
.Row(),
527 aEnd
.Col(), aEnd
.Row(),
528 TRUE
, aBtnHeader
.IsChecked() );
529 pNewEntry
->SetDoSize( aBtnDoSize
.IsChecked() );
530 pNewEntry
->SetKeepFmt( aBtnKeepFmt
.IsChecked() );
531 pNewEntry
->SetStripData( aBtnStripData
.IsChecked() );
533 if ( !aLocalDbCol
.Insert( pNewEntry
) )
539 aEdName
.SetText( EMPTY_STRING
);
541 aBtnAdd
.SetText( aStrAdd
);
543 aBtnRemove
.Disable();
544 aEdAssign
.SetText( EMPTY_STRING
);
545 aBtnHeader
.Check( TRUE
); // Default: mit Spaltenkoepfen
546 aBtnDoSize
.Check( FALSE
);
547 aBtnKeepFmt
.Check( FALSE
);
548 aBtnStripData
.Check( FALSE
);
549 SetInfoStrings( NULL
); // leer
550 theCurArea
= ScRange();
557 ERRORBOX( aStrInvalid
);
558 aEdAssign
.SetSelection( Selection( 0, SELECTION_MAX
) );
559 aEdAssign
.GrabFocus();
564 ERRORBOX( ScGlobal::GetRscString(STR_INVALIDNAME
) );
565 aEdName
.SetSelection( Selection( 0, SELECTION_MAX
) );
572 //------------------------------------------------------------------------
574 IMPL_LINK( ScDbNameDlg
, RemoveBtnHdl
, void *, EMPTYARG
)
576 USHORT nRemoveAt
= 0;
577 const String aStrEntry
= aEdName
.GetText();
579 if ( aLocalDbCol
.SearchName( aStrEntry
, nRemoveAt
) )
581 String aStrDelMsg
= ScGlobal::GetRscString( STR_QUERY_DELENTRY
);
582 String aMsg
= aStrDelMsg
.GetToken( 0, '#' );
585 aMsg
+= aStrDelMsg
.GetToken( 1, '#' );
587 if ( RET_YES
== QUERYBOX(aMsg
) )
589 ScDBData
* pEntry
= (ScDBData
*)aLocalDbCol
.At(nRemoveAt
);
594 SCCOL nColStart
, nColEnd
;
595 SCROW nRowStart
, nRowEnd
;
596 pEntry
->GetArea( nTab
, nColStart
, nRowStart
, nColEnd
, nRowEnd
);
598 new ScRange( ScAddress( nColStart
, nRowStart
, nTab
),
599 ScAddress( nColEnd
, nRowEnd
, nTab
) ) );
601 aLocalDbCol
.AtFree( nRemoveAt
);
605 aEdName
.SetText( EMPTY_STRING
);
607 aBtnAdd
.SetText( aStrAdd
);
609 aBtnRemove
.Disable();
610 aEdAssign
.SetText( EMPTY_STRING
);
611 theCurArea
= ScRange();
612 aBtnHeader
.Check( TRUE
); // Default: mit Spaltenkoepfen
613 aBtnDoSize
.Check( FALSE
);
614 aBtnKeepFmt
.Check( FALSE
);
615 aBtnStripData
.Check( FALSE
);
616 SetInfoStrings( NULL
); // leer
625 //------------------------------------------------------------------------
627 IMPL_LINK( ScDbNameDlg
, NameModifyHdl
, void *, EMPTYARG
)
629 String theName
= aEdName
.GetText();
630 BOOL bNameFound
= (COMBOBOX_ENTRY_NOTFOUND
631 != aEdName
.GetEntryPos( theName
));
633 if ( theName
.Len() == 0 )
635 if ( aBtnAdd
.GetText() != aStrAdd
)
636 aBtnAdd
.SetText( aStrAdd
);
638 aBtnRemove
.Disable();
639 aFlAssign
.Disable();
640 aBtnHeader
.Disable();
641 aBtnDoSize
.Disable();
642 aBtnKeepFmt
.Disable();
643 aBtnStripData
.Disable();
644 aFTSource
.Disable();
645 aFTOperations
.Disable();
646 aEdAssign
.Disable();
647 aRbAssign
.Disable();
649 //pSaveObj->Restore();
650 //@BugID 54702 Enablen/Disablen nur noch in Basisklasse
651 //SFX_APPWINDOW->Disable(FALSE); //! allgemeine Methode im ScAnyRefDlg
652 bRefInputMode
= FALSE
;
658 if ( aBtnAdd
.GetText() != aStrModify
)
659 aBtnAdd
.SetText( aStrModify
);
666 UpdateDBData( theName
);
670 if ( aBtnAdd
.GetText() != aStrAdd
)
671 aBtnAdd
.SetText( aStrAdd
);
676 if ( aEdAssign
.GetText().Len() > 0 )
681 aBtnKeepFmt
.Enable();
682 aBtnStripData
.Enable();
684 aFTOperations
.Enable();
689 aBtnHeader
.Disable();
690 aBtnDoSize
.Disable();
691 aBtnKeepFmt
.Disable();
692 aBtnStripData
.Disable();
694 aFTOperations
.Disable();
696 aBtnRemove
.Disable();
703 //@BugID 54702 Enablen/Disablen nur noch in Basisklasse
704 //SFX_APPWINDOW->Enable();
705 bRefInputMode
= TRUE
;
710 //------------------------------------------------------------------------
712 IMPL_LINK( ScDbNameDlg
, AssModifyHdl
, void *, EMPTYARG
)
714 // hier parsen fuer Save() etc.
717 String aText
= aEdAssign
.GetText();
718 if ( aTmpRange
.ParseAny( aText
, pDoc
, aAddrDetails
) & SCA_VALID
)
719 theCurArea
= aTmpRange
;