xref: /trunk/main/sw/source/ui/fldui/flddb.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 #ifdef SW_DLLIMPLEMENTATION
31 #undef SW_DLLIMPLEMENTATION
32 #endif
33 
34 
35 #include <swmodule.hxx>
36 #ifndef _VIEW_HXX
37 #include <view.hxx>
38 #endif
39 #include <wrtsh.hxx>
40 #ifndef _GLOBALS_HRC
41 #include <globals.hrc>
42 #endif
43 #include <dbfld.hxx>
44 #ifndef _FLDTDLG_HXX
45 #include <fldtdlg.hxx>
46 #endif
47 #include <numrule.hxx>
48 
49 #ifndef _FLDTDLG_HRC
50 #include <fldtdlg.hrc>
51 #endif
52 #ifndef _FLDUI_HRC
53 #include <fldui.hrc>
54 #endif
55 #ifndef _FLDDB_HXX
56 #include <flddb.hxx>
57 #endif
58 #include <dbconfig.hxx>
59 #ifndef _DBMGR_HXX
60 #include <dbmgr.hxx>
61 #endif
62 
63 #define USER_DATA_VERSION_1     "1"
64 #define USER_DATA_VERSION USER_DATA_VERSION_1
65 /*--------------------------------------------------------------------
66     Beschreibung:
67  --------------------------------------------------------------------*/
68 
69 SwFldDBPage::SwFldDBPage(Window* pParent, const SfxItemSet& rCoreSet ) :
70     SwFldPage( pParent, SW_RES( TP_FLD_DB ), rCoreSet ),
71     aTypeFT     (this, SW_RES(FT_DBTYPE)),
72     aTypeLB     (this, SW_RES(LB_DBTYPE)),
73     aSelectionFT(this, SW_RES(FT_DBSELECTION)),
74     aDatabaseTLB(this, SW_RES(TLB_DBLIST), 0, aEmptyStr, sal_False),
75     aAddDBFT(this,      SW_RES(FT_ADDDB)),
76     aAddDBPB(this,      SW_RES(PB_ADDDB)),
77     aConditionFT(this, SW_RES(FT_DBCONDITION)),
78     aConditionED(this, SW_RES(ED_DBCONDITION)),
79     aValueFT    (this, SW_RES(FT_DBSETNUMBER)),
80     aValueED    (this, SW_RES(ED_DBSETNUMBER)),
81     aDBFormatRB (this, SW_RES(RB_DBOWNFORMAT)),
82     aNewFormatRB(this, SW_RES(RB_DBFORMAT)),
83     aNumFormatLB(this, SW_RES(LB_DBNUMFORMAT)),
84     aFormatLB   (this, SW_RES(LB_DBFORMAT)),
85     aFormatFL   (this, SW_RES(FL_DBFORMAT)),
86     aFormatVertFL   (this, SW_RES(FL_DBFORMAT_VERT))
87 {
88     FreeResource();
89 
90     aOldNumSelectHdl = aNumFormatLB.GetSelectHdl();
91 
92     aNumFormatLB.SetSelectHdl(LINK(this, SwFldDBPage, NumSelectHdl));
93     aDatabaseTLB.SetSelectHdl(LINK(this, SwFldDBPage, TreeSelectHdl));
94     aDatabaseTLB.SetDoubleClickHdl(LINK(this, SwFldDBPage, InsertHdl));
95 
96     aValueED.SetModifyHdl(LINK(this, SwFldDBPage, ModifyHdl));
97     aAddDBPB.SetClickHdl(LINK(this, SwFldDBPage, AddDBHdl));
98 }
99 
100 /*--------------------------------------------------------------------
101     Beschreibung:
102  --------------------------------------------------------------------*/
103 
104 __EXPORT SwFldDBPage::~SwFldDBPage()
105 {
106 }
107 
108 /*--------------------------------------------------------------------
109     Beschreibung: TabPage initialisieren
110  --------------------------------------------------------------------*/
111 
112 void __EXPORT SwFldDBPage::Reset(const SfxItemSet&)
113 {
114     Init(); // Allgemeine initialisierung
115 
116     aTypeLB.SetUpdateMode(sal_False);
117     sal_uInt16 nOldPos = aTypeLB.GetSelectEntryPos();
118     sOldDBName = aDatabaseTLB.GetDBName(sOldTableName, sOldColumnName);
119 
120     aTypeLB.Clear();
121 
122     sal_uInt16 nPos, nTypeId, i;
123 
124     if (!IsFldEdit())
125     {
126         // TypeListBox initialisieren
127         const SwFldGroupRgn& rRg = GetFldMgr().GetGroupRange(IsFldDlgHtmlMode(), GetGroup());
128 
129         for(i = rRg.nStart; i < rRg.nEnd; ++i)
130         {
131             nTypeId = GetFldMgr().GetTypeId(i);
132             nPos = aTypeLB.InsertEntry(GetFldMgr().GetTypeStr(i));
133             aTypeLB.SetEntryData(nPos, reinterpret_cast<void*>(nTypeId));
134         }
135     }
136     else
137     {
138         nTypeId = GetCurField()->GetTypeId();
139         nPos = aTypeLB.InsertEntry(GetFldMgr().GetTypeStr(GetFldMgr().GetPos(nTypeId)));
140         aTypeLB.SetEntryData(nPos, reinterpret_cast<void*>(nTypeId));
141     }
142 
143     // alte Pos selektieren
144     if (GetTypeSel() != LISTBOX_ENTRY_NOTFOUND)
145         aTypeLB.SelectEntryPos(GetTypeSel());
146 
147     aFormatLB.Clear();
148 
149     sal_uInt16 nSize = GetFldMgr().GetFormatCount(TYP_DBSETNUMBERFLD, sal_False, IsFldDlgHtmlMode());
150     for( i = 0; i < nSize; ++i )
151     {
152         sal_uInt16 nEntryPos = aFormatLB.InsertEntry(GetFldMgr().GetFormatStr(TYP_DBSETNUMBERFLD, i));
153         sal_uInt16 nFmtId = GetFldMgr().GetFormatId( TYP_DBSETNUMBERFLD, i );
154         aFormatLB.SetEntryData( nEntryPos, reinterpret_cast<void*>(nFmtId) );
155         if( SVX_NUM_ARABIC == nFmtId )
156             aFormatLB.SelectEntryPos( nEntryPos );
157     }
158 
159     if (!IsFldEdit())
160     {
161         if (nOldPos != LISTBOX_ENTRY_NOTFOUND)
162             aTypeLB.SelectEntryPos(nOldPos);
163 
164         if (sOldDBName.Len())
165         {
166             aDatabaseTLB.Select(sOldDBName, sOldTableName, sOldColumnName);
167         }
168         else
169         {
170             SwWrtShell *pSh = GetWrtShell();
171             if(!pSh)
172                 pSh = ::GetActiveWrtShell();
173             if(pSh)
174             {
175                 SwDBData aTmp(pSh->GetDBData());
176                 aDatabaseTLB.Select(aTmp.sDataSource, aTmp.sCommand, aEmptyStr);
177             }
178         }
179     }
180 
181     if( !IsRefresh() )
182     {
183         String sUserData = GetUserData();
184         if(sUserData.GetToken(0, ';').EqualsIgnoreCaseAscii(USER_DATA_VERSION_1))
185         {
186             String sVal = sUserData.GetToken(1, ';');
187             sal_uInt16 nVal = (sal_uInt16)sVal.ToInt32();
188             if(nVal != USHRT_MAX)
189             {
190                 for(i = 0; i < aTypeLB.GetEntryCount(); i++)
191                     if(nVal == (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(i))
192                     {
193                         aTypeLB.SelectEntryPos(i);
194                         break;
195                     }
196             }
197         }
198     }
199     TypeHdl(0);
200 
201     aTypeLB.SetUpdateMode(sal_True);
202     aTypeLB.SetSelectHdl(LINK(this, SwFldDBPage, TypeHdl));
203     aTypeLB.SetDoubleClickHdl(LINK(this, SwFldDBPage, InsertHdl));
204 
205     if (IsFldEdit())
206     {
207         aConditionED.SaveValue();
208         aValueED.SaveValue();
209         sOldDBName = aDatabaseTLB.GetDBName(sOldTableName, sOldColumnName);
210         nOldFormat = GetCurField()->GetFormat();
211         nOldSubType = GetCurField()->GetSubType();
212     }
213 }
214 
215 /*--------------------------------------------------------------------
216     Beschreibung:
217  --------------------------------------------------------------------*/
218 
219 sal_Bool __EXPORT SwFldDBPage::FillItemSet(SfxItemSet& )
220 {
221     String sTableName, sColumnName;
222     SwDBData aData;
223     sal_Bool bIsTable;
224     aData.sDataSource = aDatabaseTLB.GetDBName(sTableName, sColumnName, &bIsTable);
225     aData.sCommand = sTableName;
226     aData.nCommandType = bIsTable ? 0 : 1;
227     SwWrtShell *pSh = GetWrtShell();
228     if(!pSh)
229         pSh = ::GetActiveWrtShell();
230 
231     if (!aData.sDataSource.getLength())
232         aData = pSh->GetDBData();
233 
234     if(aData.sDataSource.getLength())       // Ohne Datenbank kein neuer Feldbefehl
235     {
236         sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
237         String aVal(aValueED.GetText());
238         String aName(aConditionED.GetText());
239         sal_uLong nFormat = 0;
240         sal_uInt16 nSubType = 0;
241 
242         String sDBName = aData.sDataSource;
243         sDBName += DB_DELIM;
244         sDBName += (String)aData.sCommand;
245         sDBName += DB_DELIM;
246         sDBName += String::CreateFromInt32(aData.nCommandType);
247         sDBName += DB_DELIM;
248         if(sColumnName.Len())
249         {
250             sDBName += sColumnName;
251             sDBName += DB_DELIM;
252         }
253         aName.Insert(sDBName, 0);
254 
255         switch (nTypeId)
256         {
257         case TYP_DBFLD:
258             nFormat = aNumFormatLB.GetFormat();
259             if (aNewFormatRB.IsEnabled() && aNewFormatRB.IsChecked())
260                 nSubType = nsSwExtendedSubType::SUB_OWN_FMT;
261             aName = sDBName;
262             break;
263 
264         case TYP_DBSETNUMBERFLD:
265             nFormat = (sal_uInt16)(sal_uLong)aFormatLB.GetEntryData(
266                                 aFormatLB.GetSelectEntryPos() );
267             break;
268         }
269 
270 
271         String sTempDBName, sTempTableName, sTempColumnName;
272         sTempDBName = aDatabaseTLB.GetDBName(sTempTableName, sTempColumnName);
273         sal_Bool bDBListBoxChanged = sOldDBName != sTempDBName ||
274             sOldTableName != sTempTableName || sOldColumnName != sTempColumnName;
275         if (!IsFldEdit() ||
276             aConditionED.GetSavedValue() != aConditionED.GetText() ||
277              aValueED.GetSavedValue() != aValueED.GetText() ||
278              bDBListBoxChanged ||
279              nOldFormat != nFormat || nOldSubType != nSubType)
280         {
281             InsertFld( nTypeId, nSubType, aName, aVal, nFormat);
282         }
283     }
284 
285     return sal_False;
286 }
287 
288 /*--------------------------------------------------------------------
289     Beschreibung:
290  --------------------------------------------------------------------*/
291 
292 SfxTabPage* __EXPORT SwFldDBPage::Create(   Window* pParent,
293                         const SfxItemSet& rAttrSet )
294 {
295     return ( new SwFldDBPage( pParent, rAttrSet ) );
296 }
297 
298 /*--------------------------------------------------------------------
299     Beschreibung:
300  --------------------------------------------------------------------*/
301 
302 sal_uInt16 SwFldDBPage::GetGroup()
303 {
304     return GRP_DB;
305 }
306 
307 /*--------------------------------------------------------------------
308     Beschreibung:
309  --------------------------------------------------------------------*/
310 
311 IMPL_LINK( SwFldDBPage, TypeHdl, ListBox *, pBox )
312 {
313     // Alte ListBoxPos sichern
314     const sal_uInt16 nOld = GetTypeSel();
315 
316     // Aktuelle ListBoxPos
317     SetTypeSel(aTypeLB.GetSelectEntryPos());
318 
319     if(GetTypeSel() == LISTBOX_ENTRY_NOTFOUND)
320     {
321         SetTypeSel(0);
322         aTypeLB.SelectEntryPos(0);
323     }
324 
325     if (nOld != GetTypeSel())
326     {
327         SwWrtShell *pSh = GetWrtShell();
328         if(!pSh)
329             pSh = ::GetActiveWrtShell();
330         sal_Bool bCond = sal_False, bSetNo = sal_False, bFormat = sal_False, bDBFormat = sal_False;
331         sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
332 
333         aDatabaseTLB.ShowColumns(nTypeId == TYP_DBFLD);
334 
335         if (IsFldEdit())
336         {
337             SwDBData aData;
338             String sColumnName;
339             if (nTypeId == TYP_DBFLD)
340             {
341                 aData = ((SwDBField*)GetCurField())->GetDBData();
342                 sColumnName = ((SwDBFieldType*)GetCurField()->GetTyp())->GetColumnName();
343             }
344             else
345             {
346                 aData = ((SwDBNameInfField*)GetCurField())->GetDBData(pSh->GetDoc());
347             }
348             aDatabaseTLB.Select(aData.sDataSource, aData.sCommand, sColumnName);
349         }
350 
351         switch (nTypeId)
352         {
353             case TYP_DBFLD:
354                 bFormat = sal_True;
355                 bDBFormat = sal_True;
356                 aNumFormatLB.Show();
357                 aFormatLB.Hide();
358 
359                 if (pBox)   // Typ wurde vom User geaendert
360                     aDBFormatRB.Check();
361 
362                 if (IsFldEdit())
363                 {
364                     if (GetCurField()->GetFormat() != 0 && GetCurField()->GetFormat() != SAL_MAX_UINT32)
365                         aNumFormatLB.SetDefFormat(GetCurField()->GetFormat());
366 
367                     if (GetCurField()->GetSubType() & nsSwExtendedSubType::SUB_OWN_FMT)
368                         aNewFormatRB.Check();
369                     else
370                         aDBFormatRB.Check();
371                 }
372                 break;
373 
374             case TYP_DBNUMSETFLD:
375                 bSetNo = sal_True;
376                 // kein break!
377             case TYP_DBNEXTSETFLD:
378                 bCond = sal_True;
379                 if (IsFldEdit())
380                 {
381                     aConditionED.SetText(GetCurField()->GetPar1());
382                     aValueED.SetText(GetCurField()->GetPar2());
383                 }
384                 break;
385 
386             case TYP_DBNAMEFLD:
387                 break;
388 
389             case TYP_DBSETNUMBERFLD:
390                 bFormat = sal_True;
391                 aNewFormatRB.Check();
392                 aNumFormatLB.Hide();
393                 aFormatLB.Show();
394                 if( IsFldEdit() )
395                 {
396                     for( sal_uInt16 nI = aFormatLB.GetEntryCount(); nI; )
397                         if( GetCurField()->GetFormat() == (sal_uInt16)(sal_uLong)
398                             aFormatLB.GetEntryData( --nI ))
399                         {
400                             aFormatLB.SelectEntryPos( nI );
401                             break;
402                         }
403                 }
404                 break;
405         }
406 
407         aConditionFT.Enable(bCond);
408         aConditionED.Enable(bCond);
409         aValueFT.Enable(bSetNo);
410         aValueED.Enable(bSetNo);
411         if (nTypeId != TYP_DBFLD)
412         {
413             aDBFormatRB.Enable(bDBFormat);
414             aNewFormatRB.Enable(bDBFormat|bFormat);
415             aNumFormatLB.Enable(bDBFormat);
416             aFormatLB.Enable(bFormat);
417         }
418         aFormatFL.Enable(bDBFormat|bFormat);
419 
420         if (!IsFldEdit())
421         {
422             aValueED.SetText(aEmptyStr);
423             if (bCond)
424                 aConditionED.SetText( String::CreateFromAscii(
425                         RTL_CONSTASCII_STRINGPARAM( "sal_True" )));
426             else
427                 aConditionED.SetText(aEmptyStr);
428         }
429 
430         CheckInsert();
431     }
432 
433     return 0;
434 }
435 
436 /*--------------------------------------------------------------------
437     Beschreibung:
438  --------------------------------------------------------------------*/
439 
440 IMPL_LINK( SwFldDBPage, NumSelectHdl, NumFormatListBox *, pLB )
441 {
442     aNewFormatRB.Check();
443     aOldNumSelectHdl.Call(pLB);
444 
445     return 0;
446 }
447 
448 /*---------------------------------------------------------------------
449     Beschreibung:
450 ---------------------------------------------------------------------*/
451 
452 void SwFldDBPage::CheckInsert()
453 {
454     sal_Bool bInsert = sal_True;
455     sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
456 
457     SvLBoxEntry* pEntry = aDatabaseTLB.GetCurEntry();
458 
459     if (pEntry)
460     {
461         pEntry = aDatabaseTLB.GetParent(pEntry);
462 
463         if (nTypeId == TYP_DBFLD && pEntry)
464             pEntry = aDatabaseTLB.GetParent(pEntry);
465 
466         bInsert &= pEntry != 0;
467     }
468     else
469         bInsert = sal_False;
470 
471     if (nTypeId == TYP_DBNUMSETFLD)
472     {
473         sal_Bool bHasValue = aValueED.GetText().Len() != 0;
474 
475         bInsert &= bHasValue;
476     }
477 
478     EnableInsert(bInsert);
479 }
480 
481 /*---------------------------------------------------------------------
482     Beschreibung:
483 ---------------------------------------------------------------------*/
484 
485 IMPL_LINK( SwFldDBPage, TreeSelectHdl, SvTreeListBox *, pBox )
486 {
487     SvLBoxEntry* pColEntry;
488     SvLBoxEntry* pEntry = pColEntry = pBox->GetCurEntry();
489     if (pEntry)
490     {
491         sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
492 
493         pEntry = aDatabaseTLB.GetParent(pEntry);
494 
495         if (nTypeId == TYP_DBFLD && pEntry)
496             pEntry = aDatabaseTLB.GetParent(pEntry);
497 
498         CheckInsert();
499 
500         if (nTypeId == TYP_DBFLD)
501         {
502             sal_Bool bNumFormat = sal_False;
503 
504             if (pEntry != 0)
505             {
506                 String sTableName;
507                 String sColumnName;
508                 sal_Bool bIsTable;
509                 String sDBName = aDatabaseTLB.GetDBName(sTableName, sColumnName, &bIsTable);
510                 bNumFormat = GetFldMgr().IsDBNumeric(sDBName,
511                             sTableName,
512                             bIsTable,
513                             sColumnName);
514                 if (!IsFldEdit())
515                     aDBFormatRB.Check();
516             }
517 
518             aDBFormatRB.Enable(bNumFormat);
519             aNewFormatRB.Enable(bNumFormat);
520             aNumFormatLB.Enable(bNumFormat);
521             aFormatFL.Enable(bNumFormat);
522         }
523     }
524     return 0;
525 }
526 /*-- 27.05.2004 09:14:01---------------------------------------------------
527 
528   -----------------------------------------------------------------------*/
529 IMPL_LINK( SwFldDBPage, AddDBHdl, PushButton *, EMPTYARG )
530 {
531     String sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
532     if(sNewDB.Len())
533     {
534         aDatabaseTLB.AddDataSource(sNewDB);
535     }
536     return 0;
537 }
538 
539 /*--------------------------------------------------------------------
540     Beschreibung: Modify
541  --------------------------------------------------------------------*/
542 
543 IMPL_LINK( SwFldDBPage, ModifyHdl, Edit *, EMPTYARG )
544 {
545     CheckInsert();
546     return 0;
547 }
548 
549 /* -----------------12.01.99 11:21-------------------
550  *
551  * --------------------------------------------------*/
552 void    SwFldDBPage::FillUserData()
553 {
554     String sData( String::CreateFromAscii(
555                         RTL_CONSTASCII_STRINGPARAM( USER_DATA_VERSION )));
556     sData += ';';
557     sal_uInt16 nTypeSel = aTypeLB.GetSelectEntryPos();
558 
559     if( LISTBOX_ENTRY_NOTFOUND == nTypeSel )
560         nTypeSel = USHRT_MAX;
561     else
562         nTypeSel = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData( nTypeSel );
563     sData += String::CreateFromInt32( nTypeSel );
564     SetUserData(sData);
565 }
566 /* -----------------12.12.2002 11:33-----------------
567  *
568  * --------------------------------------------------*/
569 void SwFldDBPage::ActivateMailMergeAddress()
570 {
571     sal_uLong nData = TYP_DBFLD;
572     aTypeLB.SelectEntryPos(aTypeLB.GetEntryPos( (const void*) nData ));
573     aTypeLB.GetSelectHdl().Call(&aTypeLB);
574     const SwDBData& rData = SW_MOD()->GetDBConfig()->GetAddressSource();
575     aDatabaseTLB.Select(rData.sDataSource, rData.sCommand, aEmptyStr);
576 }
577 /*-- 07.10.2003 13:59:04---------------------------------------------------
578 
579   -----------------------------------------------------------------------*/
580 void SwFldDBPage::SetWrtShell(SwWrtShell& rSh)
581 {
582     aDatabaseTLB.SetWrtShell(rSh);
583 }
584