xref: /trunk/main/sw/source/ui/fldui/fldpage.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 
31 #ifdef SW_DLLIMPLEMENTATION
32 #undef SW_DLLIMPLEMENTATION
33 #endif
34 
35 
36 #include <vcl/lstbox.hxx>
37 #include <svl/stritem.hxx>
38 #include <sfx2/request.hxx>
39 #include <svx/htmlmode.hxx>
40 #include <dbfld.hxx>
41 #include <flddat.hxx>
42 #include <fmtfld.hxx>
43 #include <viewopt.hxx>
44 #include <fldedt.hxx>
45 #include <docsh.hxx>
46 #include <swmodule.hxx>
47 #include <view.hxx>
48 #include <wrtsh.hxx>
49 #include <expfld.hxx>
50 #include <fldtdlg.hxx>
51 #include <fldpage.hxx>
52 #include <docufld.hxx>
53 #include <cmdid.h>
54 #include <globals.hrc>
55 #include <sfx2/bindings.hxx>
56 #include <switerator.hxx>
57 
58 using namespace ::com::sun::star;
59 
60 /*--------------------------------------------------------------------
61     Beschreibung:
62  --------------------------------------------------------------------*/
63 
64 SwFldPage::SwFldPage( Window *pParent, const ResId &rId,
65                         const SfxItemSet &rAttrSet )
66     :SfxTabPage     (pParent, rId, rAttrSet),
67     m_pCurFld       (0),
68     m_pWrtShell     (0),
69     m_nPageId       ( static_cast< sal_uInt16 >(rId.GetId()) ),
70     m_nTypeSel      (LISTBOX_ENTRY_NOTFOUND),
71     m_nSelectionSel (LISTBOX_ENTRY_NOTFOUND),
72     m_bFldEdit      (sal_False),
73     m_bInsert           (sal_True),
74     m_bFldDlgHtmlMode   (sal_False),
75     m_bRefresh          (sal_False),
76     m_bFirstHTMLInit    (sal_True)
77 {
78 //  FreeResource();
79 }
80 
81 /*--------------------------------------------------------------------
82     Beschreibung:
83  --------------------------------------------------------------------*/
84 
85 SwFldPage::~SwFldPage()
86 {
87 }
88 
89 /*--------------------------------------------------------------------
90     Beschreibung: TabPage initialisieren
91  --------------------------------------------------------------------*/
92 
93 void SwFldPage::Init()
94 {
95     SwDocShell* pDocSh = (SwDocShell*)SfxObjectShell::Current();
96     sal_Bool bNewMode = 0 != (::GetHtmlMode(pDocSh) & HTMLMODE_ON);
97 
98     m_bFldEdit = 0 == GetTabDialog();
99 
100     // FieldManager neu initialisieren wichtig fuer
101     // Dok-Wechsel (fldtdlg:ReInitTabPage)
102     m_pCurFld = m_aMgr.GetCurFld();
103 
104     if( bNewMode != m_bFldDlgHtmlMode )
105     {
106         m_bFldDlgHtmlMode = bNewMode;
107 
108         // Bereichslistbox initialisieren
109         if( m_bFldDlgHtmlMode && m_bFirstHTMLInit )
110         {
111             m_bFirstHTMLInit = sal_False;
112             SwWrtShell *pSh = m_pWrtShell;
113             if(! pSh)
114                 pSh = ::GetActiveWrtShell();
115             if(pSh)
116             {
117                 SwDoc* pDoc = pSh->GetDoc();
118                 pSh->InsertFldType( SwSetExpFieldType( pDoc,
119                                     String::CreateFromAscii("HTML_ON"), 1));
120                 pSh->InsertFldType( SwSetExpFieldType(pDoc,
121                                     String::CreateFromAscii("HTML_OFF"), 1));
122             }
123         }
124     }
125 }
126 
127 /*--------------------------------------------------------------------
128      Beschreibung: Seite neu initialisieren
129  --------------------------------------------------------------------*/
130 
131 void SwFldPage::ActivatePage()
132 {
133     EnableInsert(m_bInsert);
134 }
135 
136 /*--------------------------------------------------------------------
137      Beschreibung: Kompletter Reset; neues Feld editieren
138  --------------------------------------------------------------------*/
139 
140 void SwFldPage::EditNewField( sal_Bool bOnlyActivate )
141 {
142     if( !bOnlyActivate )
143     {
144         m_nTypeSel = LISTBOX_ENTRY_NOTFOUND;
145     }
146     m_nSelectionSel = LISTBOX_ENTRY_NOTFOUND;
147     m_bRefresh = sal_True;
148     Reset(*(SfxItemSet*)0);
149     m_bRefresh = sal_False;
150 }
151 
152 /*--------------------------------------------------------------------
153      Beschreibung: Feld einfuegen
154  --------------------------------------------------------------------*/
155 
156 sal_Bool SwFldPage::InsertFld(sal_uInt16 nTypeId, sal_uInt16 nSubType, const String& rPar1,
157                             const String& rPar2, sal_uLong nFormatId,
158                             sal_Unicode cSeparator, sal_Bool bIsAutomaticLanguage)
159 {
160     sal_Bool bRet = sal_False;
161     SwView* pView = GetActiveView();
162     SwWrtShell *pSh = m_pWrtShell ? m_pWrtShell : pView->GetWrtShellPtr();
163 
164     if (!IsFldEdit())   // Neues Feld einfuegen
165     {
166         SwInsertFld_Data aData(nTypeId, nSubType, rPar1, rPar2, nFormatId, 0, cSeparator, bIsAutomaticLanguage );
167         //#i26566# provide parent for SwWrtShell::StartInputFldDlg
168         aData.pParent = &GetTabDialog()->GetOKButton();
169         bRet = m_aMgr.InsertFld( aData );
170 
171         uno::Reference< frame::XDispatchRecorder > xRecorder =
172                 pView->GetViewFrame()->GetBindings().GetRecorder();
173         if ( xRecorder.is() )
174         {
175             sal_Bool bRecordDB = TYP_DBFLD == nTypeId ||
176                             TYP_DBSETNUMBERFLD == nTypeId ||
177                             TYP_DBNUMSETFLD == nTypeId ||
178                             TYP_DBNEXTSETFLD == nTypeId ||
179                             TYP_DBNAMEFLD == nTypeId ;
180 
181             SfxRequest aReq( pView->GetViewFrame(),
182                     bRecordDB ?  FN_INSERT_DBFIELD : FN_INSERT_FIELD );
183             if(bRecordDB)
184             {
185                 aReq.AppendItem(SfxStringItem
186                         (FN_INSERT_DBFIELD,rPar1.GetToken(0, DB_DELIM)));
187                 aReq.AppendItem(SfxStringItem
188                         (FN_PARAM_1,rPar1.GetToken(1, DB_DELIM)));
189                 aReq.AppendItem(SfxInt32Item
190                         (FN_PARAM_3,rPar1.GetToken(1, DB_DELIM).ToInt32()));
191                 aReq.AppendItem(SfxStringItem
192                         (FN_PARAM_2,rPar1.GetToken(3, DB_DELIM)));
193             }
194             else
195             {
196                 aReq.AppendItem(SfxStringItem(FN_INSERT_FIELD, rPar1));
197                 aReq.AppendItem(SfxStringItem
198                         (FN_PARAM_3,String(cSeparator)));
199                 aReq.AppendItem(SfxUInt16Item(FN_PARAM_FIELD_SUBTYPE, nSubType));
200             }
201             aReq.AppendItem(SfxUInt16Item(FN_PARAM_FIELD_TYPE   , nTypeId));
202             aReq.AppendItem(SfxStringItem(FN_PARAM_FIELD_CONTENT, rPar2));
203             aReq.AppendItem(SfxUInt32Item(FN_PARAM_FIELD_FORMAT , nFormatId));
204             aReq.Done();
205         }
206 
207     }
208     else    // Feld aendern
209     {
210         SwField *const pTmpFld = m_pCurFld->CopyField();
211 
212         String sPar1(rPar1);
213         String sPar2(rPar2);
214         sal_Bool bDBChanged = sal_False;
215         switch( nTypeId )
216         {
217         case TYP_DATEFLD:
218         case TYP_TIMEFLD:
219             nSubType = static_cast< sal_uInt16 >(((nTypeId == TYP_DATEFLD) ? DATEFLD : TIMEFLD) |
220                        ((nSubType == DATE_VAR) ? 0 : FIXEDFLD));
221             break;
222 
223         case TYP_DBNAMEFLD:
224         case TYP_DBNEXTSETFLD:
225         case TYP_DBNUMSETFLD:
226         case TYP_DBSETNUMBERFLD:
227             {
228                 xub_StrLen nPos = 0;
229                 SwDBData aData;
230 
231                 aData.sDataSource = rPar1.GetToken(0, DB_DELIM, nPos);
232                 aData.sCommand = rPar1.GetToken(0, DB_DELIM, nPos);
233                 aData.nCommandType = rPar1.GetToken(0, DB_DELIM, nPos).ToInt32();
234                 sPar1 = rPar1.Copy(nPos);
235 
236                 ((SwDBNameInfField*)pTmpFld)->SetDBData(aData);
237                 bDBChanged = sal_True;
238             }
239             break;
240 
241         case TYP_DBFLD:
242             {
243                 SwDBData aData;
244                 aData.sDataSource = rPar1.GetToken(0, DB_DELIM);
245                 aData.sCommand = rPar1.GetToken(1, DB_DELIM);
246                 aData.nCommandType = rPar1.GetToken(2, DB_DELIM).ToInt32();
247                 String sColumn = rPar1.GetToken(3, DB_DELIM);
248 
249                 SwDBFieldType* pOldTyp = (SwDBFieldType*)pTmpFld->GetTyp();
250                 SwDBFieldType* pTyp = (SwDBFieldType*)pSh->InsertFldType(
251                         SwDBFieldType(pSh->GetDoc(), sColumn, aData));
252 
253                 SwIterator<SwFmtFld,SwFieldType> aIter( *pOldTyp );
254 
255                 for( SwFmtFld* pFmtFld = aIter.First(); pFmtFld; pFmtFld = aIter.Next() )
256                 {
257                     if( pFmtFld->GetFld() == m_pCurFld)
258                     {
259                         pFmtFld->RegisterToFieldType(*pTyp);
260                         pTmpFld->ChgTyp(pTyp);
261                         break;
262                     }
263                 }
264                 bDBChanged = sal_True;
265             }
266             break;
267 
268         case TYP_SEQFLD:
269             {
270                 SwSetExpFieldType* pTyp = (SwSetExpFieldType*)pTmpFld->GetTyp();
271                 pTyp->SetOutlineLvl( static_cast< sal_uInt8 >(nSubType & 0xff));
272                 pTyp->SetDelimiter(cSeparator);
273 
274                 nSubType = nsSwGetSetExpType::GSE_SEQ;
275             }
276             break;
277 
278         case TYP_INPUTFLD:
279             {
280                 // User- oder SetField ?
281                 if (m_aMgr.GetFldType(RES_USERFLD, sPar1) == 0 &&
282                 !(pTmpFld->GetSubType() & INP_TXT)) // SETEXPFLD
283                 {
284                     SwSetExpField* pFld = (SwSetExpField*)pTmpFld;
285                     pFld->SetPromptText(sPar2);
286                     sPar2 = pFld->GetPar2();
287                 }
288             }
289             break;
290         case TYP_DOCINFOFLD:
291             {
292                 if( nSubType == nsSwDocInfoSubType::DI_CUSTOM )
293                 {
294                     SwDocInfoField* pDocInfo = static_cast<SwDocInfoField*>( pTmpFld );
295                     pDocInfo->SetName( rPar1 );
296                 }
297             }
298             break;
299         }
300 
301         pSh->StartAllAction();
302 
303         pTmpFld->SetSubType(nSubType);
304         pTmpFld->SetAutomaticLanguage(bIsAutomaticLanguage);
305 
306         m_aMgr.UpdateCurFld( nFormatId, sPar1, sPar2, pTmpFld );
307 
308         m_pCurFld = m_aMgr.GetCurFld();
309 
310         switch (nTypeId)
311         {
312             case TYP_HIDDENTXTFLD:
313             case TYP_HIDDENPARAFLD:
314                 m_aMgr.EvalExpFlds(pSh);
315                 break;
316         }
317 
318         pSh->SetUndoNoResetModified();
319         pSh->EndAllAction();
320     }
321 
322     return bRet;
323 }
324 
325 /*--------------------------------------------------------------------
326     Beschreibung:
327  --------------------------------------------------------------------*/
328 
329 void SwFldPage::SavePos( const ListBox* pLst1, const ListBox* pLst2,
330                          const ListBox* pLst3 )
331 {
332     const ListBox* aLBArr [ coLBCount ] = { pLst1, pLst2, pLst3 };
333 
334     const ListBox** ppLB = aLBArr;
335     for( int i = 0; i < coLBCount; ++i, ++ppLB )
336         if( (*ppLB) && (*ppLB)->GetEntryCount() )
337             m_aLstStrArr[ i ] = (*ppLB)->GetSelectEntry();
338         else
339             m_aLstStrArr[ i ].Erase();
340 }
341 
342 /*--------------------------------------------------------------------
343     Beschreibung:
344  --------------------------------------------------------------------*/
345 
346 void SwFldPage::RestorePos(ListBox* pLst1, ListBox* pLst2, ListBox* pLst3)
347 {
348     sal_uInt16 nPos = 0;
349     ListBox* aLBArr [ coLBCount ] = { pLst1, pLst2, pLst3 };
350     ListBox** ppLB = aLBArr;
351     for( int i = 0; i < coLBCount; ++i, ++ppLB )
352         if( (*ppLB) && (*ppLB)->GetEntryCount() && m_aLstStrArr[ i ].Len() &&
353             LISTBOX_ENTRY_NOTFOUND !=
354                         ( nPos = (*ppLB)->GetEntryPos(m_aLstStrArr[ i ] ) ) )
355             (*ppLB)->SelectEntryPos( nPos );
356 }
357 
358 /*--------------------------------------------------------------------
359      Beschreibung: Einfuegen von neuen Feldern
360  --------------------------------------------------------------------*/
361 
362 IMPL_LINK( SwFldPage, InsertHdl, Button *, pBtn )
363 {
364     SwFldDlg *pDlg = (SwFldDlg*)GetTabDialog();
365 
366     if (pDlg)
367     {
368         pDlg->InsertHdl();
369 
370         if (pBtn)
371             pBtn->GrabFocus();  // Wegen InputField-Dlg
372     }
373     else
374     {
375         SwFldEditDlg *pEditDlg = (SwFldEditDlg *)GetParent();
376         pEditDlg->InsertHdl();
377     }
378 
379     return 0;
380 }
381 
382 /*--------------------------------------------------------------------
383      Beschreibung: "Einfuegen"-Button Enablen/Disablen
384  --------------------------------------------------------------------*/
385 
386 void SwFldPage::EnableInsert(sal_Bool bEnable)
387 {
388     SwFldDlg *pDlg = (SwFldDlg*)GetTabDialog();
389 
390     if (pDlg)
391     {
392         if (pDlg->GetCurPageId() == m_nPageId)
393             pDlg->EnableInsert(bEnable);
394     }
395     else
396     {
397         SwFldEditDlg *pEditDlg = (SwFldEditDlg *)GetParent();
398         pEditDlg->EnableInsert(bEnable);
399     }
400 
401     m_bInsert = bEnable;
402 }
403 
404 /*--------------------------------------------------------------------
405      Beschreibung:
406  --------------------------------------------------------------------*/
407 
408 IMPL_LINK( SwFldPage, NumFormatHdl, ListBox *, EMPTYARG )
409 {
410     InsertHdl();
411 
412     return 0;
413 }
414 
415 void SwFldPage::SetWrtShell( SwWrtShell* pShell )
416 {
417     m_pWrtShell = pShell;
418     m_aMgr.SetWrtShell( pShell );
419 }
420