/[winpt]/trunk/Src/wptCurrWnd.cpp
ViewVC logotype

Contents of /trunk/Src/wptCurrWnd.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 419 - (show annotations)
Sat Mar 3 19:56:06 2012 UTC (12 years, 11 months ago) by twoaday
File size: 5874 byte(s)
2012-03-03  Timo Schulz  <twoaday@gmx.net>

        * WinPT.cpp (get_file_version): Improved error checking.
	(gpg_set_debug_mode): Likewise.
		

1 /* wptCurrWnd.cpp - Current window code
2 * Copyright (C) 2001-2004, 2006, 2007, 2011-2012 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * WinPT is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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
14 * GNU General Public License for more details.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <windows.h>
22
23 #include "wptW32API.h"
24 #include "wptErrors.h"
25 #include "wptVersion.h"
26
27 #define KEYDOWN(vcode, scode) keybd_event ((vcode), (scode), 0, 0)
28 #define KEYUP(vcode, scode) keybd_event ((vcode), (scode), KEYEVENTF_KEYUP, 0)
29
30
31 /* PTD prototypes from the DLL. */
32 extern "C" HWND PTD_get_current_window (void);
33 extern "C" BOOL PTD_keyboard_send_keys (UINT *keys, UINT n_keys);
34 extern "C" int PTD_is_hook_used (void);
35
36
37 /* Clear the clipboard contents. */
38 static void
39 clip_clear (void)
40 {
41 OpenClipboard (NULL);
42 EmptyClipboard ();
43 CloseClipboard ();
44 }
45
46
47 /* Check if the clipboard returns any text.
48 Return value: 0 on success. */
49 static int
50 clip_check (void)
51 {
52 /* Little backoff time to make sure the clipboard now really
53 contains all data. */
54 Sleep (400);
55
56 if (OpenClipboard (NULL) == FALSE)
57 return WPTERR_CLIP_OPEN;
58 HANDLE clipmem = GetClipboardData (CF_TEXT);
59 if (clipmem == NULL) {
60 CloseClipboard ();
61 return WPTERR_CLIP_GET;
62 }
63 CloseClipboard ();
64 return 0;
65 }
66
67
68 /* Fabricate a "select all" keystroke sequence. */
69 static void
70 window_msg_selectall (void)
71 {
72 unsigned int keys[4];
73
74 if (PTD_is_hook_used ()) {
75 keys[0] = VK_CONTROL | 0x8000;
76 keys[1] = 'A' | 0x8000;
77 keys[2] = 'A';
78 keys[3] = VK_CONTROL;
79 PTD_keyboard_send_keys (keys, 4);
80
81 }
82 else {
83 KEYDOWN (VK_CONTROL, 0x1d);
84 KEYDOWN ((BYTE)'A', 0x1e);
85 KEYUP ((BYTE)'A', 0x1e);
86 KEYUP (VK_CONTROL, 0x1d);
87 }
88 }
89
90
91 /* Fabricate a "paste" keystrok sequence. */
92 static void
93 window_msg_paste (void)
94 {
95 unsigned int keys[4];
96
97 if (PTD_is_hook_used ()) {
98 keys[0] = VK_CONTROL | 0x8000;
99 keys[1] = 'V' | 0x8000;
100 keys[2] = 'V';
101 keys[3] = VK_CONTROL;
102 PTD_keyboard_send_keys (keys, 4);
103 }
104 else {
105 KEYDOWN (VK_CONTROL, 0x1d);
106 KEYDOWN ((BYTE)'V', 0x2f);
107 KEYUP ((BYTE)'V', 0x2f);
108 KEYUP (VK_CONTROL, 0x1d);
109 }
110 }
111
112
113 /* Fabricate a "copy" keystroke sequence. */
114 static void
115 window_msg_copy (void)
116 {
117 unsigned int keys[4];
118
119 if (PTD_is_hook_used ()) {
120 keys[0] = VK_CONTROL | 0x8000;
121 keys[1] = 'C' | 0x8000;
122 keys[2] = 'C';
123 keys[3] = VK_CONTROL;
124 PTD_keyboard_send_keys (keys, 4);
125 }
126 else {
127 KEYDOWN (VK_CONTROL, 0x1d);
128 KEYDOWN ((BYTE)'C', 0x2e);
129 KEYUP ((BYTE)'C', 0x2e);
130 KEYUP (VK_CONTROL, 0x1d);
131 }
132 }
133
134
135 /* Set the position inside the edit control @hwnd. */
136 static inline void
137 window_msg_em_set_pos (HWND hwnd, int begin)
138 {
139 SendMessage (hwnd, EM_SETSEL, 0, begin? 0 : -1);
140 }
141
142
143 /* Get the current window and return it in @r_main.
144 Return value: The window with the keyboard focus. */
145 static HWND
146 get_current_wnd (HWND main, int use_hotkey, HWND *r_main)
147 {
148 if (!use_hotkey) {
149 *r_main = GetNextWindow (main, GW_HWNDNEXT);
150 return PTD_get_current_window ();
151 }
152
153 HWND prev = GetForegroundWindow ();
154 DWORD src_thread = GetCurrentThreadId();
155 DWORD dst_thread = GetWindowThreadProcessId (prev, NULL);
156
157 SetForegroundWindow (prev);
158 AttachThreadInput (src_thread, dst_thread, TRUE);
159 HWND fg = GetFocus ();
160 AttachThreadInput (src_thread, dst_thread, FALSE);
161
162 *r_main = prev;
163 return fg;
164 }
165
166
167 /* Retrieve the contents of the current window based on
168 the old window handle @old_hwnd and store the information
169 in @ctx.
170 A return value of 0 indicates a problem. */
171 int
172 copy_window_content (HWND old_hwnd, HWND *r_main, HWND *r_focus, int use_hotkey)
173 {
174 int rc = WPTERR_GENERAL;
175 HWND hwnd = get_current_wnd (old_hwnd, use_hotkey, r_main);
176 if (!hwnd)
177 return rc;
178 *r_focus = hwnd;
179 clip_clear ();
180
181 DWORD src_thread = GetCurrentThreadId ();
182 DWORD dst_thread = GetWindowThreadProcessId (hwnd, NULL);
183
184 AttachThreadInput (src_thread, dst_thread, TRUE);
185 SetFocus (hwnd);
186 /* First we try to send a simple clipboard copying command and check
187 if the clipboard contains some text. */
188 SendMessage (hwnd, WM_COPY, 0, 0);
189 if (!clip_check ()) {
190 rc = WPTERR_SUCESS;
191 goto leave;
192 }
193 /* Then the check if the window is an edit control */
194 window_msg_em_set_pos (hwnd, 0);
195 SendMessage (hwnd, WM_COPY, 0, 0);
196 if (!clip_check ()) {
197 rc = WPTERR_SUCESS;
198 goto leave;
199 }
200
201 /* The last try is to send a mark all and copy to clipboard command */
202 window_msg_selectall ();
203 window_msg_copy ();
204 if (!clip_check ())
205 rc = WPTERR_SUCESS;
206
207 leave:
208 AttachThreadInput (src_thread, dst_thread, FALSE);
209 return rc;
210 }
211
212
213 /* Restore the clipboard contents to the window handle given
214 in @ctx and reset the current window to @old_hwnd if possible. */
215 int
216 paste_window_content (HWND old_hwnd, HWND main, HWND focus)
217 {
218 HWND hwnd = focus;
219
220 /* Restore window when in minimized state. */
221 if (IsIconic (main))
222 ShowWindow (hwnd, SW_SHOWNORMAL);
223 SetForegroundWindow (hwnd);
224
225 DWORD src_thread = GetCurrentThreadId();
226 DWORD dst_thread = GetWindowThreadProcessId (hwnd, NULL);
227
228 AttachThreadInput (src_thread, dst_thread, TRUE);
229 HWND lost = SetFocus (hwnd);
230 window_msg_em_set_pos (hwnd, 0);
231 window_msg_paste ();
232 window_msg_em_set_pos (hwnd, 1);
233 AttachThreadInput (src_thread, dst_thread, FALSE);
234
235 SetForegroundWindow (lost);
236 return 0;
237 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26