tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / svtools / source / uno / wizard / wizardshell.cxx
blobb46592a7139b433531fe570d235bf89e0a83016c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "wizardshell.hxx"
22 #include "wizardpagecontroller.hxx"
24 #include <comphelper/diagnose_ex.hxx>
26 #include <com/sun/star/ui/dialogs/WizardTravelType.hpp>
28 using vcl::RoadmapWizardTypes::WizardPath;
29 using vcl::RoadmapWizardTypes::PathId;
31 namespace svt::uno
35 using css::uno::Reference;
36 using css::uno::Exception;
37 using css::uno::Sequence;
38 using css::ui::dialogs::XWizardController;
39 using css::ui::dialogs::XWizardPage;
41 namespace WizardTravelType = css::ui::dialogs::WizardTravelType;
44 namespace
47 sal_Int16 lcl_determineFirstPageID( const Sequence< Sequence< sal_Int16 > >& i_rPaths )
49 ENSURE_OR_THROW( i_rPaths.hasElements() && i_rPaths[0].hasElements(), "illegal paths" );
50 return i_rPaths[0][0];
54 //= WizardShell
55 WizardShell::WizardShell(weld::Window* i_pParent, const Reference< XWizardController >& i_rController,
56 const Sequence< Sequence< sal_Int16 > >& i_rPaths)
57 :WizardShell_Base( i_pParent )
58 ,m_xController( i_rController )
59 ,m_nFirstPageID( lcl_determineFirstPageID( i_rPaths ) )
61 ENSURE_OR_THROW( m_xController.is(), "invalid controller" );
63 // declare the paths
64 for ( sal_Int32 i=0; i<i_rPaths.getLength(); ++i )
66 const Sequence< sal_Int16 >& rPath( i_rPaths[i] );
67 WizardPath aPath( rPath.getLength() );
68 std::transform(rPath.begin(), rPath.end(), aPath.begin(),
69 [this](const sal_Int16 nPageId) -> WizardPath::value_type { return impl_pageIdToState(nPageId); });
70 declarePath( static_cast<PathId>(i), aPath );
73 // create the first page, to know the page size
74 GetOrCreatePage( impl_pageIdToState( i_rPaths[0][0] ) );
75 m_xAssistant->set_current_page(0);
77 // some defaults
78 enableAutomaticNextButtonState();
81 short WizardShell::run()
83 ActivatePage();
84 return WizardShell_Base::run();
87 OUString WizardShell::getPageIdentForState(WizardState nState) const
89 return OUString::number(impl_stateToPageId(nState));
92 WizardState WizardShell::getStateFromPageIdent(const OUString& rIdent) const
94 return impl_pageIdToState(rIdent.toInt32());
97 sal_Int16 WizardShell::convertCommitReasonToTravelType( const CommitPageReason i_eReason )
99 switch ( i_eReason )
101 case vcl::WizardTypes::eTravelForward:
102 return WizardTravelType::FORWARD;
104 case vcl::WizardTypes::eTravelBackward:
105 return WizardTravelType::BACKWARD;
107 case vcl::WizardTypes::eFinish:
108 return WizardTravelType::FINISH;
110 default:
111 break;
113 OSL_FAIL( "WizardShell::convertCommitReasonToTravelType: unsupported CommitPageReason!" );
114 return WizardTravelType::FINISH;
118 void WizardShell::enterState( WizardState i_nState )
120 WizardShell_Base::enterState( i_nState );
122 if ( !m_xController.is() )
123 return;
127 m_xController->onActivatePage( impl_stateToPageId( i_nState ) );
129 catch( const Exception& )
131 DBG_UNHANDLED_EXCEPTION("svtools.uno");
136 bool WizardShell::leaveState( WizardState i_nState )
138 if ( !WizardShell_Base::leaveState( i_nState ) )
139 return false;
141 if ( !m_xController.is() )
142 return true;
146 m_xController->onDeactivatePage( impl_stateToPageId( i_nState ) );
148 catch( const Exception& )
150 DBG_UNHANDLED_EXCEPTION("svtools.uno");
153 return true;
157 PWizardPageController WizardShell::impl_getController(BuilderPage* i_pPage) const
159 Page2ControllerMap::const_iterator pos = m_aPageControllers.find( i_pPage );
160 ENSURE_OR_RETURN( pos != m_aPageControllers.end(), "WizardShell::impl_getController: no controller for this page!", PWizardPageController() );
161 return pos->second;
165 Reference< XWizardPage > WizardShell::getCurrentWizardPage() const
167 const WizardState eState = getCurrentState();
169 PWizardPageController pController( impl_getController( GetPage( eState ) ) );
170 ENSURE_OR_RETURN( pController, "WizardShell::getCurrentWizardPage: invalid page/controller!", nullptr );
172 return pController->getWizardPage();
175 void WizardShell::enablePage( const sal_Int16 i_nPageID, const bool i_bEnable )
177 enableState( impl_pageIdToState( i_nPageID ), i_bEnable );
180 namespace
182 class EmptyPage : public BuilderPage
184 public:
185 EmptyPage(weld::Widget* pParent, weld::DialogController* pController)
186 : BuilderPage(pParent, pController, u"svt/ui/emptypage.ui"_ustr, u"EmptyPage"_ustr)
188 m_xContainer->set_size_request(m_xContainer->get_approximate_digit_width() * 70,
189 m_xContainer->get_text_height() * 10);
191 weld::Container* GetContainer() const { return m_xContainer.get(); }
195 std::unique_ptr<BuilderPage> WizardShell::createPage( WizardState i_nState )
197 ENSURE_OR_RETURN( m_xController.is(), "WizardShell::createPage: no WizardController!", nullptr );
199 sal_Int16 nPageId = impl_stateToPageId(i_nState);
201 OUString sIdent(OUString::number(nPageId));
202 weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
204 auto xPage = std::make_unique<EmptyPage>(pPageContainer, this);
205 m_aPageControllers[xPage.get()] =
206 std::make_shared<WizardPageController>(xPage->GetContainer(), m_xController, nPageId);
208 return xPage;
211 vcl::IWizardPageController* WizardShell::getPageController(BuilderPage* i_pCurrentPage) const
213 return impl_getController( i_pCurrentPage ).get();
216 OUString WizardShell::getStateDisplayName( WizardState i_nState ) const
220 if ( m_xController.is() )
221 return m_xController->getPageTitle( impl_stateToPageId( i_nState ) );
223 catch( const Exception& )
225 DBG_UNHANDLED_EXCEPTION("svtools.uno");
227 // fallback for ill-behaved clients: the numeric state
228 return OUString::number(i_nState);
232 bool WizardShell::canAdvance() const
236 if ( m_xController.is() && !m_xController->canAdvance() )
237 return false;
239 catch( const Exception& )
241 DBG_UNHANDLED_EXCEPTION("svtools.uno");
244 return WizardShell_Base::canAdvance();
248 bool WizardShell::onFinish()
252 if ( m_xController.is() && !m_xController->confirmFinish() )
253 return false;
255 catch( const Exception& )
257 DBG_UNHANDLED_EXCEPTION("svtools.uno");
260 return WizardShell_Base::onFinish();
264 } // namespace svt::uno
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */