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