summaryrefslogtreecommitdiffstats
path: root/fbreader/src/optionsDialog/reading/KeyBindingsTab.cpp
blob: 4d4cc105bd847819f8431e60e4a870276c876216 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2004-2012 Geometer Plus <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <ZLOptionsDialog.h>
#include <ZLApplication.h>
#include <ZLOptionEntry.h>

#include <optionEntries/ZLSimpleOptionEntry.h>
#include <optionEntries/ZLSimpleKeyOptionEntry.h>

#include "ReadingOptionsDialog.h"

#include "../../fbreader/FBReader.h"
#include "../../fbreader/FBReaderActions.h"

class KeyboardControlEntry : public ZLSimpleBooleanOptionEntry {

public:
	KeyboardControlEntry();
	void onStateChanged(bool state);
};

KeyboardControlEntry::KeyboardControlEntry() : ZLSimpleBooleanOptionEntry(FBReader::Instance().KeyboardControlOption) {
}

void KeyboardControlEntry::onStateChanged(bool state) {
	ZLSimpleBooleanOptionEntry::onStateChanged(state);
	FBReader::Instance().grabAllKeys(state);
}

class SingleKeyOptionEntry : public ZLSimpleKeyOptionEntry {

public:
	SingleKeyOptionEntry(const CodeIndexBimap &bimap, ZLKeyBindings &bindings);
	const CodeIndexBimap &codeIndexBimap() const;

private:
	const CodeIndexBimap &myBimap;
};

SingleKeyOptionEntry::SingleKeyOptionEntry(const CodeIndexBimap &bimap, ZLKeyBindings &bindings) : ZLSimpleKeyOptionEntry(bindings), myBimap(bimap) {
}

const ZLSimpleKeyOptionEntry::CodeIndexBimap &SingleKeyOptionEntry::codeIndexBimap() const {
	return myBimap;
}

class MultiKeyOptionEntry : public ZLKeyOptionEntry {

public:
	MultiKeyOptionEntry(const ZLResource &resource);
	void onAccept();
	int actionIndex(const std::string &key);
	void onValueChanged(const std::string &key, int index);
	void onKeySelected(const std::string &key);

	void setOrientation(ZLView::Angle);
	void setExitOnCancelEntry(ZLOptionEntry *exitOnCancelEntry);

private:
	void addAction(const std::string &actionId);

private:
	const ZLResource &myResource;
	ZLSimpleKeyOptionEntry::CodeIndexBimap myBimap;

	SingleKeyOptionEntry myEntry0;
	SingleKeyOptionEntry myEntry90;
	SingleKeyOptionEntry myEntry180;
	SingleKeyOptionEntry myEntry270;
	SingleKeyOptionEntry *myCurrentEntry;
	ZLOptionEntry *myExitOnCancelEntry;
};

void MultiKeyOptionEntry::addAction(const std::string &actionId) {
	myBimap.insert(actionId);
	addActionName(myResource[ZLResourceKey(actionId)].value());
}

MultiKeyOptionEntry::MultiKeyOptionEntry(const ZLResource &resource) :
	ZLKeyOptionEntry(),
	myResource(resource),
	myEntry0(myBimap, *FBReader::Instance().keyBindings(ZLView::DEGREES0)),
	myEntry90(myBimap, *FBReader::Instance().keyBindings(ZLView::DEGREES90)),
	myEntry180(myBimap, *FBReader::Instance().keyBindings(ZLView::DEGREES180)),
	myEntry270(myBimap, *FBReader::Instance().keyBindings(ZLView::DEGREES270)),
	myCurrentEntry(&myEntry0),
	myExitOnCancelEntry(0) {
	addAction(ZLApplication::NoAction);

	// switch view
	addAction(ActionCode::SHOW_LIBRARY);
	addAction(ActionCode::OPEN_PREVIOUS_BOOK);
	addAction(ActionCode::SHOW_TOC);

	// navigation
	addAction(ActionCode::SCROLL_TO_HOME);
	addAction(ActionCode::SCROLL_TO_START_OF_TEXT);
	addAction(ActionCode::SCROLL_TO_END_OF_TEXT);
	addAction(ActionCode::GOTO_NEXT_TOC_SECTION);
	addAction(ActionCode::GOTO_PREVIOUS_TOC_SECTION);
	addAction(ActionCode::PAGE_SCROLL_FORWARD);
	addAction(ActionCode::PAGE_SCROLL_BACKWARD);
	addAction(ActionCode::LINE_SCROLL_FORWARD);
	addAction(ActionCode::LINE_SCROLL_BACKWARD);
	addAction(ActionCode::UNDO);
	addAction(ActionCode::REDO);

	// selection
	addAction(ActionCode::COPY_SELECTED_TEXT_TO_CLIPBOARD);
	addAction(ActionCode::OPEN_SELECTED_TEXT_IN_DICTIONARY);
	addAction(ActionCode::CLEAR_SELECTION);

	// search
	addAction(ActionCode::SEARCH);
	addAction(ActionCode::FIND_PREVIOUS);
	addAction(ActionCode::FIND_NEXT);

	// look
	addAction(ActionCode::INCREASE_FONT);
	addAction(ActionCode::DECREASE_FONT);
	addAction(ActionCode::SHOW_HIDE_POSITION_INDICATOR);
	addAction(ActionCode::TOGGLE_FULLSCREEN);
	addAction(ActionCode::ROTATE_SCREEN);

	// dialogs
	addAction(ActionCode::SHOW_OPTIONS_DIALOG);
	addAction(ActionCode::SHOW_BOOK_INFO_DIALOG);
	addAction(ActionCode::ADD_BOOK);

	// quit
	addAction(ActionCode::CANCEL);
	addAction(ActionCode::QUIT);
}

void MultiKeyOptionEntry::setOrientation(ZLView::Angle angle) {
	switch (angle) {
		case ZLView::DEGREES0:
			myCurrentEntry = &myEntry0;
			break;
		case ZLView::DEGREES90:
			myCurrentEntry = &myEntry90;
			break;
		case ZLView::DEGREES180:
			myCurrentEntry = &myEntry180;
			break;
		case ZLView::DEGREES270:
			myCurrentEntry = &myEntry270;
			break;
	}
	resetView();
}

void MultiKeyOptionEntry::onAccept() {
	myEntry0.onAccept();
	myEntry90.onAccept();
	myEntry180.onAccept();
	myEntry270.onAccept();
}

int MultiKeyOptionEntry::actionIndex(const std::string &key) {
	return myCurrentEntry->actionIndex(key);
}

void MultiKeyOptionEntry::onValueChanged(const std::string &key, int index) {
	myCurrentEntry->onValueChanged(key, index);
	if (myExitOnCancelEntry != 0) {
		myExitOnCancelEntry->setVisible(myBimap.codeByIndex(index) == ActionCode::CANCEL);
	}
}

void MultiKeyOptionEntry::setExitOnCancelEntry(ZLOptionEntry *exitOnCancelEntry) {
	myExitOnCancelEntry = exitOnCancelEntry;
}

void MultiKeyOptionEntry::onKeySelected(const std::string &key) {
	if (myExitOnCancelEntry != 0) {
		myExitOnCancelEntry->setVisible(myBimap.codeByIndex(myCurrentEntry->actionIndex(key)) == ActionCode::CANCEL);
	}
}

class OrientationEntry : public ZLComboOptionEntry {

public:
	OrientationEntry(MultiKeyOptionEntry &keyEntry, const ZLResource &resource);
	const std::string &initialValue() const;
	const std::vector<std::string> &values() const;
	void onValueSelected(int index);
	void onAccept(const std::string &value);

private:
	MultiKeyOptionEntry &myKeyEntry;
	const ZLResource &myResource;
};

OrientationEntry::OrientationEntry(MultiKeyOptionEntry &keyEntry, const ZLResource &resource) : myKeyEntry(keyEntry), myResource(resource) {
}

const std::string &OrientationEntry::initialValue() const {
	return values()[0];
}

const std::vector<std::string> &OrientationEntry::values() const {
	static std::vector<std::string> _values;
	if (_values.empty()) {
		_values.push_back(myResource["degrees0"].value());
		_values.push_back(myResource["degrees90ccw"].value());
		_values.push_back(myResource["degrees180"].value());
		_values.push_back(myResource["degrees90cw"].value());
	}
	return _values;
}

void OrientationEntry::onValueSelected(int index) {
	static ZLView::Angle angles[] = {
		ZLView::DEGREES0,
		ZLView::DEGREES90,
		ZLView::DEGREES180,
		ZLView::DEGREES270
	};
	myKeyEntry.setOrientation(angles[index]);
}

void OrientationEntry::onAccept(const std::string&) {
}

class UseSeparateOptionsEntry : public ZLSimpleBooleanOptionEntry {

public:
	UseSeparateOptionsEntry(ZLOptionEntry &keyEntry, OrientationEntry &orientationEntry);
	void onStateChanged(bool state);

private:
	ZLOptionEntry &myKeyEntry;
	OrientationEntry &myOrientationEntry;
};

UseSeparateOptionsEntry::UseSeparateOptionsEntry(ZLOptionEntry &keyEntry, OrientationEntry &orientationEntry) : ZLSimpleBooleanOptionEntry(FBReader::Instance().UseSeparateBindingsOption), myKeyEntry(keyEntry), myOrientationEntry(orientationEntry) {
}

void UseSeparateOptionsEntry::onStateChanged(bool state) {
	ZLSimpleBooleanOptionEntry::onStateChanged(state);
	myOrientationEntry.setVisible(state);
	myKeyEntry.resetView();
}


void ReadingOptionsDialog::createKeyBindingsTab() {
	ZLDialogContent &dialogTab = dialog().createTab(ZLResourceKey("Keys"));
	FBReader &fbreader = FBReader::Instance();
	if (ZLBooleanOption(ZLCategoryKey::EMPTY, ZLOption::PLATFORM_GROUP, ZLOption::FULL_KEYBOARD_CONTROL, false).value()) {
		dialogTab.addOption(ZLResourceKey("grabSystemKeys"), new KeyboardControlEntry());
	}
	ZLResourceKey actionKey("action");
	ZLResourceKey separateKey("separate");
	ZLResourceKey orientationKey("orientation");
	MultiKeyOptionEntry *keyEntry = new MultiKeyOptionEntry(dialogTab.resource(actionKey));
	OrientationEntry *orientationEntry = new OrientationEntry(*keyEntry, dialogTab.resource(orientationKey));
	ZLBooleanOptionEntry *useSeparateBindingsEntry = new UseSeparateOptionsEntry(*keyEntry, *orientationEntry);
	dialogTab.addOption(separateKey, useSeparateBindingsEntry);
	dialogTab.addOption(orientationKey, orientationEntry);
	dialogTab.addOption("", "", keyEntry);
	ZLOptionEntry *exitOnCancelEntry = new ZLSimpleBooleanOptionEntry(fbreader.QuitOnCancelOption);
	keyEntry->setExitOnCancelEntry(exitOnCancelEntry);
	dialogTab.addOption(ZLResourceKey("quitOnCancel"), exitOnCancelEntry);
	exitOnCancelEntry->setVisible(false);
	useSeparateBindingsEntry->onStateChanged(useSeparateBindingsEntry->initialState());
	dialogTab.addOption(ZLResourceKey("keyDelay"), new ZLSimpleSpinOptionEntry(fbreader.KeyDelayOption, 50));
}