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 <pattern/window.hxx>
21 #include <helper/persistentwindowstate.hxx>
22 #include <macros/generic.hxx>
25 #include <com/sun/star/awt/XWindow.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/frame/ModuleManager.hpp>
31 #include <comphelper/processfactory.hxx>
32 #include <comphelper/configurationhelper.hxx>
33 #include <vcl/window.hxx>
34 #include <vcl/syswin.hxx>
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/wrkwin.hxx>
39 #include <rtl/string.hxx>
43 PersistentWindowState::PersistentWindowState(const css::uno::Reference
< css::uno::XComponentContext
>& xContext
)
44 : m_xContext (xContext
)
45 , m_bWindowStateAlreadySet(false )
49 PersistentWindowState::~PersistentWindowState()
53 void SAL_CALL
PersistentWindowState::initialize(const css::uno::Sequence
< css::uno::Any
>& lArguments
)
54 throw(css::uno::Exception
,
55 css::uno::RuntimeException
, std::exception
)
58 css::uno::Reference
< css::frame::XFrame
> xFrame
;
59 if (lArguments
.getLength() < 1)
60 throw css::lang::IllegalArgumentException(
61 "Empty argument list!",
62 static_cast< ::cppu::OWeakObject
* >(this),
65 lArguments
[0] >>= xFrame
;
67 throw css::lang::IllegalArgumentException(
68 "No valid frame specified!",
69 static_cast< ::cppu::OWeakObject
* >(this),
78 xFrame
->addFrameActionListener(this);
81 void SAL_CALL
PersistentWindowState::frameAction(const css::frame::FrameActionEvent
& aEvent
)
82 throw(css::uno::RuntimeException
, std::exception
)
84 css::uno::Reference
< css::uno::XComponentContext
> xContext
;
85 css::uno::Reference
< css::frame::XFrame
> xFrame
;
86 bool bRestoreWindowState
;
89 xContext
= m_xContext
;
90 xFrame
.set(m_xFrame
.get(), css::uno::UNO_QUERY
);
91 bRestoreWindowState
= !m_bWindowStateAlreadySet
;
94 // frame already gone ? We hold it weak only ...
98 // no window -> no position and size available
99 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow();
103 // unknown module -> no configuration available!
104 OUString sModuleName
= PersistentWindowState::implst_identifyModule(xContext
, xFrame
);
105 if (sModuleName
.isEmpty())
108 switch(aEvent
.Action
)
110 case css::frame::FrameAction_COMPONENT_ATTACHED
:
112 if (bRestoreWindowState
)
114 OUString sWindowState
= PersistentWindowState::implst_getWindowStateFromConfig(xContext
, sModuleName
);
115 PersistentWindowState::implst_setWindowStateOnWindow(xWindow
,sWindowState
);
117 m_bWindowStateAlreadySet
= true;
122 case css::frame::FrameAction_COMPONENT_REATTACHED
:
124 // nothing todo here, because its not allowed to change position and size
125 // of an alredy existing frame!
129 case css::frame::FrameAction_COMPONENT_DETACHING
:
131 OUString sWindowState
= PersistentWindowState::implst_getWindowStateFromWindow(xWindow
);
132 PersistentWindowState::implst_setWindowStateOnConfig(xContext
, sModuleName
, sWindowState
);
140 void SAL_CALL
PersistentWindowState::disposing(const css::lang::EventObject
&)
141 throw(css::uno::RuntimeException
, std::exception
)
143 // nothing todo here - because we hold the frame as weak reference only
146 OUString
PersistentWindowState::implst_identifyModule(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
147 const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
149 OUString sModuleName
;
151 css::uno::Reference
< css::frame::XModuleManager2
> xModuleManager
=
152 css::frame::ModuleManager::create( rxContext
);
156 sModuleName
= xModuleManager
->identify(xFrame
);
158 catch(const css::uno::RuntimeException
&)
160 catch(const css::uno::Exception
&)
161 { sModuleName
.clear(); }
166 OUString
PersistentWindowState::implst_getWindowStateFromConfig(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
167 const OUString
& sModuleName
)
169 OUString sWindowState
;
171 OUStringBuffer
sRelPathBuf(256);
172 sRelPathBuf
.appendAscii("Office/Factories/*[\"");
173 sRelPathBuf
.append (sModuleName
);
174 sRelPathBuf
.appendAscii("\"]" );
176 OUString
sPackage("org.openoffice.Setup/");
177 OUString sRelPath
= sRelPathBuf
.makeStringAndClear();
178 OUString
sKey("ooSetupFactoryWindowAttributes");
182 ::comphelper::ConfigurationHelper::readDirectKey(rxContext
,
186 ::comphelper::ConfigurationHelper::E_READONLY
) >>= sWindowState
;
188 catch(const css::uno::RuntimeException
&)
190 catch(const css::uno::Exception
&)
191 { sWindowState
.clear(); }
196 void PersistentWindowState::implst_setWindowStateOnConfig(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
197 const OUString
& sModuleName
,
198 const OUString
& sWindowState
)
200 OUStringBuffer
sRelPathBuf(256);
201 sRelPathBuf
.appendAscii("Office/Factories/*[\"");
202 sRelPathBuf
.append (sModuleName
);
203 sRelPathBuf
.appendAscii("\"]" );
205 OUString
sPackage("org.openoffice.Setup/");
206 OUString sRelPath
= sRelPathBuf
.makeStringAndClear();
207 OUString
sKey("ooSetupFactoryWindowAttributes");
211 ::comphelper::ConfigurationHelper::writeDirectKey(rxContext
,
215 css::uno::makeAny(sWindowState
),
216 ::comphelper::ConfigurationHelper::E_STANDARD
);
218 catch(const css::uno::RuntimeException
&)
220 catch(const css::uno::Exception
&)
224 OUString
PersistentWindowState::implst_getWindowStateFromWindow(const css::uno::Reference
< css::awt::XWindow
>& xWindow
)
226 OUString sWindowState
;
230 // SOLAR SAFE -> ------------------------
231 SolarMutexGuard aSolarGuard
;
233 vcl::Window
* pWindow
= VCLUnoHelper::GetWindow(xWindow
);
234 // check for system window is necessary to guarantee correct pointer cast!
237 (pWindow
->IsSystemWindow())
240 sal_uLong nMask
= WINDOWSTATE_MASK_ALL
;
241 nMask
&= ~(WINDOWSTATE_MASK_MINIMIZED
);
242 sWindowState
= OStringToOUString(
243 static_cast<SystemWindow
*>(pWindow
)->GetWindowState(nMask
),
244 RTL_TEXTENCODING_UTF8
);
246 // <- SOLAR SAFE ------------------------
252 void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference
< css::awt::XWindow
>& xWindow
,
253 const OUString
& sWindowState
)
257 ( sWindowState
.isEmpty() )
261 // SOLAR SAFE -> ------------------------
262 SolarMutexGuard aSolarGuard
;
264 vcl::Window
* pWindow
= VCLUnoHelper::GetWindow(xWindow
);
268 // check for system and work window - its necessary to guarantee correct pointer cast!
269 bool bSystemWindow
= pWindow
->IsSystemWindow();
270 bool bWorkWindow
= (pWindow
->GetType() == WINDOW_WORKWINDOW
);
272 if (!bSystemWindow
&& !bWorkWindow
)
275 SystemWindow
* pSystemWindow
= static_cast<SystemWindow
*>(pWindow
);
276 WorkWindow
* pWorkWindow
= static_cast<WorkWindow
* >(pWindow
);
278 // dont save this special state!
279 if (pWorkWindow
->IsMinimized())
282 OUString sOldWindowState
= OStringToOUString( pSystemWindow
->GetWindowState(), RTL_TEXTENCODING_ASCII_US
);
283 if ( sOldWindowState
!= sWindowState
)
284 pSystemWindow
->SetWindowState(OUStringToOString(sWindowState
,RTL_TEXTENCODING_UTF8
));
285 // <- SOLAR SAFE ------------------------
288 } // namespace framework
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */