xref: /aoo42x/main/sw/source/ui/fldui/fldtdlg.cxx (revision efeef26f)
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 
33 #ifndef _CMDID_H
34 #include <cmdid.h>
35 #endif
36 #include <swtypes.hxx>
37 #include <unotools/confignode.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <sfx2/basedlgs.hxx>
40 #include <sfx2/dispatch.hxx>
41 #include <vcl/msgbox.hxx>
42 #include <svx/htmlmode.hxx>
43 #include <viewopt.hxx>
44 #include <docsh.hxx>
45 #include <fldwrap.hxx>
46 #include <flddb.hxx>
47 #include <flddinf.hxx>
48 #include <fldvar.hxx>
49 #include <flddok.hxx>
50 #include <fldfunc.hxx>
51 #include <fldref.hxx>
52 #include <wrtsh.hxx>
53 #include <view.hxx>
54 #include <fldtdlg.hxx>
55 #include <swmodule.hxx>
56 
57 #include <helpid.h>
58 #include <fldui.hrc>
59 #include <globals.hrc>
60 #include <fldtdlg.hrc>
61 
62 #include <com/sun/star/document/XDocumentProperties.hpp>
63 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
64 
65 
66 /*--------------------------------------------------------------------
67 	Beschreibung:	Der Traeger des Dialoges
68  --------------------------------------------------------------------*/
69 
70 
SwFldDlg(SfxBindings * pB,SwChildWinWrapper * pCW,Window * pParent)71 SwFldDlg::SwFldDlg(SfxBindings* pB, SwChildWinWrapper* pCW, Window *pParent)
72 	: SfxTabDialog( pParent, SW_RES( DLG_FLD_INSERT )),
73     m_pChildWin(pCW),
74     m_pBindings(pB),
75     m_bDataBaseMode(sal_False)
76 {
77 	SetStyle(GetStyle()|WB_STDMODELESS);
78     m_bHtmlMode = (::GetHtmlMode((SwDocShell*)SfxObjectShell::Current()) & HTMLMODE_ON) != 0;
79 
80 	RemoveResetButton();
81 
82 	GetOKButton().SetText(String(SW_RES(STR_FLD_INSERT)));
83 	GetOKButton().SetHelpId(HID_FIELD_INSERT);
84 	GetOKButton().SetHelpText(aEmptyStr);	// Damit generierter Hilfetext verwendet wird
85 
86 	GetCancelButton().SetText(String(SW_RES(STR_FLD_CLOSE)));
87 	GetCancelButton().SetHelpId(HID_FIELD_CLOSE);
88 	GetCancelButton().SetHelpText(aEmptyStr);	// Damit generierter Hilfetext verwendet wird
89 
90 	FreeResource();
91 
92 	GetOKButton().SetClickHdl(LINK(this, SwFldDlg, OKHdl));
93 
94 	AddTabPage(TP_FLD_DOK, SwFldDokPage::Create, 0);
95 	AddTabPage(TP_FLD_VAR, SwFldVarPage::Create, 0);
96 	AddTabPage(TP_FLD_DOKINF, SwFldDokInfPage::Create, 0);
97 
98     if (!m_bHtmlMode)
99 	{
100 		AddTabPage(TP_FLD_REF, SwFldRefPage::Create, 0);
101 		AddTabPage(TP_FLD_FUNC, SwFldFuncPage::Create, 0);
102 
103         utl::OConfigurationTreeRoot aCfgRoot
104             = utl::OConfigurationTreeRoot::createWithServiceFactory(
105                 ::comphelper::getProcessServiceFactory(),
106                 rtl::OUString(
107                     RTL_CONSTASCII_USTRINGPARAM(
108                         "/org.openoffice.Office.DataAccess/Policies/Features/Writer" ) ),
109                 -1,
110                 utl::OConfigurationTreeRoot::CM_READONLY);
111 
112         sal_Bool bDatabaseFields = sal_True;
113         aCfgRoot.getNodeValue(
114             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DatabaseFields"))) >>= bDatabaseFields;
115 
116         if (bDatabaseFields)
117             AddTabPage(TP_FLD_DB, SwFldDBPage::Create, 0);
118         else
119             RemoveTabPage(TP_FLD_DB);
120 	}
121 	else
122 	{
123 		RemoveTabPage(TP_FLD_REF);
124 		RemoveTabPage(TP_FLD_FUNC);
125 		RemoveTabPage(TP_FLD_DB);
126 	}
127 }
128 
129 /*--------------------------------------------------------------------
130 	Beschreibung:
131  --------------------------------------------------------------------*/
132 
~SwFldDlg()133 SwFldDlg::~SwFldDlg()
134 {
135 }
136 
137 /*--------------------------------------------------------------------
138 	Beschreibung:
139  --------------------------------------------------------------------*/
140 
Close()141 sal_Bool SwFldDlg::Close()
142 {
143     m_pBindings->GetDispatcher()->
144         Execute(m_bDataBaseMode ? FN_INSERT_FIELD_DATA_ONLY : FN_INSERT_FIELD,
145         SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD);
146 	return sal_True;
147 }
148 
149 /*--------------------------------------------------------------------
150 	Beschreibung:
151  --------------------------------------------------------------------*/
152 
Initialize(SfxChildWinInfo * pInfo)153 void SwFldDlg::Initialize(SfxChildWinInfo *pInfo)
154 {
155 	Point aPos;
156 	Size aSize;
157 
158 	if ( pInfo->aSize.Width() != 0 && pInfo->aSize.Height() != 0 )
159 	{
160 		aPos = pInfo->aPos;
161 		if ( GetStyle() & WB_SIZEABLE )
162 			SetSizePixel( pInfo->aSize );
163 
164 		// Initiale Gr"o\se aus pInfo merken
165 		aSize = GetSizePixel();
166 
167 		// Soll das FloatingWindow eingezoomt werden ?
168 		if ( pInfo->nFlags & SFX_CHILDWIN_ZOOMIN )
169 			RollUp();
170 	}
171 	else
172 	{
173 		// Initiale Gr"o\se aus Resource oder ctor merken
174 		aSize = GetSizePixel();
175 
176 		Size aParentSize = GetParent()->GetOutputSizePixel();
177 		aPos.X() += ( aParentSize.Width() - aSize.Width() ) / 2;
178 		aPos.Y() += ( aParentSize.Height() - aSize.Height() ) / 2;
179 	}
180 
181 	Point aPoint;
182 	Rectangle aRect = GetDesktopRectPixel();
183 	aPoint.X() = aRect.Right() - aSize.Width();
184 	aPoint.Y() = aRect.Bottom() - aSize.Height();
185 
186 	aPoint = OutputToScreenPixel( aPoint );
187 
188 	if ( aPos.X() > aPoint.X() )
189 		aPos.X() = aPoint.X() ;
190 	if ( aPos.Y() > aPoint.Y() )
191 		aPos.Y() = aPoint.Y();
192 
193 	if ( aPos.X() < 0 ) aPos.X() = 0;
194 	if ( aPos.Y() < 0 ) aPos.Y() = 0;
195 
196 	SetPosPixel( aPos );
197 }
198 
199 /*--------------------------------------------------------------------
200 	Beschreibung:
201  --------------------------------------------------------------------*/
202 
CreateInputItemSet(sal_uInt16 nID)203 SfxItemSet* SwFldDlg::CreateInputItemSet( sal_uInt16 nID  )
204 {
205 	if ( nID == TP_FLD_DOKINF )
206 	{
207 		SwDocShell* pDocSh = (SwDocShell*)SfxObjectShell::Current();
208 		SfxItemSet* pISet = new SfxItemSet( pDocSh->GetPool(), SID_DOCINFO, SID_DOCINFO );
209         using namespace ::com::sun::star;
210         uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
211             pDocSh->GetModel(), uno::UNO_QUERY_THROW);
212         uno::Reference<document::XDocumentProperties> xDocProps
213             = xDPS->getDocumentProperties();
214         uno::Reference< beans::XPropertySet > xUDProps(
215             xDocProps->getUserDefinedProperties(),
216             uno::UNO_QUERY_THROW);
217         pISet->Put( SfxUnoAnyItem( SID_DOCINFO, uno::makeAny(xUDProps) ) );
218 		return pISet;
219 	}
220 	else
221 		return 0;
222 }
223 
224 /*--------------------------------------------------------------------
225 	 Beschreibung: Einfuegen von neuen Feldern anstossen
226  --------------------------------------------------------------------*/
227 
IMPL_LINK(SwFldDlg,OKHdl,Button *,EMPTYARG)228 IMPL_LINK( SwFldDlg, OKHdl, Button *, EMPTYARG )
229 {
230 	if (GetOKButton().IsEnabled())
231 	{
232 		SfxTabPage*	pPage = GetTabPage(GetCurPageId());
233 		pPage->FillItemSet(*(SfxItemSet*)0);
234 
235 		GetOKButton().GrabFocus();	// Wegen InputField-Dlg
236 	}
237 
238 	return 0;
239 }
240 
241 /*--------------------------------------------------------------------
242 	Beschreibung: Nach Dok-Wechsel Dialog neu initialisieren
243  --------------------------------------------------------------------*/
244 
ReInitDlg()245 void SwFldDlg::ReInitDlg()
246 {
247 	SwDocShell* pDocSh = (SwDocShell*)SfxObjectShell::Current();
248 	sal_Bool bNewMode = (::GetHtmlMode(pDocSh) & HTMLMODE_ON) != 0;
249 
250     if (bNewMode != m_bHtmlMode)
251 	{
252 		SfxViewFrame::Current()->GetDispatcher()->
253 			Execute(FN_INSERT_FIELD, SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD);
254 		Close();
255 	}
256 
257 	SwView* pActiveView = ::GetActiveView();
258 	if(!pActiveView)
259 		return;
260 	const SwWrtShell& rSh = pActiveView->GetWrtShell();
261 	GetOKButton().Enable( !rSh.IsReadOnlyAvailable() ||
262 						  !rSh.HasReadonlySel() );
263 
264 	ReInitTabPage(TP_FLD_DOK);
265 	ReInitTabPage(TP_FLD_VAR);
266 	ReInitTabPage(TP_FLD_DOKINF);
267 
268     if (!m_bHtmlMode)
269 	{
270 		ReInitTabPage(TP_FLD_REF);
271 		ReInitTabPage(TP_FLD_FUNC);
272 		ReInitTabPage(TP_FLD_DB);
273 	}
274 
275     m_pChildWin->SetOldDocShell(pDocSh);
276 }
277 
278 /*--------------------------------------------------------------------
279 	Beschreibung: Nach Dok-Wechsel TabPage neu initialisieren
280  --------------------------------------------------------------------*/
281 
ReInitTabPage(sal_uInt16 nPageId,sal_Bool bOnlyActivate)282 void SwFldDlg::ReInitTabPage( sal_uInt16 nPageId, sal_Bool bOnlyActivate )
283 {
284 	SwFldPage* pPage = (SwFldPage* )GetTabPage(nPageId);
285 
286 	if ( pPage )
287 		pPage->EditNewField( bOnlyActivate );	// TabPage neu initialisieren
288 }
289 
290 /*--------------------------------------------------------------------
291 	Beschreibung: Nach Aktivierung einige TabPages neu initialisieren
292  --------------------------------------------------------------------*/
293 
Activate()294 void SwFldDlg::Activate()
295 {
296 	SwView* pView = ::GetActiveView();
297 	if( pView )
298 	{
299 		sal_Bool bHtmlMode = (::GetHtmlMode((SwDocShell*)SfxObjectShell::Current()) & HTMLMODE_ON) != 0;
300 		const SwWrtShell& rSh = pView->GetWrtShell();
301 		GetOKButton().Enable( !rSh.IsReadOnlyAvailable() ||
302 						  	!rSh.HasReadonlySel() );
303 
304 		ReInitTabPage( TP_FLD_VAR, sal_True );
305 
306 		if( !bHtmlMode )
307 		{
308 			ReInitTabPage( TP_FLD_REF, sal_True );
309 			ReInitTabPage( TP_FLD_FUNC, sal_True );
310 		}
311 	}
312 }
313 
314 /*--------------------------------------------------------------------
315 	Beschreibung:
316  --------------------------------------------------------------------*/
317 
EnableInsert(sal_Bool bEnable)318 void SwFldDlg::EnableInsert(sal_Bool bEnable)
319 {
320 	if( bEnable )
321 	{
322 		SwView* pView = ::GetActiveView();
323 		DBG_ASSERT(pView, "no view found");
324 		if( !pView ||
325 				(pView->GetWrtShell().IsReadOnlyAvailable() &&
326 					pView->GetWrtShell().HasReadonlySel()) )
327 			bEnable = sal_False;
328 	}
329 	GetOKButton().Enable(bEnable);
330 }
331 
332 /*--------------------------------------------------------------------
333 	Beschreibung:
334  --------------------------------------------------------------------*/
335 
InsertHdl()336 void SwFldDlg::InsertHdl()
337 {
338 	GetOKButton().Click();
339 }
340 /* -----------------27.11.2002 15:24-----------------
341  *
342  * --------------------------------------------------*/
ActivateDatabasePage()343 void SwFldDlg::ActivateDatabasePage()
344 {
345     m_bDataBaseMode = sal_True;
346     ShowPage( TP_FLD_DB );
347     SfxTabPage* pDBPage =  GetTabPage( TP_FLD_DB );
348     if( pDBPage )
349     {
350         ((SwFldDBPage*)pDBPage)->ActivateMailMergeAddress();
351     }
352     //remove all other pages
353     RemoveTabPage(TP_FLD_DOK);
354     RemoveTabPage(TP_FLD_VAR);
355     RemoveTabPage(TP_FLD_DOKINF);
356     RemoveTabPage(TP_FLD_REF);
357     RemoveTabPage(TP_FLD_FUNC);
358 }
359 /*-- 07.10.2003 14:01:44---------------------------------------------------
360 
361   -----------------------------------------------------------------------*/
PageCreated(sal_uInt16 nId,SfxTabPage & rPage)362 void SwFldDlg::PageCreated(sal_uInt16 nId, SfxTabPage& rPage)
363 {
364     if( TP_FLD_DB == nId)
365     {
366         SfxDispatcher* pDispatch = m_pBindings->GetDispatcher();
367         SfxViewFrame* pViewFrame = pDispatch ? pDispatch->GetFrame() : 0;
368         if(pViewFrame)
369         {
370             const TypeId aSwViewTypeId = TYPE(SwView);
371             SfxViewShell* pViewShell = SfxViewShell::GetFirst( &aSwViewTypeId );
372             while(pViewShell && pViewShell->GetViewFrame() != pViewFrame)
373             {
374                 pViewShell = SfxViewShell::GetNext( *pViewShell, &aSwViewTypeId );
375             }
376             if(pViewShell)
377                 static_cast<SwFldDBPage&>(rPage).SetWrtShell(static_cast<SwView*>(pViewShell)->GetWrtShell());
378         }
379     }
380 }
381 
382 
383