1 |
werner |
36 |
/* wptCurrWnd.cpp - Current window code |
2 |
twoaday |
255 |
* Copyright (C) 2001-2004, 2006 Timo Schulz |
3 |
werner |
36 |
* |
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 |
|
|
* 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 "wptW32API.h" |
28 |
|
|
#include "wptErrors.h" |
29 |
werner |
57 |
#include "wptVersion.h" |
30 |
werner |
36 |
|
31 |
twoaday |
255 |
#define KEYDOWN(vcode, scode) keybd_event ((vcode), (scode), 0, 0) |
32 |
twoaday |
273 |
#define KEYUP(vcode, scode) keybd_event ((vcode), (scode), KEYEVENTF_KEYUP, 0) |
33 |
werner |
36 |
|
34 |
twoaday |
255 |
extern "C" HWND PTD_get_curr_hwnd (void); |
35 |
|
|
extern "C" BOOL PTD_keyb_send (UINT *keys, UINT n_keys); |
36 |
|
|
extern "C" int PTD_is_used (void); |
37 |
werner |
36 |
|
38 |
twoaday |
255 |
|
39 |
|
|
/* Clear the clipboard contents. */ |
40 |
werner |
36 |
static void |
41 |
|
|
clip_clear (void) |
42 |
|
|
{ |
43 |
|
|
OpenClipboard (NULL); |
44 |
|
|
EmptyClipboard (); |
45 |
|
|
CloseClipboard (); |
46 |
twoaday |
226 |
} |
47 |
werner |
36 |
|
48 |
|
|
|
49 |
twoaday |
226 |
/* Check if the clipboard returns any data. |
50 |
|
|
Return value: 0 on success. */ |
51 |
werner |
36 |
static int |
52 |
twoaday |
226 |
clip_check (void) |
53 |
werner |
36 |
{ |
54 |
|
|
HANDLE clipmem; |
55 |
|
|
|
56 |
twoaday |
255 |
/* Little backoff time to make sure the clipboard now really |
57 |
|
|
contains all data. */ |
58 |
|
|
Sleep (400); |
59 |
werner |
36 |
|
60 |
twoaday |
255 |
if (OpenClipboard (NULL) == FALSE) |
61 |
werner |
36 |
return WPTERR_CLIP_OPEN; |
62 |
twoaday |
255 |
clipmem = GetClipboardData (CF_TEXT); |
63 |
|
|
if (clipmem == NULL) { |
64 |
|
|
CloseClipboard (); |
65 |
werner |
36 |
return WPTERR_CLIP_GET; |
66 |
|
|
} |
67 |
twoaday |
255 |
CloseClipboard (); |
68 |
werner |
36 |
return 0; |
69 |
twoaday |
226 |
} |
70 |
werner |
36 |
|
71 |
twoaday |
225 |
|
72 |
twoaday |
226 |
/* Fabricate a "select all" keystroke sequence. */ |
73 |
werner |
36 |
static void |
74 |
twoaday |
255 |
wnd_msg_selectall (void) |
75 |
werner |
36 |
{ |
76 |
twoaday |
273 |
unsigned int keys[4] = {0}; |
77 |
werner |
36 |
|
78 |
twoaday |
255 |
if (PTD_is_used ()) { |
79 |
werner |
36 |
keys[0] = VK_CONTROL | 0x8000; |
80 |
|
|
keys[1] = 'A' | 0x8000; |
81 |
|
|
keys[2] = 'A'; |
82 |
|
|
keys[3] = VK_CONTROL; |
83 |
twoaday |
255 |
PTD_keyb_send (keys, 4); |
84 |
werner |
36 |
|
85 |
|
|
} |
86 |
|
|
else { |
87 |
twoaday |
255 |
KEYDOWN (VK_CONTROL, 0x1d); |
88 |
|
|
KEYDOWN ((BYTE)'A', 0x1e); |
89 |
|
|
KEYUP ((BYTE)'A', 0x1e); |
90 |
|
|
KEYUP (VK_CONTROL, 0x1d); |
91 |
werner |
36 |
} |
92 |
twoaday |
225 |
} |
93 |
werner |
36 |
|
94 |
|
|
|
95 |
twoaday |
255 |
/* Fabricate a "paste" keystrok sequence. */ |
96 |
werner |
36 |
static void |
97 |
twoaday |
225 |
wnd_msg_paste (void) |
98 |
werner |
36 |
{ |
99 |
twoaday |
273 |
unsigned int keys[4] = {0}; |
100 |
werner |
36 |
|
101 |
twoaday |
255 |
if (PTD_is_used ()) { |
102 |
werner |
36 |
keys[0] = VK_CONTROL | 0x8000; |
103 |
|
|
keys[1] = 'V' | 0x8000; |
104 |
|
|
keys[2] = 'V'; |
105 |
|
|
keys[3] = VK_CONTROL; |
106 |
twoaday |
255 |
PTD_keyb_send (keys, 4); |
107 |
werner |
36 |
} |
108 |
|
|
else { |
109 |
twoaday |
255 |
KEYDOWN (VK_CONTROL, 0x1d); |
110 |
|
|
KEYDOWN ((BYTE)'V', 0x2f); |
111 |
|
|
KEYUP ((BYTE)'V', 0x2f); |
112 |
|
|
KEYUP (VK_CONTROL, 0x1d); |
113 |
werner |
36 |
} |
114 |
twoaday |
225 |
} |
115 |
werner |
36 |
|
116 |
|
|
|
117 |
twoaday |
255 |
/* Fabricate a "copy" keystroke sequence. */ |
118 |
werner |
36 |
static void |
119 |
twoaday |
225 |
wnd_msg_copy (void) |
120 |
werner |
36 |
{ |
121 |
twoaday |
273 |
unsigned int keys[4] = {0}; |
122 |
werner |
36 |
|
123 |
twoaday |
225 |
if (PTD_is_used ()) { |
124 |
werner |
36 |
keys[0] = VK_CONTROL | 0x8000; |
125 |
|
|
keys[1] = 'C' | 0x8000; |
126 |
|
|
keys[2] = 'C'; |
127 |
|
|
keys[3] = VK_CONTROL; |
128 |
twoaday |
273 |
PTD_keyb_send (keys, 4); |
129 |
werner |
36 |
} |
130 |
|
|
else { |
131 |
twoaday |
273 |
KEYDOWN (VK_CONTROL, 0x1d); |
132 |
|
|
KEYDOWN ((BYTE)'C', 0x2e); |
133 |
|
|
KEYUP ((BYTE)'C', 0x2e); |
134 |
|
|
KEYUP (VK_CONTROL, 0x1d); |
135 |
werner |
36 |
} |
136 |
twoaday |
226 |
} |
137 |
werner |
36 |
|
138 |
|
|
|
139 |
twoaday |
255 |
/* Set the position inside the edit control @hwnd. */ |
140 |
werner |
36 |
static inline void |
141 |
twoaday |
255 |
wnd_msg_em_set_pos (HWND hwnd, int begin) |
142 |
werner |
36 |
{ |
143 |
twoaday |
255 |
SendMessage (hwnd, EM_SETSEL, 0, begin? 0 : -1); |
144 |
twoaday |
226 |
} |
145 |
werner |
36 |
|
146 |
|
|
|
147 |
twoaday |
255 |
/* Get the current window and return it in @r_main. |
148 |
|
|
Return value: The Window with the keyboard focus. */ |
149 |
werner |
36 |
static HWND |
150 |
twoaday |
255 |
get_curr_wnd (HWND main, int use_hotkey, HWND *r_main) |
151 |
werner |
36 |
{ |
152 |
|
|
HWND prev, fg; |
153 |
|
|
|
154 |
twoaday |
255 |
if (use_hotkey) |
155 |
|
|
prev = GetForegroundWindow (); |
156 |
werner |
36 |
else { |
157 |
twoaday |
255 |
if (r_main) |
158 |
|
|
*r_main = GetNextWindow (main, GW_HWNDNEXT); |
159 |
werner |
36 |
return PTD_get_curr_hwnd( ); |
160 |
|
|
} |
161 |
|
|
|
162 |
twoaday |
255 |
SetForegroundWindow (prev); |
163 |
|
|
AttachThreadInput (GetCurrentThreadId(), |
164 |
|
|
GetWindowThreadProcessId (prev, NULL), |
165 |
|
|
TRUE); |
166 |
|
|
fg = GetFocus (); |
167 |
|
|
AttachThreadInput (GetCurrentThreadId (), |
168 |
|
|
GetWindowThreadProcessId (prev, NULL), |
169 |
|
|
FALSE); |
170 |
werner |
36 |
|
171 |
twoaday |
255 |
if (r_main) |
172 |
werner |
36 |
*r_main = prev; |
173 |
|
|
return fg; |
174 |
twoaday |
226 |
} |
175 |
werner |
36 |
|
176 |
|
|
|
177 |
twoaday |
273 |
/* Retrieve the contents of the current window based on |
178 |
|
|
the old window handle @old_hwnd and store the information |
179 |
|
|
in @ctx. |
180 |
|
|
A return value of 0 indicates a problem. */ |
181 |
werner |
36 |
int |
182 |
twoaday |
255 |
get_window_contents (HWND old_hwnd, curr_wnd_ctx *ctx, int *r_hotkey) |
183 |
werner |
36 |
{ |
184 |
|
|
HWND hwnd; |
185 |
|
|
int rc = 1, hotkey = 0; |
186 |
|
|
|
187 |
twoaday |
255 |
if (r_hotkey) |
188 |
werner |
36 |
hotkey = *r_hotkey; |
189 |
|
|
|
190 |
twoaday |
255 |
hwnd = get_curr_wnd (old_hwnd, hotkey, &ctx->main); |
191 |
|
|
if (!hwnd) |
192 |
werner |
36 |
return rc; |
193 |
|
|
ctx->focus = hwnd; |
194 |
twoaday |
255 |
clip_clear (); |
195 |
werner |
36 |
|
196 |
twoaday |
255 |
AttachThreadInput (GetCurrentThreadId (), |
197 |
|
|
GetWindowThreadProcessId (hwnd, NULL), |
198 |
|
|
TRUE); |
199 |
|
|
SetFocus (hwnd); |
200 |
werner |
36 |
/* First we try to send a simple clipboard copying command and check |
201 |
|
|
if the clipboard contains some text. */ |
202 |
twoaday |
255 |
SendMessage (hwnd, WM_COPY, 0, 0); |
203 |
|
|
if (!clip_check ()) { |
204 |
werner |
36 |
rc = 0; |
205 |
|
|
goto leave; |
206 |
|
|
} |
207 |
|
|
/* Then the check if the window is an edit control */ |
208 |
twoaday |
255 |
wnd_msg_em_set_pos (hwnd, 0); |
209 |
|
|
SendMessage (hwnd, WM_COPY, 0, 0); |
210 |
|
|
if (!clip_check ()) { |
211 |
werner |
36 |
rc = 0; |
212 |
|
|
goto leave; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
/* The last try is to send a mark all and copy to clipboard command */ |
216 |
twoaday |
255 |
wnd_msg_selectall (); |
217 |
twoaday |
225 |
wnd_msg_copy (); |
218 |
twoaday |
255 |
if (!clip_check ()) |
219 |
werner |
36 |
rc = 0; |
220 |
|
|
|
221 |
|
|
leave: |
222 |
twoaday |
255 |
AttachThreadInput (GetCurrentThreadId (), |
223 |
|
|
GetWindowThreadProcessId (hwnd, NULL), |
224 |
|
|
FALSE); |
225 |
|
|
if (r_hotkey) |
226 |
werner |
36 |
*r_hotkey = 0; /* reset: we need this in any case */ |
227 |
|
|
return rc; |
228 |
twoaday |
226 |
} |
229 |
werner |
36 |
|
230 |
|
|
|
231 |
twoaday |
273 |
/* Restore the clipboard contents to the window handle given |
232 |
|
|
in @ctx and reset the current window to @old_hwnd if possible. */ |
233 |
werner |
36 |
int |
234 |
twoaday |
255 |
set_window_contents (HWND old_hwnd, curr_wnd_ctx *ctx) |
235 |
werner |
36 |
{ |
236 |
twoaday |
255 |
HWND hwnd, lost; |
237 |
werner |
36 |
|
238 |
|
|
hwnd = ctx->focus; |
239 |
twoaday |
255 |
if (IsIconic (ctx->main)) |
240 |
|
|
ShowWindow (hwnd, SW_SHOWNORMAL); |
241 |
|
|
SetForegroundWindow (hwnd); |
242 |
werner |
36 |
|
243 |
twoaday |
255 |
AttachThreadInput (GetCurrentThreadId (), |
244 |
|
|
GetWindowThreadProcessId (hwnd, NULL), |
245 |
|
|
TRUE); |
246 |
werner |
36 |
|
247 |
twoaday |
255 |
lost = SetFocus (hwnd); |
248 |
werner |
36 |
|
249 |
twoaday |
255 |
wnd_msg_em_set_pos (hwnd, 0); |
250 |
twoaday |
225 |
wnd_msg_paste (); |
251 |
twoaday |
255 |
wnd_msg_em_set_pos (hwnd, 1); |
252 |
werner |
36 |
|
253 |
twoaday |
273 |
AttachThreadInput (GetCurrentThreadId(), |
254 |
|
|
GetWindowThreadProcessId (hwnd, NULL), |
255 |
|
|
FALSE); |
256 |
werner |
36 |
|
257 |
twoaday |
273 |
SetForegroundWindow (lost); |
258 |
werner |
36 |
|
259 |
|
|
return 0; |
260 |
twoaday |
226 |
} |