1 |
/* wptHotkey.cpp - Systemwide hotkeys |
2 |
* Copyright (C) 2001, 2002, 2003, 2006 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <windows.h> |
26 |
|
27 |
#include "wptHotkey.h" |
28 |
#include "wptGPG.h" |
29 |
#include "wptRegistry.h" |
30 |
#include "wptErrors.h" |
31 |
#include "wptNLS.h" |
32 |
#include "wptTypes.h" |
33 |
|
34 |
|
35 |
/* List of all predefined hotkeys. */ |
36 |
hotkey_s wpt_hotkeys[] = { |
37 |
/* ALT+CTRL+ */ |
38 |
{"ClipEncrypt", 1, WPT_CLIP_ENCRYPT_ID, 1, 0, 0x45}, /* E */ |
39 |
{"ClipDecrypt", 1, WPT_CLIP_DECRYPT_VERIFY_ID, 1, 0, 0x44}, /* D */ |
40 |
{"ClipSign", 1, WPT_CLIP_SIGN_ID, 1, 0, 0x53}, /* S */ |
41 |
{"ClipSignEnc", 1, WPT_CLIP_SIGN_ENCRYPT_ID, 1, 0, 0x42}, /* B */ |
42 |
/* ALT+SHIFT+ */ |
43 |
{"CwsEncrypt", 1, WPT_CURRWND_ENCRYPT_ID, 0, 1, 0x45}, /* E */ |
44 |
{"CwsDecrypt", 1, WPT_CURRWND_DECRYPT_VERIFY_ID, 0, 1, 0x44}, /* D */ |
45 |
{"CwsSign", 1, WPT_CURRWND_SIGN_ID, 0, 1, 0x53}, /* S */ |
46 |
{"CwsSignEnc", 1, WPT_CURRWND_SIGN_ENCRYPT_ID, 0, 1, 0x42}, /* B */ |
47 |
/* Agent ALT+CTRL+ */ |
48 |
{"AgentForget", 1, WPT_AGENT_FORGET_ID, 1, 0, 0x46}, /* F */ |
49 |
{0} |
50 |
}; |
51 |
|
52 |
|
53 |
/* Global error variable. */ |
54 |
static int err_hotkey = 0; |
55 |
|
56 |
const char* |
57 |
hotkeys_strerror (void) |
58 |
{ |
59 |
switch (err_hotkey) { |
60 |
case 0: return NULL; |
61 |
case 1: return _("Clipboard Encrypt (ALT+CTRL+E)"); |
62 |
case 2: return _("Clipboard Decrypt/Verify (ALT+CTRL+D)"); |
63 |
case 3: return _("Clipboard Sign (ALT+CTRL+S)"); |
64 |
case 4: return _("Clipboard Sign Encrypt (ALT+CTRL+B)"); |
65 |
case 5: return _("Current Window Encrypt (ALT+SHIFT+E)"); |
66 |
case 6: return _("Current Window Decrypt/Verify (ALT+SHIFT+D)"); |
67 |
case 7: return _("Current Window Sign (ALT+SHIFT+S)"); |
68 |
case 8: return _("Current Window Sign Encrypt (ALT+SHIFT+B"); |
69 |
default: return _("Unknown Hotkey"); |
70 |
} |
71 |
return NULL; |
72 |
} |
73 |
|
74 |
|
75 |
/* Register all enabled hotkeys. */ |
76 |
int |
77 |
hotkeys_register (HWND wnd) |
78 |
{ |
79 |
int j = 0, rc = 0; |
80 |
|
81 |
err_hotkey = 0; /* reset */ |
82 |
for (j=0; wpt_hotkeys[j].id != 0; j++) { |
83 |
if (wpt_hotkeys[j].enabled) { |
84 |
rc = hotkey_register_single (wnd, &wpt_hotkeys[j]); |
85 |
if (rc) { |
86 |
err_hotkey = j+1; |
87 |
break; |
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
return rc; |
93 |
} |
94 |
|
95 |
|
96 |
/* Unregister all hotkeys. */ |
97 |
int |
98 |
hotkeys_unregister (HWND wnd) |
99 |
{ |
100 |
int j = 0; |
101 |
|
102 |
for (j=0; wpt_hotkeys[j].id != 0; j++) |
103 |
hotkey_unregister_single (wnd, &wpt_hotkeys[j]); |
104 |
|
105 |
return 0; |
106 |
} |
107 |
|
108 |
|
109 |
/* Register a single hotkey @hk. */ |
110 |
int |
111 |
hotkey_register_single (HWND wnd, hotkey_t hk) |
112 |
{ |
113 |
int rc; |
114 |
int keymod = 0; |
115 |
|
116 |
if (!hk->enabled) |
117 |
return 0; |
118 |
if (hk->alt_ctrl) |
119 |
keymod = MOD_CONTROL | MOD_ALT; |
120 |
else if (hk->alt_shift) |
121 |
keymod = MOD_ALT | MOD_SHIFT; |
122 |
|
123 |
rc = RegisterHotKey (wnd, hk->id, keymod, hk->key); |
124 |
if (!rc) |
125 |
return WPTERR_HOTKEY; |
126 |
return 0; |
127 |
} |
128 |
|
129 |
|
130 |
/* Unregister a single hotkey @hk. */ |
131 |
int |
132 |
hotkey_unregister_single (HWND wnd, hotkey_t hk) |
133 |
{ |
134 |
int rc = 0; |
135 |
|
136 |
rc = UnregisterHotKey (wnd, hk->id); |
137 |
return rc==0? WPTERR_HOTKEY: 0; |
138 |
} |
139 |
|
140 |
|
141 |
void |
142 |
hotkey_disable (hotkey_t hk) |
143 |
{ |
144 |
hk->enabled = 0; |
145 |
} |
146 |
|
147 |
|
148 |
void |
149 |
hotkey_enable (hotkey_t hk, const char *key) |
150 |
{ |
151 |
if (strlen (key) > 2) |
152 |
BUG (0); |
153 |
hk->key = *key; |
154 |
hk->enabled = 1; |
155 |
} |