merged tag ooo/OOO330_m14
[LibreOffice.git] / framework / source / helper / persistentwindowstate.cxx
blobee0fceeebc7a890e76e2b7e97d5cbc7113d7f9c9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
31 //_________________________________________________________________________________________________________________
32 // my own includes
33 //_________________________________________________________________________________________________________________
34 #include <pattern/window.hxx>
35 #include <helper/persistentwindowstate.hxx>
36 #include <threadhelp/writeguard.hxx>
37 #include <threadhelp/readguard.hxx>
38 #include <macros/generic.hxx>
39 #include <services.h>
41 //_________________________________________________________________________________________________________________
42 // interface includes
43 //_________________________________________________________________________________________________________________
44 #include <com/sun/star/awt/XWindow.hpp>
46 #ifndef _COM_SUN_STAR_LANG_XSERVICXEINFO_HPP_
47 #include <com/sun/star/lang/XServiceInfo.hpp>
48 #endif
49 #include <com/sun/star/lang/IllegalArgumentException.hpp>
50 #include <com/sun/star/frame/XModuleManager.hpp>
52 //_________________________________________________________________________________________________________________
53 // other includes
54 //_________________________________________________________________________________________________________________
55 #include <comphelper/configurationhelper.hxx>
56 #include <vcl/window.hxx>
57 #include <vcl/syswin.hxx>
59 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
60 #include <toolkit/unohlp.hxx>
61 #endif
62 #include <vcl/svapp.hxx>
63 #include <vcl/wrkwin.hxx>
65 //_________________________________________________________________________________________________________________
66 // namespace
68 namespace framework{
70 //_________________________________________________________________________________________________________________
71 // definitions
73 //*****************************************************************************************************************
74 // XInterface, XTypeProvider
76 DEFINE_XINTERFACE_4(PersistentWindowState ,
77 OWeakObject ,
78 DIRECT_INTERFACE (css::lang::XTypeProvider ),
79 DIRECT_INTERFACE (css::lang::XInitialization ),
80 DIRECT_INTERFACE (css::frame::XFrameActionListener ),
81 DERIVED_INTERFACE(css::lang::XEventListener,css::frame::XFrameActionListener))
83 DEFINE_XTYPEPROVIDER_4(PersistentWindowState ,
84 css::lang::XTypeProvider ,
85 css::lang::XInitialization ,
86 css::frame::XFrameActionListener,
87 css::lang::XEventListener )
89 //*****************************************************************************************************************
90 PersistentWindowState::PersistentWindowState(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
91 : ThreadHelpBase (&Application::GetSolarMutex())
92 , m_xSMGR (xSMGR )
93 , m_bWindowStateAlreadySet(sal_False )
97 //*****************************************************************************************************************
98 PersistentWindowState::~PersistentWindowState()
102 //*****************************************************************************************************************
103 void SAL_CALL PersistentWindowState::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
104 throw(css::uno::Exception ,
105 css::uno::RuntimeException)
107 // check arguments
108 css::uno::Reference< css::frame::XFrame > xFrame;
109 if (lArguments.getLength() < 1)
110 throw css::lang::IllegalArgumentException(
111 DECLARE_ASCII("Empty argument list!"),
112 static_cast< ::cppu::OWeakObject* >(this),
115 lArguments[0] >>= xFrame;
116 if (!xFrame.is())
117 throw css::lang::IllegalArgumentException(
118 DECLARE_ASCII("No valid frame specified!"),
119 static_cast< ::cppu::OWeakObject* >(this),
122 // SAFE -> ----------------------------------
123 WriteGuard aWriteLock(m_aLock);
124 // hold the frame as weak reference(!) so it can die everytimes :-)
125 m_xFrame = xFrame;
126 aWriteLock.unlock();
127 // <- SAFE ----------------------------------
129 // start listening
130 xFrame->addFrameActionListener(this);
133 //*****************************************************************************************************************
134 void SAL_CALL PersistentWindowState::frameAction(const css::frame::FrameActionEvent& aEvent)
135 throw(css::uno::RuntimeException)
137 // SAFE -> ----------------------------------
138 ReadGuard aReadLock(m_aLock);
139 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR ;
140 css::uno::Reference< css::frame::XFrame > xFrame(m_xFrame.get(), css::uno::UNO_QUERY);
141 sal_Bool bRestoreWindowState = !m_bWindowStateAlreadySet;
142 aReadLock.unlock();
143 // <- SAFE ----------------------------------
145 // frame already gone ? We hold it weak only ...
146 if (!xFrame.is())
147 return;
149 // no window -> no position and size available
150 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
151 if (!xWindow.is())
152 return;
154 // unknown module -> no configuration available!
155 ::rtl::OUString sModuleName = PersistentWindowState::implst_identifyModule(xSMGR, xFrame);
156 if (!sModuleName.getLength())
157 return;
159 switch(aEvent.Action)
161 case css::frame::FrameAction_COMPONENT_ATTACHED :
163 if (bRestoreWindowState)
165 ::rtl::OUString sWindowState = PersistentWindowState::implst_getWindowStateFromConfig(xSMGR, sModuleName);
166 PersistentWindowState::implst_setWindowStateOnWindow(xWindow,sWindowState);
167 // SAFE -> ----------------------------------
168 WriteGuard aWriteLock(m_aLock);
169 m_bWindowStateAlreadySet = sal_True;
170 aWriteLock.unlock();
171 // <- SAFE ----------------------------------
174 break;
176 case css::frame::FrameAction_COMPONENT_REATTACHED :
178 // nothing todo here, because its not allowed to change position and size
179 // of an alredy existing frame!
181 break;
183 case css::frame::FrameAction_COMPONENT_DETACHING :
185 ::rtl::OUString sWindowState = PersistentWindowState::implst_getWindowStateFromWindow(xWindow);
186 PersistentWindowState::implst_setWindowStateOnConfig(xSMGR, sModuleName, sWindowState);
188 break;
189 default:
190 break;
194 //*****************************************************************************************************************
195 void SAL_CALL PersistentWindowState::disposing(const css::lang::EventObject&)
196 throw(css::uno::RuntimeException)
198 // nothing todo here - because we hold the frame as weak reference only
201 //*****************************************************************************************************************
202 ::rtl::OUString PersistentWindowState::implst_identifyModule(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
203 const css::uno::Reference< css::frame::XFrame >& xFrame)
205 ::rtl::OUString sModuleName;
207 css::uno::Reference< css::frame::XModuleManager > xModuleManager(
208 xSMGR->createInstance(SERVICENAME_MODULEMANAGER),
209 css::uno::UNO_QUERY_THROW);
213 sModuleName = xModuleManager->identify(xFrame);
215 catch(const css::uno::RuntimeException& exRun)
216 { throw exRun; }
217 catch(const css::uno::Exception&)
218 { sModuleName = ::rtl::OUString(); }
220 return sModuleName;
223 //*****************************************************************************************************************
224 ::rtl::OUString PersistentWindowState::implst_getWindowStateFromConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
225 const ::rtl::OUString& sModuleName)
227 ::rtl::OUString sWindowState;
229 ::rtl::OUStringBuffer sRelPathBuf(256);
230 sRelPathBuf.appendAscii("Office/Factories/*[\"");
231 sRelPathBuf.append (sModuleName );
232 sRelPathBuf.appendAscii("\"]" );
234 ::rtl::OUString sPackage = ::rtl::OUString::createFromAscii("org.openoffice.Setup/");
235 ::rtl::OUString sRelPath = sRelPathBuf.makeStringAndClear();
236 ::rtl::OUString sKey = ::rtl::OUString::createFromAscii("ooSetupFactoryWindowAttributes");
240 ::comphelper::ConfigurationHelper::readDirectKey(xSMGR,
241 sPackage,
242 sRelPath,
243 sKey,
244 ::comphelper::ConfigurationHelper::E_READONLY) >>= sWindowState;
246 catch(const css::uno::RuntimeException& exRun)
247 { throw exRun; }
248 catch(const css::uno::Exception&)
249 { sWindowState = ::rtl::OUString(); }
251 return sWindowState;
254 //*****************************************************************************************************************
255 void PersistentWindowState::implst_setWindowStateOnConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
256 const ::rtl::OUString& sModuleName ,
257 const ::rtl::OUString& sWindowState)
259 ::rtl::OUStringBuffer sRelPathBuf(256);
260 sRelPathBuf.appendAscii("Office/Factories/*[\"");
261 sRelPathBuf.append (sModuleName );
262 sRelPathBuf.appendAscii("\"]" );
264 ::rtl::OUString sPackage = ::rtl::OUString::createFromAscii("org.openoffice.Setup/");
265 ::rtl::OUString sRelPath = sRelPathBuf.makeStringAndClear();
266 ::rtl::OUString sKey = ::rtl::OUString::createFromAscii("ooSetupFactoryWindowAttributes");
270 ::comphelper::ConfigurationHelper::writeDirectKey(xSMGR,
271 sPackage,
272 sRelPath,
273 sKey,
274 css::uno::makeAny(sWindowState),
275 ::comphelper::ConfigurationHelper::E_STANDARD);
277 catch(const css::uno::RuntimeException& exRun)
278 { throw exRun; }
279 catch(const css::uno::Exception&)
283 //*****************************************************************************************************************
284 ::rtl::OUString PersistentWindowState::implst_getWindowStateFromWindow(const css::uno::Reference< css::awt::XWindow >& xWindow)
286 ::rtl::OUString sWindowState;
288 if (xWindow.is())
290 // SOLAR SAFE -> ------------------------
291 ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
293 Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
294 // check for system window is neccessary to guarantee correct pointer cast!
295 if (
296 (pWindow ) &&
297 (pWindow->IsSystemWindow())
300 ULONG nMask = WINDOWSTATE_MASK_ALL;
301 nMask &= ~(WINDOWSTATE_MASK_MINIMIZED);
302 sWindowState = B2U_ENC(
303 ((SystemWindow*)pWindow)->GetWindowState(nMask),
304 RTL_TEXTENCODING_UTF8);
307 aSolarLock.clear();
308 // <- SOLAR SAFE ------------------------
311 return sWindowState;
315 //*********************************************************************************************************
316 void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference< css::awt::XWindow >& xWindow ,
317 const ::rtl::OUString& sWindowState)
319 if (
320 (!xWindow.is() ) ||
321 ( sWindowState.getLength() < 1)
323 return;
325 // SOLAR SAFE -> ------------------------
326 ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
328 Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
329 if (!pWindow)
330 return;
332 // check for system and work window - its neccessary to guarantee correct pointer cast!
333 sal_Bool bSystemWindow = pWindow->IsSystemWindow();
334 sal_Bool bWorkWindow = (pWindow->GetType() == WINDOW_WORKWINDOW);
336 if (!bSystemWindow && !bWorkWindow)
337 return;
339 SystemWindow* pSystemWindow = (SystemWindow*)pWindow;
340 WorkWindow* pWorkWindow = (WorkWindow* )pWindow;
342 // dont save this special state!
343 if (pWorkWindow->IsMinimized())
344 return;
346 pSystemWindow->SetWindowState(U2B_ENC(sWindowState,RTL_TEXTENCODING_UTF8));
348 aSolarLock.clear();
349 // <- SOLAR SAFE ------------------------
352 } // namespace framework