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: flddb.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_sw.hxx"
33 #ifdef SW_DLLIMPLEMENTATION
34 #undef SW_DLLIMPLEMENTATION
38 #include <swmodule.hxx>
44 #include <globals.hrc>
48 #include <fldtdlg.hxx>
50 #include <numrule.hxx>
53 #include <fldtdlg.hrc>
61 #include <dbconfig.hxx>
66 #define USER_DATA_VERSION_1 "1"
67 #define USER_DATA_VERSION USER_DATA_VERSION_1
68 /*--------------------------------------------------------------------
70 --------------------------------------------------------------------*/
72 SwFldDBPage::SwFldDBPage(Window
* pParent
, const SfxItemSet
& rCoreSet
) :
73 SwFldPage( pParent
, SW_RES( TP_FLD_DB
), rCoreSet
),
74 aTypeFT (this, SW_RES(FT_DBTYPE
)),
75 aTypeLB (this, SW_RES(LB_DBTYPE
)),
76 aSelectionFT(this, SW_RES(FT_DBSELECTION
)),
77 aDatabaseTLB(this, SW_RES(TLB_DBLIST
), 0, aEmptyStr
, FALSE
),
78 aAddDBFT(this, SW_RES(FT_ADDDB
)),
79 aAddDBPB(this, SW_RES(PB_ADDDB
)),
80 aConditionFT(this, SW_RES(FT_DBCONDITION
)),
81 aConditionED(this, SW_RES(ED_DBCONDITION
)),
82 aValueFT (this, SW_RES(FT_DBSETNUMBER
)),
83 aValueED (this, SW_RES(ED_DBSETNUMBER
)),
84 aDBFormatRB (this, SW_RES(RB_DBOWNFORMAT
)),
85 aNewFormatRB(this, SW_RES(RB_DBFORMAT
)),
86 aNumFormatLB(this, SW_RES(LB_DBNUMFORMAT
)),
87 aFormatLB (this, SW_RES(LB_DBFORMAT
)),
88 aFormatFL (this, SW_RES(FL_DBFORMAT
)),
89 aFormatVertFL (this, SW_RES(FL_DBFORMAT_VERT
))
93 aOldNumSelectHdl
= aNumFormatLB
.GetSelectHdl();
95 aNumFormatLB
.SetSelectHdl(LINK(this, SwFldDBPage
, NumSelectHdl
));
96 aDatabaseTLB
.SetSelectHdl(LINK(this, SwFldDBPage
, TreeSelectHdl
));
97 aDatabaseTLB
.SetDoubleClickHdl(LINK(this, SwFldDBPage
, InsertHdl
));
99 aValueED
.SetModifyHdl(LINK(this, SwFldDBPage
, ModifyHdl
));
100 aAddDBPB
.SetClickHdl(LINK(this, SwFldDBPage
, AddDBHdl
));
103 /*--------------------------------------------------------------------
105 --------------------------------------------------------------------*/
107 __EXPORT
SwFldDBPage::~SwFldDBPage()
111 /*--------------------------------------------------------------------
112 Beschreibung: TabPage initialisieren
113 --------------------------------------------------------------------*/
115 void __EXPORT
SwFldDBPage::Reset(const SfxItemSet
&)
117 Init(); // Allgemeine initialisierung
119 aTypeLB
.SetUpdateMode(FALSE
);
120 USHORT nOldPos
= aTypeLB
.GetSelectEntryPos();
121 sOldDBName
= aDatabaseTLB
.GetDBName(sOldTableName
, sOldColumnName
);
125 USHORT nPos
, nTypeId
, i
;
129 // TypeListBox initialisieren
130 const SwFldGroupRgn
& rRg
= GetFldMgr().GetGroupRange(IsFldDlgHtmlMode(), GetGroup());
132 for(i
= rRg
.nStart
; i
< rRg
.nEnd
; ++i
)
134 nTypeId
= GetFldMgr().GetTypeId(i
);
135 nPos
= aTypeLB
.InsertEntry(GetFldMgr().GetTypeStr(i
));
136 aTypeLB
.SetEntryData(nPos
, reinterpret_cast<void*>(nTypeId
));
141 nTypeId
= GetCurField()->GetTypeId();
142 nPos
= aTypeLB
.InsertEntry(GetFldMgr().GetTypeStr(GetFldMgr().GetPos(nTypeId
)));
143 aTypeLB
.SetEntryData(nPos
, reinterpret_cast<void*>(nTypeId
));
146 // alte Pos selektieren
147 if (GetTypeSel() != LISTBOX_ENTRY_NOTFOUND
)
148 aTypeLB
.SelectEntryPos(GetTypeSel());
152 USHORT nSize
= GetFldMgr().GetFormatCount(TYP_DBSETNUMBERFLD
, FALSE
, IsFldDlgHtmlMode());
153 for( i
= 0; i
< nSize
; ++i
)
155 USHORT nEntryPos
= aFormatLB
.InsertEntry(GetFldMgr().GetFormatStr(TYP_DBSETNUMBERFLD
, i
));
156 USHORT nFmtId
= GetFldMgr().GetFormatId( TYP_DBSETNUMBERFLD
, i
);
157 aFormatLB
.SetEntryData( nEntryPos
, reinterpret_cast<void*>(nFmtId
) );
158 if( SVX_NUM_ARABIC
== nFmtId
)
159 aFormatLB
.SelectEntryPos( nEntryPos
);
164 if (nOldPos
!= LISTBOX_ENTRY_NOTFOUND
)
165 aTypeLB
.SelectEntryPos(nOldPos
);
167 if (sOldDBName
.Len())
169 aDatabaseTLB
.Select(sOldDBName
, sOldTableName
, sOldColumnName
);
173 SwWrtShell
*pSh
= GetWrtShell();
175 pSh
= ::GetActiveWrtShell();
178 SwDBData
aTmp(pSh
->GetDBData());
179 aDatabaseTLB
.Select(aTmp
.sDataSource
, aTmp
.sCommand
, aEmptyStr
);
186 String sUserData
= GetUserData();
187 if(sUserData
.GetToken(0, ';').EqualsIgnoreCaseAscii(USER_DATA_VERSION_1
))
189 String sVal
= sUserData
.GetToken(1, ';');
190 USHORT nVal
= (USHORT
)sVal
.ToInt32();
191 if(nVal
!= USHRT_MAX
)
193 for(i
= 0; i
< aTypeLB
.GetEntryCount(); i
++)
194 if(nVal
== (USHORT
)(ULONG
)aTypeLB
.GetEntryData(i
))
196 aTypeLB
.SelectEntryPos(i
);
204 aTypeLB
.SetUpdateMode(TRUE
);
205 aTypeLB
.SetSelectHdl(LINK(this, SwFldDBPage
, TypeHdl
));
206 aTypeLB
.SetDoubleClickHdl(LINK(this, SwFldDBPage
, InsertHdl
));
210 aConditionED
.SaveValue();
211 aValueED
.SaveValue();
212 sOldDBName
= aDatabaseTLB
.GetDBName(sOldTableName
, sOldColumnName
);
213 nOldFormat
= GetCurField()->GetFormat();
214 nOldSubType
= GetCurField()->GetSubType();
218 /*--------------------------------------------------------------------
220 --------------------------------------------------------------------*/
222 BOOL __EXPORT
SwFldDBPage::FillItemSet(SfxItemSet
& )
224 String sTableName
, sColumnName
;
227 aData
.sDataSource
= aDatabaseTLB
.GetDBName(sTableName
, sColumnName
, &bIsTable
);
228 aData
.sCommand
= sTableName
;
229 aData
.nCommandType
= bIsTable
? 0 : 1;
230 SwWrtShell
*pSh
= GetWrtShell();
232 pSh
= ::GetActiveWrtShell();
234 if (!aData
.sDataSource
.getLength())
235 aData
= pSh
->GetDBData();
237 if(aData
.sDataSource
.getLength()) // Ohne Datenbank kein neuer Feldbefehl
239 USHORT nTypeId
= (USHORT
)(ULONG
)aTypeLB
.GetEntryData(GetTypeSel());
240 String
aVal(aValueED
.GetText());
241 String
aName(aConditionED
.GetText());
245 String sDBName
= aData
.sDataSource
;
247 sDBName
+= (String
)aData
.sCommand
;
249 sDBName
+= String::CreateFromInt32(aData
.nCommandType
);
251 if(sColumnName
.Len())
253 sDBName
+= sColumnName
;
256 aName
.Insert(sDBName
, 0);
261 nFormat
= aNumFormatLB
.GetFormat();
262 if (aNewFormatRB
.IsEnabled() && aNewFormatRB
.IsChecked())
263 nSubType
= nsSwExtendedSubType::SUB_OWN_FMT
;
267 case TYP_DBSETNUMBERFLD
:
268 nFormat
= (USHORT
)(ULONG
)aFormatLB
.GetEntryData(
269 aFormatLB
.GetSelectEntryPos() );
274 String sTempDBName
, sTempTableName
, sTempColumnName
;
275 sTempDBName
= aDatabaseTLB
.GetDBName(sTempTableName
, sTempColumnName
);
276 BOOL bDBListBoxChanged
= sOldDBName
!= sTempDBName
||
277 sOldTableName
!= sTempTableName
|| sOldColumnName
!= sTempColumnName
;
279 aConditionED
.GetSavedValue() != aConditionED
.GetText() ||
280 aValueED
.GetSavedValue() != aValueED
.GetText() ||
282 nOldFormat
!= nFormat
|| nOldSubType
!= nSubType
)
284 InsertFld( nTypeId
, nSubType
, aName
, aVal
, nFormat
);
291 /*--------------------------------------------------------------------
293 --------------------------------------------------------------------*/
295 SfxTabPage
* __EXPORT
SwFldDBPage::Create( Window
* pParent
,
296 const SfxItemSet
& rAttrSet
)
298 return ( new SwFldDBPage( pParent
, rAttrSet
) );
301 /*--------------------------------------------------------------------
303 --------------------------------------------------------------------*/
305 USHORT
SwFldDBPage::GetGroup()
310 /*--------------------------------------------------------------------
312 --------------------------------------------------------------------*/
314 IMPL_LINK( SwFldDBPage
, TypeHdl
, ListBox
*, pBox
)
316 // Alte ListBoxPos sichern
317 const USHORT nOld
= GetTypeSel();
319 // Aktuelle ListBoxPos
320 SetTypeSel(aTypeLB
.GetSelectEntryPos());
322 if(GetTypeSel() == LISTBOX_ENTRY_NOTFOUND
)
325 aTypeLB
.SelectEntryPos(0);
328 if (nOld
!= GetTypeSel())
330 SwWrtShell
*pSh
= GetWrtShell();
332 pSh
= ::GetActiveWrtShell();
333 BOOL bCond
= FALSE
, bSetNo
= FALSE
, bFormat
= FALSE
, bDBFormat
= FALSE
;
334 USHORT nTypeId
= (USHORT
)(ULONG
)aTypeLB
.GetEntryData(GetTypeSel());
336 aDatabaseTLB
.ShowColumns(nTypeId
== TYP_DBFLD
);
342 if (nTypeId
== TYP_DBFLD
)
344 aData
= ((SwDBField
*)GetCurField())->GetDBData();
345 sColumnName
= ((SwDBFieldType
*)GetCurField()->GetTyp())->GetColumnName();
349 aData
= ((SwDBNameInfField
*)GetCurField())->GetDBData(pSh
->GetDoc());
351 aDatabaseTLB
.Select(aData
.sDataSource
, aData
.sCommand
, sColumnName
);
362 if (pBox
) // Typ wurde vom User geaendert
367 if (GetCurField()->GetFormat() != 0 && GetCurField()->GetFormat() != SAL_MAX_UINT32
)
368 aNumFormatLB
.SetDefFormat(GetCurField()->GetFormat());
370 if (GetCurField()->GetSubType() & nsSwExtendedSubType::SUB_OWN_FMT
)
371 aNewFormatRB
.Check();
377 case TYP_DBNUMSETFLD
:
380 case TYP_DBNEXTSETFLD
:
384 aConditionED
.SetText(GetCurField()->GetPar1());
385 aValueED
.SetText(GetCurField()->GetPar2());
392 case TYP_DBSETNUMBERFLD
:
394 aNewFormatRB
.Check();
399 for( USHORT nI
= aFormatLB
.GetEntryCount(); nI
; )
400 if( GetCurField()->GetFormat() == (USHORT
)(ULONG
)
401 aFormatLB
.GetEntryData( --nI
))
403 aFormatLB
.SelectEntryPos( nI
);
410 aConditionFT
.Enable(bCond
);
411 aConditionED
.Enable(bCond
);
412 aValueFT
.Enable(bSetNo
);
413 aValueED
.Enable(bSetNo
);
414 if (nTypeId
!= TYP_DBFLD
)
416 aDBFormatRB
.Enable(bDBFormat
);
417 aNewFormatRB
.Enable(bDBFormat
|bFormat
);
418 aNumFormatLB
.Enable(bDBFormat
);
419 aFormatLB
.Enable(bFormat
);
421 aFormatFL
.Enable(bDBFormat
|bFormat
);
425 aValueED
.SetText(aEmptyStr
);
427 aConditionED
.SetText( String::CreateFromAscii(
428 RTL_CONSTASCII_STRINGPARAM( "TRUE" )));
430 aConditionED
.SetText(aEmptyStr
);
439 /*--------------------------------------------------------------------
441 --------------------------------------------------------------------*/
443 IMPL_LINK( SwFldDBPage
, NumSelectHdl
, NumFormatListBox
*, pLB
)
445 aNewFormatRB
.Check();
446 aOldNumSelectHdl
.Call(pLB
);
451 /*---------------------------------------------------------------------
453 ---------------------------------------------------------------------*/
455 void SwFldDBPage::CheckInsert()
458 USHORT nTypeId
= (USHORT
)(ULONG
)aTypeLB
.GetEntryData(GetTypeSel());
460 SvLBoxEntry
* pEntry
= aDatabaseTLB
.GetCurEntry();
464 pEntry
= aDatabaseTLB
.GetParent(pEntry
);
466 if (nTypeId
== TYP_DBFLD
&& pEntry
)
467 pEntry
= aDatabaseTLB
.GetParent(pEntry
);
469 bInsert
&= pEntry
!= 0;
474 if (nTypeId
== TYP_DBNUMSETFLD
)
476 BOOL bHasValue
= aValueED
.GetText().Len() != 0;
478 bInsert
&= bHasValue
;
481 EnableInsert(bInsert
);
484 /*---------------------------------------------------------------------
486 ---------------------------------------------------------------------*/
488 IMPL_LINK( SwFldDBPage
, TreeSelectHdl
, SvTreeListBox
*, pBox
)
490 SvLBoxEntry
* pColEntry
;
491 SvLBoxEntry
* pEntry
= pColEntry
= pBox
->GetCurEntry();
494 USHORT nTypeId
= (USHORT
)(ULONG
)aTypeLB
.GetEntryData(GetTypeSel());
496 pEntry
= aDatabaseTLB
.GetParent(pEntry
);
498 if (nTypeId
== TYP_DBFLD
&& pEntry
)
499 pEntry
= aDatabaseTLB
.GetParent(pEntry
);
503 if (nTypeId
== TYP_DBFLD
)
505 BOOL bNumFormat
= FALSE
;
512 String sDBName
= aDatabaseTLB
.GetDBName(sTableName
, sColumnName
, &bIsTable
);
513 bNumFormat
= GetFldMgr().IsDBNumeric(sDBName
,
521 aDBFormatRB
.Enable(bNumFormat
);
522 aNewFormatRB
.Enable(bNumFormat
);
523 aNumFormatLB
.Enable(bNumFormat
);
524 aFormatFL
.Enable(bNumFormat
);
529 /*-- 27.05.2004 09:14:01---------------------------------------------------
531 -----------------------------------------------------------------------*/
532 IMPL_LINK( SwFldDBPage
, AddDBHdl
, PushButton
*, EMPTYARG
)
534 String sNewDB
= SwNewDBMgr::LoadAndRegisterDataSource();
537 aDatabaseTLB
.AddDataSource(sNewDB
);
542 /*--------------------------------------------------------------------
544 --------------------------------------------------------------------*/
546 IMPL_LINK( SwFldDBPage
, ModifyHdl
, Edit
*, EMPTYARG
)
552 /* -----------------12.01.99 11:21-------------------
554 * --------------------------------------------------*/
555 void SwFldDBPage::FillUserData()
557 String
sData( String::CreateFromAscii(
558 RTL_CONSTASCII_STRINGPARAM( USER_DATA_VERSION
)));
560 USHORT nTypeSel
= aTypeLB
.GetSelectEntryPos();
562 if( LISTBOX_ENTRY_NOTFOUND
== nTypeSel
)
563 nTypeSel
= USHRT_MAX
;
565 nTypeSel
= (USHORT
)(ULONG
)aTypeLB
.GetEntryData( nTypeSel
);
566 sData
+= String::CreateFromInt32( nTypeSel
);
569 /* -----------------12.12.2002 11:33-----------------
571 * --------------------------------------------------*/
572 void SwFldDBPage::ActivateMailMergeAddress()
574 ULONG nData
= TYP_DBFLD
;
575 aTypeLB
.SelectEntryPos(aTypeLB
.GetEntryPos( (const void*) nData
));
576 aTypeLB
.GetSelectHdl().Call(&aTypeLB
);
577 const SwDBData
& rData
= SW_MOD()->GetDBConfig()->GetAddressSource();
578 aDatabaseTLB
.Select(rData
.sDataSource
, rData
.sCommand
, aEmptyStr
);
580 /*-- 07.10.2003 13:59:04---------------------------------------------------
582 -----------------------------------------------------------------------*/
583 void SwFldDBPage::SetWrtShell(SwWrtShell
& rSh
)
585 aDatabaseTLB
.SetWrtShell(rSh
);