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