Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 <expfld.hxx>
28 : #include <view.hxx>
29 : #include <dbfld.hxx>
30 : #include <wrtsh.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 <fldedt.hxx>
38 : #include <crsskip.hxx>
39 :
40 : #include <cmdid.h>
41 : #include <helpid.h>
42 : #include <globals.hrc>
43 : #include <fldui.hrc>
44 : #include "swabstdlg.hxx"
45 : #include "dialog.hrc"
46 :
47 : #include <com/sun/star/document/XDocumentProperties.hpp>
48 : #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
49 : #include <boost/scoped_ptr.hpp>
50 : #include <swuiexp.hxx>
51 :
52 :
53 0 : SwFldEditDlg::SwFldEditDlg(SwView& rVw)
54 0 : : SfxSingleTabDialog(&rVw.GetViewFrame()->GetWindow(), 0,
55 : "EditFieldDialog", "modules/swriter/ui/editfielddialog.ui")
56 0 : , pSh(rVw.GetWrtShellPtr())
57 : {
58 0 : get(m_pPrevBT, "prev");
59 0 : get(m_pNextBT, "next");
60 0 : get(m_pAddressBT, "edit");
61 :
62 0 : SwFldMgr aMgr(pSh);
63 :
64 0 : SwField *pCurFld = aMgr.GetCurFld();
65 0 : if(!pCurFld)
66 0 : return;
67 :
68 0 : SwViewShell::SetCareWin(this);
69 :
70 0 : if ( pSh->CrsrInsideInputFld() )
71 : {
72 : // move cursor to start of Input Field
73 0 : SwInputField* pInputFld = dynamic_cast<SwInputField*>(pCurFld);
74 0 : if ( pInputFld != NULL
75 0 : && pInputFld->GetFmtFld() != NULL )
76 : {
77 0 : pSh->GotoField( *(pInputFld->GetFmtFld()) );
78 : }
79 : }
80 :
81 0 : if ( ! pSh->HasSelection() )
82 : {
83 0 : pSh->Right(CRSR_SKIP_CHARS, true, 1, false);
84 : }
85 :
86 0 : pSh->NormalizePam();
87 :
88 0 : sal_uInt16 nGroup = aMgr.GetGroup(false, pCurFld->GetTypeId(), pCurFld->GetSubType());
89 :
90 0 : CreatePage(nGroup);
91 :
92 0 : GetOKButton()->SetClickHdl(LINK(this, SwFldEditDlg, OKHdl));
93 :
94 0 : m_pPrevBT->SetClickHdl(LINK(this, SwFldEditDlg, NextPrevHdl));
95 0 : m_pNextBT->SetClickHdl(LINK(this, SwFldEditDlg, NextPrevHdl));
96 :
97 0 : m_pAddressBT->SetClickHdl(LINK(this, SwFldEditDlg, AddressHdl));
98 :
99 0 : Init();
100 : }
101 :
102 : // initialise controls
103 0 : void SwFldEditDlg::Init()
104 : {
105 0 : SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
106 :
107 0 : if( pTabPage )
108 : {
109 0 : SwFldMgr& rMgr = pTabPage->GetFldMgr();
110 :
111 0 : SwField *pCurFld = rMgr.GetCurFld();
112 :
113 0 : if(!pCurFld)
114 0 : return;
115 :
116 : // Traveling only when more than one field
117 0 : pSh->StartAction();
118 0 : pSh->CreateCrsr();
119 :
120 0 : bool bMove = rMgr.GoNext();
121 0 : if( bMove )
122 0 : rMgr.GoPrev();
123 0 : m_pNextBT->Enable(bMove);
124 :
125 0 : if( ( bMove = rMgr.GoPrev() ) )
126 0 : rMgr.GoNext();
127 0 : m_pPrevBT->Enable( bMove );
128 :
129 0 : if (pCurFld->GetTypeId() == TYP_EXTUSERFLD)
130 0 : m_pAddressBT->Show();
131 :
132 0 : pSh->DestroyCrsr();
133 0 : pSh->EndAction();
134 : }
135 :
136 0 : GetOKButton()->Enable( !pSh->IsReadOnlyAvailable() ||
137 0 : !pSh->HasReadonlySel() );
138 : }
139 :
140 0 : SfxTabPage* SwFldEditDlg::CreatePage(sal_uInt16 nGroup)
141 : {
142 : // create TabPage
143 0 : SfxTabPage* pTabPage = 0;
144 :
145 0 : switch (nGroup)
146 : {
147 : case GRP_DOC:
148 0 : pTabPage = SwFldDokPage::Create(get_content_area(), 0);
149 0 : break;
150 : case GRP_FKT:
151 0 : pTabPage = SwFldFuncPage::Create(get_content_area(), 0);
152 0 : break;
153 : case GRP_REF:
154 0 : pTabPage = SwFldRefPage::Create(get_content_area(), 0);
155 0 : break;
156 : case GRP_REG:
157 : {
158 0 : SfxObjectShell* pDocSh = SfxObjectShell::Current();
159 0 : SfxItemSet* pSet = new SfxItemSet( pDocSh->GetPool(), SID_DOCINFO, SID_DOCINFO );
160 : using namespace ::com::sun::star;
161 : uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
162 0 : pDocSh->GetModel(), uno::UNO_QUERY_THROW);
163 : uno::Reference<document::XDocumentProperties> xDocProps
164 0 : = xDPS->getDocumentProperties();
165 : uno::Reference< beans::XPropertySet > xUDProps(
166 0 : xDocProps->getUserDefinedProperties(),
167 0 : uno::UNO_QUERY_THROW);
168 0 : pSet->Put( SfxUnoAnyItem( SID_DOCINFO, uno::makeAny(xUDProps) ) );
169 0 : pTabPage = SwFldDokInfPage::Create(get_content_area(), pSet);
170 0 : break;
171 : }
172 : case GRP_DB:
173 0 : pTabPage = SwFldDBPage::Create(get_content_area(), 0);
174 0 : static_cast<SwFldDBPage*>(pTabPage)->SetWrtShell(*pSh);
175 0 : break;
176 : case GRP_VAR:
177 0 : pTabPage = SwFldVarPage::Create(get_content_area(), 0);
178 0 : break;
179 :
180 : }
181 :
182 : assert(pTabPage);
183 :
184 0 : if (pTabPage)
185 : {
186 0 : static_cast<SwFldPage*>(pTabPage)->SetWrtShell(pSh);
187 0 : SetTabPage(pTabPage);
188 : }
189 :
190 0 : return pTabPage;
191 : }
192 :
193 0 : SwFldEditDlg::~SwFldEditDlg()
194 : {
195 0 : SwViewShell::SetCareWin(NULL);
196 0 : pSh->EnterStdMode();
197 0 : }
198 :
199 0 : void SwFldEditDlg::EnableInsert(bool bEnable)
200 : {
201 0 : if( bEnable && pSh->IsReadOnlyAvailable() && pSh->HasReadonlySel() )
202 0 : bEnable = false;
203 0 : GetOKButton()->Enable( bEnable );
204 0 : }
205 :
206 0 : void SwFldEditDlg::InsertHdl()
207 : {
208 0 : GetOKButton()->Click();
209 0 : }
210 :
211 : // kick off changing of the field
212 0 : IMPL_LINK_NOARG(SwFldEditDlg, OKHdl)
213 : {
214 0 : if (GetOKButton()->IsEnabled())
215 : {
216 0 : SfxTabPage* pTabPage = GetTabPage();
217 0 : if (pTabPage)
218 : {
219 0 : pTabPage->FillItemSet(0);
220 :
221 : }
222 0 : EndDialog( RET_OK );
223 : }
224 :
225 0 : return 0;
226 : }
227 :
228 0 : short SwFldEditDlg::Execute()
229 : {
230 : // without TabPage no dialog
231 0 : return GetTabPage() ? Dialog::Execute() : static_cast<short>(RET_CANCEL);
232 : }
233 :
234 : // Traveling between fields of the same type
235 0 : IMPL_LINK( SwFldEditDlg, NextPrevHdl, Button *, pButton )
236 : {
237 0 : bool bNext = pButton == m_pNextBT;
238 :
239 0 : pSh->EnterStdMode();
240 :
241 0 : SwFieldType *pOldTyp = 0;
242 0 : SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
243 :
244 : //#112462# FillItemSet may delete the current field
245 : //that's why it has to be called before accessing the current field
246 0 : if( GetOKButton()->IsEnabled() )
247 0 : pTabPage->FillItemSet(0);
248 :
249 0 : SwFldMgr& rMgr = pTabPage->GetFldMgr();
250 0 : SwField *pCurFld = rMgr.GetCurFld();
251 0 : if (pCurFld->GetTypeId() == TYP_DBFLD)
252 0 : pOldTyp = (SwDBFieldType*)pCurFld->GetTyp();
253 :
254 0 : rMgr.GoNextPrev( bNext, pOldTyp );
255 0 : pCurFld = rMgr.GetCurFld();
256 :
257 : /* #108536# Only create selection if there is none
258 : already. Normalize PaM instead of swapping. */
259 0 : if ( ! pSh->HasSelection() )
260 0 : pSh->Right(CRSR_SKIP_CHARS, true, 1, false );
261 :
262 0 : pSh->NormalizePam();
263 :
264 0 : sal_uInt16 nGroup = rMgr.GetGroup(false, pCurFld->GetTypeId(), pCurFld->GetSubType());
265 :
266 0 : if (nGroup != pTabPage->GetGroup())
267 0 : pTabPage = (SwFldPage*)CreatePage(nGroup);
268 :
269 0 : pTabPage->EditNewField();
270 :
271 0 : Init();
272 :
273 0 : return 0;
274 : }
275 :
276 0 : IMPL_LINK_NOARG(SwFldEditDlg, AddressHdl)
277 : {
278 0 : SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
279 0 : SwFldMgr& rMgr = pTabPage->GetFldMgr();
280 0 : SwField *pCurFld = rMgr.GetCurFld();
281 :
282 0 : SfxItemSet aSet( pSh->GetAttrPool(),
283 : SID_FIELD_GRABFOCUS, SID_FIELD_GRABFOCUS,
284 0 : 0L );
285 :
286 0 : sal_uInt16 nEditPos = UNKNOWN_EDIT;
287 :
288 0 : switch(pCurFld->GetSubType())
289 : {
290 0 : case EU_FIRSTNAME: nEditPos = FIRSTNAME_EDIT; break;
291 0 : case EU_NAME: nEditPos = LASTNAME_EDIT; break;
292 0 : case EU_SHORTCUT: nEditPos = SHORTNAME_EDIT; break;
293 0 : case EU_COMPANY: nEditPos = COMPANY_EDIT; break;
294 0 : case EU_STREET: nEditPos = STREET_EDIT; break;
295 0 : case EU_TITLE: nEditPos = TITLE_EDIT; break;
296 0 : case EU_POSITION: nEditPos = POSITION_EDIT; break;
297 0 : case EU_PHONE_PRIVATE:nEditPos = TELPRIV_EDIT; break;
298 0 : case EU_PHONE_COMPANY:nEditPos = TELCOMPANY_EDIT; break;
299 0 : case EU_FAX: nEditPos = FAX_EDIT; break;
300 0 : case EU_EMAIL: nEditPos = EMAIL_EDIT; break;
301 0 : case EU_COUNTRY: nEditPos = COUNTRY_EDIT; break;
302 0 : case EU_ZIP: nEditPos = PLZ_EDIT; break;
303 0 : case EU_CITY: nEditPos = CITY_EDIT; break;
304 0 : case EU_STATE: nEditPos = STATE_EDIT; break;
305 :
306 0 : default: nEditPos = UNKNOWN_EDIT; break;
307 :
308 : }
309 0 : aSet.Put(SfxUInt16Item(SID_FIELD_GRABFOCUS, nEditPos));
310 0 : SwAbstractDialogFactory* pFact = swui::GetFactory();
311 : OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
312 :
313 : boost::scoped_ptr<SfxAbstractDialog> pDlg(pFact->CreateSfxDialog( this, aSet,
314 0 : pSh->GetView().GetViewFrame()->GetFrame().GetFrameInterface(),
315 0 : RC_DLG_ADDR ));
316 : OSL_ENSURE(pDlg, "Dialog creation failed!");
317 0 : if(RET_OK == pDlg->Execute())
318 : {
319 0 : pSh->UpdateFlds( *pCurFld );
320 : }
321 0 : return 0;
322 0 : }
323 :
324 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|