fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svtools / source / dialogs / wizardmachine.cxx
blob38775201b5aefa604a4a52b4d2920a02206b27c3
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 .
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 //.........................................................................
29 namespace svt
31 //.........................................................................
33 //=====================================================================
34 //= WizardPageImplData
35 //=====================================================================
36 struct WizardPageImplData
38 WizardPageImplData()
43 //=====================================================================
44 //= OWizardPage
45 //=====================================================================
46 OWizardPage::OWizardPage( Window* _pParent, const ResId& _rResId )
47 :TabPage( _pParent, _rResId )
48 ,m_pImpl( new WizardPageImplData )
52 //---------------------------------------------------------------------
53 OWizardPage::~OWizardPage()
55 delete m_pImpl;
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() );
74 if ( pWizardMachine )
75 pWizardMachine->updateTravelUI();
78 //---------------------------------------------------------------------
79 bool OWizardPage::canAdvance() const
81 return true;
84 //---------------------------------------------------------------------
85 sal_Bool OWizardPage::commitPage( WizardTypes::CommitPageReason )
87 return sal_True;
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 //=====================================================================
115 //= OWizardMachine
116 //=====================================================================
117 //---------------------------------------------------------------------
118 OWizardMachine::OWizardMachine(Window* _pParent, const ResId& _rRes, sal_uInt32 _nButtonFlags )
119 :WizardDialog( _pParent, _rRes )
120 ,m_pFinish(NULL)
121 ,m_pCancel(NULL)
122 ,m_pNextPage(NULL)
123 ,m_pPrevPage(NULL)
124 ,m_pHelp(NULL)
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 )
133 ,m_pFinish(NULL)
134 ,m_pCancel(NULL)
135 ,m_pNextPage(NULL)
136 ,m_pPrevPage(NULL)
137 ,m_pHelp(NULL)
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
149 // the help button
150 if (_nButtonFlags & WZB_HELP)
152 m_pHelp= new HelpButton(this, WB_TABSTOP);
153 m_pHelp->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT ) );
154 m_pHelp->Show();
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));
165 m_pPrevPage->Show();
167 if (_nButtonFlags & WZB_NEXT)
168 AddButton( m_pPrevPage, ( WIZARDDIALOG_BUTTON_SMALLSTDOFFSET_X) ); // half x-offset to the next button
169 else
170 AddButton( m_pPrevPage, WIZARDDIALOG_BUTTON_STDOFFSET_X );
171 SetPrevButton( m_pPrevPage );
172 m_pPrevPage->SetClickHdl( LINK( this, OWizardMachine, OnPrevPage ) );
175 // the next button
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)));
182 m_pNextPage->Show();
184 AddButton( m_pNextPage, WIZARDDIALOG_BUTTON_STDOFFSET_X );
185 SetNextButton( m_pNextPage );
186 m_pNextPage->SetClickHdl( LINK( this, OWizardMachine, OnNextPage ) );
189 // the finish button
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));
195 m_pFinish->Show();
197 AddButton( m_pFinish, WIZARDDIALOG_BUTTON_STDOFFSET_X );
198 m_pFinish->SetClickHdl( LINK( this, OWizardMachine, OnFinish ) );
201 // the cancel button
202 if (_nButtonFlags & WZB_CANCEL)
204 m_pCancel = new CancelButton(this, WB_TABSTOP);
205 m_pCancel->SetSizePixel( LogicToPixel( Size( 50, 14 ), MAP_APPFONT ) );
206 m_pCancel->Show();
208 AddButton( m_pCancel, WIZARDDIALOG_BUTTON_STDOFFSET_X );
212 //---------------------------------------------------------------------
213 OWizardMachine::~OWizardMachine()
215 delete m_pFinish;
216 delete m_pCancel;
217 delete m_pNextPage;
218 delete m_pPrevPage;
219 delete m_pHelp;
221 for (WizardState i=0; i<m_pImpl->nFirstUnknownPage; ++i)
222 delete GetPage(i);
224 delete m_pImpl;
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;
246 implUpdateTitle();
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 )
260 AddPage( NULL );
261 ++m_pImpl->nFirstUnknownPage;
264 if ( m_pImpl->nFirstUnknownPage == i_nState )
266 // encountered this page number the first time
267 AddPage( pNewPage );
268 ++m_pImpl->nFirstUnknownPage;
270 else
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())
293 return sal_False;
294 return sal_True;
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;
313 if ( pNewDefButton )
314 defaultButton( pNewDefButton );
315 else
316 implResetDefault( this );
319 //---------------------------------------------------------------------
320 void OWizardMachine::implResetDefault(Window* _pWindow)
322 Window* pChildLoop = _pWindow->GetWindow(WINDOW_FIRSTCHILD);
323 while (pChildLoop)
325 // does the window participate in the tabbing order?
326 if (pChildLoop->GetStyle() & WB_DIALOGCONTROL)
327 implResetDefault(pChildLoop);
329 // is it a button?
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);
344 // the next one ...
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
357 if (_pNewDefButton)
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)
379 // tell the page
380 IWizardPageController* pController = getPageController( GetPage( _nState ) );
381 OSL_ENSURE( pController, "OWizardMachine::enterState: no controller for the given page!" );
382 if ( pController )
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)
391 implUpdateTitle();
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
401 return sal_True;
404 //---------------------------------------------------------------------
405 sal_Bool OWizardMachine::onFinish()
407 return Finnish( RET_OK );
410 //---------------------------------------------------------------------
411 IMPL_LINK_NOARG(OWizardMachine, OnFinish)
413 if ( isTravelingSuspended() )
414 return 0;
415 WizardTravelSuspension aTravelGuard( *this );
416 if ( !prepareLeaveCurrentState( eFinish ) )
418 return 0L;
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 ) )
442 return sal_False;
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;
459 return sal_False;
461 return sal_True;
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 ) )
471 return sal_False;
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!" );
482 return sal_False;
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;
499 return sal_False;
501 return sal_True;
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 ) )
510 return sal_False;
512 WizardState nCurrentState = getCurrentState();
513 WizardState nNextState = determineNextState(nCurrentState);
514 // loop _nSteps steps
515 while (_nSteps-- > 0)
517 if (WZS_INVALID_STATE == nNextState)
518 return sal_False;
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 ...)
537 return sal_False;
540 // all fine
541 return sal_True;
544 //---------------------------------------------------------------------
545 sal_Bool OWizardMachine::travelNext()
547 // allowed to leave the current page?
548 if ( !prepareLeaveCurrentState( eTravelForward ) )
549 return sal_False;
551 // determine the next state to travel to
552 WizardState nCurrentState = getCurrentState();
553 WizardState nNextState = determineNextState(nCurrentState);
554 if (WZS_INVALID_STATE == nNextState)
555 return sal_False;
557 // the state history is used by the enterState method
558 // all fine
559 m_pImpl->aStateHistory.push(nCurrentState);
560 if (!ShowPage(nNextState))
562 m_pImpl->aStateHistory.pop();
563 return sal_False;
566 return sal_True;
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 ) )
576 return sal_False;
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();
583 // show this page
584 if (!ShowPage(nPreviousState))
586 m_pImpl->aStateHistory.push(nPreviousState);
587 return sal_False;
590 // all fine
591 return sal_True;
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 );
605 else
606 break;
608 while(!aTemp.empty())
610 m_pImpl->aStateHistory.push( aTemp.top() );
611 aTemp.pop();
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() )
631 return 0;
632 WizardTravelSuspension aTravelGuard( *this );
633 sal_Int32 nRet = travelPrevious();
634 return nRet;
637 //---------------------------------------------------------------------
638 IMPL_LINK_NOARG(OWizardMachine, OnNextPage)
640 if ( isTravelingSuspended() )
641 return 0;
642 WizardTravelSuspension aTravelGuard( *this );
643 sal_Int32 nRet = travelNext();
644 return nRet;
647 //---------------------------------------------------------------------
648 IWizardPageController* OWizardMachine::getPageController( TabPage* _pCurrentPage ) const
650 IWizardPageController* pController = dynamic_cast< IWizardPageController* >( _pCurrentPage );
651 return pController;
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() );
661 aHistoryCopy.pop();
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!" );
677 bool bCanAdvance =
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 //.........................................................................
704 } // namespace svt
705 //.........................................................................
707 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */