Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / forms / source / richtext / rtattributehandler.cxx
blob37037fc182a25bd196d84440753890c94158f0a9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "rtattributehandler.hxx"
22 #include <svx/svxids.hrc>
23 #include <editeng/eeitem.hxx>
24 #include <svl/itemset.hxx>
25 #include <svl/itempool.hxx>
26 #include <tools/mapunit.hxx>
27 #include <vcl/mapmod.hxx>
28 #include <vcl/outdev.hxx>
30 #include <editeng/adjustitem.hxx>
31 #include <editeng/wghtitem.hxx>
32 #include <editeng/escapementitem.hxx>
33 #include <editeng/lspcitem.hxx>
34 #include <editeng/fhgtitem.hxx>
35 #include <editeng/frmdiritem.hxx>
36 #include <editeng/scripttypeitem.hxx>
39 namespace frm
43 AttributeHandler::AttributeHandler( AttributeId _nAttributeId, WhichId _nWhichId )
44 :m_nAttribute( _nAttributeId )
45 ,m_nWhich ( _nWhichId )
50 AttributeHandler::~AttributeHandler()
55 AttributeId AttributeHandler::getAttributeId( ) const
57 return getAttribute();
61 AttributeCheckState AttributeHandler::implGetCheckState( const SfxPoolItem& /*_rItem*/ ) const
63 OSL_FAIL( "AttributeHandler::implGetCheckState: not to be called!" );
64 return eIndetermined;
68 void AttributeHandler::putItemForScript( SfxItemSet& _rAttribs, const SfxPoolItem& _rItem, SvtScriptType _nForScriptType ) const
70 SvxScriptSetItem aSetItem( (WhichId)getAttributeId(), *_rAttribs.GetPool() );
71 aSetItem.PutItemForScriptType( _nForScriptType, _rItem );
72 _rAttribs.Put( aSetItem.GetItemSet(), false );
76 AttributeCheckState AttributeHandler::getCheckState( const SfxItemSet& _rAttribs ) const
78 AttributeCheckState eSimpleState( eIndetermined );
79 const SfxPoolItem* pItem = _rAttribs.GetItem( getWhich() );
80 if ( pItem )
81 eSimpleState = implGetCheckState( *pItem );
82 return eSimpleState;
86 AttributeState AttributeHandler::getState( const SfxItemSet& _rAttribs ) const
88 AttributeState aState( eIndetermined );
89 aState.eSimpleState = getCheckState( _rAttribs );
90 return aState;
94 //= AttributeHandlerFactory
97 namespace
99 WhichId lcl_implGetWhich( const SfxItemPool& _rPool, AttributeId _nAttributeId )
101 WhichId nWhich = 0;
102 switch ( _nAttributeId )
104 case SID_ATTR_CHAR_LATIN_FONTHEIGHT:nWhich = EE_CHAR_FONTHEIGHT;break;
105 case SID_ATTR_CHAR_LATIN_FONT: nWhich = EE_CHAR_FONTINFO; break;
106 case SID_ATTR_CHAR_LATIN_LANGUAGE: nWhich = EE_CHAR_LANGUAGE; break;
107 case SID_ATTR_CHAR_LATIN_POSTURE: nWhich = EE_CHAR_ITALIC; break;
108 case SID_ATTR_CHAR_LATIN_WEIGHT: nWhich = EE_CHAR_WEIGHT; break;
110 default:
111 nWhich = _rPool.GetWhich( (SfxSlotId)_nAttributeId );
113 return nWhich;
117 ::rtl::Reference< AttributeHandler > AttributeHandlerFactory::getHandlerFor( AttributeId _nAttributeId, const SfxItemPool& _rEditEnginePool )
119 ::rtl::Reference< AttributeHandler > pReturn;
120 switch ( _nAttributeId )
122 case SID_ATTR_PARA_ADJUST_LEFT :
123 case SID_ATTR_PARA_ADJUST_CENTER:
124 case SID_ATTR_PARA_ADJUST_RIGHT :
125 case SID_ATTR_PARA_ADJUST_BLOCK :
126 pReturn = new ParaAlignmentHandler( _nAttributeId );
127 break;
129 case SID_ATTR_PARA_LINESPACE_10:
130 case SID_ATTR_PARA_LINESPACE_15:
131 case SID_ATTR_PARA_LINESPACE_20:
132 pReturn = new LineSpacingHandler( _nAttributeId );
133 break;
135 case SID_SET_SUPER_SCRIPT:
136 case SID_SET_SUB_SCRIPT:
137 pReturn = new EscapementHandler( _nAttributeId );
138 break;
140 case SID_ATTR_CHAR_FONTHEIGHT:
141 case SID_ATTR_CHAR_CTL_FONTHEIGHT:
142 case SID_ATTR_CHAR_CJK_FONTHEIGHT:
143 case SID_ATTR_CHAR_LATIN_FONTHEIGHT:
144 pReturn = new FontSizeHandler( _nAttributeId, lcl_implGetWhich( _rEditEnginePool, _nAttributeId ) );
145 break;
147 case SID_ATTR_PARA_LEFT_TO_RIGHT:
148 case SID_ATTR_PARA_RIGHT_TO_LEFT:
149 pReturn = new ParagraphDirectionHandler( _nAttributeId );
150 break;
152 case SID_ATTR_PARA_HANGPUNCTUATION:
153 case SID_ATTR_PARA_FORBIDDEN_RULES:
154 case SID_ATTR_PARA_SCRIPTSPACE:
155 pReturn = new BooleanHandler( _nAttributeId, lcl_implGetWhich( _rEditEnginePool, _nAttributeId ) );
156 break;
158 default:
159 pReturn = new SlotHandler( (SfxSlotId)_nAttributeId, lcl_implGetWhich( _rEditEnginePool, _nAttributeId ) );
160 break;
164 return pReturn;
167 ParaAlignmentHandler::ParaAlignmentHandler( AttributeId _nAttributeId )
168 :AttributeHandler( _nAttributeId, EE_PARA_JUST )
169 ,m_eAdjust( SVX_ADJUST_CENTER )
171 switch ( getAttribute() )
173 case SID_ATTR_PARA_ADJUST_LEFT : m_eAdjust = SVX_ADJUST_LEFT; break;
174 case SID_ATTR_PARA_ADJUST_CENTER: m_eAdjust = SVX_ADJUST_CENTER; break;
175 case SID_ATTR_PARA_ADJUST_RIGHT : m_eAdjust = SVX_ADJUST_RIGHT; break;
176 case SID_ATTR_PARA_ADJUST_BLOCK : m_eAdjust = SVX_ADJUST_BLOCK; break;
177 default:
178 OSL_FAIL( "ParaAlignmentHandler::ParaAlignmentHandler: invalid slot!" );
179 break;
184 AttributeCheckState ParaAlignmentHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
186 OSL_ENSURE( dynamic_cast<const SvxAdjustItem*>( &_rItem) != nullptr, "ParaAlignmentHandler::implGetCheckState: invalid pool item!" );
187 SvxAdjust eAdjust = static_cast< const SvxAdjustItem& >( _rItem ).GetAdjust();
188 return ( eAdjust == m_eAdjust ) ? eChecked : eUnchecked;
192 void ParaAlignmentHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType /*_nForScriptType*/ ) const
194 OSL_ENSURE( !_pAdditionalArg, "ParaAlignmentHandler::executeAttribute: this is a simple toggle attribute - no args possible!" );
195 (void)_pAdditionalArg;
196 _rNewAttribs.Put( SvxAdjustItem( m_eAdjust, getWhich() ) );
199 LineSpacingHandler::LineSpacingHandler( AttributeId _nAttributeId )
200 :AttributeHandler( _nAttributeId, EE_PARA_SBL )
201 ,m_nLineSpace( 100 )
203 switch ( getAttribute() )
205 case SID_ATTR_PARA_LINESPACE_10: m_nLineSpace = 100; break;
206 case SID_ATTR_PARA_LINESPACE_15: m_nLineSpace = 150; break;
207 case SID_ATTR_PARA_LINESPACE_20: m_nLineSpace = 200; break;
208 default:
209 OSL_FAIL( "LineSpacingHandler::LineSpacingHandler: invalid slot!" );
210 break;
215 AttributeCheckState LineSpacingHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
217 OSL_ENSURE( dynamic_cast<const SvxLineSpacingItem*>( &_rItem) != nullptr, "LineSpacingHandler::implGetCheckState: invalid pool item!" );
218 sal_uInt16 nLineSpace = static_cast< const SvxLineSpacingItem& >( _rItem ).GetPropLineSpace();
219 return ( nLineSpace == m_nLineSpace ) ? eChecked : eUnchecked;
223 void LineSpacingHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType /*_nForScriptType*/ ) const
225 OSL_ENSURE( !_pAdditionalArg, "LineSpacingHandler::executeAttribute: this is a simple toggle attribute - no args possible!" );
226 (void)_pAdditionalArg;
228 SvxLineSpacingItem aLineSpacing( m_nLineSpace, getWhich() );
229 aLineSpacing.GetLineSpaceRule() = SVX_LINE_SPACE_AUTO;
230 if ( 100 == m_nLineSpace )
231 aLineSpacing.GetInterLineSpaceRule() = SVX_INTER_LINE_SPACE_OFF;
232 else
233 aLineSpacing.SetPropLineSpace( (sal_uInt8)m_nLineSpace );
235 _rNewAttribs.Put( aLineSpacing );
238 EscapementHandler::EscapementHandler( AttributeId _nAttributeId )
239 :AttributeHandler( _nAttributeId, EE_CHAR_ESCAPEMENT )
240 ,m_eEscapement( SVX_ESCAPEMENT_OFF )
242 switch ( getAttribute() )
244 case SID_SET_SUPER_SCRIPT : m_eEscapement = SVX_ESCAPEMENT_SUPERSCRIPT; break;
245 case SID_SET_SUB_SCRIPT : m_eEscapement = SVX_ESCAPEMENT_SUBSCRIPT; break;
246 default:
247 OSL_FAIL( "EscapementHandler::EscapementHandler: invalid slot!" );
248 break;
253 AttributeCheckState EscapementHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
255 OSL_ENSURE( dynamic_cast<const SvxEscapementItem*>( &_rItem) != nullptr, "EscapementHandler::getState: invalid pool item!" );
256 SvxEscapement eEscapement = static_cast< const SvxEscapementItem& >( _rItem ).GetEscapement();
257 return ( eEscapement == m_eEscapement ) ? eChecked : eUnchecked;
261 void EscapementHandler::executeAttribute( const SfxItemSet& _rCurrentAttribs, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType /*_nForScriptType*/ ) const {
262 OSL_ENSURE( !_pAdditionalArg, "EscapementHandler::executeAttribute: this is a simple toggle attribute - no args possible!" );
263 // well, in theory we could allow an SvxEscapementItem here, but this is not needed
264 (void)_pAdditionalArg;
266 bool bIsChecked = getCheckState( _rCurrentAttribs ) == eChecked;
267 _rNewAttribs.Put( SvxEscapementItem( bIsChecked ? SVX_ESCAPEMENT_OFF : m_eEscapement, getWhich() ) );
270 SlotHandler::SlotHandler( AttributeId _nAttributeId, WhichId _nWhichId )
271 :AttributeHandler( _nAttributeId, _nWhichId )
272 ,m_bScriptDependent( false )
274 m_bScriptDependent = ( SID_ATTR_CHAR_WEIGHT == _nAttributeId )
275 || ( SID_ATTR_CHAR_POSTURE == _nAttributeId )
276 || ( SID_ATTR_CHAR_FONT == _nAttributeId );
280 AttributeState SlotHandler::getState( const SfxItemSet& _rAttribs ) const
282 AttributeState aState( eIndetermined );
284 const SfxPoolItem* pItem = _rAttribs.GetItem( getWhich() );
285 if ( pItem )
286 aState.setItem( pItem->Clone() );
288 return aState;
292 void SlotHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType _nForScriptType ) const
294 if ( _pAdditionalArg )
296 SfxPoolItem* pCorrectWich = _pAdditionalArg->Clone();
297 pCorrectWich->SetWhich( getWhich() );
299 if ( m_bScriptDependent )
300 putItemForScript( _rNewAttribs, *pCorrectWich, _nForScriptType );
301 else
302 _rNewAttribs.Put( *pCorrectWich );
303 DELETEZ( pCorrectWich );
305 else
306 OSL_FAIL( "SlotHandler::executeAttribute: need attributes to do something!" );
309 FontSizeHandler::FontSizeHandler( AttributeId _nAttributeId, WhichId _nWhichId )
310 :AttributeHandler( _nAttributeId, _nWhichId )
312 OSL_ENSURE( ( _nAttributeId == SID_ATTR_CHAR_FONTHEIGHT ) || ( _nAttributeId == SID_ATTR_CHAR_CTL_FONTHEIGHT )
313 || ( _nAttributeId == SID_ATTR_CHAR_CJK_FONTHEIGHT ) || ( _nAttributeId == SID_ATTR_CHAR_LATIN_FONTHEIGHT ),
314 "FontSizeHandler::FontSizeHandler: invalid attribute id!" );
318 AttributeState FontSizeHandler::getState( const SfxItemSet& _rAttribs ) const
320 AttributeState aState( eIndetermined );
322 const SfxPoolItem* pItem = _rAttribs.GetItem( getWhich() );
323 const SvxFontHeightItem* pFontHeightItem = dynamic_cast<const SvxFontHeightItem*>( pItem );
324 OSL_ENSURE( pFontHeightItem || !pItem, "FontSizeHandler::getState: invalid item!" );
325 if ( pFontHeightItem )
327 // by definition, the item should have the unit twip
328 sal_uLong nHeight = pFontHeightItem->GetHeight();
329 if ( _rAttribs.GetPool()->GetMetric( getWhich() ) != SFX_MAPUNIT_TWIP )
331 nHeight = OutputDevice::LogicToLogic(
332 Size( 0, nHeight ),
333 MapMode( (MapUnit)( _rAttribs.GetPool()->GetMetric( getWhich() ) ) ),
334 MapMode( MAP_TWIP )
335 ).Height();
338 SvxFontHeightItem* pNewItem = new SvxFontHeightItem( nHeight, 100, getWhich() );
339 pNewItem->SetProp( pFontHeightItem->GetProp(), pFontHeightItem->GetPropUnit() );
340 aState.setItem( pNewItem );
343 return aState;
347 void FontSizeHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType _nForScriptType ) const
349 const SvxFontHeightItem* pFontHeightItem = dynamic_cast<const SvxFontHeightItem*>( _pAdditionalArg );
350 OSL_ENSURE( pFontHeightItem, "FontSizeHandler::executeAttribute: need a FontHeightItem!" );
352 if ( pFontHeightItem )
354 // correct measurement units
355 SfxMapUnit eItemMapUnit = pFontHeightItem->GetPropUnit(); (void)eItemMapUnit;
356 sal_uLong nHeight = pFontHeightItem->GetHeight();
357 if ( _rNewAttribs.GetPool()->GetMetric( getWhich() ) != SFX_MAPUNIT_TWIP )
359 nHeight = OutputDevice::LogicToLogic(
360 Size( 0, nHeight ),
361 MapMode( (MapUnit)( SFX_MAPUNIT_TWIP ) ),
362 MapMode( (MapUnit)( _rNewAttribs.GetPool()->GetMetric( getWhich() ) ) )
363 ).Height();
366 SvxFontHeightItem aNewItem( nHeight, 100, getWhich() );
367 aNewItem.SetProp( pFontHeightItem->GetProp(), pFontHeightItem->GetPropUnit() );
369 if ( ( getAttributeId() == SID_ATTR_CHAR_FONTHEIGHT ) && _nForScriptType != SvtScriptType::NONE)
370 putItemForScript( _rNewAttribs, aNewItem, _nForScriptType );
371 else
372 _rNewAttribs.Put( aNewItem );
376 ParagraphDirectionHandler::ParagraphDirectionHandler( AttributeId _nAttributeId )
377 :AttributeHandler( _nAttributeId, EE_PARA_WRITINGDIR )
378 ,m_eParagraphDirection( FRMDIR_HORI_LEFT_TOP )
379 ,m_eDefaultAdjustment( SVX_ADJUST_RIGHT )
380 ,m_eOppositeDefaultAdjustment( SVX_ADJUST_LEFT )
382 switch ( getAttributeId() )
384 case SID_ATTR_PARA_LEFT_TO_RIGHT: m_eParagraphDirection = FRMDIR_HORI_LEFT_TOP; m_eDefaultAdjustment = SVX_ADJUST_LEFT; break;
385 case SID_ATTR_PARA_RIGHT_TO_LEFT: m_eParagraphDirection = FRMDIR_HORI_RIGHT_TOP; m_eDefaultAdjustment = SVX_ADJUST_RIGHT; break;
386 default:
387 OSL_FAIL( "ParagraphDirectionHandler::ParagraphDirectionHandler: invalid attribute id!" );
390 if ( SVX_ADJUST_RIGHT == m_eDefaultAdjustment )
391 m_eOppositeDefaultAdjustment = SVX_ADJUST_LEFT;
392 else
393 m_eOppositeDefaultAdjustment = SVX_ADJUST_RIGHT;
397 AttributeCheckState ParagraphDirectionHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
399 OSL_ENSURE( dynamic_cast<const SvxFrameDirectionItem*>( &_rItem) != nullptr, "ParagraphDirectionHandler::implGetCheckState: invalid pool item!" );
400 SvxFrameDirection eDirection = static_cast< SvxFrameDirection >( static_cast< const SvxFrameDirectionItem& >( _rItem ).GetValue() );
401 return ( eDirection == m_eParagraphDirection ) ? eChecked : eUnchecked;
405 void ParagraphDirectionHandler::executeAttribute( const SfxItemSet& _rCurrentAttribs, SfxItemSet& _rNewAttribs, const SfxPoolItem* /*_pAdditionalArg*/, SvtScriptType /*_nForScriptType*/ ) const
407 _rNewAttribs.Put( SvxFrameDirectionItem( m_eParagraphDirection, getWhich() ) );
409 // if the current adjustment of the was the default adjustment for the *previous* text direction,
410 // then we toggle the adjustment, too
411 SvxAdjust eCurrentAdjustment = SVX_ADJUST_LEFT;
412 const SfxPoolItem* pCurrentAdjustment = nullptr;
413 if ( SfxItemState::SET == _rCurrentAttribs.GetItemState( EE_PARA_JUST, true, &pCurrentAdjustment ) )
414 eCurrentAdjustment = static_cast< const SvxAdjustItem* >( pCurrentAdjustment )->GetAdjust();
416 if ( eCurrentAdjustment == m_eOppositeDefaultAdjustment )
417 _rNewAttribs.Put( SvxAdjustItem( m_eDefaultAdjustment, EE_PARA_JUST ) );
420 BooleanHandler::BooleanHandler( AttributeId _nAttributeId, WhichId _nWhichId )
421 :AttributeHandler( _nAttributeId, _nWhichId )
426 AttributeCheckState BooleanHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
428 OSL_ENSURE( dynamic_cast<const SfxBoolItem*>( &_rItem) != nullptr, "BooleanHandler::implGetCheckState: invalid item!" );
429 if ( dynamic_cast<const SfxBoolItem*>( &_rItem) != nullptr )
430 return static_cast< const SfxBoolItem& >( _rItem ).GetValue() ? eChecked : eUnchecked;
432 return eIndetermined;
436 void BooleanHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType /*_nForScriptType*/ ) const
438 OSL_ENSURE( dynamic_cast<const SfxBoolItem*>( _pAdditionalArg) != nullptr, "BooleanHandler::executeAttribute: invalid argument!" );
439 if ( _pAdditionalArg )
441 SfxPoolItem* pCorrectWich = _pAdditionalArg->Clone();
442 pCorrectWich->SetWhich( getWhich() );
443 _rNewAttribs.Put( *pCorrectWich );
444 DELETEZ( pCorrectWich );
449 } // namespace frm
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */