File: CAddScreen.cpp

package info (click to toggle)
synergy 1.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 4,664 kB
  • ctags: 5,482
  • sloc: cpp: 46,292; sh: 3,392; makefile: 938; ansic: 82
file content (427 lines) | stat: -rw-r--r-- 11,369 bytes parent folder | download | duplicates (2)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * synergy -- mouse and keyboard sharing utility
 * Copyright (C) 2002 Chris Schoeneman
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file COPYING that should have accompanied this file.
 * 
 * This package 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.
 */

#include "CConfig.h"
#include "KeyTypes.h"
#include "OptionTypes.h"
#include "ProtocolTypes.h"
#include "CStringUtil.h"
#include "CArch.h"
#include "CAddScreen.h"
#include "LaunchUtil.h"
#include "resource.h"

struct CModifierInfo {
public:
	int				m_ctrlID;
	const char*		m_name;
	KeyModifierID	m_modifierID;
	OptionID		m_optionID;
};

static const CModifierInfo s_modifiers[] = {
	{ IDC_ADD_MOD_SHIFT, "Shift",
		kKeyModifierIDShift,    kOptionModifierMapForShift   },
	{ IDC_ADD_MOD_CTRL,  "Ctrl",
		kKeyModifierIDControl,  kOptionModifierMapForControl },
	{ IDC_ADD_MOD_ALT,   "Alt",
		kKeyModifierIDAlt,      kOptionModifierMapForAlt     },
	{ IDC_ADD_MOD_META,  "Meta",
		kKeyModifierIDMeta,     kOptionModifierMapForMeta    },
	{ IDC_ADD_MOD_SUPER, "Super",
		kKeyModifierIDSuper,    kOptionModifierMapForSuper   }
};

static const KeyModifierID baseModifier = kKeyModifierIDShift;

//
// CAddScreen
//

CAddScreen*			CAddScreen::s_singleton = NULL;

CAddScreen::CAddScreen(HWND parent, CConfig* config, const CString& name) :
	m_parent(parent),
	m_config(config),
	m_name(name)
{
	assert(s_singleton == NULL);
	s_singleton = this;
}

CAddScreen::~CAddScreen()
{
	s_singleton = NULL;
}

bool
CAddScreen::doModal()
{
	// do dialog
	return (DialogBoxParam(s_instance, MAKEINTRESOURCE(IDD_ADD),
								m_parent, dlgProc, (LPARAM)this) != 0);
}

CString
CAddScreen::getName() const
{
	return m_name;
}

void
CAddScreen::init(HWND hwnd)
{
	// set title
	CString title;
	if (m_name.empty()) {
		title = getString(IDS_ADD_SCREEN);
	}
	else {
		title = CStringUtil::format(
							getString(IDS_EDIT_SCREEN).c_str(),
							m_name.c_str());
	}
	SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)title.c_str());

	// fill in screen name
	HWND child = getItem(hwnd, IDC_ADD_SCREEN_NAME_EDIT);
	SendMessage(child, WM_SETTEXT, 0, (LPARAM)m_name.c_str());

	// fill in aliases
	CString aliases;
	for (CConfig::all_const_iterator index = m_config->beginAll();
								index != m_config->endAll(); ++index) {
		if (CStringUtil::CaselessCmp::equal(index->second, m_name) &&
			!CStringUtil::CaselessCmp::equal(index->second, index->first)) {
			if (!aliases.empty()) {
				aliases += "\r\n";
			}
			aliases += index->first;
		}
	}
	child = getItem(hwnd, IDC_ADD_ALIASES_EDIT);
	SendMessage(child, WM_SETTEXT, 0, (LPARAM)aliases.c_str());

	// set options
	CConfig::CScreenOptions options;
	getOptions(options);
	CConfig::CScreenOptions::const_iterator index;
	child = getItem(hwnd, IDC_ADD_HD_CAPS_CHECK);
	index = options.find(kOptionHalfDuplexCapsLock);
	setItemChecked(child, (index != options.end() && index->second != 0));
	child = getItem(hwnd, IDC_ADD_HD_NUM_CHECK);
	index = options.find(kOptionHalfDuplexNumLock);
	setItemChecked(child, (index != options.end() && index->second != 0));
	child = getItem(hwnd, IDC_ADD_HD_SCROLL_CHECK);
	index = options.find(kOptionHalfDuplexScrollLock);
	setItemChecked(child, (index != options.end() && index->second != 0));

	// modifier options
	for (UInt32 i = 0; i < sizeof(s_modifiers) /
								sizeof(s_modifiers[0]); ++i) {
		child = getItem(hwnd, s_modifiers[i].m_ctrlID);

		// fill in options
		for (UInt32 j = 0; j < sizeof(s_modifiers) /
									sizeof(s_modifiers[0]); ++j) {
			SendMessage(child, CB_ADDSTRING, 0,
								(LPARAM)s_modifiers[j].m_name);
		}

		// choose current value
		index            = options.find(s_modifiers[i].m_optionID);
		KeyModifierID id = s_modifiers[i].m_modifierID;
		if (index != options.end()) {
			id = index->second;
		}
		SendMessage(child, CB_SETCURSEL, id - baseModifier, 0);
	}

	// dead corners
	UInt32 corners = 0;
	index = options.find(kOptionScreenSwitchCorners);
	if (index != options.end()) {
		corners = index->second;
	}
	child = getItem(hwnd, IDC_ADD_DC_TOP_LEFT);
	setItemChecked(child, (corners & kTopLeftMask) != 0);
	child = getItem(hwnd, IDC_ADD_DC_TOP_RIGHT);
	setItemChecked(child, (corners & kTopRightMask) != 0);
	child = getItem(hwnd, IDC_ADD_DC_BOTTOM_LEFT);
	setItemChecked(child, (corners & kBottomLeftMask) != 0);
	child = getItem(hwnd, IDC_ADD_DC_BOTTOM_RIGHT);
	setItemChecked(child, (corners & kBottomRightMask) != 0);
	index = options.find(kOptionScreenSwitchCornerSize);
	SInt32 size = 0;
	if (index != options.end()) {
		size = index->second;
	}
	char buffer[20];
	sprintf(buffer, "%d", size);
	child = getItem(hwnd, IDC_ADD_DC_SIZE);
	SendMessage(child, WM_SETTEXT, 0, (LPARAM)buffer);
}

bool
CAddScreen::save(HWND hwnd)
{
	// get the old aliases and options
	CStringList oldAliases;
	getAliases(oldAliases);
	CConfig::CScreenOptions options;
	getOptions(options);

	// extract name and aliases
	CString newName;
	HWND child = getItem(hwnd, IDC_ADD_SCREEN_NAME_EDIT);
	newName = getWindowText(child);
	CStringList newAliases;
	child = getItem(hwnd, IDC_ADD_ALIASES_EDIT);
	tokenize(newAliases, getWindowText(child));

	// name must be valid
	if (!m_config->isValidScreenName(newName)) {
		showError(hwnd, CStringUtil::format(
						getString(IDS_INVALID_SCREEN_NAME).c_str(),
						newName.c_str()));
		return false;
	}

	// aliases must be valid
	for (CStringList::const_iterator index = newAliases.begin();
						index != newAliases.end(); ++index) {
		if (!m_config->isValidScreenName(*index)) {
			showError(hwnd, CStringUtil::format(
						getString(IDS_INVALID_SCREEN_NAME).c_str(),
						index->c_str()));
			return false;
		}
	}

	// new name may not be in the new alias list
	if (isNameInList(newAliases, newName)) {
		showError(hwnd, CStringUtil::format(
						getString(IDS_SCREEN_NAME_IS_ALIAS).c_str(),
						newName.c_str()));
		return false;
	}

	// name must not exist in config but allow same name.  also
	// allow name if it exists in the old alias list but not the
	// new one.
	if (m_config->isScreen(newName) &&
		!CStringUtil::CaselessCmp::equal(newName, m_name) &&
		!isNameInList(oldAliases, newName)) {
		showError(hwnd, CStringUtil::format(
						getString(IDS_DUPLICATE_SCREEN_NAME).c_str(),
						newName.c_str()));
		return false;
	}

	// aliases must not exist in config but allow same aliases and
	// allow an alias to be the old name.
	for (CStringList::const_iterator index = newAliases.begin();
						index != newAliases.end(); ++index) {
		if (m_config->isScreen(*index) &&
			!CStringUtil::CaselessCmp::equal(*index, m_name) &&
			!isNameInList(oldAliases, *index)) {
			showError(hwnd, CStringUtil::format(
						getString(IDS_DUPLICATE_SCREEN_NAME).c_str(),
						index->c_str()));
			return false;
		}
	}

	// dead corner size must be non-negative
	child = getItem(hwnd, IDC_ADD_DC_SIZE);
	CString valueString = getWindowText(child);
	int cornerSize = atoi(valueString.c_str());
	if (cornerSize < 0) {
		showError(hwnd, CStringUtil::format(
							getString(IDS_INVALID_CORNER_SIZE).c_str(),
							valueString.c_str()));
		SetFocus(child);
		return false;
	}

	// collect options
	child = getItem(hwnd, IDC_ADD_HD_CAPS_CHECK);
	if (isItemChecked(child)) {
		options[kOptionHalfDuplexCapsLock] = 1;
	}
	else {
		options.erase(kOptionHalfDuplexCapsLock);
	}
	child = getItem(hwnd, IDC_ADD_HD_NUM_CHECK);
	if (isItemChecked(child)) {
		options[kOptionHalfDuplexNumLock] = 1;
	}
	else {
		options.erase(kOptionHalfDuplexNumLock);
	}
	child = getItem(hwnd, IDC_ADD_HD_SCROLL_CHECK);
	if (isItemChecked(child)) {
		options[kOptionHalfDuplexScrollLock] = 1;
	}
	else {
		options.erase(kOptionHalfDuplexScrollLock);
	}

	// save modifier options
	for (UInt32 i = 0; i < sizeof(s_modifiers) /
								sizeof(s_modifiers[0]); ++i) {
		child            = getItem(hwnd, s_modifiers[i].m_ctrlID);
		KeyModifierID id = static_cast<KeyModifierID>(
							SendMessage(child, CB_GETCURSEL, 0, 0) +
								baseModifier);
		if (id != s_modifiers[i].m_modifierID) {
			options[s_modifiers[i].m_optionID] = id;
		}
		else {
			options.erase(s_modifiers[i].m_optionID);
		}
	}

	// save dead corner options
	UInt32 corners = 0;
	if (isItemChecked(getItem(hwnd, IDC_ADD_DC_TOP_LEFT))) {
		corners |= kTopLeftMask;
	}
	if (isItemChecked(getItem(hwnd, IDC_ADD_DC_TOP_RIGHT))) {
		corners |= kTopRightMask;
	}
	if (isItemChecked(getItem(hwnd, IDC_ADD_DC_BOTTOM_LEFT))) {
		corners |= kBottomLeftMask;
	}
	if (isItemChecked(getItem(hwnd, IDC_ADD_DC_BOTTOM_RIGHT))) {
		corners |= kBottomRightMask;
	}
	options[kOptionScreenSwitchCorners]    = corners;
	options[kOptionScreenSwitchCornerSize] = cornerSize;

	// save new data to config
	if (m_name.empty()) {
		// added screen
		m_config->addScreen(newName);
	}
	else {
		// edited screen
		m_config->removeAliases(m_name);
		m_config->removeOptions(m_name);
		m_config->renameScreen(m_name, newName);
	}
	m_name = newName;
	for (CStringList::const_iterator index = newAliases.begin();
							index != newAliases.end(); ++index) {
		m_config->addAlias(m_name, *index);
	}
	for (CConfig::CScreenOptions::const_iterator
							index  = options.begin();
							index != options.end(); ++index) {
		m_config->addOption(m_name, index->first, index->second);
	}

	return true;
}

BOOL
CAddScreen::doDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM)
{
	switch (message) {
	case WM_INITDIALOG:
		init(hwnd);
		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDOK:
			if (save(hwnd)) {
				EndDialog(hwnd, 1);
			}
			return TRUE;

		case IDCANCEL:
			EndDialog(hwnd, 0);
			return TRUE;
		}
		break;

	default:
		break;
	}

	return FALSE;
}

BOOL CALLBACK
CAddScreen::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	return s_singleton->doDlgProc(hwnd, message, wParam, lParam);
}

void
CAddScreen::getAliases(CStringList& aliases) const
{
	for (CConfig::all_const_iterator index = m_config->beginAll();
								index != m_config->endAll(); ++index) {
		if (CStringUtil::CaselessCmp::equal(index->second, m_name) &&
			!CStringUtil::CaselessCmp::equal(index->second, index->first)) {
			aliases.push_back(index->first);
		}
	}
}

void
CAddScreen::getOptions(CConfig::CScreenOptions& optionsOut) const
{
	const CConfig::CScreenOptions* options = m_config->getOptions(m_name);
	if (options == NULL) {
		optionsOut = CConfig::CScreenOptions();
	}
	else {
		optionsOut = *options;
	}
}

void
CAddScreen::tokenize(CStringList& tokens, const CString& src)
{
	// find first non-whitespace
	CString::size_type x = src.find_first_not_of(" \t\r\n");
	if (x == CString::npos) {
		return;
	}

	// find next whitespace
	do {
		CString::size_type y = src.find_first_of(" \t\r\n", x);
		if (y == CString::npos) {
			y = src.size();
		}
		tokens.push_back(src.substr(x, y - x));
		x = src.find_first_not_of(" \t\r\n", y);
	} while (x != CString::npos);
}

bool
CAddScreen::isNameInList(const CStringList& names, const CString& name)
{
	for (CStringList::const_iterator index = names.begin();
								index != names.end(); ++index) {
		if (CStringUtil::CaselessCmp::equal(name, *index)) {
			return true;
		}
	}
	return false;
}