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/sheet/XSheetAnnotationAnchor.hpp>
26 #include <com/sun/star/sheet/XSheetAnnotationShapeSupplier.hpp>
27 #include <com/sun/star/sheet/XSheetAnnotations.hpp>
28 #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
29 #include <osl/diagnose.h>
30 #include <oox/helper/attributelist.hxx>
31 #include <oox/vml/vmlshape.hxx>
32 #include "addressconverter.hxx"
33 #include "biffinputstream.hxx"
34 #include "drawingfragment.hxx"
35 #include <svx/sdtaitm.hxx>
36 #include "unitconverter.hxx"
37 #include "drawingmanager.hxx"
39 #include <com/sun/star/text/XText.hpp>
40 #include <com/sun/star/text/XTextRange.hpp>
42 using ::com::sun::star::text::XText
;
43 using ::com::sun::star::text::XTextRange
;
48 using namespace ::com::sun::star::drawing
;
49 using namespace ::com::sun::star::sheet
;
50 using namespace ::com::sun::star::table
;
51 using namespace ::com::sun::star::text
;
52 using namespace ::com::sun::star::uno
;
54 static sal_Int32
lcl_ToHorizAlign( sal_Int32 nAlign
)
59 return SDRTEXTHORZADJUST_LEFT
;
61 return SDRTEXTHORZADJUST_RIGHT
;
63 return SDRTEXTHORZADJUST_CENTER
;
65 return SDRTEXTHORZADJUST_BLOCK
;
69 static sal_Int32
lcl_ToVertAlign( sal_Int32 nAlign
)
74 return SDRTEXTVERTADJUST_TOP
;
76 return SDRTEXTVERTADJUST_CENTER
;
78 return SDRTEXTVERTADJUST_BOTTOM
;
80 return SDRTEXTVERTADJUST_BLOCK
;
84 CommentModel::CommentModel()
86 , mnObjId(BIFF_OBJ_INVALID_ID
)
98 Comment::Comment( const WorksheetHelper
& rHelper
) :
99 WorksheetHelper( rHelper
)
103 void Comment::importComment( const AttributeList
& rAttribs
)
105 maModel
.mnAuthorId
= rAttribs
.getInteger( XML_authorId
, -1 );
106 // cell range will be checked while inserting the comment into the document
107 AddressConverter::convertToCellRangeUnchecked( maModel
.maRange
, rAttribs
.getString( XML_ref
, OUString() ), getSheetIndex() );
110 void Comment::importCommentPr( const AttributeList
& rAttribs
)
112 maModel
.mbAutoFill
= rAttribs
.getBool( XML_autoFill
, true );
113 maModel
.mbAutoScale
= rAttribs
.getBool( XML_autoScale
, false );
114 maModel
.mbColHidden
= rAttribs
.getBool( XML_colHidden
, false );
115 maModel
.mbLocked
= rAttribs
.getBool( XML_locked
, false );
116 maModel
.mbRowHidden
= rAttribs
.getBool( XML_rowHidden
, false );
117 maModel
.mnTHA
= rAttribs
.getToken( XML_textHAlign
, XML_left
);
118 maModel
.mnTVA
= rAttribs
.getToken( XML_textVAlign
, XML_top
);
121 void Comment::importComment( SequenceInputStream
& rStrm
)
124 maModel
.mnAuthorId
= rStrm
.readInt32();
126 // cell range will be checked while inserting the comment into the document
127 AddressConverter::convertToCellRangeUnchecked( maModel
.maRange
, aBinRange
, getSheetIndex() );
130 RichStringRef
Comment::createText()
132 maModel
.mxText
.reset( new RichString( *this ) );
133 return maModel
.mxText
;
136 void Comment::finalizeImport()
138 // BIFF12 stores cell range instead of cell address, use first cell of this range
139 OSL_ENSURE( (maModel
.maRange
.StartColumn
== maModel
.maRange
.EndColumn
) &&
140 (maModel
.maRange
.StartRow
== maModel
.maRange
.EndRow
),
141 "Comment::finalizeImport - comment anchor should be a single cell" );
142 CellAddress
aNotePos( maModel
.maRange
.Sheet
, maModel
.maRange
.StartColumn
, maModel
.maRange
.StartRow
);
143 if( getAddressConverter().checkCellAddress( aNotePos
, true ) && maModel
.mxText
.get() ) try
145 Reference
< XSheetAnnotationsSupplier
> xAnnosSupp( getSheet(), UNO_QUERY_THROW
);
146 Reference
< XSheetAnnotations
> xAnnos( xAnnosSupp
->getAnnotations(), UNO_SET_THROW
);
147 // non-empty string required by note implementation (real text will be added below)
148 xAnnos
->insertNew( aNotePos
, OUString( ' ' ) );
150 // receive created note from cell (insertNew does not return the note)
151 Reference
< XSheetAnnotationAnchor
> xAnnoAnchor( getCell( aNotePos
), UNO_QUERY_THROW
);
152 Reference
< XSheetAnnotation
> xAnno( xAnnoAnchor
->getAnnotation(), UNO_SET_THROW
);
153 Reference
< XSheetAnnotationShapeSupplier
> xAnnoShapeSupp( xAnno
, UNO_QUERY_THROW
);
154 Reference
< XShape
> xAnnoShape( xAnnoShapeSupp
->getAnnotationShape(), UNO_SET_THROW
);
156 // convert shape formatting and visibility
157 bool bVisible
= true;
158 switch( getFilterType() )
162 // Add shape formatting properties (autoFill, colHidden and rowHidden are dropped)
163 PropertySet
aCommentPr( xAnnoShape
);
164 aCommentPr
.setProperty( PROP_TextFitToSize
, maModel
.mbAutoScale
);
165 aCommentPr
.setProperty( PROP_MoveProtect
, maModel
.mbLocked
);
166 aCommentPr
.setProperty( PROP_TextHorizontalAdjust
, lcl_ToHorizAlign( maModel
.mnTHA
) );
167 aCommentPr
.setProperty( PROP_TextVerticalAdjust
, lcl_ToVertAlign( maModel
.mnTVA
) );
168 if( maModel
.maAnchor
.Width
> 0 && maModel
.maAnchor
.Height
> 0 )
170 xAnnoShape
->setPosition( css::awt::Point( maModel
.maAnchor
.X
, maModel
.maAnchor
.Y
) );
171 xAnnoShape
->setSize( css::awt::Size( maModel
.maAnchor
.Width
, maModel
.maAnchor
.Height
) );
174 // convert shape formatting and visibility
175 if( const ::oox::vml::ShapeBase
* pNoteShape
= getVmlDrawing().getNoteShape( aNotePos
) )
177 // position and formatting
178 pNoteShape
->convertFormatting( xAnnoShape
);
180 bVisible
= pNoteShape
->getTypeModel().mbVisible
;
185 bVisible
= maModel
.mbVisible
;
190 xAnno
->setIsVisible( bVisible
);
192 // insert text and convert text formatting
193 maModel
.mxText
->finalizeImport();
194 Reference
< XText
> xAnnoText( xAnnoShape
, UNO_QUERY_THROW
);
195 maModel
.mxText
->convert( xAnnoText
, true );
202 // private --------------------------------------------------------------------
204 CommentsBuffer::CommentsBuffer( const WorksheetHelper
& rHelper
) :
205 WorksheetHelper( rHelper
)
209 void CommentsBuffer::appendAuthor( const OUString
& rAuthor
)
211 maAuthors
.push_back( rAuthor
);
214 CommentRef
CommentsBuffer::createComment()
216 CommentRef
xComment( new Comment( *this ) );
217 maComments
.push_back( xComment
);
221 void CommentsBuffer::finalizeImport()
223 maComments
.forEachMem( &Comment::finalizeImport
);
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */