1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
30 #include <fldwrap.hxx>
32 #include <flddinf.hxx>
35 #include <fldfunc.hxx>
39 #include <fldtdlg.hxx>
40 #include <swmodule.hxx>
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
)),
60 m_bDataBaseMode(sal_False
)
62 SetStyle(GetStyle()|WB_STDMODELESS
);
63 m_bHtmlMode
= (::GetHtmlMode((SwDocShell
*)SfxObjectShell::Current()) & HTMLMODE_ON
) != 0;
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
));
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);
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(),
93 "/org.openoffice.Office.DataAccess/Policies/Features/Writer" ),
95 utl::OConfigurationTreeRoot::CM_READONLY
);
97 sal_Bool bDatabaseFields
= sal_True
;
98 aCfgRoot
.getNodeValue(
99 OUString("DatabaseFields")) >>= bDatabaseFields
;
102 AddTabPage(TP_FLD_DB
, SwFldDBPage::Create
, 0);
104 RemoveTabPage(TP_FLD_DB
);
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
);
126 void SwFldDlg::Initialize(SfxChildWinInfo
*pInfo
)
131 if ( pInfo
->aSize
.Width() != 0 && pInfo
->aSize
.Height() != 0 )
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
)
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;
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;
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
) ) );
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
210 IMPL_LINK_NOARG(SwFldDlg
, CancelHdl
)
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
);
233 SwView
* pActiveView
= ::GetActiveView();
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
);
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
);
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();
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
);
284 ReInitTabPage( TP_FLD_REF
, sal_True
);
285 ReInitTabPage( TP_FLD_FUNC
, sal_True
);
290 void SwFldDlg::EnableInsert(sal_Bool bEnable
)
294 SwView
* pView
= ::GetActiveView();
295 OSL_ENSURE(pView
, "no view found");
297 (pView
->GetWrtShell().IsReadOnlyAvailable() &&
298 pView
->GetWrtShell().HasReadonlySel()) )
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
);
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;
334 const TypeId aSwViewTypeId
= TYPE(SwView
);
335 SfxViewShell
* pViewShell
= SfxViewShell::GetFirst( &aSwViewTypeId
);
336 while(pViewShell
&& pViewShell
->GetViewFrame() != pViewFrame
)
338 pViewShell
= SfxViewShell::GetNext( *pViewShell
, &aSwViewTypeId
);
341 static_cast<SwFldDBPage
&>(rPage
).SetWrtShell(static_cast<SwView
*>(pViewShell
)->GetWrtShell());
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */