Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #include <ModelEventListener.hxx>
20 : #include <PropertyIds.hxx>
21 : #include <rtl/ustring.hxx>
22 : #include <com/sun/star/document/XEventBroadcaster.hpp>
23 : #include <com/sun/star/text/XDocumentIndex.hpp>
24 : #include <com/sun/star/text/XDocumentIndexesSupplier.hpp>
25 : #include <com/sun/star/text/XTextFieldsSupplier.hpp>
26 : #include <com/sun/star/util/XRefreshable.hpp>
27 : #include <com/sun/star/beans/XPropertySet.hpp>
28 : #include <com/sun/star/text/ReferenceFieldPart.hpp>
29 : #include <com/sun/star/text/ReferenceFieldSource.hpp>
30 : #include <com/sun/star/frame/XModel.hpp>
31 : #include <com/sun/star/view/XFormLayerAccess.hpp>
32 :
33 : namespace writerfilter {
34 : namespace dmapper {
35 :
36 : using namespace ::com::sun::star;
37 :
38 :
39 :
40 :
41 17 : ModelEventListener::ModelEventListener(bool bIndexes, bool bControls)
42 : : m_bIndexes(bIndexes),
43 17 : m_bControls(bControls)
44 : {
45 17 : }
46 :
47 :
48 34 : ModelEventListener::~ModelEventListener()
49 : {
50 34 : }
51 :
52 :
53 74 : void ModelEventListener::notifyEvent( const document::EventObject& rEvent ) throw (uno::RuntimeException)
54 : {
55 74 : if ( rEvent.EventName == "OnFocus" && m_bIndexes)
56 : {
57 : try
58 : {
59 13 : PropertyNameSupplier& rPropNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
60 :
61 13 : uno::Reference< text::XDocumentIndexesSupplier> xIndexesSupplier( rEvent.Source, uno::UNO_QUERY );
62 : //remove listener
63 26 : uno::Reference<document::XEventBroadcaster>(rEvent.Source, uno::UNO_QUERY )->removeEventListener(
64 13 : uno::Reference<document::XEventListener>(this));
65 :
66 26 : uno::Reference< container::XIndexAccess > xIndexes = xIndexesSupplier->getDocumentIndexes();
67 :
68 13 : sal_Int32 nIndexes = xIndexes->getCount();
69 13 : for( sal_Int32 nIndex = 0; nIndex < nIndexes; ++nIndex)
70 : {
71 0 : uno::Reference< text::XDocumentIndex> xIndex( xIndexes->getByIndex( nIndex ), uno::UNO_QUERY );
72 0 : xIndex->update();
73 0 : }
74 :
75 : // If we have PAGEREF fields, update fields as well.
76 26 : uno::Reference<text::XTextFieldsSupplier> xTextFieldsSupplier(rEvent.Source, uno::UNO_QUERY);
77 26 : uno::Reference<container::XEnumeration> xEnumeration(xTextFieldsSupplier->getTextFields()->createEnumeration(), uno::UNO_QUERY);
78 13 : sal_Int32 nIndex = 0;
79 40 : while(xEnumeration->hasMoreElements())
80 : {
81 : try
82 : {
83 14 : uno::Reference<beans::XPropertySet> xPropertySet(xEnumeration->nextElement(), uno::UNO_QUERY);
84 14 : sal_Int16 nSource = 0;
85 14 : xPropertySet->getPropertyValue(rPropNameSupplier.GetName(PROP_REFERENCE_FIELD_SOURCE)) >>= nSource;
86 0 : sal_Int16 nPart = 0;
87 0 : xPropertySet->getPropertyValue(rPropNameSupplier.GetName(PROP_REFERENCE_FIELD_PART)) >>= nPart;
88 0 : if (nSource == text::ReferenceFieldSource::BOOKMARK && nPart == text::ReferenceFieldPart::PAGE)
89 14 : ++nIndex;
90 : }
91 14 : catch( const beans::UnknownPropertyException& )
92 : {
93 : // doesn't even have such a property? ignore
94 : }
95 : }
96 13 : if (nIndex)
97 : {
98 0 : uno::Reference<util::XRefreshable> xRefreshable(xTextFieldsSupplier->getTextFields(), uno::UNO_QUERY);
99 0 : xRefreshable->refresh();
100 13 : }
101 : }
102 0 : catch( const uno::Exception& rEx )
103 : {
104 : SAL_WARN("writerfilter", "exception while updating indexes: " << rEx.Message);
105 : }
106 : }
107 :
108 74 : if ( rEvent.EventName == "OnFocus" && m_bControls)
109 : {
110 :
111 : // Form design mode is enabled by default in Writer, not in Word.
112 4 : uno::Reference<frame::XModel> xModel(rEvent.Source, uno::UNO_QUERY);
113 8 : uno::Reference<view::XFormLayerAccess> xFormLayerAccess(xModel->getCurrentController(), uno::UNO_QUERY);
114 8 : xFormLayerAccess->setFormDesignMode(false);
115 : }
116 74 : }
117 :
118 :
119 4 : void ModelEventListener::disposing( const lang::EventObject& rEvent ) throw (uno::RuntimeException)
120 : {
121 : try
122 : {
123 8 : uno::Reference<document::XEventBroadcaster>(rEvent.Source, uno::UNO_QUERY )->removeEventListener(
124 4 : uno::Reference<document::XEventListener>(this));
125 : }
126 0 : catch( const uno::Exception& )
127 : {
128 : }
129 4 : }
130 :
131 : } //namespace dmapper
132 : } //namespace writerfilter
133 :
134 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|