bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / fldui / fldedt.cxx
blobb376360a1fdf30825f4c6f7cf9905753aa49609c
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 <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>
27 #include <view.hxx>
28 #include <dbfld.hxx>
29 #include <wrtsh.hxx>
30 #include <flddb.hxx>
31 #include <flddinf.hxx>
32 #include <fldvar.hxx>
33 #include <flddok.hxx>
34 #include <fldfunc.hxx>
35 #include <fldref.hxx>
36 #include <fldedt.hxx>
37 #include <crsskip.hxx>
40 #include <cmdid.h>
41 #include <helpid.h>
42 #include <globals.hrc>
43 #include <fldui.hrc>
44 #include "swabstdlg.hxx"
45 #include "dialog.hrc"
47 #include <com/sun/star/document/XDocumentProperties.hpp>
48 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
50 namespace swui
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))
62 SwFldMgr aMgr(pSh);
64 SwField *pCurFld = aMgr.GetCurFld();
65 if(!pCurFld)
66 return;
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 );
75 pSh->NormalizePam();
77 sal_uInt16 nGroup = aMgr.GetGroup(sal_False, pCurFld->GetTypeId(), pCurFld->GetSubType());
79 CreatePage(nGroup);
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);
106 Init();
109 /*--------------------------------------------------------------------
110 Description: initialise controls
111 --------------------------------------------------------------------*/
112 void SwFldEditDlg::Init()
114 SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
116 if( pTabPage )
118 SwFldMgr& rMgr = pTabPage->GetFldMgr();
120 SwField *pCurFld = rMgr.GetCurFld();
122 if(!pCurFld)
123 return;
125 // Traveling only when more than one field
126 pSh->StartAction();
127 pSh->CreateCrsr();
129 sal_Bool bMove = rMgr.GoNext();
130 if( bMove )
131 rMgr.GoPrev();
132 aNextBT.Enable(bMove);
134 if( 0 != ( bMove = rMgr.GoPrev() ) )
135 rMgr.GoNext();
136 aPrevBT.Enable( bMove );
138 if (pCurFld->GetTypeId() == TYP_EXTUSERFLD)
139 aAddressBT.Show();
141 pSh->DestroyCrsr();
142 pSh->EndAction();
145 GetOKButton()->Enable( !pSh->IsReadOnlyAvailable() ||
146 !pSh->HasReadonlySel() );
149 SfxTabPage* SwFldEditDlg::CreatePage(sal_uInt16 nGroup)
151 // create TabPage
152 SfxTabPage* pTabPage = 0;
153 const char* pHelpId = 0;
155 switch (nGroup)
157 case GRP_DOC:
158 pTabPage = SwFldDokPage::Create(this, *(SfxItemSet*)0);
159 pHelpId = HID_EDIT_FLD_DOK;
160 break;
161 case GRP_FKT:
162 pTabPage = SwFldFuncPage::Create(this, *(SfxItemSet*)0);
163 pHelpId = HID_EDIT_FLD_FUNC;
164 break;
165 case GRP_REF:
166 pTabPage = SwFldRefPage::Create(this, *(SfxItemSet*)0);
167 pHelpId = HID_EDIT_FLD_REF;
168 break;
169 case GRP_REG:
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;
184 break;
186 case GRP_DB:
187 pTabPage = SwFldDBPage::Create(this, *(SfxItemSet*)0);
188 static_cast<SwFldDBPage*>(pTabPage)->SetWrtShell(*pSh);
189 pHelpId = HID_EDIT_FLD_DB;
190 break;
191 case GRP_VAR:
192 pTabPage = SwFldVarPage::Create(this, *(SfxItemSet*)0);
193 pHelpId = HID_EDIT_FLD_VAR;
194 break;
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);
206 SetText(sTitle);
208 return pTabPage;
211 SwFldEditDlg::~SwFldEditDlg()
213 pSh->SetCareWin(NULL);
214 pSh->EnterStdMode();
217 void SwFldEditDlg::EnableInsert(sal_Bool bEnable)
219 if( bEnable && pSh->IsReadOnlyAvailable() && pSh->HasReadonlySel() )
220 bEnable = sal_False;
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();
237 if (pTabPage)
239 pTabPage->FillItemSet(*(SfxItemSet*)0);
242 EndDialog( RET_OK );
245 return 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;
261 pSh->EnterStdMode();
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 );
284 pSh->NormalizePam();
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();
293 Init();
295 return 0;
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,
306 0L );
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(),
337 RC_DLG_ADDR );
338 OSL_ENSURE(pDlg, "Dialogdiet fail!");
339 if(RET_OK == pDlg->Execute())
341 pSh->UpdateFlds( *pCurFld );
343 delete pDlg;
344 return 0;
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */