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