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