1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25  ************************************************************************/
26 
27 #include "precompiled_svtools.hxx"
28 
29 #include "wizardshell.hxx"
30 #include "wizardpagecontroller.hxx"
31 
32 #include <tools/diagnose_ex.h>
33 
34 /** === begin UNO includes === **/
35 #include <com/sun/star/ui/dialogs/WizardTravelType.hpp>
36 /** === end UNO includes === **/
37 
38 #include <vcl/msgbox.hxx>
39 
40 //......................................................................................................................
41 namespace svt { namespace uno
42 {
43 //......................................................................................................................
44 
45 	/** === begin UNO using === **/
46 	using ::com::sun::star::uno::Reference;
47 	using ::com::sun::star::uno::XInterface;
48 	using ::com::sun::star::uno::UNO_QUERY;
49 	using ::com::sun::star::uno::UNO_QUERY_THROW;
50 	using ::com::sun::star::uno::UNO_SET_THROW;
51 	using ::com::sun::star::uno::Exception;
52 	using ::com::sun::star::uno::RuntimeException;
53 	using ::com::sun::star::uno::Any;
54 	using ::com::sun::star::uno::makeAny;
55 	using ::com::sun::star::uno::Sequence;
56 	using ::com::sun::star::uno::Type;
57     using ::com::sun::star::ui::dialogs::XWizardController;
58     using ::com::sun::star::ui::dialogs::XWizard;
59     using ::com::sun::star::ui::dialogs::XWizardPage;
60 	/** === end UNO using === **/
61     namespace WizardTravelType = ::com::sun::star::ui::dialogs::WizardTravelType;
62 
63 	//==================================================================================================================
64     namespace
65     {
66 	    //--------------------------------------------------------------------------------------------------------------
67         sal_Int16 lcl_determineFirstPageID( const Sequence< Sequence< sal_Int16 > >& i_rPaths )
68         {
69             ENSURE_OR_THROW( ( i_rPaths.getLength() > 0 ) && ( i_rPaths[0].getLength() > 0 ), "illegal paths" );
70             return i_rPaths[0][0];
71         }
72     }
73 
74 	//==================================================================================================================
75 	//= WizardShell
76 	//==================================================================================================================
77 	//------------------------------------------------------------------------------------------------------------------
78     WizardShell::WizardShell( Window* i_pParent, const Reference< XWizard >& i_rWizard, const Reference< XWizardController >& i_rController,
79             const Sequence< Sequence< sal_Int16 > >& i_rPaths )
80         :WizardShell_Base( i_pParent, WB_MOVEABLE | WB_CLOSEABLE )
81         ,m_xWizard( i_rWizard )
82         ,m_xController( i_rController )
83         ,m_nFirstPageID( lcl_determineFirstPageID( i_rPaths ) )
84     {
85         ENSURE_OR_THROW( m_xWizard.is() && m_xController.is(), "invalid wizard/controller" );
86 
87         // declare the paths
88         for ( sal_Int32 i=0; i<i_rPaths.getLength(); ++i )
89         {
90             const Sequence< sal_Int16 >& rPath( i_rPaths[i] );
91             WizardPath aPath( rPath.getLength() );
92             for ( sal_Int32 j=0; j<rPath.getLength(); ++j )
93                 aPath[j] = impl_pageIdToState( rPath[j] );
94             declarePath( i, aPath );
95         }
96 
97         // create the first page, to know the page size
98         TabPage* pStartPage = GetOrCreatePage( impl_pageIdToState( i_rPaths[0][0] ) );
99         SetPageSizePixel( pStartPage->GetSizePixel() );
100 
101         // some defaults
102         ShowButtonFixedLine( true );
103         SetRoadmapInteractive( true );
104         enableAutomaticNextButtonState();
105     }
106 
107 	//------------------------------------------------------------------------------------------------------------------
108     WizardShell::~WizardShell()
109     {
110     }
111 
112 	//------------------------------------------------------------------------------------------------------------------
113     short WizardShell::Execute()
114     {
115         ActivatePage();
116         return WizardShell_Base::Execute();
117     }
118 
119 	//------------------------------------------------------------------------------------------------------------------
120     sal_Int16 WizardShell::convertCommitReasonToTravelType( const CommitPageReason i_eReason )
121     {
122         switch ( i_eReason )
123         {
124         case WizardTypes::eTravelForward:
125             return WizardTravelType::FORWARD;
126 
127         case WizardTypes::eTravelBackward:
128             return WizardTravelType::BACKWARD;
129 
130         case WizardTypes::eFinish:
131             return WizardTravelType::FINISH;
132 
133         default:
134             break;
135         }
136         OSL_ENSURE( false, "WizardShell::convertCommitReasonToTravelType: unsupported CommitPageReason!" );
137         return WizardTravelType::FINISH;
138     }
139 
140 	//------------------------------------------------------------------------------------------------------------------
141 	void WizardShell::enterState( WizardState i_nState )
142     {
143         WizardShell_Base::enterState( i_nState );
144 
145         if ( !m_xController.is() )
146             return;
147 
148         try
149         {
150             m_xController->onActivatePage( impl_stateToPageId( i_nState ) );
151         }
152         catch( const Exception& )
153         {
154         	DBG_UNHANDLED_EXCEPTION();
155         }
156     }
157 
158 	//------------------------------------------------------------------------------------------------------------------
159     sal_Bool WizardShell::leaveState( WizardState i_nState )
160     {
161         if ( !WizardShell_Base::leaveState( i_nState ) )
162             return sal_False;
163 
164         if ( !m_xController.is() )
165             return sal_True;
166 
167         try
168         {
169             m_xController->onDeactivatePage( impl_stateToPageId( i_nState ) );
170         }
171         catch( const Exception& )
172         {
173         	DBG_UNHANDLED_EXCEPTION();
174         }
175 
176         return sal_True;
177     }
178 
179 	//------------------------------------------------------------------------------------------------------------------
180     PWizardPageController WizardShell::impl_getController( TabPage* i_pPage ) const
181     {
182         Page2ControllerMap::const_iterator pos = m_aPageControllers.find( i_pPage );
183         ENSURE_OR_RETURN( pos != m_aPageControllers.end(), "WizardShell::impl_getController: no controller for this page!", PWizardPageController() );
184         return pos->second;
185     }
186 
187 	//------------------------------------------------------------------------------------------------------------------
188     Reference< XWizardPage > WizardShell::getCurrentWizardPage() const
189     {
190         const WizardState eState = getCurrentState();
191 
192         PWizardPageController pController( impl_getController( GetPage( eState ) ) );
193         ENSURE_OR_RETURN( pController, "WizardShell::getCurrentWizardPage: invalid page/controller!", NULL );
194 
195         return pController->getWizardPage();
196     }
197 
198 	//------------------------------------------------------------------------------------------------------------------
199     void WizardShell::enablePage( const sal_Int16 i_nPageID, const sal_Bool i_bEnable )
200     {
201         enableState( impl_pageIdToState( i_nPageID ), i_bEnable );
202     }
203 
204     //------------------------------------------------------------------------------------------------------------------
205 	TabPage* WizardShell::createPage( WizardState i_nState )
206     {
207         ENSURE_OR_RETURN( m_xController.is(), "WizardShell::createPage: no WizardController!", NULL );
208 
209         ::boost::shared_ptr< WizardPageController > pController( new WizardPageController( *this, m_xController, impl_stateToPageId( i_nState ) ) );
210         TabPage* pPage = pController->getTabPage();
211         OSL_ENSURE( pPage != NULL, "WizardShell::createPage: illegal tab page!" );
212         if ( pPage == NULL )
213         {
214             // fallback for ill-behaved clients: empty page
215             pPage = new TabPage( this, 0 );
216             pPage->SetSizePixel( LogicToPixel( Size( 280, 185 ), MAP_APPFONT ) );
217         }
218 
219         m_aPageControllers[ pPage ] = pController;
220         return pPage;
221     }
222 
223 	//------------------------------------------------------------------------------------------------------------------
224 	IWizardPageController* WizardShell::getPageController( TabPage* i_pCurrentPage ) const
225     {
226         return impl_getController( i_pCurrentPage ).get();
227     }
228 
229 	//------------------------------------------------------------------------------------------------------------------
230     String WizardShell::getStateDisplayName( WizardState i_nState ) const
231     {
232         try
233         {
234             if ( m_xController.is() )
235                 return m_xController->getPageTitle( impl_stateToPageId( i_nState ) );
236         }
237         catch( const Exception& )
238         {
239         	DBG_UNHANDLED_EXCEPTION();
240         }
241         // fallback for ill-behaved clients: the numeric state
242         return String::CreateFromInt32( i_nState );
243     }
244 
245 	//------------------------------------------------------------------------------------------------------------------
246     bool WizardShell::canAdvance() const
247     {
248         try
249         {
250             if ( m_xController.is() && !m_xController->canAdvance() )
251                 return false;
252         }
253         catch( const Exception& )
254         {
255         	DBG_UNHANDLED_EXCEPTION();
256         }
257 
258         return WizardShell_Base::canAdvance();
259     }
260 
261 	//------------------------------------------------------------------------------------------------------------------
262     sal_Bool WizardShell::onFinish()
263     {
264         try
265         {
266             if ( m_xController.is() && !m_xController->confirmFinish() )
267                 return sal_False;
268         }
269         catch( const Exception& )
270         {
271     	    DBG_UNHANDLED_EXCEPTION();
272         }
273 
274         return WizardShell_Base::onFinish();
275     }
276 
277 //......................................................................................................................
278 } } // namespace svt::uno
279 //......................................................................................................................
280