1/*
2 * Copyright 2008-2010, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Michael Pfeiffer <laplace@users.sourceforge.net>
7 */
8
9
10#include "WizardController.h"
11
12#include "WizardView.h"
13#include "WizardPageView.h"
14
15
16void
17WizardController::StateStack::MakeEmpty()
18{
19	StateStack* stack = this;
20	StateStack* next;
21	do {
22		next = stack->Next();
23		delete stack;
24		stack = next;
25	} while (next != NULL);
26}
27
28
29WizardController::WizardController()
30	:
31	fStack(NULL)
32{
33}
34
35
36WizardController::~WizardController()
37{
38	if (fStack != NULL) {
39		fStack->MakeEmpty();
40		fStack = NULL;
41	}
42}
43
44
45void
46WizardController::Initialize(WizardView* view)
47{
48	if (fStack == NULL)
49		_PushState(InitialState());
50	_ShowPage(view);
51}
52
53
54void
55WizardController::Next(WizardView* wizard)
56{
57	wizard->PageCompleted();
58
59	if (fStack == NULL)
60		return;
61
62	int state = NextState(fStack->State());
63	if (state < 0)
64		return;
65
66	_PushState(state);
67	_ShowPage(wizard);
68}
69
70
71void
72WizardController::Previous(WizardView* wizard)
73{
74	wizard->PageCompleted();
75
76	if (fStack != NULL) {
77		StateStack* stack = fStack;
78		fStack = fStack->Next();
79		delete stack;
80	}
81	_ShowPage(wizard);
82}
83
84
85int32
86WizardController::CurrentState() const
87{
88	if (fStack == NULL)
89		return -1;
90
91	return fStack->State();
92}
93
94
95void
96WizardController::_PushState(int32 state)
97{
98	fStack = new StateStack(state, fStack);
99}
100
101
102void
103WizardController::_ShowPage(WizardView* wizard)
104{
105	if (fStack == NULL)
106		return;
107
108	WizardPageView* page = CreatePage(fStack->State(), wizard);
109	wizard->SetPage(page);
110}
111