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 <oox/token/properties.hxx>
21 #include <oox/token/tokens.hxx>
23 #include "commentsbuffer.hxx"
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/sheet/XSheetAnnotationAnchor.hpp>
27 #include <com/sun/star/sheet/XSheetAnnotationShapeSupplier.hpp>
28 #include <com/sun/star/sheet/XSheetAnnotations.hpp>
29 #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
30 #include <osl/diagnose.h>
31 #include <oox/helper/attributelist.hxx>
32 #include <oox/vml/vmlshape.hxx>
33 #include "addressconverter.hxx"
34 #include "biffinputstream.hxx"
35 #include "drawingfragment.hxx"
36 #include <svx/sdtaitm.hxx>
37 #include "unitconverter.hxx"
38 #include "drawingmanager.hxx"
40 #include <com/sun/star/text/XText.hpp>
42 using ::com::sun::star::text::XText
;
47 using namespace ::com::sun::star::drawing
;
48 using namespace ::com::sun::star::sheet
;
49 using namespace ::com::sun::star::table
;
50 using namespace ::com::sun::star::text
;
51 using namespace ::com::sun::star::uno
;
53 static sal_Int32
lcl_ToHorizAlign( sal_Int32 nAlign
)
58 return SDRTEXTHORZADJUST_LEFT
;
60 return SDRTEXTHORZADJUST_RIGHT
;
62 return SDRTEXTHORZADJUST_CENTER
;
64 return SDRTEXTHORZADJUST_BLOCK
;
68 static sal_Int32
lcl_ToVertAlign( sal_Int32 nAlign
)
73 return SDRTEXTVERTADJUST_TOP
;
75 return SDRTEXTVERTADJUST_CENTER
;
77 return SDRTEXTVERTADJUST_BOTTOM
;
79 return SDRTEXTVERTADJUST_BLOCK
;
83 CommentModel::CommentModel()
96 Comment::Comment( const WorksheetHelper
& rHelper
) :
97 WorksheetHelper( rHelper
)
101 void Comment::importComment( const AttributeList
& rAttribs
)
103 maModel
.mnAuthorId
= rAttribs
.getInteger( XML_authorId
, -1 );
104 // cell range will be checked while inserting the comment into the document
105 AddressConverter::convertToCellRangeUnchecked( maModel
.maRange
, rAttribs
.getString( XML_ref
, OUString() ), getSheetIndex() );
108 void Comment::importCommentPr( const AttributeList
& rAttribs
)
110 maModel
.mbAutoFill
= rAttribs
.getBool( XML_autoFill
, true );
111 maModel
.mbAutoScale
= rAttribs
.getBool( XML_autoScale
, false );
112 maModel
.mbColHidden
= rAttribs
.getBool( XML_colHidden
, false );
113 maModel
.mbLocked
= rAttribs
.getBool( XML_locked
, false );
114 maModel
.mbRowHidden
= rAttribs
.getBool( XML_rowHidden
, false );
115 maModel
.mnTHA
= rAttribs
.getToken( XML_textHAlign
, XML_left
);
116 maModel
.mnTVA
= rAttribs
.getToken( XML_textVAlign
, XML_top
);
119 void Comment::importComment( SequenceInputStream
& rStrm
)
122 maModel
.mnAuthorId
= rStrm
.readInt32();
124 // cell range will be checked while inserting the comment into the document
125 AddressConverter::convertToCellRangeUnchecked( maModel
.maRange
, aBinRange
, getSheetIndex() );
128 RichStringRef
Comment::createText()
130 maModel
.mxText
.reset( new RichString( *this ) );
131 return maModel
.mxText
;
134 void Comment::finalizeImport()
136 // BIFF12 stores cell range instead of cell address, use first cell of this range
137 OSL_ENSURE( (maModel
.maRange
.StartColumn
== maModel
.maRange
.EndColumn
) &&
138 (maModel
.maRange
.StartRow
== maModel
.maRange
.EndRow
),
139 "Comment::finalizeImport - comment anchor should be a single cell" );
140 CellAddress
aNotePos( maModel
.maRange
.Sheet
, maModel
.maRange
.StartColumn
, maModel
.maRange
.StartRow
);
141 if( getAddressConverter().checkCellAddress( aNotePos
, true ) && maModel
.mxText
.get() ) try
143 Reference
< XSheetAnnotationsSupplier
> xAnnosSupp( getSheet(), UNO_QUERY_THROW
);
144 Reference
< XSheetAnnotations
> xAnnos( xAnnosSupp
->getAnnotations(), UNO_SET_THROW
);
145 // non-empty string required by note implementation (real text will be added below)
146 xAnnos
->insertNew( aNotePos
, OUString( ' ' ) );
148 // receive created note from cell (insertNew does not return the note)
149 Reference
< XSheetAnnotationAnchor
> xAnnoAnchor( getCell( aNotePos
), UNO_QUERY_THROW
);
150 Reference
< XSheetAnnotation
> xAnno( xAnnoAnchor
->getAnnotation(), UNO_SET_THROW
);
151 Reference
< XSheetAnnotationShapeSupplier
> xAnnoShapeSupp( xAnno
, UNO_QUERY_THROW
);
152 Reference
< XShape
> xAnnoShape( xAnnoShapeSupp
->getAnnotationShape(), UNO_SET_THROW
);
154 // convert shape formatting and visibility
155 bool bVisible
= true;
156 switch( getFilterType() )
160 // Add shape formatting properties (autoFill, colHidden and rowHidden are dropped)
161 PropertySet
aCommentPr( xAnnoShape
);
162 aCommentPr
.setProperty( PROP_TextFitToSize
, maModel
.mbAutoScale
);
163 aCommentPr
.setProperty( PROP_MoveProtect
, maModel
.mbLocked
);
164 aCommentPr
.setProperty( PROP_TextHorizontalAdjust
, lcl_ToHorizAlign( maModel
.mnTHA
) );
165 aCommentPr
.setProperty( PROP_TextVerticalAdjust
, lcl_ToVertAlign( maModel
.mnTVA
) );
166 if( maModel
.maAnchor
.Width
> 0 && maModel
.maAnchor
.Height
> 0 )
168 xAnnoShape
->setPosition( css::awt::Point( maModel
.maAnchor
.X
, maModel
.maAnchor
.Y
) );
169 xAnnoShape
->setSize( css::awt::Size( maModel
.maAnchor
.Width
, maModel
.maAnchor
.Height
) );
172 // convert shape formatting and visibility
173 if( const ::oox::vml::ShapeBase
* pNoteShape
= getVmlDrawing().getNoteShape( aNotePos
) )
175 // position and formatting
176 pNoteShape
->convertFormatting( xAnnoShape
);
178 bVisible
= pNoteShape
->getTypeModel().mbVisible
;
183 bVisible
= maModel
.mbVisible
;
188 xAnno
->setIsVisible( bVisible
);
190 // insert text and convert text formatting
191 maModel
.mxText
->finalizeImport();
192 Reference
< XText
> xAnnoText( xAnnoShape
, UNO_QUERY_THROW
);
193 maModel
.mxText
->convert( xAnnoText
, true );
200 // private --------------------------------------------------------------------
202 CommentsBuffer::CommentsBuffer( const WorksheetHelper
& rHelper
) :
203 WorksheetHelper( rHelper
)
207 void CommentsBuffer::appendAuthor( const OUString
& rAuthor
)
209 maAuthors
.push_back( rAuthor
);
212 CommentRef
CommentsBuffer::createComment()
214 CommentRef
xComment( new Comment( *this ) );
215 maComments
.push_back( xComment
);
219 void CommentsBuffer::finalizeImport()
221 maComments
.forEachMem( &Comment::finalizeImport
);
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */