VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Main/Forms/WizardFrame.cpp
blob: af38cdd6ef555bfc49336dce19354e937af8609e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 Derived from source code of TrueCrypt 7.1a, which is
 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
 by the TrueCrypt License 3.0.

 Modifications and additions to the original source code (contained in this file) 
 and all other portions of this file are Copyright (c) 2013-2016 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages.
*/

#include "System.h"
#include "Main/GraphicUserInterface.h"
#include "Main/Resources.h"
#include "WizardFrame.h"

namespace VeraCrypt
{
	WizardFrame::WizardFrame (wxWindow* parent)
		: WizardFrameBase (parent),
		CurrentPage (nullptr),
		CurrentStep (-1),
		MaxStaticTextWidth (-1),
		WorkInProgress (false)
	{
		SetIcon (Resources::GetVeraCryptIcon());

		PageTitleStaticText->SetFont (wxFont (
#ifdef TC_WINDOWS
			16
#elif defined(TC_MACOSX)
			18
#elif defined(__WXGTK__)
			14
#endif
			* Gui->GetCharHeight (this) / 13, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, L"Times New Roman"));

		UpdateControls();
		this->SetDefaultItem (NextButton);
		NextButton->SetFocus();

		foreach (wxWindow *c, MainPanel->GetChildren())
			c->Connect (wxEVT_MOTION, wxMouseEventHandler (WizardFrame::OnMouseMotion), nullptr, this);
	}

	WizardFrame::~WizardFrame ()
	{
		if (CurrentPage)
			CurrentPage->Destroy();
	}

	void WizardFrame::ClearHistory ()
	{
		StepHistory.clear();
		UpdateControls();
	}

	void WizardFrame::OnActivate (wxActivateEvent& event)
	{
		Gui->SetActiveFrame (this);
		event.Skip();
	}
	
	void WizardFrame::OnClose (wxCloseEvent& event)
	{
		if (WorkInProgress)
			return;

		Gui->SetActiveFrame (nullptr);
		event.Skip();
	}

	void WizardFrame::OnHelpButtonClick (wxCommandEvent& event)
	{
		Gui->OpenUserGuide (this);
	}

	void WizardFrame::OnNextButtonClick (wxCommandEvent& event)
	{
		if (CurrentPage->IsValid())
		{
			WizardStep nextStep = ProcessPageChangeRequest (true);
			if (nextStep != CurrentStep)
				SetStep (nextStep);
		}
	}

	void WizardFrame::OnPreviousButtonClick (wxCommandEvent& event)
	{
		ProcessPageChangeRequest (false);

		if (!StepHistory.empty())
		{
			WizardStep prevStep = *StepHistory.rbegin();
			StepHistory.pop_back();
			SetStep (prevStep, false);
		}
	}
	
	void WizardFrame::SetCancelButtonText (const wxString &text)
	{
		CancelButton->SetLabel (text.empty() ? wxString (_("Cancel")) : text);
	}
	
	void WizardFrame::SetImage (const wxBitmap &bitmap)
	{
		WizardBitmap->SetBitmap (bitmap);
	}

	void WizardFrame::SetMaxStaticTextWidth (size_t charCount)
	{
		MaxStaticTextWidth = Gui->GetCharWidth (this) * charCount;
	}

	void WizardFrame::SetStep (WizardStep newStep)
	{
		SetStep (newStep, true);
	}

	void WizardFrame::SetStep (WizardStep newStep, bool forward)
	{
		bool init = false;
		FreezeScope freeze (this);

#ifdef TC_WINDOWS
		HelpButton->Disable(); // Prevent Help button from getting default focus
		NextButton->Enable();
#endif
		if (CurrentPage)
		{
			if (forward)
				StepHistory.push_back (CurrentStep);

			CurrentPage->OnPageChanging (forward);
			CurrentPage->Destroy();
			CurrentPage = nullptr;
		}
		else
			init = true;

		CurrentStep = newStep;
		CurrentPage = GetPage (newStep);

		CurrentPage->PageUpdatedEvent.Connect (EventConnector <WizardFrame> (this, &WizardFrame::OnPageUpdated));
		
		CurrentPage->Connect (wxEVT_MOTION, wxMouseEventHandler (WizardFrame::OnMouseMotion), nullptr, this);
		foreach (wxWindow *c, CurrentPage->GetChildren())
			c->Connect (wxEVT_MOTION, wxMouseEventHandler (WizardFrame::OnMouseMotion), nullptr, this);

		if (MaxStaticTextWidth > 0)
			CurrentPage->SetMaxStaticTextWidth (MaxStaticTextWidth);

		PageTitleStaticText->SetLabel (CurrentPage->GetPageTitle());
		PageSizer->Add (CurrentPage, 1, wxALL | wxEXPAND);

		if (init)
		{
			Fit();
			Layout();
			Center();
		}
		else
			MainPanel->Layout();

		CurrentPage->SetFocus();

		wxString nextButtonText = CurrentPage->GetNextButtonText();
		if (nextButtonText.empty())
			NextButton->SetLabel (_("&Next >"));
		else
			NextButton->SetLabel (nextButtonText);

#ifdef TC_WINDOWS
		HelpButton->Enable();
#endif
		UpdateControls();
	}

	void WizardFrame::SetWorkInProgress (bool state)
	{
		WorkInProgress = state;
		UpdateControls();
	}

	void WizardFrame::UpdateControls ()
	{
		CancelButton->Enable (!WorkInProgress);
		HelpButton->Enable (!WorkInProgress);
		NextButton->Enable (!WorkInProgress && CurrentPage != nullptr && CurrentPage->IsValid());
		PreviousButton->Enable (!WorkInProgress && !StepHistory.empty());
	}
}