LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / forms / source / richtext / rtattributehandler.cxx
blob653d42e46a5fd8737e1d9740ace06688b662b5d8
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 <osl/diagnose.h>
23 #include <svx/svxids.hrc>
24 #include <editeng/eeitem.hxx>
25 #include <svl/itemset.hxx>
26 #include <svl/itempool.hxx>
27 #include <tools/mapunit.hxx>
28 #include <vcl/mapmod.hxx>
29 #include <vcl/outdev.hxx>
31 #include <editeng/adjustitem.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( static_cast<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( static_cast<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( static_cast<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( SvxAdjust::Center )
171 switch ( getAttribute() )
173 case SID_ATTR_PARA_ADJUST_LEFT : m_eAdjust = SvxAdjust::Left; break;
174 case SID_ATTR_PARA_ADJUST_CENTER: m_eAdjust = SvxAdjust::Center; break;
175 case SID_ATTR_PARA_ADJUST_RIGHT : m_eAdjust = SvxAdjust::Right; break;
176 case SID_ATTR_PARA_ADJUST_BLOCK : m_eAdjust = SvxAdjust::Block; break;
177 default:
178 OSL_FAIL( "ParaAlignmentHandler::ParaAlignmentHandler: invalid slot!" );
179 break;
184 AttributeCheckState ParaAlignmentHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
186 assert( dynamic_cast<const SvxAdjustItem*>( &_rItem) && "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 _rNewAttribs.Put( SvxAdjustItem( m_eAdjust, getWhich() ) );
198 LineSpacingHandler::LineSpacingHandler( AttributeId _nAttributeId )
199 :AttributeHandler( _nAttributeId, EE_PARA_SBL )
200 ,m_nLineSpace( 100 )
202 switch ( getAttribute() )
204 case SID_ATTR_PARA_LINESPACE_10: m_nLineSpace = 100; break;
205 case SID_ATTR_PARA_LINESPACE_15: m_nLineSpace = 150; break;
206 case SID_ATTR_PARA_LINESPACE_20: m_nLineSpace = 200; break;
207 default:
208 OSL_FAIL( "LineSpacingHandler::LineSpacingHandler: invalid slot!" );
209 break;
214 AttributeCheckState LineSpacingHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
216 assert( dynamic_cast<const SvxLineSpacingItem*>( &_rItem) && "LineSpacingHandler::implGetCheckState: invalid pool item!" );
217 sal_uInt16 nLineSpace = static_cast< const SvxLineSpacingItem& >( _rItem ).GetPropLineSpace();
218 return ( nLineSpace == m_nLineSpace ) ? eChecked : eUnchecked;
222 void LineSpacingHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType /*_nForScriptType*/ ) const
224 OSL_ENSURE( !_pAdditionalArg, "LineSpacingHandler::executeAttribute: this is a simple toggle attribute - no args possible!" );
226 SvxLineSpacingItem aLineSpacing( m_nLineSpace, getWhich() );
227 aLineSpacing.SetLineSpaceRule( SvxLineSpaceRule::Auto );
228 if ( 100 == m_nLineSpace )
229 aLineSpacing.SetInterLineSpaceRule( SvxInterLineSpaceRule::Off );
230 else
231 aLineSpacing.SetPropLineSpace( m_nLineSpace );
233 _rNewAttribs.Put( aLineSpacing );
236 EscapementHandler::EscapementHandler( AttributeId _nAttributeId )
237 :AttributeHandler( _nAttributeId, EE_CHAR_ESCAPEMENT )
238 ,m_eEscapement( SvxEscapement::Off )
240 switch ( getAttribute() )
242 case SID_SET_SUPER_SCRIPT : m_eEscapement = SvxEscapement::Superscript; break;
243 case SID_SET_SUB_SCRIPT : m_eEscapement = SvxEscapement::Subscript; break;
244 default:
245 OSL_FAIL( "EscapementHandler::EscapementHandler: invalid slot!" );
246 break;
251 AttributeCheckState EscapementHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
253 assert( dynamic_cast<const SvxEscapementItem*>( &_rItem) && "EscapementHandler::getState: invalid pool item!" );
254 SvxEscapement eEscapement = static_cast< const SvxEscapementItem& >( _rItem ).GetEscapement();
255 return ( eEscapement == m_eEscapement ) ? eChecked : eUnchecked;
259 void EscapementHandler::executeAttribute( const SfxItemSet& _rCurrentAttribs, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType /*_nForScriptType*/ ) const {
260 OSL_ENSURE( !_pAdditionalArg, "EscapementHandler::executeAttribute: this is a simple toggle attribute - no args possible!" );
261 // well, in theory we could allow an SvxEscapementItem here, but this is not needed
263 bool bIsChecked = getCheckState( _rCurrentAttribs ) == eChecked;
264 _rNewAttribs.Put( SvxEscapementItem( bIsChecked ? SvxEscapement::Off : m_eEscapement, getWhich() ) );
267 SlotHandler::SlotHandler( AttributeId _nAttributeId, WhichId _nWhichId )
268 :AttributeHandler( _nAttributeId, _nWhichId )
269 ,m_bScriptDependent( false )
271 m_bScriptDependent = ( SID_ATTR_CHAR_WEIGHT == _nAttributeId )
272 || ( SID_ATTR_CHAR_POSTURE == _nAttributeId )
273 || ( SID_ATTR_CHAR_FONT == _nAttributeId );
277 AttributeState SlotHandler::getState( const SfxItemSet& _rAttribs ) const
279 AttributeState aState( eIndetermined );
281 const SfxPoolItem* pItem = _rAttribs.GetItem( getWhich() );
282 if ( pItem )
283 aState.setItem( pItem );
285 return aState;
289 void SlotHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType _nForScriptType ) const
291 if ( _pAdditionalArg )
293 std::unique_ptr<SfxPoolItem> pCorrectWich(_pAdditionalArg->CloneSetWhich(getWhich()));
295 if ( m_bScriptDependent )
296 putItemForScript( _rNewAttribs, *pCorrectWich, _nForScriptType );
297 else
298 _rNewAttribs.Put( std::move(pCorrectWich) );
300 else
301 OSL_FAIL( "SlotHandler::executeAttribute: need attributes to do something!" );
304 FontSizeHandler::FontSizeHandler( AttributeId _nAttributeId, WhichId _nWhichId )
305 :AttributeHandler( _nAttributeId, _nWhichId )
307 OSL_ENSURE( ( _nAttributeId == SID_ATTR_CHAR_FONTHEIGHT ) || ( _nAttributeId == SID_ATTR_CHAR_CTL_FONTHEIGHT )
308 || ( _nAttributeId == SID_ATTR_CHAR_CJK_FONTHEIGHT ) || ( _nAttributeId == SID_ATTR_CHAR_LATIN_FONTHEIGHT ),
309 "FontSizeHandler::FontSizeHandler: invalid attribute id!" );
313 AttributeState FontSizeHandler::getState( const SfxItemSet& _rAttribs ) const
315 AttributeState aState( eIndetermined );
317 const SfxPoolItem* pItem = _rAttribs.GetItem( getWhich() );
318 const SvxFontHeightItem* pFontHeightItem = dynamic_cast<const SvxFontHeightItem*>( pItem );
319 OSL_ENSURE( pFontHeightItem || !pItem, "FontSizeHandler::getState: invalid item!" );
320 if ( pFontHeightItem )
322 // by definition, the item should have the unit twip
323 sal_uLong nHeight = pFontHeightItem->GetHeight();
324 if ( _rAttribs.GetPool()->GetMetric( getWhich() ) != MapUnit::MapTwip )
326 nHeight = OutputDevice::LogicToLogic(
327 Size( 0, nHeight ),
328 MapMode( _rAttribs.GetPool()->GetMetric( getWhich() ) ),
329 MapMode( MapUnit::MapTwip )
330 ).Height();
333 SvxFontHeightItem* pNewItem = new SvxFontHeightItem( nHeight, 100, getWhich() );
334 pNewItem->SetProp( pFontHeightItem->GetProp(), pFontHeightItem->GetPropUnit() );
335 aState.setItem( pNewItem );
338 return aState;
342 void FontSizeHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType _nForScriptType ) const
344 const SvxFontHeightItem* pFontHeightItem = dynamic_cast<const SvxFontHeightItem*>( _pAdditionalArg );
345 OSL_ENSURE( pFontHeightItem, "FontSizeHandler::executeAttribute: need a FontHeightItem!" );
347 if ( !pFontHeightItem )
348 return;
350 sal_uLong nHeight = pFontHeightItem->GetHeight();
351 if ( _rNewAttribs.GetPool()->GetMetric( getWhich() ) != MapUnit::MapTwip )
353 nHeight = OutputDevice::LogicToLogic(
354 Size( 0, nHeight ),
355 MapMode( MapUnit::MapTwip ),
356 MapMode( _rNewAttribs.GetPool()->GetMetric( getWhich() ) )
357 ).Height();
360 SvxFontHeightItem aNewItem( nHeight, 100, getWhich() );
361 aNewItem.SetProp( pFontHeightItem->GetProp(), pFontHeightItem->GetPropUnit() );
363 if ( ( getAttributeId() == SID_ATTR_CHAR_FONTHEIGHT ) && _nForScriptType != SvtScriptType::NONE)
364 putItemForScript( _rNewAttribs, aNewItem, _nForScriptType );
365 else
366 _rNewAttribs.Put( aNewItem );
369 ParagraphDirectionHandler::ParagraphDirectionHandler( AttributeId _nAttributeId )
370 :AttributeHandler( _nAttributeId, EE_PARA_WRITINGDIR )
371 ,m_eParagraphDirection( SvxFrameDirection::Horizontal_LR_TB )
372 ,m_eDefaultAdjustment( SvxAdjust::Right )
373 ,m_eOppositeDefaultAdjustment( SvxAdjust::Left )
375 switch ( getAttributeId() )
377 case SID_ATTR_PARA_LEFT_TO_RIGHT: m_eParagraphDirection = SvxFrameDirection::Horizontal_LR_TB; m_eDefaultAdjustment = SvxAdjust::Left; break;
378 case SID_ATTR_PARA_RIGHT_TO_LEFT: m_eParagraphDirection = SvxFrameDirection::Horizontal_RL_TB; m_eDefaultAdjustment = SvxAdjust::Right; break;
379 default:
380 OSL_FAIL( "ParagraphDirectionHandler::ParagraphDirectionHandler: invalid attribute id!" );
383 if ( SvxAdjust::Right == m_eDefaultAdjustment )
384 m_eOppositeDefaultAdjustment = SvxAdjust::Left;
385 else
386 m_eOppositeDefaultAdjustment = SvxAdjust::Right;
390 AttributeCheckState ParagraphDirectionHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
392 assert( dynamic_cast<const SvxFrameDirectionItem*>( &_rItem) && "ParagraphDirectionHandler::implGetCheckState: invalid pool item!" );
393 SvxFrameDirection eDirection = static_cast< const SvxFrameDirectionItem& >( _rItem ).GetValue();
394 return ( eDirection == m_eParagraphDirection ) ? eChecked : eUnchecked;
398 void ParagraphDirectionHandler::executeAttribute( const SfxItemSet& _rCurrentAttribs, SfxItemSet& _rNewAttribs, const SfxPoolItem* /*_pAdditionalArg*/, SvtScriptType /*_nForScriptType*/ ) const
400 _rNewAttribs.Put( SvxFrameDirectionItem( m_eParagraphDirection, getWhich() ) );
402 // if the current adjustment of the was the default adjustment for the *previous* text direction,
403 // then we toggle the adjustment, too
404 SvxAdjust eCurrentAdjustment = SvxAdjust::Left;
405 const SfxPoolItem* pCurrentAdjustment = nullptr;
406 if ( SfxItemState::SET == _rCurrentAttribs.GetItemState( EE_PARA_JUST, true, &pCurrentAdjustment ) )
407 eCurrentAdjustment = static_cast< const SvxAdjustItem* >( pCurrentAdjustment )->GetAdjust();
409 if ( eCurrentAdjustment == m_eOppositeDefaultAdjustment )
410 _rNewAttribs.Put( SvxAdjustItem( m_eDefaultAdjustment, EE_PARA_JUST ) );
413 BooleanHandler::BooleanHandler( AttributeId _nAttributeId, WhichId _nWhichId )
414 :AttributeHandler( _nAttributeId, _nWhichId )
419 AttributeCheckState BooleanHandler::implGetCheckState( const SfxPoolItem& _rItem ) const
421 OSL_ENSURE( dynamic_cast<const SfxBoolItem*>( &_rItem) != nullptr, "BooleanHandler::implGetCheckState: invalid item!" );
422 if ( auto pBoolItem = dynamic_cast<const SfxBoolItem*>( &_rItem) )
423 return pBoolItem->GetValue() ? eChecked : eUnchecked;
425 return eIndetermined;
429 void BooleanHandler::executeAttribute( const SfxItemSet& /*_rCurrentAttribs*/, SfxItemSet& _rNewAttribs, const SfxPoolItem* _pAdditionalArg, SvtScriptType /*_nForScriptType*/ ) const
431 OSL_ENSURE( dynamic_cast<const SfxBoolItem*>( _pAdditionalArg) != nullptr, "BooleanHandler::executeAttribute: invalid argument!" );
432 if ( _pAdditionalArg )
434 _rNewAttribs.Put( _pAdditionalArg->CloneSetWhich(getWhich()) );
439 } // namespace frm
442 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */