Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / helper / tagwindowasmodified.cxx
blobcc27a194e1fbc59bcc34be34a712057923db5856
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <helper/tagwindowasmodified.hxx>
22 #include <com/sun/star/awt/XWindow.hpp>
24 #include <com/sun/star/frame/FrameAction.hpp>
26 #include <toolkit/helper/vclunohelper.hxx>
27 #include <vcl/window.hxx>
28 #include <vcl/svapp.hxx>
29 #include <vcl/wintypes.hxx>
31 namespace framework{
33 TagWindowAsModified::TagWindowAsModified()
37 TagWindowAsModified::~TagWindowAsModified()
41 void SAL_CALL TagWindowAsModified::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
43 css::uno::Reference< css::frame::XFrame > xFrame;
45 if (lArguments.hasElements())
46 lArguments[0] >>= xFrame;
48 if (!xFrame)
49 return;
51 m_xFrame = xFrame;
52 xFrame->addFrameActionListener(this);
53 impl_update (xFrame);
56 void SAL_CALL TagWindowAsModified::modified(const css::lang::EventObject& aEvent)
58 if (!m_xModel || !m_xWindow || aEvent.Source != m_xModel)
59 return;
61 bool bModified = m_xModel->isModified ();
63 // SYNCHRONIZED ->
64 SolarMutexGuard aSolarGuard;
66 if (m_xWindow->isDisposed())
67 return;
69 if (bModified)
70 m_xWindow->SetExtendedStyle(WindowExtendedStyle::DocModified);
71 else
72 m_xWindow->SetExtendedStyle(WindowExtendedStyle::NONE);
73 // <- SYNCHRONIZED
76 void SAL_CALL TagWindowAsModified::frameAction(const css::frame::FrameActionEvent& aEvent)
78 if (
79 (aEvent.Action != css::frame::FrameAction_COMPONENT_REATTACHED) &&
80 (aEvent.Action != css::frame::FrameAction_COMPONENT_ATTACHED )
82 return;
84 if ( aEvent.Source != m_xFrame )
85 return;
87 impl_update (m_xFrame);
90 void SAL_CALL TagWindowAsModified::disposing(const css::lang::EventObject& aEvent)
92 SolarMutexGuard g;
94 if (m_xFrame && aEvent.Source == m_xFrame)
96 m_xFrame->removeFrameActionListener(this);
97 m_xFrame.clear();
98 return;
101 if (m_xModel && aEvent.Source == m_xModel)
103 m_xModel->removeModifyListener(this);
104 m_xModel.clear();
105 return;
109 void TagWindowAsModified::impl_update (const css::uno::Reference< css::frame::XFrame >& xFrame)
111 if (!xFrame)
112 return;
114 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
115 css::uno::Reference< css::frame::XController > xController = xFrame->getController ();
116 css::uno::Reference< css::util::XModifiable > xModel;
117 if (xController.is ())
118 xModel = css::uno::Reference< css::util::XModifiable >(xController->getModel(), css::uno::UNO_QUERY);
120 if (!xWindow || !xModel)
121 return;
124 SolarMutexGuard g;
126 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow(xWindow);
127 bool bSystemWindow = pWindow->IsSystemWindow();
128 bool bWorkWindow = (pWindow->GetType() == WindowType::WORKWINDOW);
129 if (!bSystemWindow && !bWorkWindow)
130 return;
132 if (m_xModel)
133 m_xModel->removeModifyListener (this);
135 // Note: frame was set as member outside ! we have to refresh connections
136 // regarding window and model only here.
137 m_xWindow = pWindow;
138 m_xModel = xModel;
141 m_xModel->addModifyListener (this);
144 } // namespace framework
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */