/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/drawing/framework/XControllerManager.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/frame/XDispatchProvider.hpp>
#include <com/sun/star/util/URL.hpp>

#include <cppuhelper/bootstrap.hxx>
#include <cppuhelper/supportsservice.hxx>

#include <osl/mutex.hxx>

#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <svx/svdpool.hxx>
#include <svl/itemprop.hxx>

#include <sfx2/viewfrm.hxx>

#include <toolkit/helper/vclunohelper.hxx>
#include <svx/unoprov.hxx>

#include "framework/FrameworkHelper.hxx"

#include "FrameView.hxx"
#include "createpresentation.hxx"
#include "unomodel.hxx"
#include "slideshow.hxx"
#include "slideshowimpl.hxx"
#include "sdattr.hrc"
#include "FactoryIds.hxx"
#include "ViewShell.hxx"
#include "SlideShowRestarter.hxx"
#include "DrawController.hxx"
#include "customshowlist.hxx"
#include <boost/bind.hpp>

using ::com::sun::star::presentation::XSlideShowController;
using ::com::sun::star::container::XIndexAccess;
using ::sd::framework::FrameworkHelper;
using ::com::sun::star::awt::XWindow;
using namespace ::sd;
using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::drawing;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::animations;
using namespace ::com::sun::star::drawing::framework;

extern OUString getUiNameFromPageApiNameImpl( const OUString& rApiName );


namespace {
    /** This local version of the work window overloads DataChanged() so that it
        can restart the slide show when a display is added or removed.
    */
    class FullScreenWorkWindow : public WorkWindow
    {
    public:
        FullScreenWorkWindow (
            const ::rtl::Reference<SlideShow>& rpSlideShow,
            ViewShellBase* pViewShellBase)
            : WorkWindow(NULL, WB_HIDE | WB_CLIPCHILDREN),
              mpRestarter(new SlideShowRestarter(rpSlideShow, pViewShellBase))
        {}

        void Restart(bool bForce)
        {
            mpRestarter->Restart(bForce);
        }

        virtual void DataChanged (const DataChangedEvent& rEvent) SAL_OVERRIDE
        {
            if (rEvent.GetType() == DATACHANGED_DISPLAY)
                Restart(false);
        }

    private:
        ::boost::shared_ptr<SlideShowRestarter> mpRestarter;
    };
}





const SfxItemPropertyMapEntry* ImplGetPresentationPropertyMap()
{
    // NOTE: First member must be sorted
    static const SfxItemPropertyMapEntry aPresentationPropertyMap_Impl[] =
    {
        { OUString("AllowAnimations"),          ATTR_PRESENT_ANIMATION_ALLOWED, ::getBooleanCppuType(),                0, 0 },
        { OUString("CustomShow"),               ATTR_PRESENT_CUSTOMSHOW,        ::cppu::UnoType<OUString>::get(),     0, 0 },
        { OUString("Display"),                  ATTR_PRESENT_DISPLAY,           ::cppu::UnoType<sal_Int32>::get(),    0, 0 },
        { OUString("FirstPage"),                ATTR_PRESENT_DIANAME,           ::cppu::UnoType<OUString>::get(),     0, 0 },
        { OUString("IsAlwaysOnTop"),            ATTR_PRESENT_ALWAYS_ON_TOP,     ::getBooleanCppuType(),                0, 0 },
        { OUString("IsAutomatic"),              ATTR_PRESENT_MANUEL,            ::getBooleanCppuType(),                0, 0 },
        { OUString("IsEndless"),                ATTR_PRESENT_ENDLESS,           ::getBooleanCppuType(),                0, 0 },
        { OUString("IsFullScreen"),             ATTR_PRESENT_FULLSCREEN,        ::getBooleanCppuType(),                0, 0 },
        { OUString("IsShowAll"),                ATTR_PRESENT_ALL,               ::getBooleanCppuType(),                0, 0 },
        { OUString("IsMouseVisible"),           ATTR_PRESENT_MOUSE,             ::getBooleanCppuType(),                0, 0 },
        { OUString("IsShowLogo"),               ATTR_PRESENT_SHOW_PAUSELOGO,    ::getBooleanCppuType(),                0, 0 },
        { OUString("IsTransitionOnClick"),      ATTR_PRESENT_CHANGE_PAGE,       ::getBooleanCppuType(),                0, 0 },
        { OUString("Pause"),                    ATTR_PRESENT_PAUSE_TIMEOUT,     ::cppu::UnoType<sal_Int32>::get(),    0, 0 },
        { OUString("StartWithNavigator"),       ATTR_PRESENT_NAVIGATOR,         ::getBooleanCppuType(),                0, 0 },
        { OUString("UsePen"),                   ATTR_PRESENT_PEN,               ::getBooleanCppuType(),                0, 0 },
        { OUString(), 0, css::uno::Type(), 0, 0 }
    };

    return aPresentationPropertyMap_Impl;
}


// class SlideShow


SlideShow::SlideShow( SdDrawDocument* pDoc )
: SlideshowBase( m_aMutex )
, maPropSet(ImplGetPresentationPropertyMap(), SdrObject::GetGlobalDrawObjectItemPool())
, mbIsInStartup(false)
, mpDoc( pDoc )
, mpCurrentViewShellBase( 0 )
, mpFullScreenViewShellBase( 0 )
, mpFullScreenFrameView( 0 )
, mnInPlaceConfigEvent( 0 )
{
}

void SlideShow::ThrowIfDisposed() const throw (RuntimeException)
{
    if( mpDoc == 0 )
        throw DisposedException();
}

/// used by the model to create a slideshow for it
rtl::Reference< SlideShow > SlideShow::Create( SdDrawDocument* pDoc )
{
    return new SlideShow( pDoc );
}



rtl::Reference< SlideShow > SlideShow::GetSlideShow( SdDrawDocument* pDocument )
{
    rtl::Reference< SlideShow > xRet;

    if( pDocument )
        xRet = GetSlideShow( *pDocument );

    return xRet;
}



rtl::Reference< SlideShow > SlideShow::GetSlideShow( SdDrawDocument& rDocument )
{
    return rtl::Reference< SlideShow >(
        dynamic_cast< SlideShow* >( rDocument.getPresentation().get() ) );
}



rtl::Reference< SlideShow > SlideShow::GetSlideShow( ViewShellBase& rBase )
{
    return GetSlideShow( rBase.GetDocument() );
}



::com::sun::star::uno::Reference< ::com::sun::star::presentation::XSlideShowController > SlideShow::GetSlideShowController(ViewShellBase& rBase )
{
    rtl::Reference< SlideShow > xSlideShow( GetSlideShow( rBase ) );

    Reference< XSlideShowController > xRet;
    if( xSlideShow.is() )
        xRet = xSlideShow->getController();

    return xRet;
}



bool SlideShow::StartPreview( ViewShellBase& rBase,
    const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& xDrawPage,
    const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xAnimationNode,
    ::Window* pParent /* = 0 */ )
{
    rtl::Reference< SlideShow > xSlideShow( GetSlideShow( rBase ) );
    if( xSlideShow.is() )
        return xSlideShow->startPreview( xDrawPage, xAnimationNode, pParent );

    return false;
}



void SlideShow::Stop( ViewShellBase& rBase )
{
    rtl::Reference< SlideShow > xSlideShow( GetSlideShow( rBase ) );
    if( xSlideShow.is() )
        xSlideShow->end();
}



bool SlideShow::IsRunning( ViewShellBase& rBase )
{
    rtl::Reference< SlideShow > xSlideShow( GetSlideShow( rBase ) );
    return xSlideShow.is() && xSlideShow->isRunning();
}



bool SlideShow::IsRunning( ViewShell& rViewShell )
{
    rtl::Reference< SlideShow > xSlideShow( GetSlideShow( rViewShell.GetViewShellBase() ) );
    return xSlideShow.is() && xSlideShow->isRunning() && (xSlideShow->mxController->getViewShell() == &rViewShell);
}



void SlideShow::CreateController(  ViewShell* pViewSh, ::sd::View* pView, ::Window* pParentWindow )
{
    DBG_ASSERT( !mxController.is(), "sd::SlideShow::CreateController(), clean up old controller first!" );

    Reference< XPresentation2 > xThis( this );

    rtl::Reference<SlideshowImpl> xController (
        new SlideshowImpl(xThis, pViewSh, pView, mpDoc, pParentWindow));

    // Reset mbIsInStartup.  From here mxController.is() is used to prevent
    // multiple slide show instances for one document.
    mxController = xController;
    mbIsInStartup = false;

}

// XServiceInfo
OUString SAL_CALL SlideShow::getImplementationName(  ) throw(RuntimeException, std::exception)
{
    return OUString( "com.sun.star.comp.sd.SlideShow" );
}

sal_Bool SAL_CALL SlideShow::supportsService( const OUString& ServiceName ) throw(RuntimeException, std::exception)
{
    return cppu::supportsService( this, ServiceName );
}

Sequence< OUString > SAL_CALL SlideShow::getSupportedServiceNames(  ) throw(RuntimeException, std::exception)
{
    OUString aService( "com.sun.star.presentation.Presentation" );
    Sequence< OUString > aSeq( &aService, 1 );
    return aSeq;
}

// XPropertySet
Reference< XPropertySetInfo > SAL_CALL SlideShow::getPropertySetInfo() throw(RuntimeException, std::exception)
{
    SolarMutexGuard aGuard;
    static Reference< XPropertySetInfo > xInfo = maPropSet.getPropertySetInfo();
    return xInfo;
 }

void SAL_CALL SlideShow::setPropertyValue( const OUString& aPropertyName, const Any& aValue ) throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
{
    SolarMutexGuard aGuard;
    ThrowIfDisposed();

    sd::PresentationSettings& rPresSettings = mpDoc->getPresentationSettings();

    const SfxItemPropertySimpleEntry* pEntry = maPropSet.getPropertyMapEntry(aPropertyName);

    if( pEntry && ((pEntry->nFlags & PropertyAttribute::READONLY) != 0) )
        throw PropertyVetoException();

    bool bValuesChanged = false;
    bool bIllegalArgument = true;

    switch( pEntry ? pEntry->nWID : -1 )
    {
    case ATTR_PRESENT_ALL:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if( rPresSettings.mbAll != bVal )
            {
                rPresSettings.mbAll = bVal;
                bValuesChanged = true;
                if( bVal )
                    rPresSettings.mbCustomShow = false;
            }
        }
        break;
    }
    case ATTR_PRESENT_CHANGE_PAGE:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if( bVal == rPresSettings.mbLockedPages )
            {
                bValuesChanged = true;
                rPresSettings.mbLockedPages = !bVal;
            }
        }
        break;
    }

    case ATTR_PRESENT_ANIMATION_ALLOWED:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if(rPresSettings.mbAnimationAllowed != bVal)
            {
                bValuesChanged = true;
                rPresSettings.mbAnimationAllowed = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_CUSTOMSHOW:
    {
        OUString aShow;
        if( aValue >>= aShow )
        {
            bIllegalArgument = false;

            const OUString aShowName( aShow );

            SdCustomShowList* pCustomShowList = mpDoc->GetCustomShowList(false);
            if(pCustomShowList)
            {
                SdCustomShow* pCustomShow;
                for( pCustomShow = pCustomShowList->First(); pCustomShow != NULL; pCustomShow = pCustomShowList->Next() )
                {
                    if( pCustomShow->GetName() == aShowName )
                        break;
                }

                rPresSettings.mbCustomShow = true;
                bValuesChanged = true;
            }
        }
        break;
    }
    case ATTR_PRESENT_ENDLESS:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if( rPresSettings.mbEndless != bVal)
            {
                bValuesChanged = true;
                rPresSettings.mbEndless = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_FULLSCREEN:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;
            if( rPresSettings.mbFullScreen != bVal)
            {
                bValuesChanged = true;
                rPresSettings.mbFullScreen = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_DIANAME:
    {
        OUString aPresPage;
        aValue >>= aPresPage;
        bIllegalArgument = false;
        if( (rPresSettings.maPresPage != aPresPage) || !rPresSettings.mbCustomShow || !rPresSettings.mbAll )
        {
            bValuesChanged = true;
            rPresSettings.maPresPage = getUiNameFromPageApiNameImpl(aPresPage);
            rPresSettings.mbCustomShow = false;
            rPresSettings.mbAll = false;
        }
        break;
    }
    case ATTR_PRESENT_MANUEL:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if( rPresSettings.mbManual != bVal)
            {
                bValuesChanged = true;
                rPresSettings.mbManual = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_MOUSE:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;
            if( rPresSettings.mbMouseVisible != bVal)
            {
                bValuesChanged = true;
                rPresSettings.mbMouseVisible = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_ALWAYS_ON_TOP:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if( rPresSettings.mbAlwaysOnTop != bVal)
            {
                bValuesChanged = true;
                rPresSettings.mbAlwaysOnTop = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_NAVIGATOR:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if( rPresSettings.mbStartWithNavigator != bVal)
            {
                bValuesChanged = true;
                rPresSettings.mbStartWithNavigator = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_PEN:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if(rPresSettings.mbMouseAsPen != bVal)
            {
                bValuesChanged = true;
                rPresSettings.mbMouseAsPen = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_PAUSE_TIMEOUT:
    {
        sal_Int32 nValue = 0;
        if( (aValue >>= nValue) && (nValue >= 0) )
        {
            bIllegalArgument = false;
            if( rPresSettings.mnPauseTimeout != nValue )
            {
                bValuesChanged = true;
                rPresSettings.mnPauseTimeout = nValue;
            }
        }
        break;
    }
    case ATTR_PRESENT_SHOW_PAUSELOGO:
    {
        bool bVal = false;

        if( aValue >>= bVal )
        {
            bIllegalArgument = false;

            if( rPresSettings.mbShowPauseLogo != bVal )
            {
                bValuesChanged = true;
                rPresSettings.mbShowPauseLogo = bVal;
            }
        }
        break;
    }
    case ATTR_PRESENT_DISPLAY:
    {
        sal_Int32 nDisplay = 0;
        if( aValue >>= nDisplay )
        {
            bIllegalArgument = false;

            SdOptions* pOptions = SD_MOD()->GetSdOptions(DOCUMENT_TYPE_IMPRESS);
            pOptions->SetDisplay( nDisplay );

            FullScreenWorkWindow *pWin = dynamic_cast<FullScreenWorkWindow *>(GetWorkWindow());
            if( !pWin )
                return;
            pWin->Restart(true);
        }
        break;
    }

    default:
        throw UnknownPropertyException();
    }

    if( bIllegalArgument )
        throw IllegalArgumentException();

    if( bValuesChanged )
        mpDoc->SetChanged( true );
}



Any SAL_CALL SlideShow::getPropertyValue( const OUString& PropertyName ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
{
    SolarMutexGuard aGuard;
    ThrowIfDisposed();

    const sd::PresentationSettings& rPresSettings = mpDoc->getPresentationSettings();

    const SfxItemPropertySimpleEntry* pEntry = maPropSet.getPropertyMapEntry(PropertyName);

    switch( pEntry ? pEntry->nWID : -1 )
    {
    case ATTR_PRESENT_ALL:
        return Any( !rPresSettings.mbCustomShow && rPresSettings.mbAll );
    case ATTR_PRESENT_CHANGE_PAGE:
        return Any( !rPresSettings.mbLockedPages );
    case ATTR_PRESENT_ANIMATION_ALLOWED:
        return Any( rPresSettings.mbAnimationAllowed );
    case ATTR_PRESENT_CUSTOMSHOW:
        {
            SdCustomShowList* pList = mpDoc->GetCustomShowList(false);
            SdCustomShow* pShow = (pList && rPresSettings.mbCustomShow) ? pList->GetCurObject() : NULL;
            OUString aShowName;

            if(pShow)
                aShowName = pShow->GetName();

            return Any( aShowName );
        }
    case ATTR_PRESENT_ENDLESS:
        return Any( rPresSettings.mbEndless );
    case ATTR_PRESENT_FULLSCREEN:
        return Any( rPresSettings.mbFullScreen );
    case ATTR_PRESENT_DIANAME:
        {
            OUString aSlideName;

            if( !rPresSettings.mbCustomShow && !rPresSettings.mbAll )
                aSlideName = getPageApiNameFromUiName( rPresSettings.maPresPage );

            return Any( aSlideName );
        }
    case ATTR_PRESENT_MANUEL:
        return Any( rPresSettings.mbManual );
    case ATTR_PRESENT_MOUSE:
        return Any( rPresSettings.mbMouseVisible );
    case ATTR_PRESENT_ALWAYS_ON_TOP:
        return Any( rPresSettings.mbAlwaysOnTop );
    case ATTR_PRESENT_NAVIGATOR:
        return Any( rPresSettings.mbStartWithNavigator );
    case ATTR_PRESENT_PEN:
        return Any( rPresSettings.mbMouseAsPen );
    case ATTR_PRESENT_PAUSE_TIMEOUT:
        return Any( rPresSettings.mnPauseTimeout );
    case ATTR_PRESENT_SHOW_PAUSELOGO:
        return Any( rPresSettings.mbShowPauseLogo );
    case ATTR_PRESENT_DISPLAY:
    {
        SdOptions* pOptions = SD_MOD()->GetSdOptions(DOCUMENT_TYPE_IMPRESS);
        return Any(pOptions->GetDisplay());
    }

    default:
        throw UnknownPropertyException();
    }
}



void SAL_CALL SlideShow::addPropertyChangeListener( const OUString& , const Reference< XPropertyChangeListener >&  ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
{
}



void SAL_CALL SlideShow::removePropertyChangeListener( const OUString& , const Reference< XPropertyChangeListener >&  ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
{
}



void SAL_CALL SlideShow::addVetoableChangeListener( const OUString& , const Reference< XVetoableChangeListener >&  ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
{
}



void SAL_CALL SlideShow::removeVetoableChangeListener( const OUString& , const Reference< XVetoableChangeListener >&  ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
{
}


// XPresentation


void SAL_CALL SlideShow::start() throw(RuntimeException, std::exception)
{
    const Sequence< PropertyValue > aArguments;
    startWithArguments( aArguments );
}



WorkWindow *SlideShow::GetWorkWindow()
{
    if( !mpFullScreenViewShellBase )
        return NULL;

    PresentationViewShell* pShell = dynamic_cast<PresentationViewShell*>(mpFullScreenViewShellBase->GetMainViewShell().get());

    if( !pShell || !pShell->GetViewFrame() )
        return NULL;

    return dynamic_cast<WorkWindow*>(pShell->GetViewFrame()->GetTopFrame().GetWindow().GetParent());
}

bool SlideShow::IsExitAfterPresenting() const
{
    SolarMutexGuard aGuard;
    ThrowIfDisposed();
    return mpDoc->IsExitAfterPresenting();
}

void SlideShow::SetExitAfterPresenting(bool bExit)
{
    SolarMutexGuard aGuard;
    ThrowIfDisposed();
    mpDoc->SetExitAfterPresenting(bExit);
}

void SAL_CALL SlideShow::end()
    throw (RuntimeException, std::exception)
{
    SolarMutexGuard aGuard;

    // The mbIsInStartup flag should have been reset during the start of the
    // slide show.  Reset it here just in case that something has horribly
    // gone wrong.
    OSL_ASSERT(!mbIsInStartup);
    mbIsInStartup = false;

    rtl::Reference< SlideshowImpl > xController( mxController );
    if( xController.is() )
    {
        mxController.clear();

        if( mpFullScreenFrameView )
        {
            delete mpFullScreenFrameView;
            mpFullScreenFrameView = 0;
        }

        ViewShellBase* pFullScreenViewShellBase = mpFullScreenViewShellBase;
        mpFullScreenViewShellBase = 0;

        // dispose before fullscreen window changes screens
        // (potentially). If this needs to be moved behind
        // pWorkWindow->StartPresentationMode() again, read issue
        // pWorkWindow->i94007 & implement the solution outlined
        // there.
        xController->dispose();

        if( pFullScreenViewShellBase )
        {
            PresentationViewShell* pShell = dynamic_cast<PresentationViewShell*>(pFullScreenViewShellBase->GetMainViewShell().get());

            if( pShell && pShell->GetViewFrame() )
            {
                WorkWindow* pWorkWindow = dynamic_cast<WorkWindow*>(pShell->GetViewFrame()->GetTopFrame().GetWindow().GetParent());
                if( pWorkWindow )
                {
                    pWorkWindow->StartPresentationMode( false, isAlwaysOnTop() ? PRESENTATION_HIDEALLAPPS : 0 );
                }
            }
        }

        if( pFullScreenViewShellBase )
        {
            PresentationViewShell* pShell = NULL;
            {
                // Get the shell pointer in its own scope to be sure that
                // the shared_ptr to the shell is released before DoClose()
                // is called.
                ::boost::shared_ptr<ViewShell> pSharedView (pFullScreenViewShellBase->GetMainViewShell());
                pShell = dynamic_cast<PresentationViewShell*>(pSharedView.get());
            }
            if( pShell && pShell->GetViewFrame() )
                pShell->GetViewFrame()->DoClose();
        }
        else if( mpCurrentViewShellBase )
        {
            ViewShell* pViewShell = mpCurrentViewShellBase->GetMainViewShell().get();

            if( pViewShell )
            {
                FrameView* pFrameView = pViewShell->GetFrameView();

                if( pFrameView && (pFrameView->GetPresentationViewShellId() != SID_VIEWSHELL0) )
                {
                    ViewShell::ShellType ePreviousType (pFrameView->GetPreviousViewShellType());
                    pFrameView->SetPreviousViewShellType(ViewShell::ST_NONE);

                    pFrameView->SetPresentationViewShellId(SID_VIEWSHELL0);
                    pFrameView->SetSlotId(SID_OBJECT_SELECT);
                    pFrameView->SetPreviousViewShellType(pViewShell->GetShellType());

                    framework::FrameworkHelper::Instance(*mpCurrentViewShellBase)->RequestView(
                        framework::FrameworkHelper::GetViewURL(ePreviousType),
                        framework::FrameworkHelper::msCenterPaneURL);

                    pViewShell->GetViewFrame()->GetBindings().InvalidateAll( true );
                }
            }
        }

        if( mpCurrentViewShellBase )
        {
            ViewShell* pViewShell = mpCurrentViewShellBase->GetMainViewShell().get();
            if( pViewShell )
            {
                // invalidate the view shell so the presentation slot will be re-enabled
                // and the rehersing will be updated
                pViewShell->Invalidate();

                if( xController->meAnimationMode ==ANIMATIONMODE_SHOW )
                {
                    // switch to the previously visible Slide
                    DrawViewShell* pDrawViewShell = dynamic_cast<DrawViewShell*>( pViewShell );
                    if( pDrawViewShell )
                        pDrawViewShell->SwitchPage( (sal_uInt16)xController->getRestoreSlide() );
                    else
                    {
                        Reference<XDrawView> xDrawView (
                            Reference<XWeak>(&mpCurrentViewShellBase->GetDrawController()), UNO_QUERY);
                        if (xDrawView.is())
                            xDrawView->setCurrentPage(
                                Reference<XDrawPage>(
                                    mpDoc->GetSdPage(xController->getRestoreSlide(), PK_STANDARD)->getUnoPage(),
                                    UNO_QUERY));
                    }
                }

                if( pViewShell->GetDoc()->IsExitAfterPresenting() )
                {
                    pViewShell->GetDoc()->SetExitAfterPresenting( false );

                    Reference<frame::XDispatchProvider> xProvider(pViewShell->GetViewShellBase().GetController()->getFrame(),
                                                                  UNO_QUERY);
                    if( xProvider.is() )
                    {
                        util::URL aURL;
                        aURL.Complete = ".uno:CloseFrame";

                        uno::Reference< frame::XDispatch > xDispatch(
                            xProvider->queryDispatch(
                                aURL, OUString(), 0));
                        if( xDispatch.is() )
                        {
                            xDispatch->dispatch(aURL,
                                                uno::Sequence< beans::PropertyValue >());
                        }
                    }
                }
            }
            //Fire the acc focus event when focus is switched back. The above method mpCurrentViewShellBase->GetWindow()->GrabFocus() will
            //set focus to WorkWindow instead of the sd::window, so here call Shell's method to fire the focus event
            if (pViewShell)
                pViewShell->SwitchActiveViewFireFocus();
        }
        mpCurrentViewShellBase = 0;
    }
}



void SAL_CALL SlideShow::rehearseTimings() throw(RuntimeException, std::exception)
{
    Sequence< PropertyValue > aArguments(1);
    aArguments[0].Name = "RehearseTimings";
    aArguments[0].Value <<= sal_True;
    startWithArguments( aArguments );
}


// XPresentation2


void SAL_CALL SlideShow::startWithArguments(const Sequence< PropertyValue >& rArguments)
    throw (RuntimeException, std::exception)
{
    SolarMutexGuard aGuard;
    ThrowIfDisposed();

    // Stop a running show before starting a new one.
    if( mxController.is() )
    {
        OSL_ASSERT(!mbIsInStartup);
        end();
    }
    else if (mbIsInStartup)
    {
        // We are already somewhere in process of starting a slide show but
        // have not yet got to the point where mxController is set.  There
        // is not yet a slide show to end so return silently.
        return;
    }

    // Prevent multiple instance of the SlideShow class for one document.
    mbIsInStartup = true;

    mxCurrentSettings.reset( new PresentationSettingsEx( mpDoc->getPresentationSettings() ) );
    mxCurrentSettings->SetArguments( rArguments );

    // if there is no view shell base set, use the current one or the first using this document
    if( mpCurrentViewShellBase == 0 )
    {
        // first check current
        ::sd::ViewShellBase* pBase = ::sd::ViewShellBase::GetViewShellBase( SfxViewFrame::Current() );
        if( pBase && pBase->GetDocument() == mpDoc )
        {
            mpCurrentViewShellBase = pBase;
        }
        else
        {
            // current is not ours, so get first from ours
            mpCurrentViewShellBase = ::sd::ViewShellBase::GetViewShellBase( SfxViewFrame::GetFirst( mpDoc->GetDocSh() ) );
        }
    }

    // #i118456# make sure TextEdit changes get pushed to model.
    // mpDrawView is tested against NULL above already.
    if(mpCurrentViewShellBase)
    {
        ViewShell* pViewShell = mpCurrentViewShellBase->GetMainViewShell().get();

        if(pViewShell && pViewShell->GetView())
        {
            pViewShell->GetView()->SdrEndTextEdit();
        }
    }

    // Start either a full-screen or an in-place show.
    if(mxCurrentSettings->mbFullScreen && !mxCurrentSettings->mbPreview)
        StartFullscreenPresentation();
    else
        StartInPlacePresentation();

}



sal_Bool SAL_CALL SlideShow::isRunning(  ) throw (RuntimeException, std::exception)
{
    SolarMutexGuard aGuard;
    return mxController.is() && mxController->isRunning();
}



Reference< XSlideShowController > SAL_CALL SlideShow::getController(  ) throw (RuntimeException, std::exception)
{
    ThrowIfDisposed();

    Reference< XSlideShowController > xController( mxController.get() );
    return xController;
}


// XComponent


void SAL_CALL SlideShow::disposing (void)
{
    SolarMutexGuard aGuard;

    if( mnInPlaceConfigEvent )
    {
        Application::RemoveUserEvent( mnInPlaceConfigEvent );
        mnInPlaceConfigEvent = 0;
    }

    if( mxController.is() )
    {
        mxController->dispose();
        mxController.clear();
    }

    mpCurrentViewShellBase = 0;
    mpFullScreenViewShellBase = 0;
    mpDoc = 0;
}



bool SlideShow::startPreview( const Reference< XDrawPage >& xDrawPage, const Reference< XAnimationNode >& xAnimationNode, ::Window* pParent )
{
    Sequence< PropertyValue > aArguments(4);

    aArguments[0].Name = "Preview";
    aArguments[0].Value <<= sal_True;

    aArguments[1].Name = "FirstPage";
    aArguments[1].Value <<= xDrawPage;

    aArguments[2].Name = "AnimationNode";
    aArguments[2].Value <<= xAnimationNode;

    Reference< XWindow > xParentWindow;
    if( pParent )
        xParentWindow = VCLUnoHelper::GetInterface( pParent );

    aArguments[3].Name = "ParentWindow";
    aArguments[3].Value <<= xParentWindow;

    startWithArguments( aArguments );

    return true;
}



ShowWindow* SlideShow::getShowWindow()
{
    return mxController.is() ? mxController->mpShowWindow : 0;
}



int SlideShow::getAnimationMode()
{
    return mxController.is() ? mxController->meAnimationMode : ANIMATIONMODE_SHOW;
}



void SlideShow::jumpToPageIndex( sal_Int32 nPageIndex )
{
    if( mxController.is() )
        mxController->displaySlideIndex( nPageIndex );
}



void SlideShow::jumpToPageNumber( sal_Int32 nPageNumber )
{
    if( mxController.is() )
        mxController->displaySlideNumber( nPageNumber );
}



sal_Int32 SlideShow::getCurrentPageNumber()
{
    return mxController.is() ? mxController->getCurrentSlideNumber() : 0;
}



void SlideShow::jumpToBookmark( const OUString& sBookmark )
{
    if( mxController.is() )
        mxController->jumpToBookmark( sBookmark );
}



bool SlideShow::isFullScreen()
{
    return mxController.is() && mxController->maPresSettings.mbFullScreen;
}



void SlideShow::resize( const Size &rSize )
{
    if( mxController.is() )
        mxController->resize( rSize );
}



void SlideShow::activate( ViewShellBase& rBase )
{
    if( (mpFullScreenViewShellBase == &rBase) && !mxController.is() )
    {
        ::boost::shared_ptr<PresentationViewShell> pShell = ::boost::dynamic_pointer_cast<PresentationViewShell>(rBase.GetMainViewShell());
        if(pShell.get() != NULL)
        {
            pShell->FinishInitialization( mpFullScreenFrameView );
            mpFullScreenFrameView = 0;

            CreateController( pShell.get(), pShell->GetView(), rBase.GetViewWindow() );

            if( mxController->startShow(mxCurrentSettings.get()) )
            {
                pShell->Resize();
                // Defer the sd::ShowWindow's GrabFocus to here. so that the accessible event can be fired correctly.
                pShell->GetActiveWindow()->GrabFocus();
            }
            else
            {
                end();
                return;
            }
        }
    }

    if( mxController.is() )
        mxController->activate();

}



void SlideShow::deactivate( ViewShellBase& /*rBase*/ )
{
    mxController->deactivate();
}



bool SlideShow::keyInput(const KeyEvent& rKEvt)
{
    return mxController.is() && mxController->keyInput(rKEvt);
}



void SlideShow::paint( const Rectangle& rRect )
{
    if( mxController.is() )
        mxController->paint( rRect );
}



bool SlideShow::isAlwaysOnTop()
{
    return mxController.is() && mxController->maPresSettings.mbAlwaysOnTop;
}



bool SlideShow::pause( bool bPause )
{
    if( mxController.is() )
    {
        if( bPause )
            mxController->pause();
        else
            mxController->resume();
    }
    return true;
}



void SlideShow::receiveRequest(SfxRequest& rReq)
{
    if( mxController.is() )
        mxController->receiveRequest( rReq );
}



sal_Int32 SlideShow::getFirstPageNumber()
{
    return mxController.is() ? mxController->getFirstSlideNumber() : 0;
}



sal_Int32 SlideShow::getLastPageNumber()
{
    return mxController.is() ? mxController->getLastSlideNumber() : 0;
}



bool SlideShow::isEndless()
{
    return mxController.is() && mxController->isEndless();
}



bool SlideShow::isDrawingPossible()
{
    return mxController.is() && mxController->getUsePen();
}



void SlideShow::StartInPlacePresentationConfigurationCallback()
{
    if( mnInPlaceConfigEvent != 0 )
        Application::RemoveUserEvent( mnInPlaceConfigEvent );

    mnInPlaceConfigEvent = Application::PostUserEvent( LINK( this, SlideShow, StartInPlacePresentationConfigurationHdl ) );
}



IMPL_LINK_NOARG(SlideShow, StartInPlacePresentationConfigurationHdl)
{
    mnInPlaceConfigEvent = 0;
    StartInPlacePresentation();
    return 0;
}



void SlideShow::StartInPlacePresentation()
{
    if( mpCurrentViewShellBase )
    {
        // Save the current view shell type so that it can be restored after the
        // show has ended.  If there already is a saved shell type then that is
        // not overwritten.

        ViewShell::ShellType eShell = ViewShell::ST_NONE;

        ::boost::shared_ptr<FrameworkHelper> pHelper(FrameworkHelper::Instance(*mpCurrentViewShellBase));
        ::boost::shared_ptr<ViewShell> pMainViewShell(pHelper->GetViewShell(FrameworkHelper::msCenterPaneURL));

        if( pMainViewShell.get() )
            eShell = pMainViewShell->GetShellType();

        if( eShell != ViewShell::ST_IMPRESS )
        {
            // Switch temporary to a DrawViewShell which supports the in-place presentation.

            if( pMainViewShell.get() )
            {
                FrameView* pFrameView = pMainViewShell->GetFrameView();
                pFrameView->SetPresentationViewShellId(SID_VIEWSHELL1);
                pFrameView->SetPreviousViewShellType (pMainViewShell->GetShellType());
                pFrameView->SetPageKind (PK_STANDARD);
            }

            pHelper->RequestView( FrameworkHelper::msImpressViewURL, FrameworkHelper::msCenterPaneURL );
            pHelper->RunOnConfigurationEvent( FrameworkHelper::msConfigurationUpdateEndEvent, ::boost::bind(&SlideShow::StartInPlacePresentationConfigurationCallback, this) );
            return;
        }
        else
        {
            ::Window* pParentWindow = mxCurrentSettings->mpParentWindow;
            if( pParentWindow == 0 )
                pParentWindow = mpCurrentViewShellBase->GetViewWindow();

            CreateController( pMainViewShell.get(), pMainViewShell->GetView(), pParentWindow );
        }
    }
    else if( mxCurrentSettings->mpParentWindow )
    {
        // no current view shell, but parent window
        CreateController( 0, 0, mxCurrentSettings->mpParentWindow );
    }

    if( mxController.is() )
    {
        bool bSuccess = false;
        if( mxCurrentSettings.get() && mxCurrentSettings->mbPreview )
        {
            bSuccess = mxController->startPreview(mxCurrentSettings->mxStartPage, mxCurrentSettings->mxAnimationNode, mxCurrentSettings->mpParentWindow );
        }
        else
        {
            bSuccess = mxController->startShow(mxCurrentSettings.get());
        }

        if( !bSuccess )
            end();
        else
        {
            if(mpCurrentViewShellBase)
                mpCurrentViewShellBase->GetWindow()->GrabFocus();
        }
    }
}



void SlideShow::StartFullscreenPresentation( )
{
    // Create the top level window in which the PresentationViewShell(Base)
    // will be created.  This is done here explicitly so that we can make it
    // fullscreen.
    const sal_Int32 nDisplay (GetDisplay());
    WorkWindow* pWorkWindow = new FullScreenWorkWindow(this, mpCurrentViewShellBase);
    pWorkWindow->SetBackground(Wallpaper(COL_BLACK));
    pWorkWindow->StartPresentationMode( true, mpDoc->getPresentationSettings().mbAlwaysOnTop ? PRESENTATION_HIDEALLAPPS : 0, nDisplay);
    //    pWorkWindow->ShowFullScreenMode(sal_False, nDisplay);

    if (pWorkWindow->IsVisible())
    {
        // Initialize the new presentation view shell with a copy of the
        // frame view of the current view shell.  This avoids that
        // changes made by the presentation have an effect on the other
        // view shells.
        FrameView* pOriginalFrameView = 0;
        if (mpCurrentViewShellBase)
        {
            ::boost::shared_ptr<ViewShell> xShell(mpCurrentViewShellBase->GetMainViewShell());
            if (xShell.get())
                pOriginalFrameView = xShell->GetFrameView();
        }

        delete mpFullScreenFrameView;
        mpFullScreenFrameView = new FrameView(mpDoc, pOriginalFrameView);

        // The new frame is created hidden.  To make it visible and activate the
        // new view shell--a prerequisite to process slot calls and initialize
        // its panes--a GrabFocus() has to be called later on.
        SfxFrame* pNewFrame = SfxFrame::Create( *mpDoc->GetDocSh(), *pWorkWindow, PRESENTATION_FACTORY_ID, true );
        pNewFrame->SetPresentationMode(true);

        mpFullScreenViewShellBase = static_cast<ViewShellBase*>(pNewFrame->GetCurrentViewFrame()->GetViewShell());
        if(mpFullScreenViewShellBase != NULL)
        {
            // The following GrabFocus() is responsible for activating the
            // new view shell.  Without it the screen remains blank (under
            // Windows and some Linux variants.)
            mpFullScreenViewShellBase->GetWindow()->GrabFocus();
        }
    }
}



/// convert configuration setting display concept to real screens
sal_Int32 SlideShow::GetDisplay()
{
    sal_Int32 nDisplay = 0;

    SdOptions* pOptions = SD_MOD()->GetSdOptions(DOCUMENT_TYPE_IMPRESS);
    if( pOptions )
        nDisplay = pOptions->GetDisplay();

    if( nDisplay < 0 )
        nDisplay = -1;
    else if( nDisplay == 0)
        nDisplay = (sal_Int32)Application::GetDisplayExternalScreen();
    else
        nDisplay--;

    SAL_INFO("sd", "Presenting on real screen " << nDisplay);

    return nDisplay;
}




bool SlideShow::dependsOn( ViewShellBase* pViewShellBase )
{
    return mxController.is() && (pViewShellBase == mpCurrentViewShellBase) && mpFullScreenViewShellBase;
}



Reference< presentation::XPresentation2 > CreatePresentation( const SdDrawDocument& rDocument )
{
    return Reference< presentation::XPresentation2 >( SlideShow::Create( const_cast< SdDrawDocument* >( &rDocument ) ).get() );
}



/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
