merge the formfield patch from ooo-build
[ooovba.git] / framework / source / helper / persistentwindowstate.cxx
blobde7fa59eeae69d149950ca8d259725a42adf6c45
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: persistentwindowstate.cxx,v $
10 * $Revision: 1.15 $
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 //_________________________________________________________________________________________________________________
35 // my own includes
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>
42 #include <services.h>
44 //_________________________________________________________________________________________________________________
45 // interface includes
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>
51 #endif
52 #include <com/sun/star/lang/IllegalArgumentException.hpp>
53 #include <com/sun/star/frame/XModuleManager.hpp>
55 //_________________________________________________________________________________________________________________
56 // other includes
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>
64 #endif
65 #include <vcl/svapp.hxx>
66 #include <vcl/wrkwin.hxx>
68 //_________________________________________________________________________________________________________________
69 // namespace
71 namespace framework{
73 //_________________________________________________________________________________________________________________
74 // definitions
76 //*****************************************************************************************************************
77 // XInterface, XTypeProvider
79 DEFINE_XINTERFACE_4(PersistentWindowState ,
80 OWeakObject ,
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())
95 , m_xSMGR (xSMGR )
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)
110 // check arguments
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;
119 if (!xFrame.is())
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 :-)
128 m_xFrame = xFrame;
129 aWriteLock.unlock();
130 // <- SAFE ----------------------------------
132 // start listening
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;
145 aReadLock.unlock();
146 // <- SAFE ----------------------------------
148 // frame already gone ? We hold it weak only ...
149 if (!xFrame.is())
150 return;
152 // no window -> no position and size available
153 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
154 if (!xWindow.is())
155 return;
157 // unknown module -> no configuration available!
158 ::rtl::OUString sModuleName = PersistentWindowState::implst_identifyModule(xSMGR, xFrame);
159 if (!sModuleName.getLength())
160 return;
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;
173 aWriteLock.unlock();
174 // <- SAFE ----------------------------------
177 break;
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!
184 break;
186 case css::frame::FrameAction_COMPONENT_DETACHING :
188 ::rtl::OUString sWindowState = PersistentWindowState::implst_getWindowStateFromWindow(xWindow);
189 PersistentWindowState::implst_setWindowStateOnConfig(xSMGR, sModuleName, sWindowState);
191 break;
192 default:
193 break;
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)
219 { throw exRun; }
220 catch(const css::uno::Exception&)
221 { sModuleName = ::rtl::OUString(); }
223 return sModuleName;
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,
244 sPackage,
245 sRelPath,
246 sKey,
247 ::comphelper::ConfigurationHelper::E_READONLY) >>= sWindowState;
249 catch(const css::uno::RuntimeException& exRun)
250 { throw exRun; }
251 catch(const css::uno::Exception&)
252 { sWindowState = ::rtl::OUString(); }
254 return sWindowState;
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,
274 sPackage,
275 sRelPath,
276 sKey,
277 css::uno::makeAny(sWindowState),
278 ::comphelper::ConfigurationHelper::E_STANDARD);
280 catch(const css::uno::RuntimeException& exRun)
281 { throw 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;
291 if (xWindow.is())
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!
298 if (
299 (pWindow ) &&
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);
310 aSolarLock.clear();
311 // <- SOLAR SAFE ------------------------
314 return sWindowState;
318 //*********************************************************************************************************
319 void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference< css::awt::XWindow >& xWindow ,
320 const ::rtl::OUString& sWindowState)
322 if (
323 (!xWindow.is() ) ||
324 ( sWindowState.getLength() < 1)
326 return;
328 // SOLAR SAFE -> ------------------------
329 ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
331 Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
332 if (!pWindow)
333 return;
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)
340 return;
342 SystemWindow* pSystemWindow = (SystemWindow*)pWindow;
343 WorkWindow* pWorkWindow = (WorkWindow* )pWindow;
345 // dont save this special state!
346 if (pWorkWindow->IsMinimized())
347 return;
349 pSystemWindow->SetWindowState(U2B_ENC(sWindowState,RTL_TEXTENCODING_UTF8));
351 aSolarLock.clear();
352 // <- SOLAR SAFE ------------------------
355 } // namespace framework