bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / fldui / fldtdlg.cxx
blob176a91b51b0527052ad2d433c5ab2b26638ee92a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cmdid.h>
21 #include <swtypes.hxx>
22 #include <unotools/confignode.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <sfx2/basedlgs.hxx>
25 #include <sfx2/dispatch.hxx>
26 #include <vcl/msgbox.hxx>
27 #include <sfx2/htmlmode.hxx>
28 #include <viewopt.hxx>
29 #include <docsh.hxx>
30 #include <fldwrap.hxx>
31 #include <flddb.hxx>
32 #include <flddinf.hxx>
33 #include <fldvar.hxx>
34 #include <flddok.hxx>
35 #include <fldfunc.hxx>
36 #include <fldref.hxx>
37 #include <wrtsh.hxx>
38 #include <view.hxx>
39 #include <fldtdlg.hxx>
40 #include <swmodule.hxx>
42 #include <helpid.h>
43 #include <fldui.hrc>
44 #include <globals.hrc>
45 #include <fldtdlg.hrc>
47 #include <com/sun/star/document/XDocumentProperties.hpp>
48 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
51 /*--------------------------------------------------------------------
52 Description: carrier of the dialog
53 --------------------------------------------------------------------*/
56 SwFldDlg::SwFldDlg(SfxBindings* pB, SwChildWinWrapper* pCW, Window *pParent)
57 : SfxTabDialog( pParent, SW_RES( DLG_FLD_INSERT )),
58 m_pChildWin(pCW),
59 m_pBindings(pB),
60 m_bDataBaseMode(sal_False)
62 SetStyle(GetStyle()|WB_STDMODELESS);
63 m_bHtmlMode = (::GetHtmlMode((SwDocShell*)SfxObjectShell::Current()) & HTMLMODE_ON) != 0;
65 RemoveResetButton();
67 GetOKButton().SetText(String(SW_RES(STR_FLD_INSERT)));
68 GetOKButton().SetHelpId(HID_FIELD_INSERT);
69 GetOKButton().SetHelpText(aEmptyStr); // so that generated help text is used
71 GetCancelButton().SetText(String(SW_RES(STR_FLD_CLOSE)));
72 GetCancelButton().SetHelpId(HID_FIELD_CLOSE);
73 GetCancelButton().SetHelpText(aEmptyStr); // so that generated help text is used
74 GetCancelButton().SetClickHdl(LINK(this, SwFldDlg, CancelHdl));
76 FreeResource();
78 GetOKButton().SetClickHdl(LINK(this, SwFldDlg, OKHdl));
80 AddTabPage(TP_FLD_DOK, SwFldDokPage::Create, 0);
81 AddTabPage(TP_FLD_VAR, SwFldVarPage::Create, 0);
82 AddTabPage(TP_FLD_DOKINF, SwFldDokInfPage::Create, 0);
84 if (!m_bHtmlMode)
86 AddTabPage(TP_FLD_REF, SwFldRefPage::Create, 0);
87 AddTabPage(TP_FLD_FUNC, SwFldFuncPage::Create, 0);
89 utl::OConfigurationTreeRoot aCfgRoot
90 = utl::OConfigurationTreeRoot::createWithComponentContext(
91 ::comphelper::getProcessComponentContext(),
92 OUString(
93 "/org.openoffice.Office.DataAccess/Policies/Features/Writer" ),
94 -1,
95 utl::OConfigurationTreeRoot::CM_READONLY);
97 sal_Bool bDatabaseFields = sal_True;
98 aCfgRoot.getNodeValue(
99 OUString("DatabaseFields")) >>= bDatabaseFields;
101 if (bDatabaseFields)
102 AddTabPage(TP_FLD_DB, SwFldDBPage::Create, 0);
103 else
104 RemoveTabPage(TP_FLD_DB);
106 else
108 RemoveTabPage(TP_FLD_REF);
109 RemoveTabPage(TP_FLD_FUNC);
110 RemoveTabPage(TP_FLD_DB);
114 SwFldDlg::~SwFldDlg()
118 sal_Bool SwFldDlg::Close()
120 m_pBindings->GetDispatcher()->
121 Execute(m_bDataBaseMode ? FN_INSERT_FIELD_DATA_ONLY : FN_INSERT_FIELD,
122 SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD);
123 return sal_True;
126 void SwFldDlg::Initialize(SfxChildWinInfo *pInfo)
128 Point aPos;
129 Size aSize;
131 if ( pInfo->aSize.Width() != 0 && pInfo->aSize.Height() != 0 )
133 aPos = pInfo->aPos;
134 if ( GetStyle() & WB_SIZEABLE )
135 SetSizePixel( pInfo->aSize );
137 // remember initial size from pInfo
138 aSize = GetSizePixel();
140 // should the FloatingWindow get zoomed?
141 if ( pInfo->nFlags & SFX_CHILDWIN_ZOOMIN )
142 RollUp();
144 else
146 // remember initial size from resource or ctor
147 aSize = GetSizePixel();
149 Size aParentSize = GetParent()->GetOutputSizePixel();
150 aPos.X() += ( aParentSize.Width() - aSize.Width() ) / 2;
151 aPos.Y() += ( aParentSize.Height() - aSize.Height() ) / 2;
154 Point aPoint;
155 Rectangle aRect = GetDesktopRectPixel();
156 aPoint.X() = aRect.Right() - aSize.Width();
157 aPoint.Y() = aRect.Bottom() - aSize.Height();
159 aPoint = OutputToScreenPixel( aPoint );
161 if ( aPos.X() > aPoint.X() )
162 aPos.X() = aPoint.X() ;
163 if ( aPos.Y() > aPoint.Y() )
164 aPos.Y() = aPoint.Y();
166 if ( aPos.X() < 0 ) aPos.X() = 0;
167 if ( aPos.Y() < 0 ) aPos.Y() = 0;
169 SetPosPixel( aPos );
172 SfxItemSet* SwFldDlg::CreateInputItemSet( sal_uInt16 nID )
174 if ( nID == TP_FLD_DOKINF )
176 SwDocShell* pDocSh = (SwDocShell*)SfxObjectShell::Current();
177 SfxItemSet* pISet = new SfxItemSet( pDocSh->GetPool(), SID_DOCINFO, SID_DOCINFO );
178 using namespace ::com::sun::star;
179 uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
180 pDocSh->GetModel(), uno::UNO_QUERY_THROW);
181 uno::Reference<document::XDocumentProperties> xDocProps
182 = xDPS->getDocumentProperties();
183 uno::Reference< beans::XPropertySet > xUDProps(
184 xDocProps->getUserDefinedProperties(),
185 uno::UNO_QUERY_THROW);
186 pISet->Put( SfxUnoAnyItem( SID_DOCINFO, uno::makeAny(xUDProps) ) );
187 return pISet;
189 else
190 return 0;
193 /*--------------------------------------------------------------------
194 Description: kick off inserting of new fields
195 --------------------------------------------------------------------*/
197 IMPL_LINK_NOARG(SwFldDlg, OKHdl)
199 if (GetOKButton().IsEnabled())
201 SfxTabPage* pPage = GetTabPage(GetCurPageId());
202 pPage->FillItemSet(*(SfxItemSet*)0);
204 GetOKButton().GrabFocus(); // because of InputField-Dlg
207 return 0;
210 IMPL_LINK_NOARG(SwFldDlg, CancelHdl)
212 Close();
213 return 0;
217 /*--------------------------------------------------------------------
218 Description: newly initialise dialog after Doc-Switch
219 --------------------------------------------------------------------*/
221 void SwFldDlg::ReInitDlg()
223 SwDocShell* pDocSh = (SwDocShell*)SfxObjectShell::Current();
224 sal_Bool bNewMode = (::GetHtmlMode(pDocSh) & HTMLMODE_ON) != 0;
226 if (bNewMode != m_bHtmlMode)
228 SfxViewFrame::Current()->GetDispatcher()->
229 Execute(FN_INSERT_FIELD, SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD);
230 Close();
233 SwView* pActiveView = ::GetActiveView();
234 if(!pActiveView)
235 return;
236 const SwWrtShell& rSh = pActiveView->GetWrtShell();
237 GetOKButton().Enable( !rSh.IsReadOnlyAvailable() ||
238 !rSh.HasReadonlySel() );
240 ReInitTabPage(TP_FLD_DOK);
241 ReInitTabPage(TP_FLD_VAR);
242 ReInitTabPage(TP_FLD_DOKINF);
244 if (!m_bHtmlMode)
246 ReInitTabPage(TP_FLD_REF);
247 ReInitTabPage(TP_FLD_FUNC);
248 ReInitTabPage(TP_FLD_DB);
251 m_pChildWin->SetOldDocShell(pDocSh);
254 /*--------------------------------------------------------------------
255 Description: newly initialise TabPage after Doc-Switch
256 --------------------------------------------------------------------*/
258 void SwFldDlg::ReInitTabPage( sal_uInt16 nPageId, sal_Bool bOnlyActivate )
260 SwFldPage* pPage = (SwFldPage* )GetTabPage(nPageId);
262 if ( pPage )
263 pPage->EditNewField( bOnlyActivate ); // newly initialise TabPage
266 /*--------------------------------------------------------------------
267 Description: newly initialise after activation of a few TabPages
268 --------------------------------------------------------------------*/
270 void SwFldDlg::Activate()
272 SwView* pView = ::GetActiveView();
273 if( pView )
275 bool bHtmlMode = (::GetHtmlMode((SwDocShell*)SfxObjectShell::Current()) & HTMLMODE_ON) != 0;
276 const SwWrtShell& rSh = pView->GetWrtShell();
277 GetOKButton().Enable( !rSh.IsReadOnlyAvailable() ||
278 !rSh.HasReadonlySel() );
280 ReInitTabPage( TP_FLD_VAR, sal_True );
282 if( !bHtmlMode )
284 ReInitTabPage( TP_FLD_REF, sal_True );
285 ReInitTabPage( TP_FLD_FUNC, sal_True );
290 void SwFldDlg::EnableInsert(sal_Bool bEnable)
292 if( bEnable )
294 SwView* pView = ::GetActiveView();
295 OSL_ENSURE(pView, "no view found");
296 if( !pView ||
297 (pView->GetWrtShell().IsReadOnlyAvailable() &&
298 pView->GetWrtShell().HasReadonlySel()) )
299 bEnable = sal_False;
301 GetOKButton().Enable(bEnable);
304 void SwFldDlg::InsertHdl()
306 GetOKButton().Click();
309 void SwFldDlg::ActivateDatabasePage()
311 m_bDataBaseMode = sal_True;
312 ShowPage( TP_FLD_DB );
313 SfxTabPage* pDBPage = GetTabPage( TP_FLD_DB );
314 if( pDBPage )
316 ((SwFldDBPage*)pDBPage)->ActivateMailMergeAddress();
318 //remove all other pages
319 RemoveTabPage(TP_FLD_DOK);
320 RemoveTabPage(TP_FLD_VAR);
321 RemoveTabPage(TP_FLD_DOKINF);
322 RemoveTabPage(TP_FLD_REF);
323 RemoveTabPage(TP_FLD_FUNC);
326 void SwFldDlg::PageCreated(sal_uInt16 nId, SfxTabPage& rPage)
328 if( TP_FLD_DB == nId)
330 SfxDispatcher* pDispatch = m_pBindings->GetDispatcher();
331 SfxViewFrame* pViewFrame = pDispatch ? pDispatch->GetFrame() : 0;
332 if(pViewFrame)
334 const TypeId aSwViewTypeId = TYPE(SwView);
335 SfxViewShell* pViewShell = SfxViewShell::GetFirst( &aSwViewTypeId );
336 while(pViewShell && pViewShell->GetViewFrame() != pViewFrame)
338 pViewShell = SfxViewShell::GetNext( *pViewShell, &aSwViewTypeId );
340 if(pViewShell)
341 static_cast<SwFldDBPage&>(rPage).SetWrtShell(static_cast<SwView*>(pViewShell)->GetWrtShell());
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */