xref: /trunk/main/sw/source/ui/fldui/fldedt.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 #ifdef SW_DLLIMPLEMENTATION
27 #undef SW_DLLIMPLEMENTATION
28 #endif
29 #include <vcl/msgbox.hxx>
30 #include <sfx2/basedlgs.hxx>
31 #include <sfx2/viewfrm.hxx>
32 #include <sfx2/request.hxx>
33 #include <sfx2/app.hxx>
34 #include <svx/optgenrl.hxx>
35 #include <docufld.hxx>
36 #ifndef _VIEW_HXX
37 #include <view.hxx>
38 #endif
39 #include <dbfld.hxx>
40 #include <wrtsh.hxx>
41 #ifndef _FLDDB_HXX
42 #include <flddb.hxx>
43 #endif
44 #ifndef _FLDDINF_HXX
45 #include <flddinf.hxx>
46 #endif
47 #ifndef _FLDVAR_HXX
48 #include <fldvar.hxx>
49 #endif
50 #ifndef _FLDDOK_HXX
51 #include <flddok.hxx>
52 #endif
53 #ifndef _FLDFUNC_HXX
54 #include <fldfunc.hxx>
55 #endif
56 #ifndef _FLDREF_HXX
57 #include <fldref.hxx>
58 #endif
59 #include <fldedt.hxx>
60 #include <crsskip.hxx>
61 
62 
63 #ifndef _CMDID_H
64 #include <cmdid.h>
65 #endif
66 #ifndef _HELPID_H
67 #include <helpid.h>
68 #endif
69 #ifndef _GLOBALS_HRC
70 #include <globals.hrc>
71 #endif
72 #ifndef _FLDUI_HRC
73 #include <fldui.hrc>
74 #endif
75 #include "swabstdlg.hxx"
76 #include "dialog.hrc"
77 
78 #include <com/sun/star/document/XDocumentProperties.hpp>
79 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
80 
81 
82 namespace swui
83 {
84 	SwAbstractDialogFactory * GetFactory();
85 }
86 
87 /*--------------------------------------------------------------------
88 	Beschreibung:
89  --------------------------------------------------------------------*/
90 
91 SwFldEditDlg::SwFldEditDlg(SwView& rVw) :
92 	SfxSingleTabDialog(&rVw.GetViewFrame()->GetWindow(), 0, 0),
93 	pSh			(rVw.GetWrtShellPtr()),
94 	aPrevBT		(this, SW_RES(BTN_FLDEDT_PREV)),
95     aNextBT     (this, SW_RES(BTN_FLDEDT_NEXT)),
96     aAddressBT  (this, SW_RES(PB_FLDEDT_ADDRESS))
97 {
98 	SwFldMgr aMgr(pSh);
99 
100 	SwField *pCurFld = aMgr.GetCurFld();
101 	if(!pCurFld)
102 		return;
103 
104 	pSh->SetCareWin(this);
105 
106     /* #108536# Only create selection if there is none
107         already. Normalize PaM instead of swapping. */
108     if ( ! pSh->HasSelection() )
109         pSh->Right(CRSR_SKIP_CHARS, sal_True, 1, sal_False );
110 
111 	pSh->NormalizePam();
112 
113 	sal_uInt16 nGroup = aMgr.GetGroup(sal_False, pCurFld->GetTypeId(), pCurFld->GetSubType());
114 
115 	CreatePage(nGroup);
116 
117 	GetOKButton()->SetClickHdl(LINK(this, SwFldEditDlg, OKHdl));
118 
119 	// Buttons selbst positionieren, da sie sonst bei unterschiedlichen
120 	// Fontgroessen im Wald stehen, und da PB im SingleTabDlg feste Pixelgroessen
121 	// fuer seine Buttons und die Dialogbreite verwendet.
122 	aPrevBT.SetPosPixel(Point(GetOKButton()->GetPosPixel().X(), aPrevBT.GetPosPixel().Y()));
123     sal_uInt16 nWidth = static_cast< sal_uInt16 >(GetOKButton()->GetOutputSize().Width() / 2 - 3);
124 	Size aNewSize(LogicToPixel(Size(nWidth, GetOKButton()->GetOutputSize().Height())));
125 	aPrevBT.SetSizePixel(aNewSize);
126 
127 	aNextBT.SetSizePixel(aPrevBT.GetSizePixel());
128 
129 	long nXPos = GetOKButton()->GetPosPixel().X() + GetOKButton()->GetSizePixel().Width()
130 					- aNextBT.GetSizePixel().Width() - 1;
131 	aNextBT.SetPosPixel(Point(nXPos, aNextBT.GetPosPixel().Y()));
132 
133 	aAddressBT.SetPosPixel(Point(GetOKButton()->GetPosPixel().X(), aAddressBT.GetPosPixel().Y()));
134 	aAddressBT.SetSizePixel(GetOKButton()->GetSizePixel());
135 
136 	aPrevBT.SetClickHdl(LINK(this, SwFldEditDlg, NextPrevHdl));
137 	aNextBT.SetClickHdl(LINK(this, SwFldEditDlg, NextPrevHdl));
138 
139 	aAddressBT.SetClickHdl(LINK(this, SwFldEditDlg, AddressHdl));
140 	aAddressBT.SetHelpId(HID_FLDEDT_ADDRESS);
141 
142 	Init();
143 }
144 
145 /*--------------------------------------------------------------------
146 	Beschreibung: Controlls initialisieren
147  --------------------------------------------------------------------*/
148 
149 void SwFldEditDlg::Init()
150 {
151     SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
152 
153     if( pTabPage )
154 	{
155         SwFldMgr& rMgr = pTabPage->GetFldMgr();
156 
157 		SwField *pCurFld = rMgr.GetCurFld();
158 
159 		if(!pCurFld)
160 			return;
161 
162 		// Traveling nur bei mehr als einem Feld
163 		pSh->StartAction();
164 		pSh->CreateCrsr();
165 
166 		sal_Bool bMove = rMgr.GoNext();
167 		if( bMove )
168 			rMgr.GoPrev();
169 		aNextBT.Enable(bMove);
170 
171 		if( 0 != ( bMove = rMgr.GoPrev() ) )
172 			rMgr.GoNext();
173 		aPrevBT.Enable( bMove );
174 
175 		if (pCurFld->GetTypeId() == TYP_EXTUSERFLD)
176 			aAddressBT.Show();
177 
178 		pSh->DestroyCrsr();
179 		pSh->EndAction();
180 	}
181 
182 	GetOKButton()->Enable( !pSh->IsReadOnlyAvailable() ||
183 						   !pSh->HasReadonlySel() );
184 }
185 
186 /*--------------------------------------------------------------------
187 	Beschreibung:
188  --------------------------------------------------------------------*/
189 
190 SfxTabPage* SwFldEditDlg::CreatePage(sal_uInt16 nGroup)
191 {
192 	// TabPage erzeugen
193     SfxTabPage* pTabPage = 0;
194 	const char* pHelpId = 0;
195 
196 	switch (nGroup)
197 	{
198 		case GRP_DOC:
199             pTabPage = SwFldDokPage::Create(this, *(SfxItemSet*)0);
200 			pHelpId = HID_EDIT_FLD_DOK;
201 			break;
202 		case GRP_FKT:
203             pTabPage = SwFldFuncPage::Create(this, *(SfxItemSet*)0);
204 			pHelpId = HID_EDIT_FLD_FUNC;
205 			break;
206 		case GRP_REF:
207             pTabPage = SwFldRefPage::Create(this, *(SfxItemSet*)0);
208 			pHelpId = HID_EDIT_FLD_REF;
209 			break;
210 		case GRP_REG:
211 			{
212 				SfxObjectShell* pDocSh = SfxObjectShell::Current();
213 				SfxItemSet* pSet = new SfxItemSet( pDocSh->GetPool(), SID_DOCINFO, SID_DOCINFO );
214                 using namespace ::com::sun::star;
215                 uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
216                     pDocSh->GetModel(), uno::UNO_QUERY_THROW);
217                 uno::Reference<document::XDocumentProperties> xDocProps
218                     = xDPS->getDocumentProperties();
219                 uno::Reference< beans::XPropertySet > xUDProps(
220                     xDocProps->getUserDefinedProperties(),
221                     uno::UNO_QUERY_THROW);
222 //                uno::Reference< beans::XPropertySetInfo > xSetInfo
223 //                    = xUDProps->getPropertySetInfo();
224 //                const uno::Sequence< beans::Property > props
225 //                    = xSetInfo->getProperties();
226 //                uno::Sequence< ::rtl::OUString > names(props.getLength());
227 //                for (sal_Int32 i = 0; i < props.getLength(); ++i) {
228 //                    names[i] = props[i].Name;
229 //                }
230                 pSet->Put( SfxUnoAnyItem( SID_DOCINFO, uno::makeAny(xUDProps) ) );
231 		        pTabPage = SwFldDokInfPage::Create(this, *pSet);
232 				pHelpId = HID_EDIT_FLD_DOKINF;
233 				break;
234 			}
235 		case GRP_DB:
236             pTabPage = SwFldDBPage::Create(this, *(SfxItemSet*)0);
237             static_cast<SwFldDBPage*>(pTabPage)->SetWrtShell(*pSh);
238 			pHelpId = HID_EDIT_FLD_DB;
239 			break;
240 		case GRP_VAR:
241             pTabPage = SwFldVarPage::Create(this, *(SfxItemSet*)0);
242 			pHelpId = HID_EDIT_FLD_VAR;
243 			break;
244 
245 	}
246 
247     pTabPage->SetHelpId(pHelpId);
248     static_cast<SwFldPage*>(pTabPage)->SetWrtShell(pSh);
249 
250     SetTabPage(pTabPage);
251 
252 	String sTitle(GetText());
253 	sTitle.Insert(String::CreateFromAscii(": "), 0);
254 	sTitle.Insert(SW_RESSTR(STR_FLD_EDIT_DLG), 0);
255 	SetText(sTitle);
256 
257     return pTabPage;
258 }
259 
260 /*--------------------------------------------------------------------
261 	Beschreibung:
262  --------------------------------------------------------------------*/
263 
264 SwFldEditDlg::~SwFldEditDlg()
265 {
266 	pSh->SetCareWin(NULL);
267 	pSh->EnterStdMode();
268 }
269 
270 /*--------------------------------------------------------------------
271 	Beschreibung:
272  --------------------------------------------------------------------*/
273 
274 void SwFldEditDlg::EnableInsert(sal_Bool bEnable)
275 {
276 	if( bEnable && pSh->IsReadOnlyAvailable() && pSh->HasReadonlySel() )
277 		bEnable = sal_False;
278 	GetOKButton()->Enable( bEnable );
279 }
280 
281 /*--------------------------------------------------------------------
282 	Beschreibung:
283  --------------------------------------------------------------------*/
284 
285 void SwFldEditDlg::InsertHdl()
286 {
287 	GetOKButton()->Click();
288 }
289 
290 /*--------------------------------------------------------------------
291 	 Beschreibung: Aendern des Feldes anstossen
292  --------------------------------------------------------------------*/
293 
294 IMPL_LINK( SwFldEditDlg, OKHdl, Button *, EMPTYARG )
295 {
296 	if (GetOKButton()->IsEnabled())
297 	{
298         SfxTabPage* pTabPage = GetTabPage();
299         if (pTabPage)
300 		{
301             pTabPage->FillItemSet(*(SfxItemSet*)0);
302 
303 		}
304 		EndDialog( RET_OK );
305 	}
306 
307 	return 0;
308 }
309 
310 /*--------------------------------------------------------------------
311 	Beschreibung:
312  --------------------------------------------------------------------*/
313 
314 short SwFldEditDlg::Execute()
315 {
316 	// Ohne TabPage kein Dialog
317 	return GetTabPage() ? Dialog::Execute() : RET_CANCEL;
318 }
319 
320 /*--------------------------------------------------------------------
321 	Beschreibung: Traveling zwishen Feldern gleichen Typs
322  --------------------------------------------------------------------*/
323 
324 IMPL_LINK( SwFldEditDlg, NextPrevHdl, Button *, pButton )
325 {
326 	sal_Bool bNext = pButton == &aNextBT;
327 
328 	pSh->EnterStdMode();
329 
330 	SwFieldType *pOldTyp = 0;
331     SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
332 
333 	//#112462# FillItemSet may delete the current field
334 	//that's why it has to be called before accessing the current field
335 	if( GetOKButton()->IsEnabled() )
336         pTabPage->FillItemSet(*(SfxItemSet*)0);
337 
338     SwFldMgr& rMgr = pTabPage->GetFldMgr();
339 	SwField *pCurFld = rMgr.GetCurFld();
340 	if (pCurFld->GetTypeId() == TYP_DBFLD)
341 		pOldTyp = (SwDBFieldType*)pCurFld->GetTyp();
342 
343 	rMgr.GoNextPrev( bNext, pOldTyp );
344 	pCurFld = rMgr.GetCurFld();
345 
346     /* #108536# Only create selection if there is none
347         already. Normalize PaM instead of swapping. */
348     if ( ! pSh->HasSelection() )
349         pSh->Right(CRSR_SKIP_CHARS, sal_True, 1, sal_False );
350 
351 	pSh->NormalizePam();
352 
353 	sal_uInt16 nGroup = rMgr.GetGroup(sal_False, pCurFld->GetTypeId(), pCurFld->GetSubType());
354 
355     if (nGroup != pTabPage->GetGroup())
356         pTabPage = (SwFldPage*)CreatePage(nGroup);
357 
358     pTabPage->EditNewField();
359 
360 	Init();
361 
362 	return 0;
363 }
364 
365 /*--------------------------------------------------------------------
366 	Beschreibung:
367  --------------------------------------------------------------------*/
368 
369 IMPL_LINK( SwFldEditDlg, AddressHdl, PushButton *, EMPTYARG )
370 {
371     SwFldPage* pTabPage = (SwFldPage*)GetTabPage();
372     SwFldMgr& rMgr = pTabPage->GetFldMgr();
373 	SwField *pCurFld = rMgr.GetCurFld();
374 
375 	SfxItemSet aSet( pSh->GetAttrPool(),
376 						SID_FIELD_GRABFOCUS, SID_FIELD_GRABFOCUS,
377 						0L );
378 
379 	sal_uInt16 nEditPos = UNKNOWN_EDIT;
380 
381 	switch(pCurFld->GetSubType())
382 	{
383 		case EU_FIRSTNAME:	nEditPos = FIRSTNAME_EDIT;	break;
384 		case EU_NAME:   	nEditPos = LASTNAME_EDIT;	break;
385 		case EU_SHORTCUT: 	nEditPos = SHORTNAME_EDIT;	break;
386 		case EU_COMPANY:  	nEditPos = COMPANY_EDIT;	break;
387 		case EU_STREET:		nEditPos = STREET_EDIT;		break;
388 		case EU_TITLE:  	nEditPos = TITLE_EDIT;		break;
389 		case EU_POSITION: 	nEditPos = POSITION_EDIT;	break;
390 		case EU_PHONE_PRIVATE:nEditPos = TELPRIV_EDIT;	break;
391 		case EU_PHONE_COMPANY:nEditPos = TELCOMPANY_EDIT;	break;
392 		case EU_FAX:		nEditPos = FAX_EDIT;		break;
393 		case EU_EMAIL:		nEditPos = EMAIL_EDIT;		break;
394 		case EU_COUNTRY:	nEditPos = COUNTRY_EDIT;	break;
395 		case EU_ZIP:		nEditPos = PLZ_EDIT;		break;
396 		case EU_CITY:		nEditPos = CITY_EDIT;		break;
397 		case EU_STATE:		nEditPos = STATE_EDIT;		break;
398 
399 		default:			nEditPos = UNKNOWN_EDIT;	break;
400 
401 	}
402 	aSet.Put(SfxUInt16Item(SID_FIELD_GRABFOCUS, nEditPos));
403     SwAbstractDialogFactory* pFact = swui::GetFactory();
404     DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!");
405 
406     SfxAbstractDialog* pDlg = pFact->CreateSfxDialog( this, aSet,
407 		pSh->GetView().GetViewFrame()->GetFrame().GetFrameInterface(),
408 		RC_DLG_ADDR );
409     DBG_ASSERT(pDlg, "Dialogdiet fail!");
410 	if(RET_OK == pDlg->Execute())
411     {
412     	pSh->UpdateFlds( *pCurFld );
413     }
414     delete pDlg;
415 	return 0;
416 }
417 
418 
419