1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "vbainformationhelper.hxx"
24 #include <com/sun/star/text/XPageCursor.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include "wordvbahelper.hxx"
27 #include <docsh.hxx>
28 #include <doc.hxx>
29 #include <vbahelper/vbahelper.hxx>
30 #include <swtypes.hxx>
31 #include <viewsh.hxx>
32 
33 using namespace ::ooo::vba;
34 using namespace ::com::sun::star;
35 
36 static const sal_Int32 DEFAULT_PAGE_DISTANCE = 500;
37 
handleWdActiveEndPageNumber(const css::uno::Reference<css::text::XTextViewCursor> & xTVCursor)38 sal_Int32 SwVbaInformationHelper::handleWdActiveEndPageNumber( const css::uno::Reference< css::text::XTextViewCursor >& xTVCursor ) throw( css::uno::RuntimeException )
39 {
40     uno::Reference< text::XPageCursor > xPageCursor( xTVCursor, uno::UNO_QUERY_THROW );
41     return xPageCursor->getPage();
42 }
43 
handleWdNumberOfPagesInDocument(const css::uno::Reference<css::frame::XModel> & xModel)44 sal_Int32 SwVbaInformationHelper::handleWdNumberOfPagesInDocument( const css::uno::Reference< css::frame::XModel >& xModel ) throw( css::uno::RuntimeException )
45 {
46     return word::getPageCount( xModel );
47 }
48 
handleWdVerticalPositionRelativeToPage(const css::uno::Reference<css::frame::XModel> & xModel,const css::uno::Reference<css::text::XTextViewCursor> & xTVCursor)49 double SwVbaInformationHelper::handleWdVerticalPositionRelativeToPage( const css::uno::Reference< css::frame::XModel >& xModel, const css::uno::Reference< css::text::XTextViewCursor >& xTVCursor ) throw( css::uno::RuntimeException )
50 {
51     xTVCursor->collapseToStart();
52     uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( xModel ), uno::UNO_QUERY_THROW );
53     sal_Int32 nTopMargin = 0;
54     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin") ) ) >>= nTopMargin;
55     sal_Int32 nCurrentPos = xTVCursor->getPosition().Y;
56 
57     sal_Int32 nCurrentPage = handleWdActiveEndPageNumber( xTVCursor );
58     SwDoc* pDoc = word::getDocShell( xModel )->GetDoc();
59     ViewShell* pViewSh = pDoc->GetCurrentViewShell();
60     sal_Int32 nPageHeight = pViewSh ? pViewSh->GetPageSize( nCurrentPage, sal_False ).Height() : 0;
61     // FIXME: handle multipul page style
62     // it is very strange that the curros position is incorrect when open Word file.
63     // e.g. if current cursor in the top left of the text body of the first page without header,
64     // the top value of current position should be 0, but is 201 when open a Word file.
65     nCurrentPos = nCurrentPos + nTopMargin - ( DEFAULT_PAGE_DISTANCE + TWIP_TO_MM100( nPageHeight ) ) * (  nCurrentPage - 1  );
66     return Millimeter::getInPoints( nCurrentPos );
67 }
68