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