1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 #include "wizardshell.hxx"
22 #include "wizardpagecontroller.hxx"
23 
24 #include <tools/diagnose_ex.h>
25 
26 #include <com/sun/star/ui/dialogs/WizardTravelType.hpp>
27 
28 using vcl::RoadmapWizardTypes::WizardPath;
29 
30 namespace svt::uno
31 {
32 
33 
34     using css::uno::Reference;
35     using css::uno::Exception;
36     using css::uno::Sequence;
37     using css::ui::dialogs::XWizardController;
38     using css::ui::dialogs::XWizardPage;
39 
40     namespace WizardTravelType = css::ui::dialogs::WizardTravelType;
41 
42 
43     namespace
44     {
45 
lcl_determineFirstPageID(const Sequence<Sequence<sal_Int16>> & i_rPaths)46         sal_Int16 lcl_determineFirstPageID( const Sequence< Sequence< sal_Int16 > >& i_rPaths )
47         {
48             ENSURE_OR_THROW( i_rPaths.hasElements() && i_rPaths[0].hasElements(), "illegal paths" );
49             return i_rPaths[0][0];
50         }
51     }
52 
53     //= WizardShell
WizardShell(weld::Window * i_pParent,const Reference<XWizardController> & i_rController,const Sequence<Sequence<sal_Int16>> & i_rPaths)54     WizardShell::WizardShell(weld::Window* i_pParent, const Reference< XWizardController >& i_rController,
55             const Sequence< Sequence< sal_Int16 > >& i_rPaths)
56         :WizardShell_Base( i_pParent )
57         ,m_xController( i_rController )
58         ,m_nFirstPageID( lcl_determineFirstPageID( i_rPaths ) )
59     {
60         ENSURE_OR_THROW( m_xController.is(), "invalid controller" );
61 
62         // declare the paths
63         for ( sal_Int32 i=0; i<i_rPaths.getLength(); ++i )
64         {
65             const Sequence< sal_Int16 >& rPath( i_rPaths[i] );
66             WizardPath aPath( rPath.getLength() );
67             std::transform(rPath.begin(), rPath.end(), aPath.begin(),
68                 [this](const sal_Int16 nPageId) -> WizardPath::value_type { return impl_pageIdToState(nPageId); });
69             declarePath( i, aPath );
70         }
71 
72         // create the first page, to know the page size
73         GetOrCreatePage( impl_pageIdToState( i_rPaths[0][0] ) );
74         m_xAssistant->set_current_page(0);
75 
76         // some defaults
77         enableAutomaticNextButtonState();
78     }
79 
run()80     short WizardShell::run()
81     {
82         ActivatePage();
83         return WizardShell_Base::run();
84     }
85 
convertCommitReasonToTravelType(const CommitPageReason i_eReason)86     sal_Int16 WizardShell::convertCommitReasonToTravelType( const CommitPageReason i_eReason )
87     {
88         switch ( i_eReason )
89         {
90         case vcl::WizardTypes::eTravelForward:
91             return WizardTravelType::FORWARD;
92 
93         case vcl::WizardTypes::eTravelBackward:
94             return WizardTravelType::BACKWARD;
95 
96         case vcl::WizardTypes::eFinish:
97             return WizardTravelType::FINISH;
98 
99         default:
100             break;
101         }
102         OSL_FAIL( "WizardShell::convertCommitReasonToTravelType: unsupported CommitPageReason!" );
103         return WizardTravelType::FINISH;
104     }
105 
106 
enterState(WizardState i_nState)107     void WizardShell::enterState( WizardState i_nState )
108     {
109         WizardShell_Base::enterState( i_nState );
110 
111         if ( !m_xController.is() )
112             return;
113 
114         try
115         {
116             m_xController->onActivatePage( impl_stateToPageId( i_nState ) );
117         }
118         catch( const Exception& )
119         {
120             DBG_UNHANDLED_EXCEPTION("svtools.uno");
121         }
122     }
123 
124 
leaveState(WizardState i_nState)125     bool WizardShell::leaveState( WizardState i_nState )
126     {
127         if ( !WizardShell_Base::leaveState( i_nState ) )
128             return false;
129 
130         if ( !m_xController.is() )
131             return true;
132 
133         try
134         {
135             m_xController->onDeactivatePage( impl_stateToPageId( i_nState ) );
136         }
137         catch( const Exception& )
138         {
139             DBG_UNHANDLED_EXCEPTION("svtools.uno");
140         }
141 
142         return true;
143     }
144 
145 
impl_getController(BuilderPage * i_pPage) const146     PWizardPageController WizardShell::impl_getController(BuilderPage* i_pPage) const
147     {
148         Page2ControllerMap::const_iterator pos = m_aPageControllers.find( i_pPage );
149         ENSURE_OR_RETURN( pos != m_aPageControllers.end(), "WizardShell::impl_getController: no controller for this page!", PWizardPageController() );
150         return pos->second;
151     }
152 
153 
getCurrentWizardPage() const154     Reference< XWizardPage > WizardShell::getCurrentWizardPage() const
155     {
156         const WizardState eState = getCurrentState();
157 
158         PWizardPageController pController( impl_getController( GetPage( eState ) ) );
159         ENSURE_OR_RETURN( pController, "WizardShell::getCurrentWizardPage: invalid page/controller!", nullptr );
160 
161         return pController->getWizardPage();
162     }
163 
enablePage(const sal_Int16 i_nPageID,const bool i_bEnable)164     void WizardShell::enablePage( const sal_Int16 i_nPageID, const bool i_bEnable )
165     {
166         enableState( impl_pageIdToState( i_nPageID ), i_bEnable );
167     }
168 
169     namespace
170     {
171         class EmptyPage : public BuilderPage
172         {
173         public:
EmptyPage(weld::Widget * pParent,weld::DialogController * pController)174             EmptyPage(weld::Widget* pParent, weld::DialogController* pController)
175                 : BuilderPage(pParent, pController, "svt/ui/emptypage.ui", "EmptyPage")
176             {
177                 m_xContainer->set_size_request(m_xContainer->get_approximate_digit_width() * 70,
178                                                m_xContainer->get_text_height() * 10);
179             }
GetContainer() const180             weld::Container* GetContainer() const { return m_xContainer.get(); }
181         };
182     }
183 
createPage(WizardState i_nState)184     std::unique_ptr<BuilderPage> WizardShell::createPage( WizardState i_nState )
185     {
186         ENSURE_OR_RETURN( m_xController.is(), "WizardShell::createPage: no WizardController!", nullptr );
187 
188         sal_Int16 nPageId = impl_stateToPageId(i_nState);
189 
190         OString sIdent(OString::number(nPageId));
191         weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
192 
193         auto xPage = std::make_unique<EmptyPage>(pPageContainer, this);
194         auto pController = std::make_shared<WizardPageController>(xPage->GetContainer(), m_xController, nPageId);
195 
196         m_aPageControllers[xPage.get()] = pController;
197 
198         return xPage;
199     }
200 
getPageController(BuilderPage * i_pCurrentPage) const201     vcl::IWizardPageController* WizardShell::getPageController(BuilderPage* i_pCurrentPage) const
202     {
203         return impl_getController( i_pCurrentPage ).get();
204     }
205 
getStateDisplayName(WizardState i_nState) const206     OUString WizardShell::getStateDisplayName( WizardState i_nState ) const
207     {
208         try
209         {
210             if ( m_xController.is() )
211                 return m_xController->getPageTitle( impl_stateToPageId( i_nState ) );
212         }
213         catch( const Exception& )
214         {
215             DBG_UNHANDLED_EXCEPTION("svtools.uno");
216         }
217         // fallback for ill-behaved clients: the numeric state
218         return OUString::number(i_nState);
219     }
220 
221 
canAdvance() const222     bool WizardShell::canAdvance() const
223     {
224         try
225         {
226             if ( m_xController.is() && !m_xController->canAdvance() )
227                 return false;
228         }
229         catch( const Exception& )
230         {
231             DBG_UNHANDLED_EXCEPTION("svtools.uno");
232         }
233 
234         return WizardShell_Base::canAdvance();
235     }
236 
237 
onFinish()238     bool WizardShell::onFinish()
239     {
240         try
241         {
242             if ( m_xController.is() && !m_xController->confirmFinish() )
243                 return false;
244         }
245         catch( const Exception& )
246         {
247             DBG_UNHANDLED_EXCEPTION("svtools.uno");
248         }
249 
250         return WizardShell_Base::onFinish();
251     }
252 
253 
254 } // namespace svt::uno
255 
256 
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
258