xref: /trunk/main/sw/source/core/unocore/SwXTextDefaults.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 
29 #include <vos/mutex.hxx>
30 #include <vcl/svapp.hxx>
31 
32 #include <SwXTextDefaults.hxx>
33 #include <SwStyleNameMapper.hxx>
34 #include <fchrfmt.hxx>
35 #include <charfmt.hxx>
36 #include <docstyle.hxx>
37 #include <doc.hxx>
38 #include <docsh.hxx>
39 #include <unomap.hxx>
40 #include <unomid.h>
41 #include <paratr.hxx>
42 #include <unoprnms.hxx>
43 #include <unocrsrhelper.hxx>
44 #include <hintids.hxx>
45 
46 #include <unomid.h>
47 
48 
49 using rtl::OUString;
50 using namespace rtl;
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::lang;
55 
56 
SwXTextDefaults(SwDoc * pNewDoc)57 SwXTextDefaults::SwXTextDefaults ( SwDoc * pNewDoc ) :
58     m_pPropSet( aSwMapProvider.GetPropertySet( PROPERTY_MAP_TEXT_DEFAULT ) ),
59     m_pDoc   ( pNewDoc )
60 {
61 }
62 
63 
~SwXTextDefaults()64 SwXTextDefaults::~SwXTextDefaults ()
65 {
66 }
67 
68 
getPropertySetInfo()69 uno::Reference< XPropertySetInfo > SAL_CALL SwXTextDefaults::getPropertySetInfo(  )
70 {
71     static uno::Reference < XPropertySetInfo > xRef = m_pPropSet->getPropertySetInfo();
72     return xRef;
73 }
74 
75 
setPropertyValue(const OUString & rPropertyName,const Any & aValue)76 void SAL_CALL SwXTextDefaults::setPropertyValue( const OUString& rPropertyName, const Any& aValue )
77 {
78     vos::OGuard aGuard( Application::GetSolarMutex());
79     if (!m_pDoc)
80         throw RuntimeException();
81     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
82     if (!pMap)
83         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
84     if ( pMap->nFlags & PropertyAttribute::READONLY)
85         throw PropertyVetoException ( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Property is read-only: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
86 
87     const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
88     if (RES_PAGEDESC == pMap->nWID && MID_PAGEDESC_PAGEDESCNAME == pMap->nMemberId)
89     {
90         SfxItemSet aSet( m_pDoc->GetAttrPool(), RES_PAGEDESC, RES_PAGEDESC );
91         aSet.Put(rItem);
92         SwUnoCursorHelper::SetPageDesc( aValue, *m_pDoc, aSet );
93         m_pDoc->SetDefault(aSet.Get(RES_PAGEDESC));
94     }
95     else if ((RES_PARATR_DROP == pMap->nWID && MID_DROPCAP_CHAR_STYLE_NAME == pMap->nMemberId) ||
96              (RES_TXTATR_CHARFMT == pMap->nWID))
97     {
98         OUString uStyle;
99         if(aValue >>= uStyle)
100         {
101             String sStyle;
102             SwStyleNameMapper::FillUIName(uStyle, sStyle, nsSwGetPoolIdFromName::GET_POOLID_CHRFMT, sal_True );
103             SwDocStyleSheet* pStyle =
104                 (SwDocStyleSheet*)m_pDoc->GetDocShell()->GetStyleSheetPool()->Find(sStyle, SFX_STYLE_FAMILY_CHAR);
105             SwFmtDrop* pDrop = 0;
106             SwFmtCharFmt *pCharFmt = 0;
107             if(pStyle)
108             {
109                 rtl::Reference< SwDocStyleSheet > xStyle( new SwDocStyleSheet( *(SwDocStyleSheet*)pStyle ) );
110                 if (RES_PARATR_DROP == pMap->nWID)
111                 {
112                     pDrop = (SwFmtDrop*)rItem.Clone();   // because rItem ist const...
113                     pDrop->SetCharFmt(xStyle->GetCharFmt());
114                     m_pDoc->SetDefault(*pDrop);
115                 }
116                 else // RES_TXTATR_CHARFMT == pMap->nWID
117                 {
118                     pCharFmt = (SwFmtCharFmt*)rItem.Clone();   // because rItem ist const...
119                     pCharFmt->SetCharFmt(xStyle->GetCharFmt());
120                     m_pDoc->SetDefault(*pCharFmt);
121                 }
122             }
123             else
124                 throw lang::IllegalArgumentException();
125             delete pDrop;
126             delete pCharFmt;
127         }
128         else
129             throw lang::IllegalArgumentException();
130     }
131     else
132     {
133         SfxPoolItem * pNewItem = rItem.Clone();
134         pNewItem->PutValue( aValue, pMap->nMemberId);
135         m_pDoc->SetDefault(*pNewItem);
136         delete pNewItem;
137     }
138 }
139 
140 
getPropertyValue(const OUString & rPropertyName)141 Any SAL_CALL SwXTextDefaults::getPropertyValue( const OUString& rPropertyName )
142 {
143     vos::OGuard aGuard( Application::GetSolarMutex());
144     if (!m_pDoc)
145         throw RuntimeException();
146     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
147     if (!pMap)
148         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
149     Any aRet;
150     const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
151     rItem.QueryValue( aRet, pMap->nMemberId );
152     return aRet;
153 }
154 
155 
addPropertyChangeListener(const OUString &,const uno::Reference<XPropertyChangeListener> &)156 void SAL_CALL SwXTextDefaults::addPropertyChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XPropertyChangeListener >& /*xListener*/ )
157 {
158     DBG_WARNING ( "not implemented" );
159 }
160 
161 
removePropertyChangeListener(const OUString &,const uno::Reference<XPropertyChangeListener> &)162 void SAL_CALL SwXTextDefaults::removePropertyChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XPropertyChangeListener >& /*xListener*/ )
163 {
164     DBG_WARNING ( "not implemented" );
165 }
166 
167 
addVetoableChangeListener(const OUString &,const uno::Reference<XVetoableChangeListener> &)168 void SAL_CALL SwXTextDefaults::addVetoableChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XVetoableChangeListener >& /*xListener*/ )
169 {
170     DBG_WARNING ( "not implemented" );
171 }
172 
173 
removeVetoableChangeListener(const OUString &,const uno::Reference<XVetoableChangeListener> &)174 void SAL_CALL SwXTextDefaults::removeVetoableChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XVetoableChangeListener >& /*xListener*/ )
175 {
176     DBG_WARNING ( "not implemented" );
177 }
178 
179 
180 // XPropertyState
getPropertyState(const OUString & rPropertyName)181 PropertyState SAL_CALL SwXTextDefaults::getPropertyState( const OUString& rPropertyName )
182 {
183     vos::OGuard aGuard( Application::GetSolarMutex());
184     PropertyState eRet = PropertyState_DIRECT_VALUE;
185     if (!m_pDoc)
186         throw RuntimeException();
187     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
188     if (!pMap)
189         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
190 
191     const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
192     if (IsStaticDefaultItem ( &rItem ) )
193         eRet = PropertyState_DEFAULT_VALUE;
194     return eRet;
195 }
196 
197 
getPropertyStates(const Sequence<OUString> & rPropertyNames)198 Sequence< PropertyState > SAL_CALL SwXTextDefaults::getPropertyStates( const Sequence< OUString >& rPropertyNames )
199 {
200     const sal_Int32 nCount = rPropertyNames.getLength();
201     const OUString * pNames = rPropertyNames.getConstArray();
202     Sequence < PropertyState > aRet ( nCount );
203     PropertyState *pState = aRet.getArray();
204 
205     for ( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++)
206         pState[nIndex] = getPropertyState( pNames[nIndex] );
207 
208     return aRet;
209 }
210 
211 
setPropertyToDefault(const OUString & rPropertyName)212 void SAL_CALL SwXTextDefaults::setPropertyToDefault( const OUString& rPropertyName )
213 {
214     if (!m_pDoc)
215         throw RuntimeException();
216     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
217     if (!pMap)
218         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
219     if ( pMap->nFlags & PropertyAttribute::READONLY)
220         throw RuntimeException( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "setPropertyToDefault: property is read-only: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
221     SfxItemPool& rSet (m_pDoc->GetAttrPool());
222     rSet.ResetPoolDefaultItem ( pMap->nWID );
223 }
224 
225 
getPropertyDefault(const OUString & rPropertyName)226 Any SAL_CALL SwXTextDefaults::getPropertyDefault( const OUString& rPropertyName )
227 {
228     if (!m_pDoc)
229         throw RuntimeException();
230     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
231     if (!pMap)
232         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
233     Any aRet;
234     SfxItemPool& rSet (m_pDoc->GetAttrPool());
235     const SfxPoolItem *pItem = rSet.GetPoolDefaultItem ( pMap->nWID );
236     pItem->QueryValue( aRet, pMap->nMemberId );
237     return aRet;
238 }
239 
240 
getImplementationName()241 rtl::OUString SAL_CALL SwXTextDefaults::getImplementationName(  )
242 {
243     return C2U("SwXTextDefaults");
244 }
245 
246 
supportsService(const::rtl::OUString & rServiceName)247 sal_Bool SAL_CALL SwXTextDefaults::supportsService( const ::rtl::OUString& rServiceName )
248 {
249     return  rServiceName == C2U("com.sun.star.text.Defaults") ||
250             rServiceName == C2U("com.sun.star.style.CharacterProperties") ||
251             rServiceName == C2U("com.sun.star.style.CharacterPropertiesAsian") ||
252             rServiceName == C2U("com.sun.star.style.CharacterPropertiesComplex") ||
253             rServiceName == C2U("com.sun.star.style.ParagraphProperties") ||
254             rServiceName == C2U("com.sun.star.style.ParagraphPropertiesAsian") ||
255             rServiceName == C2U("com.sun.star.style.ParagraphPropertiesComplex");
256 }
257 
258 
getSupportedServiceNames()259 uno::Sequence< ::rtl::OUString > SAL_CALL SwXTextDefaults::getSupportedServiceNames(  )
260 {
261     uno::Sequence< OUString > aRet(7);
262     OUString* pArr = aRet.getArray();
263     *pArr++ = C2U("com.sun.star.text.Defaults");
264     *pArr++ = C2U("com.sun.star.style.CharacterProperties");
265     *pArr++ = C2U("com.sun.star.style.CharacterPropertiesAsian");
266     *pArr++ = C2U("com.sun.star.style.CharacterPropertiesComplex");
267     *pArr++ = C2U("com.sun.star.style.ParagraphProperties");
268     *pArr++ = C2U("com.sun.star.style.ParagraphPropertiesAsian");
269     *pArr++ = C2U("com.sun.star.style.ParagraphPropertiesComplex");
270     return aRet;
271 }
272