1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <svtools/wizardmachine.hxx>
21 #include <svtools/helpid.hrc>
22 #include <tools/debug.hxx>
23 #include <tools/diagnose_ex.h>
24 #include <vcl/msgbox.hxx>
25 #include <svtools/svtresid.hxx>
26 #include <svtools/svtools.hrc>
34 //= WizardPageImplData
36 struct WizardPageImplData
43 OWizardPage::OWizardPage(vcl::Window
*pParent
, const OString
& rID
,
44 const OUString
& rUIXMLDescription
)
45 : TabPage(pParent
, rID
, rUIXMLDescription
)
46 , m_pImpl(new WizardPageImplData
)
50 OWizardPage::~OWizardPage()
55 void OWizardPage::dispose()
61 void OWizardPage::initializePage()
65 void OWizardPage::ActivatePage()
67 TabPage::ActivatePage();
68 updateDialogTravelUI();
71 void OWizardPage::updateDialogTravelUI()
73 OWizardMachine
* pWizardMachine
= dynamic_cast< OWizardMachine
* >( GetParent() );
75 pWizardMachine
->updateTravelUI();
78 bool OWizardPage::canAdvance() const
83 bool OWizardPage::commitPage( WizardTypes::CommitPageReason
)
88 struct WizardMachineImplData
: public WizardTypes
90 OUString sTitleBase
; // the base for the title
91 ::std::stack
< WizardState
> aStateHistory
; // the history of all states (used for implementing "Back")
93 WizardState nFirstUnknownPage
;
94 // the WizardDialog does not allow non-linear transitions (e.g. it's
95 // not possible to add pages in a non-linear order), so we need some own maintenance data
97 bool m_bAutoNextButtonState
;
99 bool m_bTravelingSuspended
;
101 WizardMachineImplData()
102 :nFirstUnknownPage( 0 )
103 ,m_bAutoNextButtonState( false )
104 ,m_bTravelingSuspended( false )
109 OWizardMachine::OWizardMachine(vcl::Window
* _pParent
, const WinBits i_nStyle
, WizardButtonFlags _nButtonFlags
)
110 :WizardDialog( _pParent
, i_nStyle
)
116 ,m_pImpl( new WizardMachineImplData
)
118 implConstruct( _nButtonFlags
);
121 OWizardMachine::OWizardMachine(vcl::Window
* _pParent
, WizardButtonFlags _nButtonFlags
)
122 :WizardDialog( _pParent
, "WizardDialog", "svt/ui/wizarddialog.ui" )
128 ,m_pImpl( new WizardMachineImplData
)
130 implConstruct( _nButtonFlags
);
134 void OWizardMachine::implConstruct( const WizardButtonFlags _nButtonFlags
)
136 m_pImpl
->sTitleBase
= GetText();
138 // create the buttons according to the wizard button flags
140 if (_nButtonFlags
& WizardButtonFlags::HELP
)
142 m_pHelp
= VclPtr
<HelpButton
>::Create(this, WB_TABSTOP
);
143 m_pHelp
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
145 AddButton( m_pHelp
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
148 // the previous button
149 if (_nButtonFlags
& WizardButtonFlags::PREVIOUS
)
151 m_pPrevPage
= VclPtr
<PushButton
>::Create(this, WB_TABSTOP
);
152 m_pPrevPage
->SetHelpId( HID_WIZARD_PREVIOUS
);
153 m_pPrevPage
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
154 m_pPrevPage
->SetText(SVT_RESSTR(STR_WIZDLG_PREVIOUS
));
157 if (_nButtonFlags
& WizardButtonFlags::NEXT
)
158 AddButton( m_pPrevPage
, ( WIZARDDIALOG_BUTTON_SMALLSTDOFFSET_X
) ); // half x-offset to the next button
160 AddButton( m_pPrevPage
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
161 SetPrevButton( m_pPrevPage
);
162 m_pPrevPage
->SetClickHdl( LINK( this, OWizardMachine
, OnPrevPage
) );
166 if (_nButtonFlags
& WizardButtonFlags::NEXT
)
168 m_pNextPage
= VclPtr
<PushButton
>::Create(this, WB_TABSTOP
);
169 m_pNextPage
->SetHelpId( HID_WIZARD_NEXT
);
170 m_pNextPage
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
171 m_pNextPage
->SetText(OUString(SVT_RESSTR(STR_WIZDLG_NEXT
)));
174 AddButton( m_pNextPage
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
175 SetNextButton( m_pNextPage
);
176 m_pNextPage
->SetClickHdl( LINK( this, OWizardMachine
, OnNextPage
) );
180 if (_nButtonFlags
& WizardButtonFlags::FINISH
)
182 m_pFinish
= VclPtr
<OKButton
>::Create(this, WB_TABSTOP
);
183 m_pFinish
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
184 m_pFinish
->SetText(SVT_RESSTR(STR_WIZDLG_FINISH
));
187 AddButton( m_pFinish
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
188 m_pFinish
->SetClickHdl( LINK( this, OWizardMachine
, OnFinish
) );
192 if (_nButtonFlags
& WizardButtonFlags::CANCEL
)
194 m_pCancel
= VclPtr
<CancelButton
>::Create(this, WB_TABSTOP
);
195 m_pCancel
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
198 AddButton( m_pCancel
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
203 OWizardMachine::~OWizardMachine()
208 void OWizardMachine::dispose()
210 m_pFinish
.disposeAndClear();
211 m_pCancel
.disposeAndClear();
212 m_pNextPage
.disposeAndClear();
213 m_pPrevPage
.disposeAndClear();
214 m_pHelp
.disposeAndClear();
218 for (WizardState i
= 0; i
< m_pImpl
->nFirstUnknownPage
; ++i
)
220 TabPage
*pPage
= GetPage(i
);
222 pPage
->disposeOnce();
228 WizardDialog::dispose();
232 void OWizardMachine::implUpdateTitle()
234 OUString
sCompleteTitle(m_pImpl
->sTitleBase
);
236 // append the page title
237 TabPage
* pCurrentPage
= GetPage(getCurrentState());
238 if ( pCurrentPage
&& !pCurrentPage
->GetText().isEmpty() )
240 sCompleteTitle
+= (" - " + pCurrentPage
->GetText());
243 SetText(sCompleteTitle
);
247 void OWizardMachine::setTitleBase(const OUString
& _rTitleBase
)
249 m_pImpl
->sTitleBase
= _rTitleBase
;
254 TabPage
* OWizardMachine::GetOrCreatePage( const WizardState i_nState
)
256 if ( NULL
== GetPage( i_nState
) )
258 TabPage
* pNewPage
= createPage( i_nState
);
259 DBG_ASSERT( pNewPage
, "OWizardMachine::GetOrCreatePage: invalid new page (NULL)!" );
261 // fill up the page sequence of our base class (with dummies)
262 while ( m_pImpl
->nFirstUnknownPage
< i_nState
)
265 ++m_pImpl
->nFirstUnknownPage
;
268 if ( m_pImpl
->nFirstUnknownPage
== i_nState
)
270 // encountered this page number the first time
272 ++m_pImpl
->nFirstUnknownPage
;
275 // already had this page - just change it
276 SetPage( i_nState
, pNewPage
);
278 return GetPage( i_nState
);
282 void OWizardMachine::ActivatePage()
284 WizardDialog::ActivatePage();
286 WizardState nCurrentLevel
= GetCurLevel();
287 GetOrCreatePage( nCurrentLevel
);
289 enterState( nCurrentLevel
);
293 bool OWizardMachine::DeactivatePage()
295 WizardState nCurrentState
= getCurrentState();
296 if (!leaveState(nCurrentState
) || !WizardDialog::DeactivatePage())
302 void OWizardMachine::defaultButton(WizardButtonFlags _nWizardButtonFlags
)
304 // the new default button
305 PushButton
* pNewDefButton
= NULL
;
306 if (m_pFinish
&& (_nWizardButtonFlags
& WizardButtonFlags::FINISH
))
307 pNewDefButton
= m_pFinish
;
308 if (m_pNextPage
&& (_nWizardButtonFlags
& WizardButtonFlags::NEXT
))
309 pNewDefButton
= m_pNextPage
;
310 if (m_pPrevPage
&& (_nWizardButtonFlags
& WizardButtonFlags::PREVIOUS
))
311 pNewDefButton
= m_pPrevPage
;
312 if (m_pHelp
&& (_nWizardButtonFlags
& WizardButtonFlags::HELP
))
313 pNewDefButton
= m_pHelp
;
314 if (m_pCancel
&& (_nWizardButtonFlags
& WizardButtonFlags::CANCEL
))
315 pNewDefButton
= m_pCancel
;
318 defaultButton( pNewDefButton
);
320 implResetDefault( this );
324 void OWizardMachine::implResetDefault(vcl::Window
* _pWindow
)
326 vcl::Window
* pChildLoop
= _pWindow
->GetWindow(GetWindowType::FirstChild
);
329 // does the window participate in the tabbing order?
330 if (pChildLoop
->GetStyle() & WB_DIALOGCONTROL
)
331 implResetDefault(pChildLoop
);
334 WindowType eType
= pChildLoop
->GetType();
335 if ( (WINDOW_BUTTON
== eType
)
336 || (WINDOW_PUSHBUTTON
== eType
)
337 || (WINDOW_OKBUTTON
== eType
)
338 || (WINDOW_CANCELBUTTON
== eType
)
339 || (WINDOW_HELPBUTTON
== eType
)
340 || (WINDOW_IMAGEBUTTON
== eType
)
341 || (WINDOW_MENUBUTTON
== eType
)
342 || (WINDOW_MOREBUTTON
== eType
)
345 pChildLoop
->SetStyle(pChildLoop
->GetStyle() & ~WB_DEFBUTTON
);
349 pChildLoop
= pChildLoop
->GetWindow(GetWindowType::Next
);
354 void OWizardMachine::defaultButton(PushButton
* _pNewDefButton
)
356 // loop through all (direct and indirect) descendants which participate in our tabbing order, and
357 // reset the WB_DEFBUTTON for every window which is a button
358 implResetDefault(this);
360 // set it's new style
362 _pNewDefButton
->SetStyle(_pNewDefButton
->GetStyle() | WB_DEFBUTTON
);
366 void OWizardMachine::enableButtons(WizardButtonFlags _nWizardButtonFlags
, bool _bEnable
)
368 if (m_pFinish
&& (_nWizardButtonFlags
& WizardButtonFlags::FINISH
))
369 m_pFinish
->Enable(_bEnable
);
370 if (m_pNextPage
&& (_nWizardButtonFlags
& WizardButtonFlags::NEXT
))
371 m_pNextPage
->Enable(_bEnable
);
372 if (m_pPrevPage
&& (_nWizardButtonFlags
& WizardButtonFlags::PREVIOUS
))
373 m_pPrevPage
->Enable(_bEnable
);
374 if (m_pHelp
&& (_nWizardButtonFlags
& WizardButtonFlags::HELP
))
375 m_pHelp
->Enable(_bEnable
);
376 if (m_pCancel
&& (_nWizardButtonFlags
& WizardButtonFlags::CANCEL
))
377 m_pCancel
->Enable(_bEnable
);
381 void OWizardMachine::enterState(WizardState _nState
)
384 IWizardPageController
* pController
= getPageController( GetPage( _nState
) );
385 OSL_ENSURE( pController
, "OWizardMachine::enterState: no controller for the given page!" );
387 pController
->initializePage();
389 if ( isAutomaticNextButtonStateEnabled() )
390 enableButtons( WizardButtonFlags::NEXT
, canAdvance() );
392 enableButtons( WizardButtonFlags::PREVIOUS
, !m_pImpl
->aStateHistory
.empty() );
394 // set the new title - it depends on the current page (i.e. state)
399 bool OWizardMachine::leaveState(WizardState
)
401 // no need to ask the page here.
402 // If we reach this point, we already gave the current page the chance to commit it's data,
403 // and it was allowed to commit it's data
409 bool OWizardMachine::onFinish()
411 return Finish( RET_OK
);
415 IMPL_LINK_NOARG(OWizardMachine
, OnFinish
)
417 if ( isTravelingSuspended() )
419 WizardTravelSuspension
aTravelGuard( *this );
420 if ( !prepareLeaveCurrentState( eFinish
) )
424 return onFinish() ? 1L : 0L;
428 OWizardMachine::WizardState
OWizardMachine::determineNextState( WizardState _nCurrentState
) const
430 return _nCurrentState
+ 1;
434 bool OWizardMachine::prepareLeaveCurrentState( CommitPageReason _eReason
)
436 IWizardPageController
* pController
= getPageController( GetPage( getCurrentState() ) );
437 ENSURE_OR_RETURN( pController
!= NULL
, "OWizardMachine::prepareLeaveCurrentState: no controller for the current page!", true );
438 return pController
->commitPage( _eReason
);
442 bool OWizardMachine::skipBackwardUntil( WizardState _nTargetState
)
444 // allowed to leave the current page?
445 if ( !prepareLeaveCurrentState( eTravelBackward
) )
448 // don't travel directly on m_pImpl->aStateHistory, in case something goes wrong
449 ::std::stack
< WizardState
> aTravelVirtually
= m_pImpl
->aStateHistory
;
450 ::std::stack
< WizardState
> aOldStateHistory
= m_pImpl
->aStateHistory
;
452 WizardState nCurrentRollbackState
= getCurrentState();
453 while ( nCurrentRollbackState
!= _nTargetState
)
455 DBG_ASSERT( !aTravelVirtually
.empty(), "OWizardMachine::skipBackwardUntil: this target state does not exist in the history!" );
456 nCurrentRollbackState
= aTravelVirtually
.top();
457 aTravelVirtually
.pop();
459 m_pImpl
->aStateHistory
= aTravelVirtually
;
460 if ( !ShowPage( _nTargetState
) )
462 m_pImpl
->aStateHistory
= aOldStateHistory
;
469 bool OWizardMachine::skipUntil( WizardState _nTargetState
)
471 WizardState nCurrentState
= getCurrentState();
473 // allowed to leave the current page?
474 if ( !prepareLeaveCurrentState( nCurrentState
< _nTargetState
? eTravelForward
: eTravelBackward
) )
477 // don't travel directly on m_pImpl->aStateHistory, in case something goes wrong
478 ::std::stack
< WizardState
> aTravelVirtually
= m_pImpl
->aStateHistory
;
479 ::std::stack
< WizardState
> aOldStateHistory
= m_pImpl
->aStateHistory
;
480 while ( nCurrentState
!= _nTargetState
)
482 WizardState nNextState
= determineNextState( nCurrentState
);
483 if ( WZS_INVALID_STATE
== nNextState
)
485 OSL_FAIL( "OWizardMachine::skipUntil: the given target state does not exist!" );
489 // remember the skipped state in the history
490 aTravelVirtually
.push( nCurrentState
);
492 // get the next state
493 nCurrentState
= nNextState
;
495 m_pImpl
->aStateHistory
= aTravelVirtually
;
496 // show the target page
497 if ( !ShowPage( nCurrentState
) )
499 // argh! prepareLeaveCurrentPage succeeded, determineNextState succeeded,
500 // but ShowPage doesn't? Somebody behaves very strange here ....
501 OSL_FAIL( "OWizardMachine::skipUntil: very unpolite ...." );
502 m_pImpl
->aStateHistory
= aOldStateHistory
;
509 bool OWizardMachine::skip(sal_Int32 _nSteps
)
511 DBG_ASSERT(_nSteps
> 0, "OWizardMachine::skip: invalid number of steps!");
512 // allowed to leave the current page?
513 if ( !prepareLeaveCurrentState( eTravelForward
) )
516 WizardState nCurrentState
= getCurrentState();
517 WizardState nNextState
= determineNextState(nCurrentState
);
518 // loop _nSteps steps
519 while (_nSteps
-- > 0)
521 if (WZS_INVALID_STATE
== nNextState
)
524 // remember the skipped state in the history
525 m_pImpl
->aStateHistory
.push(nCurrentState
);
527 // get the next state
528 nCurrentState
= nNextState
;
529 nNextState
= determineNextState(nCurrentState
);
532 // show the (n+1)th page
533 if (!ShowPage(nCurrentState
))
535 // TODO: this leaves us in a state where we have no current page and an inconsistent state history.
536 // Perhaps we should rollback the skipping here ....
537 OSL_FAIL("OWizardMachine::skip: very unpolite ....");
538 // if somebody does a skip and then does not allow to leave ...
539 // (can't be a commit error, as we've already committed the current page. So if ShowPage fails here,
540 // somebody behaves really strange ...)
549 bool OWizardMachine::travelNext()
551 // allowed to leave the current page?
552 if ( !prepareLeaveCurrentState( eTravelForward
) )
555 // determine the next state to travel to
556 WizardState nCurrentState
= getCurrentState();
557 WizardState nNextState
= determineNextState(nCurrentState
);
558 if (WZS_INVALID_STATE
== nNextState
)
561 // the state history is used by the enterState method
563 m_pImpl
->aStateHistory
.push(nCurrentState
);
564 if (!ShowPage(nNextState
))
566 m_pImpl
->aStateHistory
.pop();
574 bool OWizardMachine::travelPrevious()
576 DBG_ASSERT(m_pImpl
->aStateHistory
.size() > 0, "OWizardMachine::travelPrevious: have no previous page!");
578 // allowed to leave the current page?
579 if ( !prepareLeaveCurrentState( eTravelBackward
) )
582 // the next state to switch to
583 WizardState nPreviousState
= m_pImpl
->aStateHistory
.top();
585 // the state history is used by the enterState method
586 m_pImpl
->aStateHistory
.pop();
588 if (!ShowPage(nPreviousState
))
590 m_pImpl
->aStateHistory
.push(nPreviousState
);
599 void OWizardMachine::removePageFromHistory( WizardState nToRemove
)
602 ::std::stack
< WizardState
> aTemp
;
603 while(!m_pImpl
->aStateHistory
.empty())
605 WizardState nPreviousState
= m_pImpl
->aStateHistory
.top();
606 m_pImpl
->aStateHistory
.pop();
607 if(nPreviousState
!= nToRemove
)
608 aTemp
.push( nPreviousState
);
612 while(!aTemp
.empty())
614 m_pImpl
->aStateHistory
.push( aTemp
.top() );
620 void OWizardMachine::enableAutomaticNextButtonState( bool _bEnable
)
622 m_pImpl
->m_bAutoNextButtonState
= _bEnable
;
626 bool OWizardMachine::isAutomaticNextButtonStateEnabled() const
628 return m_pImpl
->m_bAutoNextButtonState
;
632 IMPL_LINK_NOARG(OWizardMachine
, OnPrevPage
)
634 if ( isTravelingSuspended() )
636 WizardTravelSuspension
aTravelGuard( *this );
637 bool nRet
= travelPrevious();
642 IMPL_LINK_NOARG(OWizardMachine
, OnNextPage
)
644 if ( isTravelingSuspended() )
646 WizardTravelSuspension
aTravelGuard( *this );
647 bool nRet
= travelNext();
652 IWizardPageController
* OWizardMachine::getPageController( TabPage
* _pCurrentPage
) const
654 IWizardPageController
* pController
= dynamic_cast< IWizardPageController
* >( _pCurrentPage
);
659 void OWizardMachine::getStateHistory( ::std::vector
< WizardState
>& _out_rHistory
)
661 ::std::stack
< WizardState
> aHistoryCopy( m_pImpl
->aStateHistory
);
662 while ( !aHistoryCopy
.empty() )
664 _out_rHistory
.push_back( aHistoryCopy
.top() );
670 bool OWizardMachine::canAdvance() const
672 return WZS_INVALID_STATE
!= determineNextState( getCurrentState() );
676 void OWizardMachine::updateTravelUI()
678 const IWizardPageController
* pController
= getPageController( GetPage( getCurrentState() ) );
679 OSL_ENSURE( pController
!= NULL
, "RoadmapWizard::updateTravelUI: no controller for the current page!" );
682 ( !pController
|| pController
->canAdvance() ) // the current page allows to advance
683 && canAdvance(); // the dialog as a whole allows to advance
684 enableButtons( WizardButtonFlags::NEXT
, bCanAdvance
);
688 bool OWizardMachine::isTravelingSuspended() const
690 return m_pImpl
->m_bTravelingSuspended
;
694 void OWizardMachine::suspendTraveling( AccessGuard
)
696 DBG_ASSERT( !m_pImpl
->m_bTravelingSuspended
, "OWizardMachine::suspendTraveling: already suspended!" );
697 m_pImpl
->m_bTravelingSuspended
= true;
701 void OWizardMachine::resumeTraveling( AccessGuard
)
703 DBG_ASSERT( m_pImpl
->m_bTravelingSuspended
, "OWizardMachine::resumeTraveling: nothing to resume!" );
704 m_pImpl
->m_bTravelingSuspended
= false;
711 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */