1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: persistentwindowstate.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
37 #include <pattern/window.hxx>
38 #include <helper/persistentwindowstate.hxx>
39 #include <threadhelp/writeguard.hxx>
40 #include <threadhelp/readguard.hxx>
41 #include <macros/generic.hxx>
44 //_________________________________________________________________________________________________________________
46 //_________________________________________________________________________________________________________________
47 #include <com/sun/star/awt/XWindow.hpp>
49 #ifndef _COM_SUN_STAR_LANG_XSERVICXEINFO_HPP_
50 #include <com/sun/star/lang/XServiceInfo.hpp>
52 #include <com/sun/star/lang/IllegalArgumentException.hpp>
53 #include <com/sun/star/frame/XModuleManager.hpp>
55 //_________________________________________________________________________________________________________________
57 //_________________________________________________________________________________________________________________
58 #include <comphelper/configurationhelper.hxx>
59 #include <vcl/window.hxx>
60 #include <vcl/syswin.hxx>
62 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
63 #include <toolkit/unohlp.hxx>
65 #include <vcl/svapp.hxx>
66 #include <vcl/wrkwin.hxx>
68 //_________________________________________________________________________________________________________________
73 //_________________________________________________________________________________________________________________
76 //*****************************************************************************************************************
77 // XInterface, XTypeProvider
79 DEFINE_XINTERFACE_4(PersistentWindowState
,
81 DIRECT_INTERFACE (css::lang::XTypeProvider
),
82 DIRECT_INTERFACE (css::lang::XInitialization
),
83 DIRECT_INTERFACE (css::frame::XFrameActionListener
),
84 DERIVED_INTERFACE(css::lang::XEventListener
,css::frame::XFrameActionListener
))
86 DEFINE_XTYPEPROVIDER_4(PersistentWindowState
,
87 css::lang::XTypeProvider
,
88 css::lang::XInitialization
,
89 css::frame::XFrameActionListener
,
90 css::lang::XEventListener
)
92 //*****************************************************************************************************************
93 PersistentWindowState::PersistentWindowState(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
94 : ThreadHelpBase (&Application::GetSolarMutex())
96 , m_bWindowStateAlreadySet(sal_False
)
100 //*****************************************************************************************************************
101 PersistentWindowState::~PersistentWindowState()
105 //*****************************************************************************************************************
106 void SAL_CALL
PersistentWindowState::initialize(const css::uno::Sequence
< css::uno::Any
>& lArguments
)
107 throw(css::uno::Exception
,
108 css::uno::RuntimeException
)
111 css::uno::Reference
< css::frame::XFrame
> xFrame
;
112 if (lArguments
.getLength() < 1)
113 throw css::lang::IllegalArgumentException(
114 DECLARE_ASCII("Empty argument list!"),
115 static_cast< ::cppu::OWeakObject
* >(this),
118 lArguments
[0] >>= xFrame
;
120 throw css::lang::IllegalArgumentException(
121 DECLARE_ASCII("No valid frame specified!"),
122 static_cast< ::cppu::OWeakObject
* >(this),
125 // SAFE -> ----------------------------------
126 WriteGuard
aWriteLock(m_aLock
);
127 // hold the frame as weak reference(!) so it can die everytimes :-)
130 // <- SAFE ----------------------------------
133 xFrame
->addFrameActionListener(this);
136 //*****************************************************************************************************************
137 void SAL_CALL
PersistentWindowState::frameAction(const css::frame::FrameActionEvent
& aEvent
)
138 throw(css::uno::RuntimeException
)
140 // SAFE -> ----------------------------------
141 ReadGuard
aReadLock(m_aLock
);
142 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
143 css::uno::Reference
< css::frame::XFrame
> xFrame(m_xFrame
.get(), css::uno::UNO_QUERY
);
144 sal_Bool bRestoreWindowState
= !m_bWindowStateAlreadySet
;
146 // <- SAFE ----------------------------------
148 // frame already gone ? We hold it weak only ...
152 // no window -> no position and size available
153 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow();
157 // unknown module -> no configuration available!
158 ::rtl::OUString sModuleName
= PersistentWindowState::implst_identifyModule(xSMGR
, xFrame
);
159 if (!sModuleName
.getLength())
162 switch(aEvent
.Action
)
164 case css::frame::FrameAction_COMPONENT_ATTACHED
:
166 if (bRestoreWindowState
)
168 ::rtl::OUString sWindowState
= PersistentWindowState::implst_getWindowStateFromConfig(xSMGR
, sModuleName
);
169 PersistentWindowState::implst_setWindowStateOnWindow(xWindow
,sWindowState
);
170 // SAFE -> ----------------------------------
171 WriteGuard
aWriteLock(m_aLock
);
172 m_bWindowStateAlreadySet
= sal_True
;
174 // <- SAFE ----------------------------------
179 case css::frame::FrameAction_COMPONENT_REATTACHED
:
181 // nothing todo here, because its not allowed to change position and size
182 // of an alredy existing frame!
186 case css::frame::FrameAction_COMPONENT_DETACHING
:
188 ::rtl::OUString sWindowState
= PersistentWindowState::implst_getWindowStateFromWindow(xWindow
);
189 PersistentWindowState::implst_setWindowStateOnConfig(xSMGR
, sModuleName
, sWindowState
);
197 //*****************************************************************************************************************
198 void SAL_CALL
PersistentWindowState::disposing(const css::lang::EventObject
&)
199 throw(css::uno::RuntimeException
)
201 // nothing todo here - because we hold the frame as weak reference only
204 //*****************************************************************************************************************
205 ::rtl::OUString
PersistentWindowState::implst_identifyModule(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
,
206 const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
208 ::rtl::OUString sModuleName
;
210 css::uno::Reference
< css::frame::XModuleManager
> xModuleManager(
211 xSMGR
->createInstance(SERVICENAME_MODULEMANAGER
),
212 css::uno::UNO_QUERY_THROW
);
216 sModuleName
= xModuleManager
->identify(xFrame
);
218 catch(const css::uno::RuntimeException
& exRun
)
220 catch(const css::uno::Exception
&)
221 { sModuleName
= ::rtl::OUString(); }
226 //*****************************************************************************************************************
227 ::rtl::OUString
PersistentWindowState::implst_getWindowStateFromConfig(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
,
228 const ::rtl::OUString
& sModuleName
)
230 ::rtl::OUString sWindowState
;
232 ::rtl::OUStringBuffer
sRelPathBuf(256);
233 sRelPathBuf
.appendAscii("Office/Factories/*[\"");
234 sRelPathBuf
.append (sModuleName
);
235 sRelPathBuf
.appendAscii("\"]" );
237 ::rtl::OUString sPackage
= ::rtl::OUString::createFromAscii("org.openoffice.Setup/");
238 ::rtl::OUString sRelPath
= sRelPathBuf
.makeStringAndClear();
239 ::rtl::OUString sKey
= ::rtl::OUString::createFromAscii("ooSetupFactoryWindowAttributes");
243 ::comphelper::ConfigurationHelper::readDirectKey(xSMGR
,
247 ::comphelper::ConfigurationHelper::E_READONLY
) >>= sWindowState
;
249 catch(const css::uno::RuntimeException
& exRun
)
251 catch(const css::uno::Exception
&)
252 { sWindowState
= ::rtl::OUString(); }
257 //*****************************************************************************************************************
258 void PersistentWindowState::implst_setWindowStateOnConfig(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
,
259 const ::rtl::OUString
& sModuleName
,
260 const ::rtl::OUString
& sWindowState
)
262 ::rtl::OUStringBuffer
sRelPathBuf(256);
263 sRelPathBuf
.appendAscii("Office/Factories/*[\"");
264 sRelPathBuf
.append (sModuleName
);
265 sRelPathBuf
.appendAscii("\"]" );
267 ::rtl::OUString sPackage
= ::rtl::OUString::createFromAscii("org.openoffice.Setup/");
268 ::rtl::OUString sRelPath
= sRelPathBuf
.makeStringAndClear();
269 ::rtl::OUString sKey
= ::rtl::OUString::createFromAscii("ooSetupFactoryWindowAttributes");
273 ::comphelper::ConfigurationHelper::writeDirectKey(xSMGR
,
277 css::uno::makeAny(sWindowState
),
278 ::comphelper::ConfigurationHelper::E_STANDARD
);
280 catch(const css::uno::RuntimeException
& exRun
)
282 catch(const css::uno::Exception
&)
286 //*****************************************************************************************************************
287 ::rtl::OUString
PersistentWindowState::implst_getWindowStateFromWindow(const css::uno::Reference
< css::awt::XWindow
>& xWindow
)
289 ::rtl::OUString sWindowState
;
293 // SOLAR SAFE -> ------------------------
294 ::vos::OClearableGuard
aSolarLock(Application::GetSolarMutex());
296 Window
* pWindow
= VCLUnoHelper::GetWindow(xWindow
);
297 // check for system window is neccessary to guarantee correct pointer cast!
300 (pWindow
->IsSystemWindow())
303 ULONG nMask
= WINDOWSTATE_MASK_ALL
;
304 nMask
&= ~(WINDOWSTATE_MASK_MINIMIZED
);
305 sWindowState
= B2U_ENC(
306 ((SystemWindow
*)pWindow
)->GetWindowState(nMask
),
307 RTL_TEXTENCODING_UTF8
);
311 // <- SOLAR SAFE ------------------------
318 //*********************************************************************************************************
319 void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference
< css::awt::XWindow
>& xWindow
,
320 const ::rtl::OUString
& sWindowState
)
324 ( sWindowState
.getLength() < 1)
328 // SOLAR SAFE -> ------------------------
329 ::vos::OClearableGuard
aSolarLock(Application::GetSolarMutex());
331 Window
* pWindow
= VCLUnoHelper::GetWindow(xWindow
);
335 // check for system and work window - its neccessary to guarantee correct pointer cast!
336 sal_Bool bSystemWindow
= pWindow
->IsSystemWindow();
337 sal_Bool bWorkWindow
= (pWindow
->GetType() == WINDOW_WORKWINDOW
);
339 if (!bSystemWindow
&& !bWorkWindow
)
342 SystemWindow
* pSystemWindow
= (SystemWindow
*)pWindow
;
343 WorkWindow
* pWorkWindow
= (WorkWindow
* )pWindow
;
345 // dont save this special state!
346 if (pWorkWindow
->IsMinimized())
349 pSystemWindow
->SetWindowState(U2B_ENC(sWindowState
,RTL_TEXTENCODING_UTF8
));
352 // <- SOLAR SAFE ------------------------
355 } // namespace framework