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>
28 //.........................................................................
31 //.........................................................................
33 //=====================================================================
34 //= WizardPageImplData
35 //=====================================================================
36 struct WizardPageImplData
43 //=====================================================================
45 //=====================================================================
46 OWizardPage::OWizardPage( Window
* _pParent
, const ResId
& _rResId
)
47 :TabPage( _pParent
, _rResId
)
48 ,m_pImpl( new WizardPageImplData
)
52 //---------------------------------------------------------------------
53 OWizardPage::~OWizardPage()
58 //---------------------------------------------------------------------
59 void OWizardPage::initializePage()
63 //---------------------------------------------------------------------
64 void OWizardPage::ActivatePage()
66 TabPage::ActivatePage();
67 updateDialogTravelUI();
70 //---------------------------------------------------------------------
71 void OWizardPage::updateDialogTravelUI()
73 OWizardMachine
* pWizardMachine
= dynamic_cast< OWizardMachine
* >( GetParent() );
75 pWizardMachine
->updateTravelUI();
78 //---------------------------------------------------------------------
79 bool OWizardPage::canAdvance() const
84 //---------------------------------------------------------------------
85 sal_Bool
OWizardPage::commitPage( WizardTypes::CommitPageReason
)
90 //=====================================================================
91 //= WizardMachineImplData
92 //=====================================================================
93 struct WizardMachineImplData
: public WizardTypes
95 String sTitleBase
; // the base for the title
96 ::std::stack
< WizardState
> aStateHistory
; // the history of all states (used for implementing "Back")
98 WizardState nFirstUnknownPage
;
99 // the WizardDialog does not allow non-linear transitions (e.g. it's
100 // not possible to add pages in a non-linear order), so we need some own maintainance data
102 sal_Bool m_bAutoNextButtonState
;
104 bool m_bTravelingSuspended
;
106 WizardMachineImplData()
107 :nFirstUnknownPage( 0 )
108 ,m_bAutoNextButtonState( sal_False
)
109 ,m_bTravelingSuspended( false )
114 //=====================================================================
116 //=====================================================================
117 //---------------------------------------------------------------------
118 OWizardMachine::OWizardMachine(Window
* _pParent
, const ResId
& _rRes
, sal_uInt32 _nButtonFlags
)
119 :WizardDialog( _pParent
, _rRes
)
125 ,m_pImpl( new WizardMachineImplData
)
127 implConstruct( _nButtonFlags
);
130 //---------------------------------------------------------------------
131 OWizardMachine::OWizardMachine(Window
* _pParent
, const WinBits i_nStyle
, sal_uInt32 _nButtonFlags
)
132 :WizardDialog( _pParent
, i_nStyle
)
138 ,m_pImpl( new WizardMachineImplData
)
140 implConstruct( _nButtonFlags
);
143 //---------------------------------------------------------------------
144 void OWizardMachine::implConstruct( const sal_uInt32 _nButtonFlags
)
146 m_pImpl
->sTitleBase
= GetText();
148 // create the buttons according to the wizard button flags
150 if (_nButtonFlags
& WZB_HELP
)
152 m_pHelp
= new HelpButton(this, WB_TABSTOP
);
153 m_pHelp
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
155 AddButton( m_pHelp
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
158 // the previous button
159 if (_nButtonFlags
& WZB_PREVIOUS
)
161 m_pPrevPage
= new PushButton(this, WB_TABSTOP
);
162 m_pPrevPage
->SetHelpId( HID_WIZARD_PREVIOUS
);
163 m_pPrevPage
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
164 m_pPrevPage
->SetText(SVT_RESSTR(STR_WIZDLG_PREVIOUS
));
167 if (_nButtonFlags
& WZB_NEXT
)
168 AddButton( m_pPrevPage
, ( WIZARDDIALOG_BUTTON_SMALLSTDOFFSET_X
) ); // half x-offset to the next button
170 AddButton( m_pPrevPage
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
171 SetPrevButton( m_pPrevPage
);
172 m_pPrevPage
->SetClickHdl( LINK( this, OWizardMachine
, OnPrevPage
) );
176 if (_nButtonFlags
& WZB_NEXT
)
178 m_pNextPage
= new PushButton(this, WB_TABSTOP
);
179 m_pNextPage
->SetHelpId( HID_WIZARD_NEXT
);
180 m_pNextPage
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
181 m_pNextPage
->SetText(String(SVT_RESSTR(STR_WIZDLG_NEXT
)));
184 AddButton( m_pNextPage
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
185 SetNextButton( m_pNextPage
);
186 m_pNextPage
->SetClickHdl( LINK( this, OWizardMachine
, OnNextPage
) );
190 if (_nButtonFlags
& WZB_FINISH
)
192 m_pFinish
= new OKButton(this, WB_TABSTOP
);
193 m_pFinish
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
194 m_pFinish
->SetText(SVT_RESSTR(STR_WIZDLG_FINISH
));
197 AddButton( m_pFinish
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
198 m_pFinish
->SetClickHdl( LINK( this, OWizardMachine
, OnFinish
) );
202 if (_nButtonFlags
& WZB_CANCEL
)
204 m_pCancel
= new CancelButton(this, WB_TABSTOP
);
205 m_pCancel
->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT
) );
208 AddButton( m_pCancel
, WIZARDDIALOG_BUTTON_STDOFFSET_X
);
212 //---------------------------------------------------------------------
213 OWizardMachine::~OWizardMachine()
221 for (WizardState i
=0; i
<m_pImpl
->nFirstUnknownPage
; ++i
)
227 //---------------------------------------------------------------------
228 void OWizardMachine::implUpdateTitle()
230 OUString
sCompleteTitle(m_pImpl
->sTitleBase
);
232 // append the page title
233 TabPage
* pCurrentPage
= GetPage(getCurrentState());
234 if ( pCurrentPage
&& !pCurrentPage
->GetText().isEmpty() )
236 sCompleteTitle
+= (" - " + pCurrentPage
->GetText());
239 SetText(sCompleteTitle
);
242 //---------------------------------------------------------------------
243 void OWizardMachine::setTitleBase(const String
& _rTitleBase
)
245 m_pImpl
->sTitleBase
= _rTitleBase
;
249 //---------------------------------------------------------------------
250 TabPage
* OWizardMachine::GetOrCreatePage( const WizardState i_nState
)
252 if ( NULL
== GetPage( i_nState
) )
254 TabPage
* pNewPage
= createPage( i_nState
);
255 DBG_ASSERT( pNewPage
, "OWizardMachine::GetOrCreatePage: invalid new page (NULL)!" );
257 // fill up the page sequence of our base class (with dummies)
258 while ( m_pImpl
->nFirstUnknownPage
< i_nState
)
261 ++m_pImpl
->nFirstUnknownPage
;
264 if ( m_pImpl
->nFirstUnknownPage
== i_nState
)
266 // encountered this page number the first time
268 ++m_pImpl
->nFirstUnknownPage
;
271 // already had this page - just change it
272 SetPage( i_nState
, pNewPage
);
274 return GetPage( i_nState
);
277 //---------------------------------------------------------------------
278 void OWizardMachine::ActivatePage()
280 WizardDialog::ActivatePage();
282 WizardState nCurrentLevel
= GetCurLevel();
283 GetOrCreatePage( nCurrentLevel
);
285 enterState( nCurrentLevel
);
288 //---------------------------------------------------------------------
289 long OWizardMachine::DeactivatePage()
291 WizardState nCurrentState
= getCurrentState();
292 if (!leaveState(nCurrentState
) || !WizardDialog::DeactivatePage())
297 //---------------------------------------------------------------------
298 void OWizardMachine::defaultButton(sal_uInt32 _nWizardButtonFlags
)
300 // the new default button
301 PushButton
* pNewDefButton
= NULL
;
302 if (m_pFinish
&& (_nWizardButtonFlags
& WZB_FINISH
))
303 pNewDefButton
= m_pFinish
;
304 if (m_pNextPage
&& (_nWizardButtonFlags
& WZB_NEXT
))
305 pNewDefButton
= m_pNextPage
;
306 if (m_pPrevPage
&& (_nWizardButtonFlags
& WZB_PREVIOUS
))
307 pNewDefButton
= m_pPrevPage
;
308 if (m_pHelp
&& (_nWizardButtonFlags
& WZB_HELP
))
309 pNewDefButton
= m_pHelp
;
310 if (m_pCancel
&& (_nWizardButtonFlags
& WZB_CANCEL
))
311 pNewDefButton
= m_pCancel
;
314 defaultButton( pNewDefButton
);
316 implResetDefault( this );
319 //---------------------------------------------------------------------
320 void OWizardMachine::implResetDefault(Window
* _pWindow
)
322 Window
* pChildLoop
= _pWindow
->GetWindow(WINDOW_FIRSTCHILD
);
325 // does the window participate in the tabbing order?
326 if (pChildLoop
->GetStyle() & WB_DIALOGCONTROL
)
327 implResetDefault(pChildLoop
);
330 WindowType eType
= pChildLoop
->GetType();
331 if ( (WINDOW_BUTTON
== eType
)
332 || (WINDOW_PUSHBUTTON
== eType
)
333 || (WINDOW_OKBUTTON
== eType
)
334 || (WINDOW_CANCELBUTTON
== eType
)
335 || (WINDOW_HELPBUTTON
== eType
)
336 || (WINDOW_IMAGEBUTTON
== eType
)
337 || (WINDOW_MENUBUTTON
== eType
)
338 || (WINDOW_MOREBUTTON
== eType
)
341 pChildLoop
->SetStyle(pChildLoop
->GetStyle() & ~WB_DEFBUTTON
);
345 pChildLoop
= pChildLoop
->GetWindow(WINDOW_NEXT
);
349 //---------------------------------------------------------------------
350 void OWizardMachine::defaultButton(PushButton
* _pNewDefButton
)
352 // loop through all (direct and indirect) descendants which participate in our tabbing order, and
353 // reset the WB_DEFBUTTON for every window which is a button
354 implResetDefault(this);
356 // set it's new style
358 _pNewDefButton
->SetStyle(_pNewDefButton
->GetStyle() | WB_DEFBUTTON
);
361 //---------------------------------------------------------------------
362 void OWizardMachine::enableButtons(sal_uInt32 _nWizardButtonFlags
, sal_Bool _bEnable
)
364 if (m_pFinish
&& (_nWizardButtonFlags
& WZB_FINISH
))
365 m_pFinish
->Enable(_bEnable
);
366 if (m_pNextPage
&& (_nWizardButtonFlags
& WZB_NEXT
))
367 m_pNextPage
->Enable(_bEnable
);
368 if (m_pPrevPage
&& (_nWizardButtonFlags
& WZB_PREVIOUS
))
369 m_pPrevPage
->Enable(_bEnable
);
370 if (m_pHelp
&& (_nWizardButtonFlags
& WZB_HELP
))
371 m_pHelp
->Enable(_bEnable
);
372 if (m_pCancel
&& (_nWizardButtonFlags
& WZB_CANCEL
))
373 m_pCancel
->Enable(_bEnable
);
376 //---------------------------------------------------------------------
377 void OWizardMachine::enterState(WizardState _nState
)
380 IWizardPageController
* pController
= getPageController( GetPage( _nState
) );
381 OSL_ENSURE( pController
, "OWizardMachine::enterState: no controller for the given page!" );
383 pController
->initializePage();
385 if ( isAutomaticNextButtonStateEnabled() )
386 enableButtons( WZB_NEXT
, canAdvance() );
388 enableButtons( WZB_PREVIOUS
, !m_pImpl
->aStateHistory
.empty() );
390 // set the new title - it depends on the current page (i.e. state)
394 //---------------------------------------------------------------------
395 sal_Bool
OWizardMachine::leaveState(WizardState
)
397 // no need to ask the page here.
398 // If we reach this point, we already gave the current page the chance to commit it's data,
399 // and it was allowed to commit it's data
404 //---------------------------------------------------------------------
405 sal_Bool
OWizardMachine::onFinish()
407 return Finnish( RET_OK
);
410 //---------------------------------------------------------------------
411 IMPL_LINK_NOARG(OWizardMachine
, OnFinish
)
413 if ( isTravelingSuspended() )
415 WizardTravelSuspension
aTravelGuard( *this );
416 if ( !prepareLeaveCurrentState( eFinish
) )
420 return onFinish() ? 1L : 0L;
423 //---------------------------------------------------------------------
424 OWizardMachine::WizardState
OWizardMachine::determineNextState( WizardState _nCurrentState
) const
426 return _nCurrentState
+ 1;
429 //---------------------------------------------------------------------
430 sal_Bool
OWizardMachine::prepareLeaveCurrentState( CommitPageReason _eReason
)
432 IWizardPageController
* pController
= getPageController( GetPage( getCurrentState() ) );
433 ENSURE_OR_RETURN( pController
!= NULL
, "OWizardMachine::prepareLeaveCurrentState: no controller for the current page!", sal_True
);
434 return pController
->commitPage( _eReason
);
437 //---------------------------------------------------------------------
438 sal_Bool
OWizardMachine::skipBackwardUntil( WizardState _nTargetState
)
440 // alowed to leave the current page?
441 if ( !prepareLeaveCurrentState( eTravelBackward
) )
444 // don't travel directly on m_pImpl->aStateHistory, in case something goes wrong
445 ::std::stack
< WizardState
> aTravelVirtually
= m_pImpl
->aStateHistory
;
446 ::std::stack
< WizardState
> aOldStateHistory
= m_pImpl
->aStateHistory
;
448 WizardState nCurrentRollbackState
= getCurrentState();
449 while ( nCurrentRollbackState
!= _nTargetState
)
451 DBG_ASSERT( !aTravelVirtually
.empty(), "OWizardMachine::skipBackwardUntil: this target state does not exist in the history!" );
452 nCurrentRollbackState
= aTravelVirtually
.top();
453 aTravelVirtually
.pop();
455 m_pImpl
->aStateHistory
= aTravelVirtually
;
456 if ( !ShowPage( _nTargetState
) )
458 m_pImpl
->aStateHistory
= aOldStateHistory
;
464 //---------------------------------------------------------------------
465 sal_Bool
OWizardMachine::skipUntil( WizardState _nTargetState
)
467 WizardState nCurrentState
= getCurrentState();
469 // alowed to leave the current page?
470 if ( !prepareLeaveCurrentState( nCurrentState
< _nTargetState
? eTravelForward
: eTravelBackward
) )
473 // don't travel directly on m_pImpl->aStateHistory, in case something goes wrong
474 ::std::stack
< WizardState
> aTravelVirtually
= m_pImpl
->aStateHistory
;
475 ::std::stack
< WizardState
> aOldStateHistory
= m_pImpl
->aStateHistory
;
476 while ( nCurrentState
!= _nTargetState
)
478 WizardState nNextState
= determineNextState( nCurrentState
);
479 if ( WZS_INVALID_STATE
== nNextState
)
481 OSL_FAIL( "OWizardMachine::skipUntil: the given target state does not exist!" );
485 // remember the skipped state in the history
486 aTravelVirtually
.push( nCurrentState
);
488 // get the next state
489 nCurrentState
= nNextState
;
491 m_pImpl
->aStateHistory
= aTravelVirtually
;
492 // show the target page
493 if ( !ShowPage( nCurrentState
) )
495 // argh! prepareLeaveCurrentPage succeeded, determineNextState succeeded,
496 // but ShowPage doesn't? Somebody behaves very strange here ....
497 OSL_FAIL( "OWizardMachine::skipUntil: very unpolite ...." );
498 m_pImpl
->aStateHistory
= aOldStateHistory
;
504 //---------------------------------------------------------------------
505 sal_Bool
OWizardMachine::skip(sal_Int32 _nSteps
)
507 DBG_ASSERT(_nSteps
> 0, "OWizardMachine::skip: invalid number of steps!");
508 // alowed to leave the current page?
509 if ( !prepareLeaveCurrentState( eTravelForward
) )
512 WizardState nCurrentState
= getCurrentState();
513 WizardState nNextState
= determineNextState(nCurrentState
);
514 // loop _nSteps steps
515 while (_nSteps
-- > 0)
517 if (WZS_INVALID_STATE
== nNextState
)
520 // remember the skipped state in the history
521 m_pImpl
->aStateHistory
.push(nCurrentState
);
523 // get the next state
524 nCurrentState
= nNextState
;
525 nNextState
= determineNextState(nCurrentState
);
528 // show the (n+1)th page
529 if (!ShowPage(nCurrentState
))
531 // TODO: this leaves us in a state where we have no current page and an inconsistent state history.
532 // Perhaps we should rollback the skipping here ....
533 OSL_FAIL("OWizardMachine::skip: very unpolite ....");
534 // if somebody does a skip and then does not allow to leave ...
535 // (can't be a commit error, as we've already committed the current page. So if ShowPage fails here,
536 // somebody behaves really strange ...)
544 //---------------------------------------------------------------------
545 sal_Bool
OWizardMachine::travelNext()
547 // allowed to leave the current page?
548 if ( !prepareLeaveCurrentState( eTravelForward
) )
551 // determine the next state to travel to
552 WizardState nCurrentState
= getCurrentState();
553 WizardState nNextState
= determineNextState(nCurrentState
);
554 if (WZS_INVALID_STATE
== nNextState
)
557 // the state history is used by the enterState method
559 m_pImpl
->aStateHistory
.push(nCurrentState
);
560 if (!ShowPage(nNextState
))
562 m_pImpl
->aStateHistory
.pop();
569 //---------------------------------------------------------------------
570 sal_Bool
OWizardMachine::travelPrevious()
572 DBG_ASSERT(m_pImpl
->aStateHistory
.size() > 0, "OWizardMachine::travelPrevious: have no previous page!");
574 // alowed to leave the current page?
575 if ( !prepareLeaveCurrentState( eTravelBackward
) )
578 // the next state to switch to
579 WizardState nPreviousState
= m_pImpl
->aStateHistory
.top();
581 // the state history is used by the enterState method
582 m_pImpl
->aStateHistory
.pop();
584 if (!ShowPage(nPreviousState
))
586 m_pImpl
->aStateHistory
.push(nPreviousState
);
594 //---------------------------------------------------------------------
595 void OWizardMachine::removePageFromHistory( WizardState nToRemove
)
598 ::std::stack
< WizardState
> aTemp
;
599 while(!m_pImpl
->aStateHistory
.empty())
601 WizardState nPreviousState
= m_pImpl
->aStateHistory
.top();
602 m_pImpl
->aStateHistory
.pop();
603 if(nPreviousState
!= nToRemove
)
604 aTemp
.push( nPreviousState
);
608 while(!aTemp
.empty())
610 m_pImpl
->aStateHistory
.push( aTemp
.top() );
615 //---------------------------------------------------------------------
616 void OWizardMachine::enableAutomaticNextButtonState( bool _bEnable
)
618 m_pImpl
->m_bAutoNextButtonState
= _bEnable
;
621 //---------------------------------------------------------------------
622 bool OWizardMachine::isAutomaticNextButtonStateEnabled() const
624 return m_pImpl
->m_bAutoNextButtonState
;
627 //---------------------------------------------------------------------
628 IMPL_LINK_NOARG(OWizardMachine
, OnPrevPage
)
630 if ( isTravelingSuspended() )
632 WizardTravelSuspension
aTravelGuard( *this );
633 sal_Int32 nRet
= travelPrevious();
637 //---------------------------------------------------------------------
638 IMPL_LINK_NOARG(OWizardMachine
, OnNextPage
)
640 if ( isTravelingSuspended() )
642 WizardTravelSuspension
aTravelGuard( *this );
643 sal_Int32 nRet
= travelNext();
647 //---------------------------------------------------------------------
648 IWizardPageController
* OWizardMachine::getPageController( TabPage
* _pCurrentPage
) const
650 IWizardPageController
* pController
= dynamic_cast< IWizardPageController
* >( _pCurrentPage
);
654 //---------------------------------------------------------------------
655 void OWizardMachine::getStateHistory( ::std::vector
< WizardState
>& _out_rHistory
)
657 ::std::stack
< WizardState
> aHistoryCopy( m_pImpl
->aStateHistory
);
658 while ( !aHistoryCopy
.empty() )
660 _out_rHistory
.push_back( aHistoryCopy
.top() );
665 //---------------------------------------------------------------------
666 bool OWizardMachine::canAdvance() const
668 return WZS_INVALID_STATE
!= determineNextState( getCurrentState() );
671 //---------------------------------------------------------------------
672 void OWizardMachine::updateTravelUI()
674 const IWizardPageController
* pController
= getPageController( GetPage( getCurrentState() ) );
675 OSL_ENSURE( pController
!= NULL
, "RoadmapWizard::updateTravelUI: no controller for the current page!" );
678 ( !pController
|| pController
->canAdvance() ) // the current page allows to advance
679 && canAdvance(); // the dialog as a whole allows to advance
680 enableButtons( WZB_NEXT
, bCanAdvance
);
683 //---------------------------------------------------------------------
684 bool OWizardMachine::isTravelingSuspended() const
686 return m_pImpl
->m_bTravelingSuspended
;
689 //---------------------------------------------------------------------
690 void OWizardMachine::suspendTraveling( AccessGuard
)
692 DBG_ASSERT( !m_pImpl
->m_bTravelingSuspended
, "OWizardMachine::suspendTraveling: already suspended!" );
693 m_pImpl
->m_bTravelingSuspended
= true;
696 //---------------------------------------------------------------------
697 void OWizardMachine::resumeTraveling( AccessGuard
)
699 DBG_ASSERT( m_pImpl
->m_bTravelingSuspended
, "OWizardMachine::resumeTraveling: nothing to resume!" );
700 m_pImpl
->m_bTravelingSuspended
= false;
703 //.........................................................................
705 //.........................................................................
707 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */