LibreOffice Module writerfilter (master) 1
ModelEventListener.cxx
Go to the documentation of this file.
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 */
20#include "PropertyIds.hxx"
21#include <rtl/ustring.hxx>
22#include <com/sun/star/document/XEventBroadcaster.hpp>
23#include <com/sun/star/text/XTextFieldsSupplier.hpp>
24#include <com/sun/star/util/XRefreshable.hpp>
25#include <com/sun/star/beans/XPropertySet.hpp>
26#include <com/sun/star/text/ReferenceFieldPart.hpp>
27#include <com/sun/star/text/ReferenceFieldSource.hpp>
28#include <com/sun/star/frame/XModel.hpp>
29#include <com/sun/star/view/XFormLayerAccess.hpp>
31
32namespace writerfilter::dmapper {
33
34using namespace ::com::sun::star;
35
36
37ModelEventListener::ModelEventListener(bool bIndexes, bool bControls)
38 : m_bIndexes(bIndexes),
39 m_bControls(bControls)
40{
41}
42
43
45{
46}
47
48
49void ModelEventListener::notifyEvent( const document::EventObject& rEvent )
50{
51 if ( rEvent.EventName == "OnFocus" && m_bIndexes)
52 {
53 try
54 {
55 //remove listener
56 uno::Reference<document::XEventBroadcaster>(rEvent.Source, uno::UNO_QUERY_THROW )->removeEventListener(
58
59 // If we have PAGEREF fields, update fields as well.
60 uno::Reference<text::XTextFieldsSupplier> xTextFieldsSupplier(rEvent.Source, uno::UNO_QUERY);
61 uno::Reference<container::XEnumeration> xEnumeration = xTextFieldsSupplier->getTextFields()->createEnumeration();
62 sal_Int32 nIndex = 0;
63 while(xEnumeration->hasMoreElements())
64 {
65 try
66 {
67 uno::Reference<beans::XPropertySet> xPropertySet(xEnumeration->nextElement(), uno::UNO_QUERY);
68 sal_Int16 nSource = 0;
69 xPropertySet->getPropertyValue(getPropertyName(PROP_REFERENCE_FIELD_SOURCE)) >>= nSource;
70 sal_Int16 nPart = 0;
71 xPropertySet->getPropertyValue(getPropertyName(PROP_REFERENCE_FIELD_PART)) >>= nPart;
72 if (nSource == text::ReferenceFieldSource::BOOKMARK && nPart == text::ReferenceFieldPart::PAGE)
73 ++nIndex;
74 }
75 catch( const beans::UnknownPropertyException& )
76 {
77 // doesn't even have such a property? ignore
78 }
79 }
80 if (nIndex)
81 {
82 uno::Reference<util::XRefreshable> xRefreshable(xTextFieldsSupplier->getTextFields(), uno::UNO_QUERY);
83 xRefreshable->refresh();
84 }
85 }
86 catch( const uno::Exception& )
87 {
88 DBG_UNHANDLED_EXCEPTION("writerfilter", "exception while updating indexes");
89 }
90 }
91
92 if ( rEvent.EventName == "OnFocus" && m_bControls)
93 {
94
95 // Form design mode is enabled by default in Writer, not in Word.
96 uno::Reference<frame::XModel> xModel(rEvent.Source, uno::UNO_QUERY);
97 uno::Reference<view::XFormLayerAccess> xFormLayerAccess(xModel->getCurrentController(), uno::UNO_QUERY);
98 xFormLayerAccess->setFormDesignMode(false);
99 }
100}
101
102
103void ModelEventListener::disposing( const lang::EventObject& rEvent )
104{
105 try
106 {
107 uno::Reference<document::XEventBroadcaster>(rEvent.Source, uno::UNO_QUERY_THROW )->removeEventListener(
109 }
110 catch( const uno::Exception& )
111 {
112 }
113}
114
115} //namespace writerfilter::dmapper
116
117/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ModelEventListener(bool bIndexes, bool bControls)
virtual void SAL_CALL notifyEvent(const css::document::EventObject &Event) override
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
#define DBG_UNHANDLED_EXCEPTION(...)
sal_Int32 nIndex
OUString getPropertyName(PropertyIds eId)
Reference< XModel > xModel