merge the formfield patch from ooo-build
[ooovba.git] / sw / source / ui / envelp / labelexp.cxx
blob21a9dcf59989eedf66407c228928418bae508dd9
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: labelexp.cxx,v $
10 * $Revision: 1.11 $
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
38 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
39 #include <com/sun/star/util/XRefreshable.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <comphelper/processfactory.hxx>
42 #include <swtypes.hxx>
43 #include <labfmt.hxx>
44 #include <unotools.hxx>
45 #include <unoatxt.hxx>
46 #include <unoobj.hxx>
47 #include <unoprnms.hxx>
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::uno;
54 using namespace ::comphelper;
55 using ::rtl::OUString;
57 #ifdef SW_PROP_NAME_STR
58 #undef SW_PROP_NAME_STR
59 #endif
60 #define SW_PROP_NAME_STR(nId) SwGetPropName((nId)).pName
62 /* -----------------08.07.99 15:15-------------------
64 --------------------------------------------------*/
65 void SwVisitingCardPage::InitFrameControl()
67 Link aLink(LINK(this, SwVisitingCardPage, FrameControlInitializedHdl));
68 pExampleFrame = new SwOneExampleFrame( aExampleWIN,
69 EX_SHOW_BUSINESS_CARDS, &aLink );
71 uno::Reference< lang::XMultiServiceFactory > xMgr =
72 getProcessServiceFactory();
73 //now the AutoText ListBoxes have to be filled
75 uno::Reference< uno::XInterface > xAText =
76 xMgr->createInstance( C2U("com.sun.star.text.AutoTextContainer") );
77 _xAutoText = uno::Reference< container::XNameAccess >(xAText, uno::UNO_QUERY);
79 uno::Sequence<OUString> aNames = _xAutoText->getElementNames();
80 const OUString* pGroups = aNames.getConstArray();
81 OUString uTitleName( C2U(SW_PROP_NAME_STR(UNO_NAME_TITLE)) );
83 for(sal_uInt16 i = 0; i < aNames.getLength(); i++)
85 uno::Any aGroup = _xAutoText->getByName(pGroups[i]);
86 uno::Reference< text::XAutoTextGroup > xGroup;
87 aGroup >>= xGroup;
88 uno::Reference< container::XIndexAccess > xIdxAcc(xGroup, uno::UNO_QUERY);
89 try
91 if(!xIdxAcc.is() || xIdxAcc->getCount())
93 uno::Reference< beans::XPropertySet > xPrSet(xGroup, uno::UNO_QUERY);
94 uno::Any aTitle = xPrSet->getPropertyValue( uTitleName );
95 OUString uTitle;
96 aTitle >>= uTitle;
97 String sGroup(pGroups[i]);
98 sal_uInt16 nEntry = aAutoTextGroupLB.InsertEntry(uTitle);
99 aAutoTextGroupLB.SetEntryData(nEntry, new String(sGroup));
102 catch(Exception&)
106 if(aAutoTextGroupLB.GetEntryCount())
108 if(LISTBOX_ENTRY_NOTFOUND == aAutoTextGroupLB.GetSelectEntryPos())
109 aAutoTextGroupLB.SelectEntryPos(0);
110 String sCurGroupName(
111 *(String*)aAutoTextGroupLB.GetEntryData(aAutoTextGroupLB.GetSelectEntryPos()));
112 if(_xAutoText->hasByName(sCurGroupName))
114 uno::Any aGroup = _xAutoText->getByName(sCurGroupName);
117 uno::Reference< text::XAutoTextGroup > xGroup;
118 aGroup >>= xGroup;
119 uno::Sequence< OUString > aBlockNames = xGroup->getElementNames();
120 uno::Sequence< OUString > aTitles = xGroup->getTitles() ;
122 SetUserData( aBlockNames.getLength(), aTitles.getConstArray(),
123 aBlockNames.getConstArray() );
125 catch( uno::RuntimeException& )
127 // we'll be her if path settings were wrong
132 /* -----------------01.10.99 13:19-------------------
134 --------------------------------------------------*/
135 IMPL_LINK( SwVisitingCardPage, FrameControlInitializedHdl, void*, EMPTYARG )
137 SvLBoxEntry* pSel = aAutoTextLB.FirstSelected();
138 String sEntry;
139 if( pSel )
140 sEntry = *(String*)pSel->GetUserData();
141 uno::Reference< text::XTextCursor > & xCrsr = pExampleFrame->GetTextCursor();
142 OUString uEntry(sEntry);
144 if(LISTBOX_ENTRY_NOTFOUND != aAutoTextGroupLB.GetSelectEntryPos())
146 String sGroup( *(String*)aAutoTextGroupLB.GetEntryData(
147 aAutoTextGroupLB.GetSelectEntryPos() ) );
148 uno::Any aGroup = _xAutoText->getByName(sGroup);
149 uno::Reference< text::XAutoTextGroup > xGroup;
150 aGroup >>= xGroup;
152 if( sEntry.Len() && xGroup->hasByName( uEntry ) )
154 uno::Any aEntry(xGroup->getByName(uEntry));
155 uno::Reference< text::XAutoTextEntry > xEntry;
156 aEntry >>= xEntry;
157 if(xEntry.is())
159 uno::Reference< text::XTextRange > xRange(xCrsr, uno::UNO_QUERY);
160 xEntry->applyTo(xRange);
162 UpdateFields();
165 return 0;
167 /* -----------------22.07.99 11:06-------------------
169 --------------------------------------------------*/
170 IMPL_LINK( SwVisitingCardPage, AutoTextSelectHdl, void*, pBox )
172 if(_xAutoText.is())
174 if( &aAutoTextGroupLB == pBox )
176 String sGroup( *(String*)aAutoTextGroupLB.GetEntryData(
177 aAutoTextGroupLB.GetSelectEntryPos()));
178 uno::Any aGroup = _xAutoText->getByName(sGroup);
179 uno::Reference< text::XAutoTextGroup > xGroup;
180 aGroup >>= xGroup;
182 ClearUserData();
183 aAutoTextLB.Clear();
185 uno::Sequence<OUString> aBlockNames = xGroup->getElementNames();
186 uno::Sequence< OUString > aTitles = xGroup->getTitles() ;
187 SetUserData( aBlockNames.getLength(), aTitles.getConstArray(),
188 aBlockNames.getConstArray() );
190 if(pExampleFrame->IsInitialized())
191 pExampleFrame->ClearDocument( TRUE );
193 return 0;
196 /* -----------------01.10.99 11:59-------------------
198 --------------------------------------------------*/
199 void SwVisitingCardPage::UpdateFields()
201 uno::Reference< frame::XModel > xModel;
202 if( pExampleFrame && (xModel = pExampleFrame->GetModel()).is())
204 SwLabDlg::UpdateFieldInformation(xModel, aLabItem);
207 /* -----------------01.10.99 15:16-------------------
209 --------------------------------------------------*/
210 void SwLabDlg::UpdateFieldInformation(uno::Reference< frame::XModel > & xModel, const SwLabItem& rItem)
212 uno::Reference< text::XTextFieldsSupplier > xFlds(xModel, uno::UNO_QUERY);
213 uno::Reference< container::XNameAccess > xFldMasters = xFlds->getTextFieldMasters();
215 static const struct _SwLabItemMap {
216 const char* pName;
217 rtl::OUString SwLabItem:: *pValue;
218 } aArr[] = {
219 { "BC_PRIV_FIRSTNAME" , &SwLabItem::aPrivFirstName },
220 { "BC_PRIV_NAME" , &SwLabItem::aPrivName },
221 { "BC_PRIV_INITIALS" , &SwLabItem::aPrivShortCut },
222 { "BC_PRIV_FIRSTNAME_2", &SwLabItem::aPrivFirstName2 },
223 { "BC_PRIV_NAME_2" , &SwLabItem::aPrivName2 },
224 { "BC_PRIV_INITIALS_2" , &SwLabItem::aPrivShortCut2 },
225 { "BC_PRIV_STREET" , &SwLabItem::aPrivStreet },
226 { "BC_PRIV_ZIP" , &SwLabItem::aPrivZip },
227 { "BC_PRIV_CITY" , &SwLabItem::aPrivCity },
228 { "BC_PRIV_COUNTRY" , &SwLabItem::aPrivCountry },
229 { "BC_PRIV_STATE" , &SwLabItem::aPrivState },
230 { "BC_PRIV_TITLE" , &SwLabItem::aPrivTitle },
231 { "BC_PRIV_PROFESSION" , &SwLabItem::aPrivProfession },
232 { "BC_PRIV_PHONE" , &SwLabItem::aPrivPhone },
233 { "BC_PRIV_MOBILE" , &SwLabItem::aPrivMobile },
234 { "BC_PRIV_FAX" , &SwLabItem::aPrivFax },
235 { "BC_PRIV_WWW" , &SwLabItem::aPrivWWW },
236 { "BC_PRIV_MAIL" , &SwLabItem::aPrivMail },
237 { "BC_COMP_COMPANY" , &SwLabItem::aCompCompany },
238 { "BC_COMP_COMPANYEXT" , &SwLabItem::aCompCompanyExt },
239 { "BC_COMP_SLOGAN" , &SwLabItem::aCompSlogan },
240 { "BC_COMP_STREET" , &SwLabItem::aCompStreet },
241 { "BC_COMP_ZIP" , &SwLabItem::aCompZip },
242 { "BC_COMP_CITY" , &SwLabItem::aCompCity },
243 { "BC_COMP_COUNTRY" , &SwLabItem::aCompCountry },
244 { "BC_COMP_STATE" , &SwLabItem::aCompState },
245 { "BC_COMP_POSITION" , &SwLabItem::aCompPosition },
246 { "BC_COMP_PHONE" , &SwLabItem::aCompPhone },
247 { "BC_COMP_MOBILE" , &SwLabItem::aCompMobile },
248 { "BC_COMP_FAX" , &SwLabItem::aCompFax },
249 { "BC_COMP_WWW" , &SwLabItem::aCompWWW },
250 { "BC_COMP_MAIL" , &SwLabItem::aCompMail },
251 { 0, 0 }
256 String sFldName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM(
257 "com.sun.star.text.FieldMaster.User." )));
258 OUString uCntName( C2U( SW_PROP_NAME_STR(UNO_NAME_CONTENT )));
259 for( const _SwLabItemMap* p = aArr; p->pName; ++p )
261 String sCurFldName( sFldName );
262 sCurFldName.AppendAscii( p->pName );
263 OUString uFldName( sCurFldName );
264 if( xFldMasters->hasByName( uFldName ))
266 uno::Any aFirstName = xFldMasters->getByName( uFldName );
267 uno::Reference< beans::XPropertySet > xFld;
268 aFirstName >>= xFld;
269 uno::Any aContent;
270 aContent <<= rItem.*p->pValue;
271 xFld->setPropertyValue( uCntName, aContent );
275 catch( uno::RuntimeException&)
280 uno::Reference< container::XEnumerationAccess > xFldAcc = xFlds->getTextFields();
281 uno::Reference< util::XRefreshable > xRefresh(xFldAcc, uno::UNO_QUERY);
282 xRefresh->refresh();