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