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 .
20 #include <vcl/msgbox.hxx>
21 #include <sfx2/basedlgs.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include <sfx2/request.hxx>
24 #include <sfx2/app.hxx>
25 #include <svx/optgenrl.hxx>
26 #include <docufld.hxx>
31 #include <flddinf.hxx>
34 #include <fldfunc.hxx>
37 #include <crsskip.hxx>
42 #include <globals.hrc>
44 #include "swabstdlg.hxx"
47 #include <com/sun/star/document/XDocumentProperties.hpp>
48 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
52 SwAbstractDialogFactory
* GetFactory();
55 SwFldEditDlg::SwFldEditDlg(SwView
& rVw
) :
56 SfxNoLayoutSingleTabDialog(&rVw
.GetViewFrame()->GetWindow(), 0, 0),
57 pSh (rVw
.GetWrtShellPtr()),
58 aPrevBT (this, SW_RES(BTN_FLDEDT_PREV
)),
59 aNextBT (this, SW_RES(BTN_FLDEDT_NEXT
)),
60 aAddressBT (this, SW_RES(PB_FLDEDT_ADDRESS
))
64 SwField
*pCurFld
= aMgr
.GetCurFld();
68 pSh
->SetCareWin(this);
70 /* #108536# Only create selection if there is none
71 already. Normalize PaM instead of swapping. */
72 if ( ! pSh
->HasSelection() )
73 pSh
->Right(CRSR_SKIP_CHARS
, sal_True
, 1, sal_False
);
77 sal_uInt16 nGroup
= aMgr
.GetGroup(sal_False
, pCurFld
->GetTypeId(), pCurFld
->GetSubType());
81 GetOKButton()->SetClickHdl(LINK(this, SwFldEditDlg
, OKHdl
));
83 // position buttons ourselves because otherwise when font sizes are
84 // varying, they are in the woods, and because PB uses fixed pixel sizes
85 // for its buttons and dialog width in SingleTabDlg.
86 aPrevBT
.SetPosPixel(Point(GetOKButton()->GetPosPixel().X(), aPrevBT
.GetPosPixel().Y()));
87 sal_uInt16 nWidth
= static_cast< sal_uInt16
>(GetOKButton()->GetOutputSize().Width() / 2 - 3);
88 Size
aNewSize(LogicToPixel(Size(nWidth
, GetOKButton()->GetOutputSize().Height())));
89 aPrevBT
.SetSizePixel(aNewSize
);
91 aNextBT
.SetSizePixel(aPrevBT
.GetSizePixel());
93 long nXPos
= GetOKButton()->GetPosPixel().X() + GetOKButton()->GetSizePixel().Width()
94 - aNextBT
.GetSizePixel().Width() - 1;
95 aNextBT
.SetPosPixel(Point(nXPos
, aNextBT
.GetPosPixel().Y()));
97 aAddressBT
.SetPosPixel(Point(GetOKButton()->GetPosPixel().X(), aAddressBT
.GetPosPixel().Y()));
98 aAddressBT
.SetSizePixel(GetOKButton()->GetSizePixel());
100 aPrevBT
.SetClickHdl(LINK(this, SwFldEditDlg
, NextPrevHdl
));
101 aNextBT
.SetClickHdl(LINK(this, SwFldEditDlg
, NextPrevHdl
));
103 aAddressBT
.SetClickHdl(LINK(this, SwFldEditDlg
, AddressHdl
));
104 aAddressBT
.SetHelpId(HID_FLDEDT_ADDRESS
);
109 /*--------------------------------------------------------------------
110 Description: initialise controls
111 --------------------------------------------------------------------*/
112 void SwFldEditDlg::Init()
114 SwFldPage
* pTabPage
= (SwFldPage
*)GetTabPage();
118 SwFldMgr
& rMgr
= pTabPage
->GetFldMgr();
120 SwField
*pCurFld
= rMgr
.GetCurFld();
125 // Traveling only when more than one field
129 sal_Bool bMove
= rMgr
.GoNext();
132 aNextBT
.Enable(bMove
);
134 if( 0 != ( bMove
= rMgr
.GoPrev() ) )
136 aPrevBT
.Enable( bMove
);
138 if (pCurFld
->GetTypeId() == TYP_EXTUSERFLD
)
145 GetOKButton()->Enable( !pSh
->IsReadOnlyAvailable() ||
146 !pSh
->HasReadonlySel() );
149 SfxTabPage
* SwFldEditDlg::CreatePage(sal_uInt16 nGroup
)
152 SfxTabPage
* pTabPage
= 0;
153 const char* pHelpId
= 0;
158 pTabPage
= SwFldDokPage::Create(this, *(SfxItemSet
*)0);
159 pHelpId
= HID_EDIT_FLD_DOK
;
162 pTabPage
= SwFldFuncPage::Create(this, *(SfxItemSet
*)0);
163 pHelpId
= HID_EDIT_FLD_FUNC
;
166 pTabPage
= SwFldRefPage::Create(this, *(SfxItemSet
*)0);
167 pHelpId
= HID_EDIT_FLD_REF
;
171 SfxObjectShell
* pDocSh
= SfxObjectShell::Current();
172 SfxItemSet
* pSet
= new SfxItemSet( pDocSh
->GetPool(), SID_DOCINFO
, SID_DOCINFO
);
173 using namespace ::com::sun::star
;
174 uno::Reference
<document::XDocumentPropertiesSupplier
> xDPS(
175 pDocSh
->GetModel(), uno::UNO_QUERY_THROW
);
176 uno::Reference
<document::XDocumentProperties
> xDocProps
177 = xDPS
->getDocumentProperties();
178 uno::Reference
< beans::XPropertySet
> xUDProps(
179 xDocProps
->getUserDefinedProperties(),
180 uno::UNO_QUERY_THROW
);
181 pSet
->Put( SfxUnoAnyItem( SID_DOCINFO
, uno::makeAny(xUDProps
) ) );
182 pTabPage
= SwFldDokInfPage::Create(this, *pSet
);
183 pHelpId
= HID_EDIT_FLD_DOKINF
;
187 pTabPage
= SwFldDBPage::Create(this, *(SfxItemSet
*)0);
188 static_cast<SwFldDBPage
*>(pTabPage
)->SetWrtShell(*pSh
);
189 pHelpId
= HID_EDIT_FLD_DB
;
192 pTabPage
= SwFldVarPage::Create(this, *(SfxItemSet
*)0);
193 pHelpId
= HID_EDIT_FLD_VAR
;
198 pTabPage
->SetHelpId(pHelpId
);
199 static_cast<SwFldPage
*>(pTabPage
)->SetWrtShell(pSh
);
201 SetTabPage(pTabPage
);
203 String
sTitle(GetText());
204 sTitle
.Insert(OUString(": "), 0);
205 sTitle
.Insert(SW_RESSTR(STR_FLD_EDIT_DLG
), 0);
211 SwFldEditDlg::~SwFldEditDlg()
213 pSh
->SetCareWin(NULL
);
217 void SwFldEditDlg::EnableInsert(sal_Bool bEnable
)
219 if( bEnable
&& pSh
->IsReadOnlyAvailable() && pSh
->HasReadonlySel() )
221 GetOKButton()->Enable( bEnable
);
224 void SwFldEditDlg::InsertHdl()
226 GetOKButton()->Click();
229 /*--------------------------------------------------------------------
230 Description: kick off changing of the field
231 --------------------------------------------------------------------*/
232 IMPL_LINK_NOARG(SwFldEditDlg
, OKHdl
)
234 if (GetOKButton()->IsEnabled())
236 SfxTabPage
* pTabPage
= GetTabPage();
239 pTabPage
->FillItemSet(*(SfxItemSet
*)0);
248 short SwFldEditDlg::Execute()
250 // without TabPage no dialog
251 return GetTabPage() ? Dialog::Execute() : static_cast<short>(RET_CANCEL
);
254 /*--------------------------------------------------------------------
255 Description: Traveling between fields of the same type
256 --------------------------------------------------------------------*/
257 IMPL_LINK( SwFldEditDlg
, NextPrevHdl
, Button
*, pButton
)
259 sal_Bool bNext
= pButton
== &aNextBT
;
263 SwFieldType
*pOldTyp
= 0;
264 SwFldPage
* pTabPage
= (SwFldPage
*)GetTabPage();
266 //#112462# FillItemSet may delete the current field
267 //that's why it has to be called before accessing the current field
268 if( GetOKButton()->IsEnabled() )
269 pTabPage
->FillItemSet(*(SfxItemSet
*)0);
271 SwFldMgr
& rMgr
= pTabPage
->GetFldMgr();
272 SwField
*pCurFld
= rMgr
.GetCurFld();
273 if (pCurFld
->GetTypeId() == TYP_DBFLD
)
274 pOldTyp
= (SwDBFieldType
*)pCurFld
->GetTyp();
276 rMgr
.GoNextPrev( bNext
, pOldTyp
);
277 pCurFld
= rMgr
.GetCurFld();
279 /* #108536# Only create selection if there is none
280 already. Normalize PaM instead of swapping. */
281 if ( ! pSh
->HasSelection() )
282 pSh
->Right(CRSR_SKIP_CHARS
, sal_True
, 1, sal_False
);
286 sal_uInt16 nGroup
= rMgr
.GetGroup(sal_False
, pCurFld
->GetTypeId(), pCurFld
->GetSubType());
288 if (nGroup
!= pTabPage
->GetGroup())
289 pTabPage
= (SwFldPage
*)CreatePage(nGroup
);
291 pTabPage
->EditNewField();
298 IMPL_LINK_NOARG(SwFldEditDlg
, AddressHdl
)
300 SwFldPage
* pTabPage
= (SwFldPage
*)GetTabPage();
301 SwFldMgr
& rMgr
= pTabPage
->GetFldMgr();
302 SwField
*pCurFld
= rMgr
.GetCurFld();
304 SfxItemSet
aSet( pSh
->GetAttrPool(),
305 SID_FIELD_GRABFOCUS
, SID_FIELD_GRABFOCUS
,
308 sal_uInt16 nEditPos
= UNKNOWN_EDIT
;
310 switch(pCurFld
->GetSubType())
312 case EU_FIRSTNAME
: nEditPos
= FIRSTNAME_EDIT
; break;
313 case EU_NAME
: nEditPos
= LASTNAME_EDIT
; break;
314 case EU_SHORTCUT
: nEditPos
= SHORTNAME_EDIT
; break;
315 case EU_COMPANY
: nEditPos
= COMPANY_EDIT
; break;
316 case EU_STREET
: nEditPos
= STREET_EDIT
; break;
317 case EU_TITLE
: nEditPos
= TITLE_EDIT
; break;
318 case EU_POSITION
: nEditPos
= POSITION_EDIT
; break;
319 case EU_PHONE_PRIVATE
:nEditPos
= TELPRIV_EDIT
; break;
320 case EU_PHONE_COMPANY
:nEditPos
= TELCOMPANY_EDIT
; break;
321 case EU_FAX
: nEditPos
= FAX_EDIT
; break;
322 case EU_EMAIL
: nEditPos
= EMAIL_EDIT
; break;
323 case EU_COUNTRY
: nEditPos
= COUNTRY_EDIT
; break;
324 case EU_ZIP
: nEditPos
= PLZ_EDIT
; break;
325 case EU_CITY
: nEditPos
= CITY_EDIT
; break;
326 case EU_STATE
: nEditPos
= STATE_EDIT
; break;
328 default: nEditPos
= UNKNOWN_EDIT
; break;
331 aSet
.Put(SfxUInt16Item(SID_FIELD_GRABFOCUS
, nEditPos
));
332 SwAbstractDialogFactory
* pFact
= swui::GetFactory();
333 OSL_ENSURE(pFact
, "SwAbstractDialogFactory fail!");
335 SfxAbstractDialog
* pDlg
= pFact
->CreateSfxDialog( this, aSet
,
336 pSh
->GetView().GetViewFrame()->GetFrame().GetFrameInterface(),
338 OSL_ENSURE(pDlg
, "Dialogdiet fail!");
339 if(RET_OK
== pDlg
->Execute())
341 pSh
->UpdateFlds( *pCurFld
);
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */