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