summaryrefslogtreecommitdiff
path: root/plugins/ChangeKeyboardLayout/src/hook_events.cpp
blob: ea788fb912632b77ea90a1133492d7e0e17731d2 (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
#include "commonheaders.h"

INT_PTR APIChangeLayout(WPARAM wParam, LPARAM lParam)
{
	BOOL CurrentWord;
	CurrentWord = moOptions.CurrentWordLayout;
	return ChangeLayout((HWND)wParam, TOT_Layout, CurrentWord);
}

INT_PTR APIGetLayoutOfText(WPARAM wParam, LPARAM lParam)
{
	LPTSTR ptszInText;
	ptszInText = (TCHAR*)lParam;
	return (int) GetLayoutOfText(ptszInText);
}

INT_PTR APIChangeTextLayout(WPARAM wParam, LPARAM lParam)
{
	LPTSTR ptszInText;
	CKLLayouts *ckllFromToLay;

	ptszInText = (TCHAR*)wParam;
	ckllFromToLay = (CKLLayouts*)lParam;

	return (int) ChangeTextLayout(ptszInText, ckllFromToLay->hklFrom, ckllFromToLay->hklTo, ckllFromToLay->bTwoWay);
}

void ReadMainOptions()
{
	moOptions.dwHotkey_Layout = DBGetContactSettingDword(NULL, ModuleName, "HotkeyLayout", 119);
	moOptions.dwHotkey_Layout2 = DBGetContactSettingDword(NULL, ModuleName, "HotkeyLayout2", 120);
	moOptions.dwHotkey_Case = DBGetContactSettingDword(NULL, ModuleName, "HotkeyCase", 121);
	moOptions.CurrentWordLayout = DBGetContactSettingByte(NULL, ModuleName, "CurrentWordLayout", 0);
	moOptions.CurrentWordLayout2 = DBGetContactSettingByte(NULL, ModuleName, "CurrentWordLayout2", 1);
	moOptions.CurrentWordCase = DBGetContactSettingByte(NULL, ModuleName, "CurrentWordCase", 0);
	moOptions.TwoWay = DBGetContactSettingByte(NULL, ModuleName, "TwoWay", 1);
	moOptions.ChangeSystemLayout = DBGetContactSettingByte(NULL, ModuleName, "ChangeSystemLayout", 1);
	moOptions.CopyToClipboard = DBGetContactSettingByte(NULL, ModuleName, "CopyToClipboard", 0);
	moOptions.ShowPopup = DBGetContactSettingByte(NULL, ModuleName, "ShowPopup", 1);
	moOptions.bCaseOperations = DBGetContactSettingByte(NULL, ModuleName, "CaseOperations", 0);
}

void WriteMainOptions()
{
	DBWriteContactSettingDword(NULL, ModuleName, "HotkeyLayout", moOptions.dwHotkey_Layout);
	DBWriteContactSettingDword(NULL, ModuleName, "HotkeyLayout2", moOptions.dwHotkey_Layout2);
	DBWriteContactSettingDword(NULL, ModuleName, "HotkeyCase", moOptions.dwHotkey_Case);
	DBWriteContactSettingByte(NULL, ModuleName, "CurrentWordLayout", moOptions.CurrentWordLayout);
	DBWriteContactSettingByte(NULL, ModuleName, "CurrentWordLayout2", moOptions.CurrentWordLayout2);
	DBWriteContactSettingByte(NULL, ModuleName, "CurrentWordCase", moOptions.CurrentWordCase);
	DBWriteContactSettingByte(NULL, ModuleName, "TwoWay", moOptions.TwoWay);
	DBWriteContactSettingByte(NULL, ModuleName, "ChangeSystemLayout", moOptions.ChangeSystemLayout);
	DBWriteContactSettingByte(NULL, ModuleName, "CopyToClipboard", moOptions.CopyToClipboard);
	DBWriteContactSettingByte(NULL, ModuleName, "ShowPopup", moOptions.ShowPopup);
	DBWriteContactSettingByte(NULL, ModuleName, "CaseOperations", moOptions.bCaseOperations);
}


void ReadPopupOptions()
{
	poOptions.bColourType = DBGetContactSettingByte(NULL, ModuleName, "ColourType", 0);
	poOptions.crBackColour = (COLORREF)DBGetContactSettingDword(NULL, ModuleName, "BackColor", 0xD2CABF);
	poOptions.crTextColour = (COLORREF)DBGetContactSettingDword(NULL, ModuleName, "TextColor", 0x000000);
	poOptions.bTimeoutType = DBGetContactSettingByte(NULL, ModuleName, "TimeoutType", 0);
	poOptions.bTimeout = DBGetContactSettingByte(NULL, ModuleName, "Timeout", 10);
	poOptions.bLeftClick = DBGetContactSettingByte(NULL, ModuleName, "LeftClick", 0);
	poOptions.bRightClick = DBGetContactSettingByte(NULL, ModuleName, "RightClick", 1);
}

void WritePopupOptions()
{
	DBWriteContactSettingByte(NULL, ModuleName, "ColourType", poOptions.bColourType);
	DBWriteContactSettingDword(NULL, ModuleName, "BackColor", poOptions.crBackColour);
	DBWriteContactSettingDword(NULL, ModuleName, "TextColor", poOptions.crTextColour);
	DBWriteContactSettingByte(NULL, ModuleName, "TimeoutType", poOptions.bTimeoutType);
	DBWriteContactSettingByte(NULL, ModuleName, "Timeout", poOptions.bTimeout);
	DBWriteContactSettingByte(NULL, ModuleName, "LeftClick", poOptions.bLeftClick);
	DBWriteContactSettingByte(NULL, ModuleName, "RightClick", poOptions.bRightClick);
}

void RegPopupActions()
{
	if ( ServiceExists(MS_POPUP_ADDPOPUP)) {
		poOptions.paActions[0].cbSize = sizeof(POPUPACTION);
		strcpy(poOptions.paActions[0].lpzTitle, ModuleName);
		strcat(poOptions.paActions[0].lpzTitle, "/Copy to clipboard");
		poOptions.paActions[0].flags = PAF_ENABLED;
		poOptions.paActions[0].wParam = poOptions.paActions[0].lParam = 0;
		poOptions.paActions[0].lchIcon = hCopyIcon;
		CallService(MS_POPUP_REGISTERACTIONS, (WPARAM)&poOptions.paActions, 1);
	}
}

int OnIconsChanged(WPARAM wParam, LPARAM lParam)
{
	hPopupIcon = Skin_GetIcon("ckl_popup_icon");
	hCopyIcon = Skin_GetIcon("ckl_copy_icon");
	RegPopupActions();
	return 0;
}

static IconItem iconList[] = 
{
	{ LPGEN("Popup"), "ckl_popup_icon", IDI_POPUPICON },
	{ LPGEN("Copy to clipboard"), "ckl_copy_icon", IDI_COPYICON }
};

int ModulesLoaded(WPARAM wParam, LPARAM lParam)
{
	int i, iRes;
	DBVARIANT dbv = {0};
	LPCTSTR ptszEmptySting = _T("");
	LPTSTR ptszCurrLayout;
	LPSTR ptszTemp;

	//Çàïîëíÿåì êîíôèãóðàöèîííûå ñòðîêè èç áàçû. Åñëè èõ òàì íåò - ãåíåðèðóåì.
	for (i = 0; i < bLayNum; i++) {
		ptszCurrLayout = GenerateLayoutString(hklLayouts[i]);
		ptszTemp = GetNameOfLayout(hklLayouts[i]);
		iRes = DBGetContactSettingTString(NULL, ModuleName, ptszTemp, &dbv);
		if (iRes != 0)
			ptszLayStrings[i] = ptszCurrLayout;
		else
			if(_tcscmp((dbv.ptszVal), ptszEmptySting) == 0) {
				ptszLayStrings[i] = ptszCurrLayout;
				mir_free(dbv.ptszVal);
			}
			else
			{
				ptszLayStrings[i] = dbv.ptszVal;
				if(_tcscmp(ptszCurrLayout, ptszLayStrings[i]) == 0)
					DBDeleteContactSetting(NULL, ModuleName, ptszTemp);
				mir_free(ptszCurrLayout);
			}
			mir_free(ptszTemp);
	}

	// Ïðî÷èòàåì îñíîâíûå íàñòðîéêè
	ReadMainOptions();

	// Ïðî÷èòàåì íàñòðîéêè ïîïàïîâ
	ReadPopupOptions();

	// Çàðåãèì çâóê
	SkinAddNewSoundEx(SND_ChangeLayout, ModuleName, LPGEN("Changing Layout"));
	SkinAddNewSoundEx(SND_ChangeCase, ModuleName, LPGEN("Changing Case"));

	// Õóê íà íàæàòèå êëàâèøè
	kbHook_All = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)Keyboard_Hook, NULL, GetCurrentThreadId());

	// Çàðåãèì ñåðâèñû
	hChangeLayout = CreateServiceFunction(MS_CKL_CHANGELAYOUT, APIChangeLayout);
	hGetLayoutOfText = CreateServiceFunction(MS_CKL_GETLAYOUTOFTEXT, APIGetLayoutOfText);
	hChangeTextLayout = CreateServiceFunction(MS_CKL_CHANGETEXTLAYOUT, APIChangeTextLayout);

	// IcoLib support
	Icon_Register(hInst, ModuleName, iconList, SIZEOF(iconList));

	HookEvent(ME_SKIN2_ICONSCHANGED, OnIconsChanged);

	OnIconsChanged(0, 0);
	RegPopupActions();

	DBWriteContactSettingDword(NULL, ModuleName, "CurrentVer", PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM));
	return 0;
}

int OnOptionsInitialise(WPARAM wParam, LPARAM lParam)
{
	OPTIONSDIALOGPAGE odp = { sizeof(odp) };
	odp.hInstance = hInst;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_MAIN_OPTION_FORM);
	odp.pszTitle = ModuleName;
	odp.pszGroup = LPGEN("Message Sessions");
	odp.flags = ODPF_BOLDGROUPS;
	odp.pfnDlgProc = DlgMainProcOptions;
	Options_AddPage(wParam, &odp);

	if ( ServiceExists(MS_POPUP_ADDPOPUP)) {
		odp.pszTemplate = MAKEINTRESOURCEA(IDD_POPUP_OPTION_FORM);
		odp.pszGroup = LPGEN("PopUps");
		odp.pfnDlgProc = DlgPopupsProcOptions;
		Options_AddPage(wParam, &odp);
	}
	return 0;
}

LRESULT CALLBACK Keyboard_Hook(int code, WPARAM wParam, LPARAM lParam)
{
	DWORD lcode;

	if (code == HC_ACTION)
	{
		lcode = 0;
		if ((GetKeyState(VK_SHIFT)&0x8000)) lcode |= HOTKEYF_SHIFT;
		if ((GetKeyState(VK_CONTROL)&0x8000)) lcode |= HOTKEYF_CONTROL;
		if ((GetKeyState(VK_MENU)&0x8000)) lcode |= HOTKEYF_ALT;
		if ((GetKeyState(VK_LWIN)&0x8000)||(GetKeyState(VK_RWIN)&0x8000)) lcode |= HOTKEYF_EXT;
		lcode = lcode<<8;

		if ((wParam != VK_SHIFT) && (wParam != VK_MENU) && (wParam != VK_CONTROL) && (wParam != VK_LWIN) && (wParam != VK_RWIN))
			lcode += wParam;

		// Ïðîâåðêà íà ïóñòîé õîòêåé. Èíà÷å - ïèçäåö, êàê â âåðñèè 1.4
		if (lcode != 0) {
			if ((lcode == moOptions.dwHotkey_Layout) && (!(lParam & 0x40000000))) {
				ChangeLayout(NULL, TOT_Layout, moOptions.CurrentWordLayout);
				return 1;
			}
			if ((lcode == moOptions.dwHotkey_Layout2) && (!(lParam & 0x40000000))) {
				ChangeLayout(NULL, TOT_Layout, moOptions.CurrentWordLayout2);
				return 1;
			}
			if ((lcode == moOptions.dwHotkey_Case) && (!(lParam & 0x40000000))) {
				ChangeLayout(NULL, TOT_Case, moOptions.CurrentWordCase);
				return 1;
			}
		}
	}
	return CallNextHookEx(kbHook_All, code, wParam, lParam);
}

int CALLBACK CKLPopupDlgProc(HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam)
{
	LPTSTR ptszPopupText;

	ptszPopupText = (LPTSTR)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd, (LPARAM)&ptszPopupText);
	switch(uiMessage) {
	case WM_COMMAND:
		if (HIWORD(wParam) == STN_CLICKED) {
			if (!IsBadStringPtr(ptszPopupText, MaxTextSize))
				CopyTextToClipboard(ptszPopupText);
			PUDeletePopUp(hWnd);
		}
		break;

	case WM_CONTEXTMENU:
		PUDeletePopUp(hWnd);
		break;

	case UM_POPUPACTION:
		if ((lParam == 0) && (!IsBadStringPtr(ptszPopupText, MaxTextSize)))
			CopyTextToClipboard(ptszPopupText);
		break;

	case UM_FREEPLUGINDATA:
		mir_free(ptszPopupText);
		return TRUE;
	}

	return DefWindowProc(hWnd, uiMessage, wParam, lParam);
}