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 <swmodule.hxx>
23 #include <globals.hrc>
25 #include <fldtdlg.hxx>
26 #include <numrule.hxx>
28 #include <fldtdlg.hrc>
31 #include <dbconfig.hxx>
34 #define USER_DATA_VERSION_1 "1"
35 #define USER_DATA_VERSION USER_DATA_VERSION_1
37 SwFldDBPage::SwFldDBPage(Window
* pParent
, const SfxItemSet
& rCoreSet
) :
38 SwFldPage( pParent
, SW_RES( TP_FLD_DB
), rCoreSet
),
39 aTypeFT (this, SW_RES(FT_DBTYPE
)),
40 aTypeLB (this, SW_RES(LB_DBTYPE
)),
41 aSelectionFT(this, SW_RES(FT_DBSELECTION
)),
42 aDatabaseTLB(this, SW_RES(TLB_DBLIST
), 0, aEmptyStr
, sal_False
),
43 aAddDBFT(this, SW_RES(FT_ADDDB
)),
44 aAddDBPB(this, SW_RES(PB_ADDDB
)),
45 aConditionFT(this, SW_RES(FT_DBCONDITION
)),
46 aConditionED(this, SW_RES(ED_DBCONDITION
)),
47 aValueFT (this, SW_RES(FT_DBSETNUMBER
)),
48 aValueED (this, SW_RES(ED_DBSETNUMBER
)),
49 aDBFormatRB (this, SW_RES(RB_DBOWNFORMAT
)),
50 aNewFormatRB(this, SW_RES(RB_DBFORMAT
)),
51 aNumFormatLB(this, SW_RES(LB_DBNUMFORMAT
)),
52 aFormatLB (this, SW_RES(LB_DBFORMAT
)),
53 aFormatFL (this, SW_RES(FL_DBFORMAT
)),
54 aFormatVertFL (this, SW_RES(FL_DBFORMAT_VERT
))
58 aOldNumSelectHdl
= aNumFormatLB
.GetSelectHdl();
60 aNumFormatLB
.SetSelectHdl(LINK(this, SwFldDBPage
, NumSelectHdl
));
61 aDatabaseTLB
.SetSelectHdl(LINK(this, SwFldDBPage
, TreeSelectHdl
));
62 aDatabaseTLB
.SetDoubleClickHdl(LINK(this, SwFldDBPage
, InsertHdl
));
64 aValueED
.SetModifyHdl(LINK(this, SwFldDBPage
, ModifyHdl
));
65 aAddDBPB
.SetClickHdl(LINK(this, SwFldDBPage
, AddDBHdl
));
68 SwFldDBPage::~SwFldDBPage()
72 /*--------------------------------------------------------------------
73 Description: initialise TabPage
74 --------------------------------------------------------------------*/
75 void SwFldDBPage::Reset(const SfxItemSet
&)
77 Init(); // Allgemeine initialisierung
79 aTypeLB
.SetUpdateMode(sal_False
);
80 sal_uInt16 nOldPos
= aTypeLB
.GetSelectEntryPos();
81 sOldDBName
= aDatabaseTLB
.GetDBName(sOldTableName
, sOldColumnName
);
85 sal_uInt16 nPos
, nTypeId
, i
;
89 // initialise TypeListBox
90 const SwFldGroupRgn
& rRg
= GetFldMgr().GetGroupRange(IsFldDlgHtmlMode(), GetGroup());
92 for(i
= rRg
.nStart
; i
< rRg
.nEnd
; ++i
)
94 nTypeId
= GetFldMgr().GetTypeId(i
);
95 nPos
= aTypeLB
.InsertEntry(GetFldMgr().GetTypeStr(i
));
96 aTypeLB
.SetEntryData(nPos
, reinterpret_cast<void*>(nTypeId
));
101 nTypeId
= GetCurField()->GetTypeId();
102 nPos
= aTypeLB
.InsertEntry(GetFldMgr().GetTypeStr(GetFldMgr().GetPos(nTypeId
)));
103 aTypeLB
.SetEntryData(nPos
, reinterpret_cast<void*>(nTypeId
));
107 if (GetTypeSel() != LISTBOX_ENTRY_NOTFOUND
)
108 aTypeLB
.SelectEntryPos(GetTypeSel());
112 sal_uInt16 nSize
= GetFldMgr().GetFormatCount(TYP_DBSETNUMBERFLD
, false, IsFldDlgHtmlMode());
113 for( i
= 0; i
< nSize
; ++i
)
115 sal_uInt16 nEntryPos
= aFormatLB
.InsertEntry(GetFldMgr().GetFormatStr(TYP_DBSETNUMBERFLD
, i
));
116 sal_uInt16 nFmtId
= GetFldMgr().GetFormatId( TYP_DBSETNUMBERFLD
, i
);
117 aFormatLB
.SetEntryData( nEntryPos
, reinterpret_cast<void*>(nFmtId
) );
118 if( SVX_NUM_ARABIC
== nFmtId
)
119 aFormatLB
.SelectEntryPos( nEntryPos
);
124 if (nOldPos
!= LISTBOX_ENTRY_NOTFOUND
)
125 aTypeLB
.SelectEntryPos(nOldPos
);
127 if (sOldDBName
.Len())
129 aDatabaseTLB
.Select(sOldDBName
, sOldTableName
, sOldColumnName
);
133 SwWrtShell
*pSh
= GetWrtShell();
135 pSh
= ::GetActiveWrtShell();
138 SwDBData
aTmp(pSh
->GetDBData());
139 aDatabaseTLB
.Select(aTmp
.sDataSource
, aTmp
.sCommand
, aEmptyStr
);
146 String sUserData
= GetUserData();
147 if(sUserData
.GetToken(0, ';').EqualsIgnoreCaseAscii(USER_DATA_VERSION_1
))
149 String sVal
= sUserData
.GetToken(1, ';');
150 sal_uInt16 nVal
= (sal_uInt16
)sVal
.ToInt32();
151 if(nVal
!= USHRT_MAX
)
153 for(i
= 0; i
< aTypeLB
.GetEntryCount(); i
++)
154 if(nVal
== (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(i
))
156 aTypeLB
.SelectEntryPos(i
);
164 aTypeLB
.SetUpdateMode(sal_True
);
165 aTypeLB
.SetSelectHdl(LINK(this, SwFldDBPage
, TypeHdl
));
166 aTypeLB
.SetDoubleClickHdl(LINK(this, SwFldDBPage
, InsertHdl
));
170 aConditionED
.SaveValue();
171 aValueED
.SaveValue();
172 sOldDBName
= aDatabaseTLB
.GetDBName(sOldTableName
, sOldColumnName
);
173 nOldFormat
= GetCurField()->GetFormat();
174 nOldSubType
= GetCurField()->GetSubType();
178 sal_Bool
SwFldDBPage::FillItemSet(SfxItemSet
& )
180 String sTableName
, sColumnName
;
183 aData
.sDataSource
= aDatabaseTLB
.GetDBName(sTableName
, sColumnName
, &bIsTable
);
184 aData
.sCommand
= sTableName
;
185 aData
.nCommandType
= bIsTable
? 0 : 1;
186 SwWrtShell
*pSh
= GetWrtShell();
188 pSh
= ::GetActiveWrtShell();
190 if (aData
.sDataSource
.isEmpty())
191 aData
= pSh
->GetDBData();
193 if(!aData
.sDataSource
.isEmpty()) // without database no new field command
195 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
196 String
aVal(aValueED
.GetText());
197 String
aName(aConditionED
.GetText());
198 sal_uLong nFormat
= 0;
199 sal_uInt16 nSubType
= 0;
201 String sDBName
= aData
.sDataSource
;
203 sDBName
+= (String
)aData
.sCommand
;
205 sDBName
+= OUString::number(aData
.nCommandType
);
207 if(sColumnName
.Len())
209 sDBName
+= sColumnName
;
212 aName
.Insert(sDBName
, 0);
217 nFormat
= aNumFormatLB
.GetFormat();
218 if (aNewFormatRB
.IsEnabled() && aNewFormatRB
.IsChecked())
219 nSubType
= nsSwExtendedSubType::SUB_OWN_FMT
;
223 case TYP_DBSETNUMBERFLD
:
224 nFormat
= (sal_uInt16
)(sal_uLong
)aFormatLB
.GetEntryData(
225 aFormatLB
.GetSelectEntryPos() );
230 String sTempDBName
, sTempTableName
, sTempColumnName
;
231 sTempDBName
= aDatabaseTLB
.GetDBName(sTempTableName
, sTempColumnName
);
232 sal_Bool bDBListBoxChanged
= sOldDBName
!= sTempDBName
||
233 sOldTableName
!= sTempTableName
|| sOldColumnName
!= sTempColumnName
;
235 aConditionED
.GetSavedValue() != aConditionED
.GetText() ||
236 aValueED
.GetSavedValue() != aValueED
.GetText() ||
238 nOldFormat
!= nFormat
|| nOldSubType
!= nSubType
)
240 InsertFld( nTypeId
, nSubType
, aName
, aVal
, nFormat
);
247 SfxTabPage
* SwFldDBPage::Create( Window
* pParent
,
248 const SfxItemSet
& rAttrSet
)
250 return ( new SwFldDBPage( pParent
, rAttrSet
) );
253 sal_uInt16
SwFldDBPage::GetGroup()
258 IMPL_LINK( SwFldDBPage
, TypeHdl
, ListBox
*, pBox
)
260 // save old ListBoxPos
261 const sal_uInt16 nOld
= GetTypeSel();
263 // current ListBoxPos
264 SetTypeSel(aTypeLB
.GetSelectEntryPos());
266 if(GetTypeSel() == LISTBOX_ENTRY_NOTFOUND
)
269 aTypeLB
.SelectEntryPos(0);
272 if (nOld
!= GetTypeSel())
274 SwWrtShell
*pSh
= GetWrtShell();
276 pSh
= ::GetActiveWrtShell();
277 sal_Bool bCond
= sal_False
, bSetNo
= sal_False
, bFormat
= sal_False
, bDBFormat
= sal_False
;
278 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
280 aDatabaseTLB
.ShowColumns(nTypeId
== TYP_DBFLD
);
286 if (nTypeId
== TYP_DBFLD
)
288 aData
= ((SwDBField
*)GetCurField())->GetDBData();
289 sColumnName
= ((SwDBFieldType
*)GetCurField()->GetTyp())->GetColumnName();
293 aData
= ((SwDBNameInfField
*)GetCurField())->GetDBData(pSh
->GetDoc());
295 aDatabaseTLB
.Select(aData
.sDataSource
, aData
.sCommand
, sColumnName
);
302 bDBFormat
= sal_True
;
306 if (pBox
) // type was changed by user
311 if (GetCurField()->GetFormat() != 0 && GetCurField()->GetFormat() != SAL_MAX_UINT32
)
312 aNumFormatLB
.SetDefFormat(GetCurField()->GetFormat());
314 if (GetCurField()->GetSubType() & nsSwExtendedSubType::SUB_OWN_FMT
)
315 aNewFormatRB
.Check();
321 case TYP_DBNUMSETFLD
:
324 case TYP_DBNEXTSETFLD
:
328 aConditionED
.SetText(GetCurField()->GetPar1());
329 aValueED
.SetText(GetCurField()->GetPar2());
336 case TYP_DBSETNUMBERFLD
:
338 aNewFormatRB
.Check();
343 for( sal_uInt16 nI
= aFormatLB
.GetEntryCount(); nI
; )
344 if( GetCurField()->GetFormat() == (sal_uInt16
)(sal_uLong
)
345 aFormatLB
.GetEntryData( --nI
))
347 aFormatLB
.SelectEntryPos( nI
);
354 aConditionFT
.Enable(bCond
);
355 aConditionED
.Enable(bCond
);
356 aValueFT
.Enable(bSetNo
);
357 aValueED
.Enable(bSetNo
);
358 if (nTypeId
!= TYP_DBFLD
)
360 aDBFormatRB
.Enable(bDBFormat
);
361 aNewFormatRB
.Enable(bDBFormat
|bFormat
);
362 aNumFormatLB
.Enable(bDBFormat
);
363 aFormatLB
.Enable(bFormat
);
365 aFormatFL
.Enable(bDBFormat
|bFormat
);
369 aValueED
.SetText(aEmptyStr
);
371 aConditionED
.SetText(OUString("TRUE"));
373 aConditionED
.SetText(aEmptyStr
);
382 IMPL_LINK( SwFldDBPage
, NumSelectHdl
, NumFormatListBox
*, pLB
)
384 aNewFormatRB
.Check();
385 aOldNumSelectHdl
.Call(pLB
);
390 void SwFldDBPage::CheckInsert()
392 sal_Bool bInsert
= sal_True
;
393 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
395 SvTreeListEntry
* pEntry
= aDatabaseTLB
.GetCurEntry();
399 pEntry
= aDatabaseTLB
.GetParent(pEntry
);
401 if (nTypeId
== TYP_DBFLD
&& pEntry
)
402 pEntry
= aDatabaseTLB
.GetParent(pEntry
);
404 bInsert
&= pEntry
!= 0;
409 if (nTypeId
== TYP_DBNUMSETFLD
)
411 sal_Bool bHasValue
= !aValueED
.GetText().isEmpty();
413 bInsert
&= bHasValue
;
416 EnableInsert(bInsert
);
419 IMPL_LINK( SwFldDBPage
, TreeSelectHdl
, SvTreeListBox
*, pBox
)
421 SvTreeListEntry
* pColEntry
;
422 SvTreeListEntry
* pEntry
= pColEntry
= pBox
->GetCurEntry();
425 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
427 pEntry
= aDatabaseTLB
.GetParent(pEntry
);
429 if (nTypeId
== TYP_DBFLD
&& pEntry
)
430 pEntry
= aDatabaseTLB
.GetParent(pEntry
);
434 if (nTypeId
== TYP_DBFLD
)
436 sal_Bool bNumFormat
= sal_False
;
443 String sDBName
= aDatabaseTLB
.GetDBName(sTableName
, sColumnName
, &bIsTable
);
444 bNumFormat
= GetFldMgr().IsDBNumeric(sDBName
,
452 aDBFormatRB
.Enable(bNumFormat
);
453 aNewFormatRB
.Enable(bNumFormat
);
454 aNumFormatLB
.Enable(bNumFormat
);
455 aFormatFL
.Enable(bNumFormat
);
461 IMPL_LINK_NOARG(SwFldDBPage
, AddDBHdl
)
463 String sNewDB
= SwNewDBMgr::LoadAndRegisterDataSource();
466 aDatabaseTLB
.AddDataSource(sNewDB
);
471 /*--------------------------------------------------------------------
473 --------------------------------------------------------------------*/
474 IMPL_LINK_NOARG(SwFldDBPage
, ModifyHdl
)
480 void SwFldDBPage::FillUserData()
482 String
sData(OUString(USER_DATA_VERSION
));
484 sal_uInt16 nTypeSel
= aTypeLB
.GetSelectEntryPos();
486 if( LISTBOX_ENTRY_NOTFOUND
== nTypeSel
)
487 nTypeSel
= USHRT_MAX
;
489 nTypeSel
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData( nTypeSel
);
490 sData
+= OUString::number( nTypeSel
);
494 void SwFldDBPage::ActivateMailMergeAddress()
496 sal_uLong nData
= TYP_DBFLD
;
497 aTypeLB
.SelectEntryPos(aTypeLB
.GetEntryPos( (const void*) nData
));
498 aTypeLB
.GetSelectHdl().Call(&aTypeLB
);
499 const SwDBData
& rData
= SW_MOD()->GetDBConfig()->GetAddressSource();
500 aDatabaseTLB
.Select(rData
.sDataSource
, rData
.sCommand
, aEmptyStr
);
503 void SwFldDBPage::SetWrtShell(SwWrtShell
& rSh
)
505 aDatabaseTLB
.SetWrtShell(rSh
);
508 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */