update dev300-m58
[ooovba.git] / sw / source / ui / fldui / flddb.cxx
blobefa33e0f6788dfb6161ceae3ebaf265e1227d000
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: flddb.cxx,v $
10 * $Revision: 1.23 $
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
35 #endif
38 #include <swmodule.hxx>
39 #ifndef _VIEW_HXX
40 #include <view.hxx>
41 #endif
42 #include <wrtsh.hxx>
43 #ifndef _GLOBALS_HRC
44 #include <globals.hrc>
45 #endif
46 #include <dbfld.hxx>
47 #ifndef _FLDTDLG_HXX
48 #include <fldtdlg.hxx>
49 #endif
50 #include <numrule.hxx>
52 #ifndef _FLDTDLG_HRC
53 #include <fldtdlg.hrc>
54 #endif
55 #ifndef _FLDUI_HRC
56 #include <fldui.hrc>
57 #endif
58 #ifndef _FLDDB_HXX
59 #include <flddb.hxx>
60 #endif
61 #include <dbconfig.hxx>
62 #ifndef _DBMGR_HXX
63 #include <dbmgr.hxx>
64 #endif
66 #define USER_DATA_VERSION_1 "1"
67 #define USER_DATA_VERSION USER_DATA_VERSION_1
68 /*--------------------------------------------------------------------
69 Beschreibung:
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))
91 FreeResource();
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 /*--------------------------------------------------------------------
104 Beschreibung:
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);
123 aTypeLB.Clear();
125 USHORT nPos, nTypeId, i;
127 if (!IsFldEdit())
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));
139 else
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());
150 aFormatLB.Clear();
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 );
162 if (!IsFldEdit())
164 if (nOldPos != LISTBOX_ENTRY_NOTFOUND)
165 aTypeLB.SelectEntryPos(nOldPos);
167 if (sOldDBName.Len())
169 aDatabaseTLB.Select(sOldDBName, sOldTableName, sOldColumnName);
171 else
173 SwWrtShell *pSh = GetWrtShell();
174 if(!pSh)
175 pSh = ::GetActiveWrtShell();
176 if(pSh)
178 SwDBData aTmp(pSh->GetDBData());
179 aDatabaseTLB.Select(aTmp.sDataSource, aTmp.sCommand, aEmptyStr);
184 if( !IsRefresh() )
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);
197 break;
202 TypeHdl(0);
204 aTypeLB.SetUpdateMode(TRUE);
205 aTypeLB.SetSelectHdl(LINK(this, SwFldDBPage, TypeHdl));
206 aTypeLB.SetDoubleClickHdl(LINK(this, SwFldDBPage, InsertHdl));
208 if (IsFldEdit())
210 aConditionED.SaveValue();
211 aValueED.SaveValue();
212 sOldDBName = aDatabaseTLB.GetDBName(sOldTableName, sOldColumnName);
213 nOldFormat = GetCurField()->GetFormat();
214 nOldSubType = GetCurField()->GetSubType();
218 /*--------------------------------------------------------------------
219 Beschreibung:
220 --------------------------------------------------------------------*/
222 BOOL __EXPORT SwFldDBPage::FillItemSet(SfxItemSet& )
224 String sTableName, sColumnName;
225 SwDBData aData;
226 sal_Bool bIsTable;
227 aData.sDataSource = aDatabaseTLB.GetDBName(sTableName, sColumnName, &bIsTable);
228 aData.sCommand = sTableName;
229 aData.nCommandType = bIsTable ? 0 : 1;
230 SwWrtShell *pSh = GetWrtShell();
231 if(!pSh)
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());
242 ULONG nFormat = 0;
243 USHORT nSubType = 0;
245 String sDBName = aData.sDataSource;
246 sDBName += DB_DELIM;
247 sDBName += (String)aData.sCommand;
248 sDBName += DB_DELIM;
249 sDBName += String::CreateFromInt32(aData.nCommandType);
250 sDBName += DB_DELIM;
251 if(sColumnName.Len())
253 sDBName += sColumnName;
254 sDBName += DB_DELIM;
256 aName.Insert(sDBName, 0);
258 switch (nTypeId)
260 case TYP_DBFLD:
261 nFormat = aNumFormatLB.GetFormat();
262 if (aNewFormatRB.IsEnabled() && aNewFormatRB.IsChecked())
263 nSubType = nsSwExtendedSubType::SUB_OWN_FMT;
264 aName = sDBName;
265 break;
267 case TYP_DBSETNUMBERFLD:
268 nFormat = (USHORT)(ULONG)aFormatLB.GetEntryData(
269 aFormatLB.GetSelectEntryPos() );
270 break;
274 String sTempDBName, sTempTableName, sTempColumnName;
275 sTempDBName = aDatabaseTLB.GetDBName(sTempTableName, sTempColumnName);
276 BOOL bDBListBoxChanged = sOldDBName != sTempDBName ||
277 sOldTableName != sTempTableName || sOldColumnName != sTempColumnName;
278 if (!IsFldEdit() ||
279 aConditionED.GetSavedValue() != aConditionED.GetText() ||
280 aValueED.GetSavedValue() != aValueED.GetText() ||
281 bDBListBoxChanged ||
282 nOldFormat != nFormat || nOldSubType != nSubType)
284 InsertFld( nTypeId, nSubType, aName, aVal, nFormat);
288 return FALSE;
291 /*--------------------------------------------------------------------
292 Beschreibung:
293 --------------------------------------------------------------------*/
295 SfxTabPage* __EXPORT SwFldDBPage::Create( Window* pParent,
296 const SfxItemSet& rAttrSet )
298 return ( new SwFldDBPage( pParent, rAttrSet ) );
301 /*--------------------------------------------------------------------
302 Beschreibung:
303 --------------------------------------------------------------------*/
305 USHORT SwFldDBPage::GetGroup()
307 return GRP_DB;
310 /*--------------------------------------------------------------------
311 Beschreibung:
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)
324 SetTypeSel(0);
325 aTypeLB.SelectEntryPos(0);
328 if (nOld != GetTypeSel())
330 SwWrtShell *pSh = GetWrtShell();
331 if(!pSh)
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);
338 if (IsFldEdit())
340 SwDBData aData;
341 String sColumnName;
342 if (nTypeId == TYP_DBFLD)
344 aData = ((SwDBField*)GetCurField())->GetDBData();
345 sColumnName = ((SwDBFieldType*)GetCurField()->GetTyp())->GetColumnName();
347 else
349 aData = ((SwDBNameInfField*)GetCurField())->GetDBData(pSh->GetDoc());
351 aDatabaseTLB.Select(aData.sDataSource, aData.sCommand, sColumnName);
354 switch (nTypeId)
356 case TYP_DBFLD:
357 bFormat = TRUE;
358 bDBFormat = TRUE;
359 aNumFormatLB.Show();
360 aFormatLB.Hide();
362 if (pBox) // Typ wurde vom User geaendert
363 aDBFormatRB.Check();
365 if (IsFldEdit())
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();
372 else
373 aDBFormatRB.Check();
375 break;
377 case TYP_DBNUMSETFLD:
378 bSetNo = TRUE;
379 // kein break!
380 case TYP_DBNEXTSETFLD:
381 bCond = TRUE;
382 if (IsFldEdit())
384 aConditionED.SetText(GetCurField()->GetPar1());
385 aValueED.SetText(GetCurField()->GetPar2());
387 break;
389 case TYP_DBNAMEFLD:
390 break;
392 case TYP_DBSETNUMBERFLD:
393 bFormat = TRUE;
394 aNewFormatRB.Check();
395 aNumFormatLB.Hide();
396 aFormatLB.Show();
397 if( IsFldEdit() )
399 for( USHORT nI = aFormatLB.GetEntryCount(); nI; )
400 if( GetCurField()->GetFormat() == (USHORT)(ULONG)
401 aFormatLB.GetEntryData( --nI ))
403 aFormatLB.SelectEntryPos( nI );
404 break;
407 break;
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);
423 if (!IsFldEdit())
425 aValueED.SetText(aEmptyStr);
426 if (bCond)
427 aConditionED.SetText( String::CreateFromAscii(
428 RTL_CONSTASCII_STRINGPARAM( "TRUE" )));
429 else
430 aConditionED.SetText(aEmptyStr);
433 CheckInsert();
436 return 0;
439 /*--------------------------------------------------------------------
440 Beschreibung:
441 --------------------------------------------------------------------*/
443 IMPL_LINK( SwFldDBPage, NumSelectHdl, NumFormatListBox *, pLB )
445 aNewFormatRB.Check();
446 aOldNumSelectHdl.Call(pLB);
448 return 0;
451 /*---------------------------------------------------------------------
452 Beschreibung:
453 ---------------------------------------------------------------------*/
455 void SwFldDBPage::CheckInsert()
457 BOOL bInsert = TRUE;
458 USHORT nTypeId = (USHORT)(ULONG)aTypeLB.GetEntryData(GetTypeSel());
460 SvLBoxEntry* pEntry = aDatabaseTLB.GetCurEntry();
462 if (pEntry)
464 pEntry = aDatabaseTLB.GetParent(pEntry);
466 if (nTypeId == TYP_DBFLD && pEntry)
467 pEntry = aDatabaseTLB.GetParent(pEntry);
469 bInsert &= pEntry != 0;
471 else
472 bInsert = FALSE;
474 if (nTypeId == TYP_DBNUMSETFLD)
476 BOOL bHasValue = aValueED.GetText().Len() != 0;
478 bInsert &= bHasValue;
481 EnableInsert(bInsert);
484 /*---------------------------------------------------------------------
485 Beschreibung:
486 ---------------------------------------------------------------------*/
488 IMPL_LINK( SwFldDBPage, TreeSelectHdl, SvTreeListBox *, pBox )
490 SvLBoxEntry* pColEntry;
491 SvLBoxEntry* pEntry = pColEntry = pBox->GetCurEntry();
492 if (pEntry)
494 USHORT nTypeId = (USHORT)(ULONG)aTypeLB.GetEntryData(GetTypeSel());
496 pEntry = aDatabaseTLB.GetParent(pEntry);
498 if (nTypeId == TYP_DBFLD && pEntry)
499 pEntry = aDatabaseTLB.GetParent(pEntry);
501 CheckInsert();
503 if (nTypeId == TYP_DBFLD)
505 BOOL bNumFormat = FALSE;
507 if (pEntry != 0)
509 String sTableName;
510 String sColumnName;
511 BOOL bIsTable;
512 String sDBName = aDatabaseTLB.GetDBName(sTableName, sColumnName, &bIsTable);
513 bNumFormat = GetFldMgr().IsDBNumeric(sDBName,
514 sTableName,
515 bIsTable,
516 sColumnName);
517 if (!IsFldEdit())
518 aDBFormatRB.Check();
521 aDBFormatRB.Enable(bNumFormat);
522 aNewFormatRB.Enable(bNumFormat);
523 aNumFormatLB.Enable(bNumFormat);
524 aFormatFL.Enable(bNumFormat);
527 return 0;
529 /*-- 27.05.2004 09:14:01---------------------------------------------------
531 -----------------------------------------------------------------------*/
532 IMPL_LINK( SwFldDBPage, AddDBHdl, PushButton *, EMPTYARG )
534 String sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
535 if(sNewDB.Len())
537 aDatabaseTLB.AddDataSource(sNewDB);
539 return 0;
542 /*--------------------------------------------------------------------
543 Beschreibung: Modify
544 --------------------------------------------------------------------*/
546 IMPL_LINK( SwFldDBPage, ModifyHdl, Edit *, EMPTYARG )
548 CheckInsert();
549 return 0;
552 /* -----------------12.01.99 11:21-------------------
554 * --------------------------------------------------*/
555 void SwFldDBPage::FillUserData()
557 String sData( String::CreateFromAscii(
558 RTL_CONSTASCII_STRINGPARAM( USER_DATA_VERSION )));
559 sData += ';';
560 USHORT nTypeSel = aTypeLB.GetSelectEntryPos();
562 if( LISTBOX_ENTRY_NOTFOUND == nTypeSel )
563 nTypeSel = USHRT_MAX;
564 else
565 nTypeSel = (USHORT)(ULONG)aTypeLB.GetEntryData( nTypeSel );
566 sData += String::CreateFromInt32( nTypeSel );
567 SetUserData(sData);
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);