bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / fldui / flddb.cxx
blob322a7afe60b031632d1d7a2b18c71ce31c8747d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <view.hxx>
22 #include <wrtsh.hxx>
23 #include <globals.hrc>
24 #include <dbfld.hxx>
25 #include <fldtdlg.hxx>
26 #include <numrule.hxx>
28 #include <fldtdlg.hrc>
29 #include <fldui.hrc>
30 #include <flddb.hxx>
31 #include <dbconfig.hxx>
32 #include <dbmgr.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))
56 FreeResource();
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);
83 aTypeLB.Clear();
85 sal_uInt16 nPos, nTypeId, i;
87 if (!IsFldEdit())
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));
99 else
101 nTypeId = GetCurField()->GetTypeId();
102 nPos = aTypeLB.InsertEntry(GetFldMgr().GetTypeStr(GetFldMgr().GetPos(nTypeId)));
103 aTypeLB.SetEntryData(nPos, reinterpret_cast<void*>(nTypeId));
106 // select old Pos
107 if (GetTypeSel() != LISTBOX_ENTRY_NOTFOUND)
108 aTypeLB.SelectEntryPos(GetTypeSel());
110 aFormatLB.Clear();
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 );
122 if (!IsFldEdit())
124 if (nOldPos != LISTBOX_ENTRY_NOTFOUND)
125 aTypeLB.SelectEntryPos(nOldPos);
127 if (sOldDBName.Len())
129 aDatabaseTLB.Select(sOldDBName, sOldTableName, sOldColumnName);
131 else
133 SwWrtShell *pSh = GetWrtShell();
134 if(!pSh)
135 pSh = ::GetActiveWrtShell();
136 if(pSh)
138 SwDBData aTmp(pSh->GetDBData());
139 aDatabaseTLB.Select(aTmp.sDataSource, aTmp.sCommand, aEmptyStr);
144 if( !IsRefresh() )
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);
157 break;
162 TypeHdl(0);
164 aTypeLB.SetUpdateMode(sal_True);
165 aTypeLB.SetSelectHdl(LINK(this, SwFldDBPage, TypeHdl));
166 aTypeLB.SetDoubleClickHdl(LINK(this, SwFldDBPage, InsertHdl));
168 if (IsFldEdit())
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;
181 SwDBData aData;
182 sal_Bool bIsTable;
183 aData.sDataSource = aDatabaseTLB.GetDBName(sTableName, sColumnName, &bIsTable);
184 aData.sCommand = sTableName;
185 aData.nCommandType = bIsTable ? 0 : 1;
186 SwWrtShell *pSh = GetWrtShell();
187 if(!pSh)
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;
202 sDBName += DB_DELIM;
203 sDBName += (String)aData.sCommand;
204 sDBName += DB_DELIM;
205 sDBName += OUString::number(aData.nCommandType);
206 sDBName += DB_DELIM;
207 if(sColumnName.Len())
209 sDBName += sColumnName;
210 sDBName += DB_DELIM;
212 aName.Insert(sDBName, 0);
214 switch (nTypeId)
216 case TYP_DBFLD:
217 nFormat = aNumFormatLB.GetFormat();
218 if (aNewFormatRB.IsEnabled() && aNewFormatRB.IsChecked())
219 nSubType = nsSwExtendedSubType::SUB_OWN_FMT;
220 aName = sDBName;
221 break;
223 case TYP_DBSETNUMBERFLD:
224 nFormat = (sal_uInt16)(sal_uLong)aFormatLB.GetEntryData(
225 aFormatLB.GetSelectEntryPos() );
226 break;
230 String sTempDBName, sTempTableName, sTempColumnName;
231 sTempDBName = aDatabaseTLB.GetDBName(sTempTableName, sTempColumnName);
232 sal_Bool bDBListBoxChanged = sOldDBName != sTempDBName ||
233 sOldTableName != sTempTableName || sOldColumnName != sTempColumnName;
234 if (!IsFldEdit() ||
235 aConditionED.GetSavedValue() != aConditionED.GetText() ||
236 aValueED.GetSavedValue() != aValueED.GetText() ||
237 bDBListBoxChanged ||
238 nOldFormat != nFormat || nOldSubType != nSubType)
240 InsertFld( nTypeId, nSubType, aName, aVal, nFormat);
244 return sal_False;
247 SfxTabPage* SwFldDBPage::Create( Window* pParent,
248 const SfxItemSet& rAttrSet )
250 return ( new SwFldDBPage( pParent, rAttrSet ) );
253 sal_uInt16 SwFldDBPage::GetGroup()
255 return GRP_DB;
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)
268 SetTypeSel(0);
269 aTypeLB.SelectEntryPos(0);
272 if (nOld != GetTypeSel())
274 SwWrtShell *pSh = GetWrtShell();
275 if(!pSh)
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);
282 if (IsFldEdit())
284 SwDBData aData;
285 String sColumnName;
286 if (nTypeId == TYP_DBFLD)
288 aData = ((SwDBField*)GetCurField())->GetDBData();
289 sColumnName = ((SwDBFieldType*)GetCurField()->GetTyp())->GetColumnName();
291 else
293 aData = ((SwDBNameInfField*)GetCurField())->GetDBData(pSh->GetDoc());
295 aDatabaseTLB.Select(aData.sDataSource, aData.sCommand, sColumnName);
298 switch (nTypeId)
300 case TYP_DBFLD:
301 bFormat = sal_True;
302 bDBFormat = sal_True;
303 aNumFormatLB.Show();
304 aFormatLB.Hide();
306 if (pBox) // type was changed by user
307 aDBFormatRB.Check();
309 if (IsFldEdit())
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();
316 else
317 aDBFormatRB.Check();
319 break;
321 case TYP_DBNUMSETFLD:
322 bSetNo = sal_True;
323 // no break!
324 case TYP_DBNEXTSETFLD:
325 bCond = sal_True;
326 if (IsFldEdit())
328 aConditionED.SetText(GetCurField()->GetPar1());
329 aValueED.SetText(GetCurField()->GetPar2());
331 break;
333 case TYP_DBNAMEFLD:
334 break;
336 case TYP_DBSETNUMBERFLD:
337 bFormat = sal_True;
338 aNewFormatRB.Check();
339 aNumFormatLB.Hide();
340 aFormatLB.Show();
341 if( IsFldEdit() )
343 for( sal_uInt16 nI = aFormatLB.GetEntryCount(); nI; )
344 if( GetCurField()->GetFormat() == (sal_uInt16)(sal_uLong)
345 aFormatLB.GetEntryData( --nI ))
347 aFormatLB.SelectEntryPos( nI );
348 break;
351 break;
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);
367 if (!IsFldEdit())
369 aValueED.SetText(aEmptyStr);
370 if (bCond)
371 aConditionED.SetText(OUString("TRUE"));
372 else
373 aConditionED.SetText(aEmptyStr);
376 CheckInsert();
379 return 0;
382 IMPL_LINK( SwFldDBPage, NumSelectHdl, NumFormatListBox *, pLB )
384 aNewFormatRB.Check();
385 aOldNumSelectHdl.Call(pLB);
387 return 0;
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();
397 if (pEntry)
399 pEntry = aDatabaseTLB.GetParent(pEntry);
401 if (nTypeId == TYP_DBFLD && pEntry)
402 pEntry = aDatabaseTLB.GetParent(pEntry);
404 bInsert &= pEntry != 0;
406 else
407 bInsert = sal_False;
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();
423 if (pEntry)
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);
432 CheckInsert();
434 if (nTypeId == TYP_DBFLD)
436 sal_Bool bNumFormat = sal_False;
438 if (pEntry != 0)
440 String sTableName;
441 String sColumnName;
442 sal_Bool bIsTable;
443 String sDBName = aDatabaseTLB.GetDBName(sTableName, sColumnName, &bIsTable);
444 bNumFormat = GetFldMgr().IsDBNumeric(sDBName,
445 sTableName,
446 bIsTable,
447 sColumnName);
448 if (!IsFldEdit())
449 aDBFormatRB.Check();
452 aDBFormatRB.Enable(bNumFormat);
453 aNewFormatRB.Enable(bNumFormat);
454 aNumFormatLB.Enable(bNumFormat);
455 aFormatFL.Enable(bNumFormat);
458 return 0;
461 IMPL_LINK_NOARG(SwFldDBPage, AddDBHdl)
463 String sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
464 if(sNewDB.Len())
466 aDatabaseTLB.AddDataSource(sNewDB);
468 return 0;
471 /*--------------------------------------------------------------------
472 Description: Modify
473 --------------------------------------------------------------------*/
474 IMPL_LINK_NOARG(SwFldDBPage, ModifyHdl)
476 CheckInsert();
477 return 0;
480 void SwFldDBPage::FillUserData()
482 String sData(OUString(USER_DATA_VERSION));
483 sData += ';';
484 sal_uInt16 nTypeSel = aTypeLB.GetSelectEntryPos();
486 if( LISTBOX_ENTRY_NOTFOUND == nTypeSel )
487 nTypeSel = USHRT_MAX;
488 else
489 nTypeSel = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData( nTypeSel );
490 sData += OUString::number( nTypeSel );
491 SetUserData(sData);
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: */