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