merge the formfield patch from ooo-build
[ooovba.git] / sw / source / ui / fldui / fldedt.cxx
blob2826df5dd0512827cebd2d3abdf71ba4095e58e4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fldedt.cxx,v $
10 * $Revision: 1.20 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #ifdef SW_DLLIMPLEMENTATION
34 #undef SW_DLLIMPLEMENTATION
35 #endif
36 #include <vcl/msgbox.hxx>
37 #include <sfx2/basedlgs.hxx>
38 #include <sfx2/viewfrm.hxx>
39 #include <sfx2/request.hxx>
40 #include <sfx2/app.hxx>
41 #include <svx/optgenrl.hxx>
42 #include <docufld.hxx>
43 #ifndef _VIEW_HXX
44 #include <view.hxx>
45 #endif
46 #include <dbfld.hxx>
47 #include <wrtsh.hxx>
48 #ifndef _FLDDB_HXX
49 #include <flddb.hxx>
50 #endif
51 #ifndef _FLDDINF_HXX
52 #include <flddinf.hxx>
53 #endif
54 #ifndef _FLDVAR_HXX
55 #include <fldvar.hxx>
56 #endif
57 #ifndef _FLDDOK_HXX
58 #include <flddok.hxx>
59 #endif
60 #ifndef _FLDFUNC_HXX
61 #include <fldfunc.hxx>
62 #endif
63 #ifndef _FLDREF_HXX
64 #include <fldref.hxx>
65 #endif
66 #include <fldedt.hxx>
67 #include <crsskip.hxx>
70 #ifndef _CMDID_H
71 #include <cmdid.h>
72 #endif
73 #ifndef _HELPID_H
74 #include <helpid.h>
75 #endif
76 #ifndef _GLOBALS_HRC
77 #include <globals.hrc>
78 #endif
79 #ifndef _FLDUI_HRC
80 #include <fldui.hrc>
81 #endif
82 #include "swabstdlg.hxx"
83 #include "dialog.hrc"
85 #include <com/sun/star/document/XDocumentProperties.hpp>
86 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
89 namespace swui
91 SwAbstractDialogFactory * GetFactory();
94 /*--------------------------------------------------------------------
95 Beschreibung:
96 --------------------------------------------------------------------*/
98 SwFldEditDlg::SwFldEditDlg(SwView& rVw) :
99 SfxSingleTabDialog(&rVw.GetViewFrame()->GetWindow(), 0, 0),
100 pSh (rVw.GetWrtShellPtr()),
101 aPrevBT (this, SW_RES(BTN_FLDEDT_PREV)),
102 aNextBT (this, SW_RES(BTN_FLDEDT_NEXT)),
103 aAddressBT (this, SW_RES(PB_FLDEDT_ADDRESS))
105 SwFldMgr aMgr(pSh);
107 SwField *pCurFld = aMgr.GetCurFld();
108 if(!pCurFld)
109 return;
111 pSh->SetCareWin(this);
113 /* #108536# Only create selection if there is none
114 already. Normalize PaM instead of swapping. */
115 if ( ! pSh->HasSelection() )
116 pSh->Right(CRSR_SKIP_CHARS, TRUE, 1, FALSE );
118 pSh->NormalizePam();
120 USHORT nGroup = aMgr.GetGroup(FALSE, pCurFld->GetTypeId(), pCurFld->GetSubType());
122 CreatePage(nGroup);
124 GetOKButton()->SetClickHdl(LINK(this, SwFldEditDlg, OKHdl));
126 // Buttons selbst positionieren, da sie sonst bei unterschiedlichen
127 // Fontgroessen im Wald stehen, und da PB im SingleTabDlg feste Pixelgroessen
128 // fuer seine Buttons und die Dialogbreite verwendet.
129 aPrevBT.SetPosPixel(Point(GetOKButton()->GetPosPixel().X(), aPrevBT.GetPosPixel().Y()));
130 USHORT nWidth = static_cast< USHORT >(GetOKButton()->GetOutputSize().Width() / 2 - 3);
131 Size aNewSize(LogicToPixel(Size(nWidth, GetOKButton()->GetOutputSize().Height())));
132 aPrevBT.SetSizePixel(aNewSize);
134 aNextBT.SetSizePixel(aPrevBT.GetSizePixel());
136 long nXPos = GetOKButton()->GetPosPixel().X() + GetOKButton()->GetSizePixel().Width()
137 - aNextBT.GetSizePixel().Width() - 1;
138 aNextBT.SetPosPixel(Point(nXPos, aNextBT.GetPosPixel().Y()));
140 aAddressBT.SetPosPixel(Point(GetOKButton()->GetPosPixel().X(), aAddressBT.GetPosPixel().Y()));
141 aAddressBT.SetSizePixel(GetOKButton()->GetSizePixel());
143 aPrevBT.SetClickHdl(LINK(this, SwFldEditDlg, NextPrevHdl));
144 aNextBT.SetClickHdl(LINK(this, SwFldEditDlg, NextPrevHdl));
146 aAddressBT.SetClickHdl(LINK(this, SwFldEditDlg, AddressHdl));
147 aAddressBT.SetHelpId(HID_FLDEDT_ADDRESS);
149 Init();
152 /*--------------------------------------------------------------------
153 Beschreibung: Controlls initialisieren
154 --------------------------------------------------------------------*/
156 void SwFldEditDlg::Init()
158 SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
160 if( pTabPage )
162 SwFldMgr& rMgr = pTabPage->GetFldMgr();
164 SwField *pCurFld = rMgr.GetCurFld();
166 if(!pCurFld)
167 return;
169 // Traveling nur bei mehr als einem Feld
170 pSh->StartAction();
171 pSh->CreateCrsr();
173 BOOL bMove = rMgr.GoNext();
174 if( bMove )
175 rMgr.GoPrev();
176 aNextBT.Enable(bMove);
178 if( 0 != ( bMove = rMgr.GoPrev() ) )
179 rMgr.GoNext();
180 aPrevBT.Enable( bMove );
182 if (pCurFld->GetTypeId() == TYP_EXTUSERFLD)
183 aAddressBT.Show();
185 pSh->DestroyCrsr();
186 pSh->EndAction();
189 GetOKButton()->Enable( !pSh->IsReadOnlyAvailable() ||
190 !pSh->HasReadonlySel() );
193 /*--------------------------------------------------------------------
194 Beschreibung:
195 --------------------------------------------------------------------*/
197 SfxTabPage* SwFldEditDlg::CreatePage(USHORT nGroup)
199 // TabPage erzeugen
200 SfxTabPage* pTabPage = 0;
201 USHORT nHelpId = 0;
203 switch (nGroup)
205 case GRP_DOC:
206 pTabPage = SwFldDokPage::Create(this, *(SfxItemSet*)0);
207 nHelpId = HID_EDIT_FLD_DOK;
208 break;
209 case GRP_FKT:
210 pTabPage = SwFldFuncPage::Create(this, *(SfxItemSet*)0);
211 nHelpId = HID_EDIT_FLD_FUNC;
212 break;
213 case GRP_REF:
214 pTabPage = SwFldRefPage::Create(this, *(SfxItemSet*)0);
215 nHelpId = HID_EDIT_FLD_REF;
216 break;
217 case GRP_REG:
219 SfxObjectShell* pDocSh = SfxObjectShell::Current();
220 SfxItemSet* pSet = new SfxItemSet( pDocSh->GetPool(), SID_DOCINFO, SID_DOCINFO );
221 using namespace ::com::sun::star;
222 uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
223 pDocSh->GetModel(), uno::UNO_QUERY_THROW);
224 uno::Reference<document::XDocumentProperties> xDocProps
225 = xDPS->getDocumentProperties();
226 uno::Reference< beans::XPropertySet > xUDProps(
227 xDocProps->getUserDefinedProperties(),
228 uno::UNO_QUERY_THROW);
229 uno::Reference< beans::XPropertySetInfo > xSetInfo
230 = xUDProps->getPropertySetInfo();
231 const uno::Sequence< beans::Property > props
232 = xSetInfo->getProperties();
233 uno::Sequence< ::rtl::OUString > names(props.getLength());
234 for (sal_Int32 i = 0; i < props.getLength(); ++i) {
235 names[i] = props[i].Name;
237 pSet->Put( SfxUnoAnyItem( SID_DOCINFO, uno::makeAny(names) ) );
238 pTabPage = SwFldDokInfPage::Create(this, *pSet);
239 nHelpId = HID_EDIT_FLD_DOKINF;
240 break;
242 case GRP_DB:
243 pTabPage = SwFldDBPage::Create(this, *(SfxItemSet*)0);
244 static_cast<SwFldDBPage*>(pTabPage)->SetWrtShell(*pSh);
245 nHelpId = HID_EDIT_FLD_DB;
246 break;
247 case GRP_VAR:
248 pTabPage = SwFldVarPage::Create(this, *(SfxItemSet*)0);
249 nHelpId = HID_EDIT_FLD_VAR;
250 break;
253 pTabPage->SetHelpId(nHelpId);
254 static_cast<SwFldPage*>(pTabPage)->SetWrtShell(pSh);
256 SetTabPage(pTabPage);
258 String sTitle(GetText());
259 sTitle.Insert(String::CreateFromAscii(": "), 0);
260 sTitle.Insert(SW_RESSTR(STR_FLD_EDIT_DLG), 0);
261 SetText(sTitle);
263 return pTabPage;
266 /*--------------------------------------------------------------------
267 Beschreibung:
268 --------------------------------------------------------------------*/
270 SwFldEditDlg::~SwFldEditDlg()
272 pSh->SetCareWin(NULL);
273 pSh->EnterStdMode();
276 /*--------------------------------------------------------------------
277 Beschreibung:
278 --------------------------------------------------------------------*/
280 void SwFldEditDlg::EnableInsert(BOOL bEnable)
282 if( bEnable && pSh->IsReadOnlyAvailable() && pSh->HasReadonlySel() )
283 bEnable = FALSE;
284 GetOKButton()->Enable( bEnable );
287 /*--------------------------------------------------------------------
288 Beschreibung:
289 --------------------------------------------------------------------*/
291 void SwFldEditDlg::InsertHdl()
293 GetOKButton()->Click();
296 /*--------------------------------------------------------------------
297 Beschreibung: Aendern des Feldes anstossen
298 --------------------------------------------------------------------*/
300 IMPL_LINK( SwFldEditDlg, OKHdl, Button *, EMPTYARG )
302 if (GetOKButton()->IsEnabled())
304 SfxTabPage* pTabPage = GetTabPage();
305 if (pTabPage)
307 pTabPage->FillItemSet(*(SfxItemSet*)0);
310 EndDialog( RET_OK );
313 return 0;
316 /*--------------------------------------------------------------------
317 Beschreibung:
318 --------------------------------------------------------------------*/
320 short SwFldEditDlg::Execute()
322 // Ohne TabPage kein Dialog
323 return GetTabPage() ? Dialog::Execute() : RET_CANCEL;
326 /*--------------------------------------------------------------------
327 Beschreibung: Traveling zwishen Feldern gleichen Typs
328 --------------------------------------------------------------------*/
330 IMPL_LINK( SwFldEditDlg, NextPrevHdl, Button *, pButton )
332 BOOL bNext = pButton == &aNextBT;
334 pSh->EnterStdMode();
336 SwFieldType *pOldTyp = 0;
337 SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
339 //#112462# FillItemSet may delete the current field
340 //that's why it has to be called before accessing the current field
341 if( GetOKButton()->IsEnabled() )
342 pTabPage->FillItemSet(*(SfxItemSet*)0);
344 SwFldMgr& rMgr = pTabPage->GetFldMgr();
345 SwField *pCurFld = rMgr.GetCurFld();
346 if (pCurFld->GetTypeId() == TYP_DBFLD)
347 pOldTyp = (SwDBFieldType*)pCurFld->GetTyp();
349 rMgr.GoNextPrev( bNext, pOldTyp );
350 pCurFld = rMgr.GetCurFld();
352 /* #108536# Only create selection if there is none
353 already. Normalize PaM instead of swapping. */
354 if ( ! pSh->HasSelection() )
355 pSh->Right(CRSR_SKIP_CHARS, TRUE, 1, FALSE );
357 pSh->NormalizePam();
359 USHORT nGroup = rMgr.GetGroup(FALSE, pCurFld->GetTypeId(), pCurFld->GetSubType());
361 if (nGroup != pTabPage->GetGroup())
362 pTabPage = (SwFldPage*)CreatePage(nGroup);
364 pTabPage->EditNewField();
366 Init();
368 return 0;
371 /*--------------------------------------------------------------------
372 Beschreibung:
373 --------------------------------------------------------------------*/
375 IMPL_LINK( SwFldEditDlg, AddressHdl, PushButton *, EMPTYARG )
377 SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
378 SwFldMgr& rMgr = pTabPage->GetFldMgr();
379 SwField *pCurFld = rMgr.GetCurFld();
381 SfxItemSet aSet( pSh->GetAttrPool(),
382 SID_FIELD_GRABFOCUS, SID_FIELD_GRABFOCUS,
383 0L );
385 USHORT nEditPos = UNKNOWN_EDIT;
387 switch(pCurFld->GetSubType())
389 case EU_FIRSTNAME: nEditPos = FIRSTNAME_EDIT; break;
390 case EU_NAME: nEditPos = LASTNAME_EDIT; break;
391 case EU_SHORTCUT: nEditPos = SHORTNAME_EDIT; break;
392 case EU_COMPANY: nEditPos = COMPANY_EDIT; break;
393 case EU_STREET: nEditPos = STREET_EDIT; break;
394 case EU_TITLE: nEditPos = TITLE_EDIT; break;
395 case EU_POSITION: nEditPos = POSITION_EDIT; break;
396 case EU_PHONE_PRIVATE:nEditPos = TELPRIV_EDIT; break;
397 case EU_PHONE_COMPANY:nEditPos = TELCOMPANY_EDIT; break;
398 case EU_FAX: nEditPos = FAX_EDIT; break;
399 case EU_EMAIL: nEditPos = EMAIL_EDIT; break;
400 case EU_COUNTRY: nEditPos = COUNTRY_EDIT; break;
401 case EU_ZIP: nEditPos = PLZ_EDIT; break;
402 case EU_CITY: nEditPos = CITY_EDIT; break;
403 case EU_STATE: nEditPos = STATE_EDIT; break;
405 default: nEditPos = UNKNOWN_EDIT; break;
408 aSet.Put(SfxUInt16Item(SID_FIELD_GRABFOCUS, nEditPos));
409 SwAbstractDialogFactory* pFact = swui::GetFactory();
410 DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!");
412 SfxAbstractDialog* pDlg = pFact->CreateSfxDialog( this, aSet,
413 pSh->GetView().GetViewFrame()->GetFrame()->GetFrameInterface(),
414 RC_DLG_ADDR );
415 DBG_ASSERT(pDlg, "Dialogdiet fail!");
416 if(RET_OK == pDlg->Execute())
418 pSh->UpdateFlds( *pCurFld );
420 delete pDlg;
421 return 0;