1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
38 //........................................................................
41 //........................................................................
42 //====================================================================
44 //====================================================================
45 //--------------------------------------------------------------------
46 oslInterlockedCount SAL_CALL
ReferenceBase::acquire()
48 return osl_atomic_increment( &m_refCount
);
51 //--------------------------------------------------------------------
52 oslInterlockedCount SAL_CALL
ReferenceBase::release()
54 return osl_atomic_decrement( &m_refCount
);
57 //--------------------------------------------------------------------
58 ReferenceBase::~ReferenceBase()
62 //====================================================================
64 //====================================================================
65 //--------------------------------------------------------------------
66 AttributeHandler::AttributeHandler( AttributeId _nAttributeId
, WhichId _nWhichId
)
67 :m_nAttribute( _nAttributeId
)
68 ,m_nWhich ( _nWhichId
)
72 //--------------------------------------------------------------------
73 AttributeHandler::~AttributeHandler()
77 //--------------------------------------------------------------------
78 oslInterlockedCount SAL_CALL
AttributeHandler::acquire()
80 return ReferenceBase::acquire();
83 //--------------------------------------------------------------------
84 oslInterlockedCount SAL_CALL
AttributeHandler::release()
86 return ReferenceBase::release();
89 //--------------------------------------------------------------------
90 AttributeId
AttributeHandler::getAttributeId( ) const
92 return getAttribute();
95 //--------------------------------------------------------------------
96 AttributeCheckState
AttributeHandler::implGetCheckState( const SfxPoolItem
& /*_rItem*/ ) const
98 OSL_FAIL( "AttributeHandler::implGetCheckState: not to be called!" );
102 //--------------------------------------------------------------------
103 void AttributeHandler::putItemForScript( SfxItemSet
& _rAttribs
, const SfxPoolItem
& _rItem
, ScriptType _nForScriptType
) const
105 SvxScriptSetItem
aSetItem( (WhichId
)getAttributeId(), *_rAttribs
.GetPool() );
106 aSetItem
.PutItemForScriptType( _nForScriptType
, _rItem
);
107 _rAttribs
.Put( aSetItem
.GetItemSet(), sal_False
);
110 //--------------------------------------------------------------------
111 AttributeCheckState
AttributeHandler::getCheckState( const SfxItemSet
& _rAttribs
) const
113 AttributeCheckState
eSimpleState( eIndetermined
);
114 const SfxPoolItem
* pItem
= _rAttribs
.GetItem( getWhich() );
116 eSimpleState
= implGetCheckState( *pItem
);
120 //--------------------------------------------------------------------
121 AttributeState
AttributeHandler::getState( const SfxItemSet
& _rAttribs
) const
123 AttributeState
aState( eIndetermined
);
124 aState
.eSimpleState
= getCheckState( _rAttribs
);
128 //====================================================================
129 //= AttributeHandlerFactory
130 //====================================================================
131 //--------------------------------------------------------------------
134 static WhichId
lcl_implGetWhich( const SfxItemPool
& _rPool
, AttributeId _nAttributeId
)
137 switch ( _nAttributeId
)
139 case SID_ATTR_CHAR_LATIN_FONTHEIGHT
:nWhich
= EE_CHAR_FONTHEIGHT
;break;
140 case SID_ATTR_CHAR_LATIN_FONT
: nWhich
= EE_CHAR_FONTINFO
; break;
141 case SID_ATTR_CHAR_LATIN_LANGUAGE
: nWhich
= EE_CHAR_LANGUAGE
; break;
142 case SID_ATTR_CHAR_LATIN_POSTURE
: nWhich
= EE_CHAR_ITALIC
; break;
143 case SID_ATTR_CHAR_LATIN_WEIGHT
: nWhich
= EE_CHAR_WEIGHT
; break;
146 nWhich
= _rPool
.GetWhich( (SfxSlotId
)_nAttributeId
);
151 //--------------------------------------------------------------------
152 ::rtl::Reference
< IAttributeHandler
> AttributeHandlerFactory::getHandlerFor( AttributeId _nAttributeId
, const SfxItemPool
& _rEditEnginePool
)
154 ::rtl::Reference
< IAttributeHandler
> pReturn
;
155 switch ( _nAttributeId
)
157 case SID_ATTR_PARA_ADJUST_LEFT
:
158 case SID_ATTR_PARA_ADJUST_CENTER
:
159 case SID_ATTR_PARA_ADJUST_RIGHT
:
160 case SID_ATTR_PARA_ADJUST_BLOCK
:
161 pReturn
= new ParaAlignmentHandler( _nAttributeId
);
164 case SID_ATTR_PARA_LINESPACE_10
:
165 case SID_ATTR_PARA_LINESPACE_15
:
166 case SID_ATTR_PARA_LINESPACE_20
:
167 pReturn
= new LineSpacingHandler( _nAttributeId
);
170 case SID_SET_SUPER_SCRIPT
:
171 case SID_SET_SUB_SCRIPT
:
172 pReturn
= new EscapementHandler( _nAttributeId
);
175 case SID_ATTR_CHAR_FONTHEIGHT
:
176 case SID_ATTR_CHAR_CTL_FONTHEIGHT
:
177 case SID_ATTR_CHAR_CJK_FONTHEIGHT
:
178 case SID_ATTR_CHAR_LATIN_FONTHEIGHT
:
179 pReturn
= new FontSizeHandler( _nAttributeId
, lcl_implGetWhich( _rEditEnginePool
, _nAttributeId
) );
182 case SID_ATTR_PARA_LEFT_TO_RIGHT
:
183 case SID_ATTR_PARA_RIGHT_TO_LEFT
:
184 pReturn
= new ParagraphDirectionHandler( _nAttributeId
);
187 case SID_ATTR_PARA_HANGPUNCTUATION
:
188 case SID_ATTR_PARA_FORBIDDEN_RULES
:
189 case SID_ATTR_PARA_SCRIPTSPACE
:
190 pReturn
= new BooleanHandler( _nAttributeId
, lcl_implGetWhich( _rEditEnginePool
, _nAttributeId
) );
194 pReturn
= new SlotHandler( (SfxSlotId
)_nAttributeId
, lcl_implGetWhich( _rEditEnginePool
, _nAttributeId
) );
202 //====================================================================
203 //= ParaAlignmentHandler
204 //====================================================================
205 //--------------------------------------------------------------------
206 ParaAlignmentHandler::ParaAlignmentHandler( AttributeId _nAttributeId
)
207 :AttributeHandler( _nAttributeId
, EE_PARA_JUST
)
208 ,m_eAdjust( SVX_ADJUST_CENTER
)
210 switch ( getAttribute() )
212 case SID_ATTR_PARA_ADJUST_LEFT
: m_eAdjust
= SVX_ADJUST_LEFT
; break;
213 case SID_ATTR_PARA_ADJUST_CENTER
: m_eAdjust
= SVX_ADJUST_CENTER
; break;
214 case SID_ATTR_PARA_ADJUST_RIGHT
: m_eAdjust
= SVX_ADJUST_RIGHT
; break;
215 case SID_ATTR_PARA_ADJUST_BLOCK
: m_eAdjust
= SVX_ADJUST_BLOCK
; break;
217 OSL_FAIL( "ParaAlignmentHandler::ParaAlignmentHandler: invalid slot!" );
222 //--------------------------------------------------------------------
223 AttributeCheckState
ParaAlignmentHandler::implGetCheckState( const SfxPoolItem
& _rItem
) const
225 OSL_ENSURE( _rItem
.ISA( SvxAdjustItem
), "ParaAlignmentHandler::implGetCheckState: invalid pool item!" );
226 SvxAdjust eAdjust
= static_cast< const SvxAdjustItem
& >( _rItem
).GetAdjust();
227 return ( eAdjust
== m_eAdjust
) ? eChecked
: eUnchecked
;
230 //--------------------------------------------------------------------
231 void ParaAlignmentHandler::executeAttribute( const SfxItemSet
& /*_rCurrentAttribs*/, SfxItemSet
& _rNewAttribs
, const SfxPoolItem
* _pAdditionalArg
, ScriptType
/*_nForScriptType*/ ) const
233 OSL_ENSURE( !_pAdditionalArg
, "ParaAlignmentHandler::executeAttribute: this is a simple toggle attribute - no args possible!" );
234 (void)_pAdditionalArg
;
235 _rNewAttribs
.Put( SvxAdjustItem( m_eAdjust
, getWhich() ) );
238 //====================================================================
239 //= LineSpacingHandler
240 //====================================================================
241 //--------------------------------------------------------------------
242 LineSpacingHandler::LineSpacingHandler( AttributeId _nAttributeId
)
243 :AttributeHandler( _nAttributeId
, EE_PARA_SBL
)
246 switch ( getAttribute() )
248 case SID_ATTR_PARA_LINESPACE_10
: m_nLineSpace
= 100; break;
249 case SID_ATTR_PARA_LINESPACE_15
: m_nLineSpace
= 150; break;
250 case SID_ATTR_PARA_LINESPACE_20
: m_nLineSpace
= 200; break;
252 OSL_FAIL( "LineSpacingHandler::LineSpacingHandler: invalid slot!" );
257 //--------------------------------------------------------------------
258 AttributeCheckState
LineSpacingHandler::implGetCheckState( const SfxPoolItem
& _rItem
) const
260 OSL_ENSURE( _rItem
.ISA( SvxLineSpacingItem
), "LineSpacingHandler::implGetCheckState: invalid pool item!" );
261 sal_uInt16 nLineSpace
= static_cast< const SvxLineSpacingItem
& >( _rItem
).GetPropLineSpace();
262 return ( nLineSpace
== m_nLineSpace
) ? eChecked
: eUnchecked
;
265 //--------------------------------------------------------------------
266 void LineSpacingHandler::executeAttribute( const SfxItemSet
& /*_rCurrentAttribs*/, SfxItemSet
& _rNewAttribs
, const SfxPoolItem
* _pAdditionalArg
, ScriptType
/*_nForScriptType*/ ) const
268 OSL_ENSURE( !_pAdditionalArg
, "LineSpacingHandler::executeAttribute: this is a simple toggle attribute - no args possible!" );
269 (void)_pAdditionalArg
;
271 SvxLineSpacingItem
aLineSpacing( m_nLineSpace
, getWhich() );
272 aLineSpacing
.GetLineSpaceRule() = SVX_LINE_SPACE_AUTO
;
273 if ( 100 == m_nLineSpace
)
274 aLineSpacing
.GetInterLineSpaceRule() = SVX_INTER_LINE_SPACE_OFF
;
276 aLineSpacing
.SetPropLineSpace( (sal_uInt8
)m_nLineSpace
);
278 _rNewAttribs
.Put( aLineSpacing
);
281 //====================================================================
282 //= EscapementHandler
283 //====================================================================
284 //--------------------------------------------------------------------
285 EscapementHandler::EscapementHandler( AttributeId _nAttributeId
)
286 :AttributeHandler( _nAttributeId
, EE_CHAR_ESCAPEMENT
)
287 ,m_eEscapement( SVX_ESCAPEMENT_OFF
)
289 switch ( getAttribute() )
291 case SID_SET_SUPER_SCRIPT
: m_eEscapement
= SVX_ESCAPEMENT_SUPERSCRIPT
; break;
292 case SID_SET_SUB_SCRIPT
: m_eEscapement
= SVX_ESCAPEMENT_SUBSCRIPT
; break;
294 OSL_FAIL( "EscapementHandler::EscapementHandler: invalid slot!" );
299 //--------------------------------------------------------------------
300 AttributeCheckState
EscapementHandler::implGetCheckState( const SfxPoolItem
& _rItem
) const
302 OSL_ENSURE( _rItem
.ISA( SvxEscapementItem
), "EscapementHandler::getState: invalid pool item!" );
303 SvxEscapement eEscapement
= static_cast< const SvxEscapementItem
& >( _rItem
).GetEscapement();
304 return ( eEscapement
== m_eEscapement
) ? eChecked
: eUnchecked
;
307 //--------------------------------------------------------------------
308 void EscapementHandler::executeAttribute( const SfxItemSet
& _rCurrentAttribs
, SfxItemSet
& _rNewAttribs
, const SfxPoolItem
* _pAdditionalArg
, ScriptType
/*_nForScriptType*/ ) const
310 OSL_ENSURE( !_pAdditionalArg
, "EscapementHandler::executeAttribute: this is a simple toggle attribute - no args possible!" );
311 // well, in theory we could allow an SvxEscapementItem here, but this is not needed
312 (void)_pAdditionalArg
;
314 bool bIsChecked
= getCheckState( _rCurrentAttribs
) == eChecked
;
315 _rNewAttribs
.Put( SvxEscapementItem( bIsChecked
? SVX_ESCAPEMENT_OFF
: m_eEscapement
, getWhich() ) );
318 //====================================================================
320 //====================================================================
321 //--------------------------------------------------------------------
322 SlotHandler::SlotHandler( AttributeId _nAttributeId
, WhichId _nWhichId
)
323 :AttributeHandler( _nAttributeId
, _nWhichId
)
324 ,m_bScriptDependent( false )
326 m_bScriptDependent
= ( SID_ATTR_CHAR_WEIGHT
== _nAttributeId
)
327 || ( SID_ATTR_CHAR_POSTURE
== _nAttributeId
)
328 || ( SID_ATTR_CHAR_FONT
== _nAttributeId
);
331 //--------------------------------------------------------------------
332 AttributeState
SlotHandler::getState( const SfxItemSet
& _rAttribs
) const
334 AttributeState
aState( eIndetermined
);
336 const SfxPoolItem
* pItem
= _rAttribs
.GetItem( getWhich() );
338 aState
.setItem( pItem
->Clone() );
343 //--------------------------------------------------------------------
344 void SlotHandler::executeAttribute( const SfxItemSet
& /*_rCurrentAttribs*/, SfxItemSet
& _rNewAttribs
, const SfxPoolItem
* _pAdditionalArg
, ScriptType _nForScriptType
) const
346 if ( _pAdditionalArg
)
348 SfxPoolItem
* pCorrectWich
= _pAdditionalArg
->Clone();
349 pCorrectWich
->SetWhich( getWhich() );
351 if ( m_bScriptDependent
)
352 putItemForScript( _rNewAttribs
, *pCorrectWich
, _nForScriptType
);
354 _rNewAttribs
.Put( *pCorrectWich
);
355 DELETEZ( pCorrectWich
);
358 OSL_FAIL( "SlotHandler::executeAttribute: need attributes to do something!" );
361 //====================================================================
363 //====================================================================
364 //--------------------------------------------------------------------
365 FontSizeHandler::FontSizeHandler( AttributeId _nAttributeId
, WhichId _nWhichId
)
366 :AttributeHandler( _nAttributeId
, _nWhichId
)
368 OSL_ENSURE( ( _nAttributeId
== SID_ATTR_CHAR_FONTHEIGHT
) || ( _nAttributeId
== SID_ATTR_CHAR_CTL_FONTHEIGHT
)
369 || ( _nAttributeId
== SID_ATTR_CHAR_CJK_FONTHEIGHT
) || ( _nAttributeId
== SID_ATTR_CHAR_LATIN_FONTHEIGHT
),
370 "FontSizeHandler::FontSizeHandler: invalid attribute id!" );
373 //--------------------------------------------------------------------
374 AttributeState
FontSizeHandler::getState( const SfxItemSet
& _rAttribs
) const
376 AttributeState
aState( eIndetermined
);
378 const SfxPoolItem
* pItem
= _rAttribs
.GetItem( getWhich() );
379 const SvxFontHeightItem
* pFontHeightItem
= PTR_CAST( SvxFontHeightItem
, pItem
);
380 OSL_ENSURE( pFontHeightItem
|| !pItem
, "FontSizeHandler::getState: invalid item!" );
381 if ( pFontHeightItem
)
383 // by definition, the item should have the unit twip
384 sal_uLong nHeight
= pFontHeightItem
->GetHeight();
385 if ( _rAttribs
.GetPool()->GetMetric( getWhich() ) != SFX_MAPUNIT_TWIP
)
387 nHeight
= OutputDevice::LogicToLogic(
389 MapMode( (MapUnit
)( _rAttribs
.GetPool()->GetMetric( getWhich() ) ) ),
394 SvxFontHeightItem
* pNewItem
= new SvxFontHeightItem( nHeight
, 100, getWhich() );
395 pNewItem
->SetProp( pFontHeightItem
->GetProp(), pFontHeightItem
->GetPropUnit() );
396 aState
.setItem( pNewItem
);
402 //--------------------------------------------------------------------
403 void FontSizeHandler::executeAttribute( const SfxItemSet
& /*_rCurrentAttribs*/, SfxItemSet
& _rNewAttribs
, const SfxPoolItem
* _pAdditionalArg
, ScriptType _nForScriptType
) const
405 const SvxFontHeightItem
* pFontHeightItem
= PTR_CAST( SvxFontHeightItem
, _pAdditionalArg
);
406 OSL_ENSURE( pFontHeightItem
, "FontSizeHandler::executeAttribute: need a FontHeightItem!" );
408 if ( pFontHeightItem
)
410 // corect measurement units
411 SfxMapUnit eItemMapUnit
= pFontHeightItem
->GetPropUnit(); (void)eItemMapUnit
;
412 sal_uLong nHeight
= pFontHeightItem
->GetHeight();
413 if ( _rNewAttribs
.GetPool()->GetMetric( getWhich() ) != SFX_MAPUNIT_TWIP
)
415 nHeight
= OutputDevice::LogicToLogic(
417 MapMode( (MapUnit
)( SFX_MAPUNIT_TWIP
) ),
418 MapMode( (MapUnit
)( _rNewAttribs
.GetPool()->GetMetric( getWhich() ) ) )
422 SvxFontHeightItem
aNewItem( nHeight
, 100, getWhich() );
423 aNewItem
.SetProp( pFontHeightItem
->GetProp(), pFontHeightItem
->GetPropUnit() );
425 if ( ( getAttributeId() == SID_ATTR_CHAR_FONTHEIGHT
) && _nForScriptType
)
426 putItemForScript( _rNewAttribs
, aNewItem
, _nForScriptType
);
428 _rNewAttribs
.Put( aNewItem
);
432 //====================================================================
433 //= ParagraphDirectionHandler
434 //====================================================================
435 //--------------------------------------------------------------------
436 ParagraphDirectionHandler::ParagraphDirectionHandler( AttributeId _nAttributeId
)
437 :AttributeHandler( _nAttributeId
, EE_PARA_WRITINGDIR
)
438 ,m_eParagraphDirection( FRMDIR_HORI_LEFT_TOP
)
439 ,m_eDefaultAdjustment( SVX_ADJUST_RIGHT
)
440 ,m_eOppositeDefaultAdjustment( SVX_ADJUST_LEFT
)
442 switch ( getAttributeId() )
444 case SID_ATTR_PARA_LEFT_TO_RIGHT
: m_eParagraphDirection
= FRMDIR_HORI_LEFT_TOP
; m_eDefaultAdjustment
= SVX_ADJUST_LEFT
; break;
445 case SID_ATTR_PARA_RIGHT_TO_LEFT
: m_eParagraphDirection
= FRMDIR_HORI_RIGHT_TOP
; m_eDefaultAdjustment
= SVX_ADJUST_RIGHT
; break;
447 OSL_FAIL( "ParagraphDirectionHandler::ParagraphDirectionHandler: invalid attribute id!" );
450 if ( SVX_ADJUST_RIGHT
== m_eDefaultAdjustment
)
451 m_eOppositeDefaultAdjustment
= SVX_ADJUST_LEFT
;
453 m_eOppositeDefaultAdjustment
= SVX_ADJUST_RIGHT
;
456 //--------------------------------------------------------------------
457 AttributeCheckState
ParagraphDirectionHandler::implGetCheckState( const SfxPoolItem
& _rItem
) const
459 OSL_ENSURE( _rItem
.ISA( SvxFrameDirectionItem
), "ParagraphDirectionHandler::implGetCheckState: invalid pool item!" );
460 SvxFrameDirection eDirection
= static_cast< SvxFrameDirection
>( static_cast< const SvxFrameDirectionItem
& >( _rItem
).GetValue() );
461 return ( eDirection
== m_eParagraphDirection
) ? eChecked
: eUnchecked
;
464 //--------------------------------------------------------------------
465 void ParagraphDirectionHandler::executeAttribute( const SfxItemSet
& _rCurrentAttribs
, SfxItemSet
& _rNewAttribs
, const SfxPoolItem
* /*_pAdditionalArg*/, ScriptType
/*_nForScriptType*/ ) const
467 _rNewAttribs
.Put( SvxFrameDirectionItem( m_eParagraphDirection
, getWhich() ) );
469 // if the current adjustment of the was the default adjustment for the *previous* text direction,
470 // then we toggle the adjustment, too
471 SvxAdjust eCurrentAdjustment
= SVX_ADJUST_LEFT
;
472 const SfxPoolItem
* pCurrentAdjustment
= NULL
;
473 if ( SFX_ITEM_ON
== _rCurrentAttribs
.GetItemState( EE_PARA_JUST
, sal_True
, &pCurrentAdjustment
) )
474 eCurrentAdjustment
= static_cast< const SvxAdjustItem
* >( pCurrentAdjustment
)->GetAdjust();
476 if ( eCurrentAdjustment
== m_eOppositeDefaultAdjustment
)
477 _rNewAttribs
.Put( SvxAdjustItem( m_eDefaultAdjustment
, EE_PARA_JUST
) );
480 //====================================================================
482 //====================================================================
483 //--------------------------------------------------------------------
484 BooleanHandler::BooleanHandler( AttributeId _nAttributeId
, WhichId _nWhichId
)
485 :AttributeHandler( _nAttributeId
, _nWhichId
)
489 //--------------------------------------------------------------------
490 AttributeCheckState
BooleanHandler::implGetCheckState( const SfxPoolItem
& _rItem
) const
492 OSL_ENSURE( _rItem
.ISA( SfxBoolItem
), "BooleanHandler::implGetCheckState: invalid item!" );
493 if ( _rItem
.ISA( SfxBoolItem
) )
494 return static_cast< const SfxBoolItem
& >( _rItem
).GetValue() ? eChecked
: eUnchecked
;
496 return eIndetermined
;
499 //--------------------------------------------------------------------
500 void BooleanHandler::executeAttribute( const SfxItemSet
& /*_rCurrentAttribs*/, SfxItemSet
& _rNewAttribs
, const SfxPoolItem
* _pAdditionalArg
, ScriptType
/*_nForScriptType*/ ) const
502 OSL_ENSURE( _pAdditionalArg
&& _pAdditionalArg
->ISA( SfxBoolItem
), "BooleanHandler::executeAttribute: invalid argument!" );
503 if ( _pAdditionalArg
)
505 SfxPoolItem
* pCorrectWich
= _pAdditionalArg
->Clone();
506 pCorrectWich
->SetWhich( getWhich() );
507 _rNewAttribs
.Put( *pCorrectWich
);
508 DELETEZ( pCorrectWich
);
512 //........................................................................
514 //........................................................................
516 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */