LibreOffice Module sw (master)  1
SwXTextDefaults.cxx
Go to the documentation of this file.
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 <com/sun/star/beans/PropertyAttribute.hpp>
21 
22 #include <vcl/svapp.hxx>
23 #include <osl/diagnose.h>
24 #include <svl/itemprop.hxx>
25 
26 #include <SwXTextDefaults.hxx>
27 #include <SwStyleNameMapper.hxx>
28 #include <fchrfmt.hxx>
29 #include <charfmt.hxx>
31 #include <docstyle.hxx>
32 #include <doc.hxx>
33 #include <docsh.hxx>
34 #include <unomap.hxx>
35 #include <unomid.h>
36 #include <paratr.hxx>
37 #include <unocrsrhelper.hxx>
38 #include <hintids.hxx>
39 #include <fmtpdsc.hxx>
40 
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::lang;
45 
47  m_pPropSet( aSwMapProvider.GetPropertySet( PROPERTY_MAP_TEXT_DEFAULT ) ),
48  m_pDoc ( pNewDoc )
49 {
50 }
51 
53 {
54 }
55 
56 uno::Reference< XPropertySetInfo > SAL_CALL SwXTextDefaults::getPropertySetInfo( )
57 {
58  static uno::Reference < XPropertySetInfo > xRef = m_pPropSet->getPropertySetInfo();
59  return xRef;
60 }
61 
62 void SAL_CALL SwXTextDefaults::setPropertyValue( const OUString& rPropertyName, const Any& aValue )
63 {
64  SolarMutexGuard aGuard;
65  if (!m_pDoc)
66  throw RuntimeException();
67  const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap().getByName( rPropertyName );
68  if (!pMap)
69  throw UnknownPropertyException( "Unknown property: " + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
70  if ( pMap->nFlags & PropertyAttribute::READONLY)
71  throw PropertyVetoException ( "Property is read-only: " + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
72 
73  const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
74  if (RES_PAGEDESC == pMap->nWID && MID_PAGEDESC_PAGEDESCNAME == pMap->nMemberId)
75  {
77  aSet.Put(rItem);
78  SwUnoCursorHelper::SetPageDesc( aValue, *m_pDoc, aSet );
79  m_pDoc->SetDefault(aSet.Get(RES_PAGEDESC));
80  }
81  else if ((RES_PARATR_DROP == pMap->nWID && MID_DROPCAP_CHAR_STYLE_NAME == pMap->nMemberId) ||
82  (RES_TXTATR_CHARFMT == pMap->nWID))
83  {
84  OUString uStyle;
85  if(!(aValue >>= uStyle))
86  throw lang::IllegalArgumentException();
87 
88  OUString sStyle;
90  SwDocStyleSheet* pStyle =
91  static_cast<SwDocStyleSheet*>(m_pDoc->GetDocShell()->GetStyleSheetPool()->Find(sStyle, SfxStyleFamily::Char));
92  std::unique_ptr<SwFormatDrop> pDrop;
93  std::unique_ptr<SwFormatCharFormat> pCharFormat;
94  if(!pStyle)
95  throw lang::IllegalArgumentException();
96 
97  rtl::Reference< SwDocStyleSheet > xStyle( new SwDocStyleSheet( *pStyle ) );
98  if (xStyle->GetCharFormat() == m_pDoc->GetDfltCharFormat())
99  return; // don't SetCharFormat with formats from mpDfltCharFormat
100 
101  if (RES_PARATR_DROP == pMap->nWID)
102  {
103  pDrop.reset(static_cast<SwFormatDrop*>(rItem.Clone())); // because rItem is const...
104  pDrop->SetCharFormat(xStyle->GetCharFormat());
105  m_pDoc->SetDefault(*pDrop);
106  }
107  else // RES_TXTATR_CHARFMT == pMap->nWID
108  {
109  pCharFormat.reset(static_cast<SwFormatCharFormat*>(rItem.Clone())); // because rItem is const...
110  pCharFormat->SetCharFormat(xStyle->GetCharFormat());
111  m_pDoc->SetDefault(*pCharFormat);
112  }
113  }
114  else
115  {
116  std::unique_ptr<SfxPoolItem> pNewItem(rItem.Clone());
117  pNewItem->PutValue( aValue, pMap->nMemberId);
118  m_pDoc->SetDefault(*pNewItem);
119  }
120 }
121 
122 Any SAL_CALL SwXTextDefaults::getPropertyValue( const OUString& rPropertyName )
123 {
124  SolarMutexGuard aGuard;
125  if (!m_pDoc)
126  throw RuntimeException();
127  const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap().getByName( rPropertyName );
128  if (!pMap)
129  throw UnknownPropertyException( "Unknown property: " + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
130  Any aRet;
131  const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
132  rItem.QueryValue( aRet, pMap->nMemberId );
133  return aRet;
134 }
135 
136 void SAL_CALL SwXTextDefaults::addPropertyChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XPropertyChangeListener >& /*xListener*/ )
137 {
138  OSL_FAIL ( "not implemented" );
139 }
140 
141 void SAL_CALL SwXTextDefaults::removePropertyChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XPropertyChangeListener >& /*xListener*/ )
142 {
143  OSL_FAIL ( "not implemented" );
144 }
145 
146 void SAL_CALL SwXTextDefaults::addVetoableChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XVetoableChangeListener >& /*xListener*/ )
147 {
148  OSL_FAIL ( "not implemented" );
149 }
150 
151 void SAL_CALL SwXTextDefaults::removeVetoableChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XVetoableChangeListener >& /*xListener*/ )
152 {
153  OSL_FAIL ( "not implemented" );
154 }
155 
156 // XPropertyState
157 PropertyState SAL_CALL SwXTextDefaults::getPropertyState( const OUString& rPropertyName )
158 {
159  SolarMutexGuard aGuard;
160  PropertyState eRet = PropertyState_DIRECT_VALUE;
161  if (!m_pDoc)
162  throw RuntimeException();
163  const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap().getByName( rPropertyName );
164  if (!pMap)
165  throw UnknownPropertyException( "Unknown property: " + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
166 
167  const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
168  if (IsStaticDefaultItem ( &rItem ) )
169  eRet = PropertyState_DEFAULT_VALUE;
170  return eRet;
171 }
172 
173 Sequence< PropertyState > SAL_CALL SwXTextDefaults::getPropertyStates( const Sequence< OUString >& rPropertyNames )
174 {
175  const sal_Int32 nCount = rPropertyNames.getLength();
176  Sequence < PropertyState > aRet ( nCount );
177 
178  std::transform(rPropertyNames.begin(), rPropertyNames.end(), aRet.begin(),
179  [this](const OUString& rName) -> PropertyState { return getPropertyState(rName); });
180 
181  return aRet;
182 }
183 
184 void SAL_CALL SwXTextDefaults::setPropertyToDefault( const OUString& rPropertyName )
185 {
186  if (!m_pDoc)
187  throw RuntimeException();
188  const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap().getByName( rPropertyName );
189  if (!pMap)
190  throw UnknownPropertyException( "Unknown property: " + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
191  if ( pMap->nFlags & PropertyAttribute::READONLY)
192  throw RuntimeException( "setPropertyToDefault: property is read-only: " + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
193  SfxItemPool& rSet (m_pDoc->GetAttrPool());
194  rSet.ResetPoolDefaultItem ( pMap->nWID );
195 }
196 
197 Any SAL_CALL SwXTextDefaults::getPropertyDefault( const OUString& rPropertyName )
198 {
199  if (!m_pDoc)
200  throw RuntimeException();
201  const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap().getByName( rPropertyName );
202  if (!pMap)
203  throw UnknownPropertyException( "Unknown property: " + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
204  Any aRet;
205  SfxItemPool& rSet (m_pDoc->GetAttrPool());
206  SfxPoolItem const*const pItem = rSet.GetPoolDefaultItem(pMap->nWID);
207  if (pItem)
208  {
209  pItem->QueryValue( aRet, pMap->nMemberId );
210  }
211  return aRet;
212 }
213 
215 {
216  return "SwXTextDefaults";
217 }
218 
219 sal_Bool SAL_CALL SwXTextDefaults::supportsService( const OUString& rServiceName )
220 {
221  return cppu::supportsService(this, rServiceName);
222 }
223 
224 uno::Sequence< OUString > SAL_CALL SwXTextDefaults::getSupportedServiceNames( )
225 {
226  return { "com.sun.star.text.Defaults",
227  "com.sun.star.style.CharacterProperties",
228  "com.sun.star.style.CharacterPropertiesAsian",
229  "com.sun.star.style.CharacterPropertiesComplex",
230  "com.sun.star.style.ParagraphProperties",
231  "com.sun.star.style.ParagraphPropertiesAsian",
232  "com.sun.star.style.ParagraphPropertiesComplex" };
233 }
234 
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr TypedWhichId< SwFormatPageDesc > RES_PAGEDESC(93)
virtual void SAL_CALL addVetoableChangeListener(const OUString &rPropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
SwDocShell * GetDocShell()
Definition: doc.hxx:1348
virtual void SAL_CALL setPropertyToDefault(const OUString &rPropertyName) override
static void FillUIName(const OUString &rName, OUString &rFillName, SwGetPoolIdFromName)
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
#define PROPERTY_MAP_TEXT_DEFAULT
Definition: unomap.hxx:65
virtual void SAL_CALL removeVetoableChangeListener(const OUString &rPropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
virtual OUString SAL_CALL getImplementationName() override
Definition: doc.hxx:184
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &rPropertyName) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
#define MID_PAGEDESC_PAGEDESCNAME
Definition: unomid.h:26
#define MID_DROPCAP_CHAR_STYLE_NAME
Definition: unomid.h:65
css::uno::Reference< css::beans::XPropertySetInfo > const & getPropertySetInfo() const
virtual void SAL_CALL removePropertyChangeListener(const OUString &rPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
virtual ~SwXTextDefaults() override
const SwCharFormat * GetDfltCharFormat() const
Definition: doc.hxx:748
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const
constexpr TypedWhichId< SwFormatDrop > RES_PARATR_DROP(70)
const SfxPoolItem & GetDefault(sal_uInt16 nFormatHint) const
Get the default attribute in this document.
Definition: docfmt.cxx:653
int nCount
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr TypedWhichId< SwFormatCharFormat > RES_TXTATR_CHARFMT(52)
virtual css::uno::Any SAL_CALL getPropertyDefault(const OUString &rPropertyName) override
bool IsStaticDefaultItem(const SfxPoolItem *pItem)
virtual SfxStyleSheetBasePool * GetStyleSheetPool() override
For Style PI.
Definition: docsh.cxx:1139
unsigned char sal_Bool
const SfxItemPropertyMap & getPropertyMap() const
virtual css::uno::Sequence< css::beans::PropertyState > SAL_CALL getPropertyStates(const css::uno::Sequence< OUString > &rPropertyNames) override
virtual void SAL_CALL setPropertyValue(const OUString &rPropertyName, const css::uno::Any &aValue) override
const SfxItemPropertySimpleEntry * getByName(const OUString &rName) const
virtual css::beans::PropertyState SAL_CALL getPropertyState(const OUString &rPropertyName) override
void SetDefault(const SfxPoolItem &)
Set attribute as new default attribute in current document.
Definition: docfmt.cxx:526
SwXTextDefaults(SwDoc *pNewDoc)
SwUnoPropertyMapProvider aSwMapProvider
Definition: unomap1.cxx:89
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
bool SetPageDesc(const css::uno::Any &rValue, SwDoc &rDoc, SfxItemSet &rSet)
virtual void SAL_CALL addPropertyChangeListener(const OUString &rPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
const SfxItemPropertySet * m_pPropSet
virtual SfxStyleSheetBase * Find(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All)
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1315