1 |
/* wptCurrWnd.cpp - Current window code |
/* wptCurrWnd.cpp - Current window code |
2 |
* Copyright (C) 2001-2004 Timo Schulz |
* Copyright (C) 2001-2004 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* WinPT is free software; you can redistribute it and/or modify |
* 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 |
* 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 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
* (at your option) any later version. |
10 |
* |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
15 |
* |
* |
16 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include <windows.h> |
#ifdef HAVE_CONFIG_H |
22 |
|
#include <config.h> |
23 |
#include "wptW32API.h" |
#endif |
24 |
#include "wptErrors.h" |
|
25 |
|
#include <windows.h> |
26 |
#define KEYDOWN( vcode, scode ) keybd_event( (vcode), (scode), 0, 0 ) |
|
27 |
#define KEYUP( vcode, scode) keybd_event( (vcode), (scode), KEYEVENTF_KEYUP, 0 ) |
#include "wptW32API.h" |
28 |
|
#include "wptErrors.h" |
29 |
|
#include "wptVersion.h" |
30 |
extern HWND PTD_get_curr_hwnd( void ); |
|
31 |
extern BOOL PTD_keyb_send( UINT *keys, UINT n_keys ); |
#define KEYDOWN( vcode, scode ) keybd_event( (vcode), (scode), 0, 0 ) |
32 |
int PTD_is_used( void ); |
#define KEYUP( vcode, scode) keybd_event( (vcode), (scode), KEYEVENTF_KEYUP, 0 ) |
33 |
|
|
34 |
static void |
|
35 |
clip_clear (void) |
extern "C" HWND PTD_get_curr_hwnd( void ); |
36 |
{ |
extern "C" BOOL PTD_keyb_send( UINT *keys, UINT n_keys ); |
37 |
OpenClipboard (NULL); |
extern "C" int PTD_is_used( void ); |
38 |
EmptyClipboard (); |
|
39 |
CloseClipboard (); |
static void |
40 |
} /* clip_clear */ |
clip_clear (void) |
41 |
|
{ |
42 |
|
OpenClipboard (NULL); |
43 |
static int |
EmptyClipboard (); |
44 |
clip_check( void ) |
CloseClipboard (); |
45 |
{ |
} |
46 |
HANDLE clipmem; |
|
47 |
|
|
48 |
/* Arrrgg: it seems that we've to wait until the remote thread |
/* Check if the clipboard returns any data. |
49 |
processed the message. W32 said it does, but I don't think so :-((. */ |
Return value: 0 on success. */ |
50 |
Sleep( 400 ); |
static int |
51 |
|
clip_check (void) |
52 |
if( OpenClipboard( NULL ) == FALSE ) |
{ |
53 |
return WPTERR_CLIP_OPEN; |
HANDLE clipmem; |
54 |
clipmem = GetClipboardData( CF_TEXT ); |
|
55 |
if( clipmem == NULL ) { |
/* Arrrgg: it seems that we've to wait until the remote thread |
56 |
CloseClipboard( ); |
processed the message. W32 said it does, but I don't think so :-((. */ |
57 |
return WPTERR_CLIP_GET; |
Sleep( 400 ); |
58 |
} |
|
59 |
CloseClipboard( ); |
if( OpenClipboard( NULL ) == FALSE ) |
60 |
return 0; |
return WPTERR_CLIP_OPEN; |
61 |
} /* clip_check */ |
clipmem = GetClipboardData( CF_TEXT ); |
62 |
|
if( clipmem == NULL ) { |
63 |
static void |
CloseClipboard( ); |
64 |
wnd_msg_markall( HWND hwnd ) |
return WPTERR_CLIP_GET; |
65 |
{ |
} |
66 |
unsigned keys[4] = {0}; |
CloseClipboard( ); |
67 |
|
return 0; |
68 |
if( PTD_is_used() ) { |
} |
69 |
keys[0] = VK_CONTROL | 0x8000; |
|
70 |
keys[1] = 'A' | 0x8000; |
|
71 |
keys[2] = 'A'; |
/* Fabricate a "select all" keystroke sequence. */ |
72 |
keys[3] = VK_CONTROL; |
static void |
73 |
PTD_keyb_send( keys, 4 ); |
wnd_msg_markall (void) |
74 |
|
{ |
75 |
} |
unsigned keys[4] = {0}; |
76 |
else { |
|
77 |
KEYDOWN( VK_CONTROL, 0x1d ); |
if( PTD_is_used() ) { |
78 |
KEYDOWN( (BYTE)'A', 0x1e ); |
keys[0] = VK_CONTROL | 0x8000; |
79 |
KEYUP( (BYTE)'A', 0x1e ); |
keys[1] = 'A' | 0x8000; |
80 |
KEYUP( VK_CONTROL, 0x1d ); |
keys[2] = 'A'; |
81 |
} |
keys[3] = VK_CONTROL; |
82 |
} /* msg_wm_markall */ |
PTD_keyb_send( keys, 4 ); |
83 |
|
|
84 |
|
} |
85 |
static void |
else { |
86 |
wnd_msg_paste( HWND hwnd ) |
KEYDOWN( VK_CONTROL, 0x1d ); |
87 |
{ |
KEYDOWN( (BYTE)'A', 0x1e ); |
88 |
unsigned keys[4] = {0}; |
KEYUP( (BYTE)'A', 0x1e ); |
89 |
|
KEYUP( VK_CONTROL, 0x1d ); |
90 |
if( PTD_is_used() ) { |
} |
91 |
keys[0] = VK_CONTROL | 0x8000; |
} |
92 |
keys[1] = 'V' | 0x8000; |
|
93 |
keys[2] = 'V'; |
|
94 |
keys[3] = VK_CONTROL; |
static void |
95 |
PTD_keyb_send( keys, 4 ); |
wnd_msg_paste (void) |
96 |
} |
{ |
97 |
else { |
unsigned keys[4] = {0}; |
98 |
KEYDOWN( VK_CONTROL, 0x1d ); |
|
99 |
KEYDOWN( (BYTE)'V', 0x2f ); |
if( PTD_is_used() ) { |
100 |
KEYUP( (BYTE)'V', 0x2f ); |
keys[0] = VK_CONTROL | 0x8000; |
101 |
KEYUP( VK_CONTROL, 0x1d ); |
keys[1] = 'V' | 0x8000; |
102 |
} |
keys[2] = 'V'; |
103 |
} /* msg_wm_paste */ |
keys[3] = VK_CONTROL; |
104 |
|
PTD_keyb_send( keys, 4 ); |
105 |
|
} |
106 |
static void |
else { |
107 |
wnd_msg_copy( HWND hwnd ) |
KEYDOWN( VK_CONTROL, 0x1d ); |
108 |
{ |
KEYDOWN( (BYTE)'V', 0x2f ); |
109 |
unsigned keys[4] = {0}; |
KEYUP( (BYTE)'V', 0x2f ); |
110 |
|
KEYUP( VK_CONTROL, 0x1d ); |
111 |
if( PTD_is_used() ) { |
} |
112 |
keys[0] = VK_CONTROL | 0x8000; |
} |
113 |
keys[1] = 'C' | 0x8000; |
|
114 |
keys[2] = 'C'; |
|
115 |
keys[3] = VK_CONTROL; |
static void |
116 |
PTD_keyb_send( keys, 4 ); |
wnd_msg_copy (void) |
117 |
} |
{ |
118 |
else { |
unsigned keys[4] = {0}; |
119 |
KEYDOWN( VK_CONTROL, 0x1d ); |
|
120 |
KEYDOWN( (BYTE)'C', 0x2e ); |
if (PTD_is_used ()) { |
121 |
KEYUP( (BYTE)'C', 0x2e ); |
keys[0] = VK_CONTROL | 0x8000; |
122 |
KEYUP( VK_CONTROL, 0x1d ); |
keys[1] = 'C' | 0x8000; |
123 |
} |
keys[2] = 'C'; |
124 |
} /* msg_wm_copy */ |
keys[3] = VK_CONTROL; |
125 |
|
PTD_keyb_send( keys, 4 ); |
126 |
|
} |
127 |
static inline void |
else { |
128 |
wnd_msg_em_set_pos( HWND hwnd, int begin ) |
KEYDOWN( VK_CONTROL, 0x1d ); |
129 |
{ |
KEYDOWN( (BYTE)'C', 0x2e ); |
130 |
SendMessage( hwnd, EM_SETSEL, 0, begin? 1 : -1 ); |
KEYUP( (BYTE)'C', 0x2e ); |
131 |
} /* wnd_msg_em_set_pos */ |
KEYUP( VK_CONTROL, 0x1d ); |
132 |
|
} |
133 |
|
} |
134 |
static HWND |
|
135 |
get_curr_wnd( HWND main, int hotkey, HWND *r_main ) |
|
136 |
{ |
static inline void |
137 |
HWND prev, fg; |
wnd_msg_em_set_pos( HWND hwnd, int begin ) |
138 |
|
{ |
139 |
if( hotkey ) |
SendMessage( hwnd, EM_SETSEL, 0, begin? 1 : -1 ); |
140 |
prev = GetForegroundWindow(); |
} |
141 |
else { |
|
142 |
if( r_main ) |
|
143 |
*r_main = GetNextWindow( main, GW_HWNDNEXT ); |
static HWND |
144 |
return PTD_get_curr_hwnd( ); |
get_curr_wnd( HWND main, int hotkey, HWND *r_main ) |
145 |
} |
{ |
146 |
|
HWND prev, fg; |
147 |
SetForegroundWindow( prev ); |
|
148 |
|
if( hotkey ) |
149 |
AttachThreadInput( GetCurrentThreadId(), |
prev = GetForegroundWindow(); |
150 |
GetWindowThreadProcessId( prev, NULL ), |
else { |
151 |
TRUE ); |
if( r_main ) |
152 |
fg = GetFocus(); |
*r_main = GetNextWindow( main, GW_HWNDNEXT ); |
153 |
AttachThreadInput( GetCurrentThreadId(), |
return PTD_get_curr_hwnd( ); |
154 |
GetWindowThreadProcessId( prev, NULL ), |
} |
155 |
FALSE ); |
|
156 |
|
SetForegroundWindow( prev ); |
157 |
if( r_main ) |
|
158 |
*r_main = prev; |
AttachThreadInput( GetCurrentThreadId(), |
159 |
|
GetWindowThreadProcessId( prev, NULL ), |
160 |
return fg; |
TRUE ); |
161 |
} /* get_curr_wnd */ |
fg = GetFocus(); |
162 |
|
AttachThreadInput( GetCurrentThreadId(), |
163 |
|
GetWindowThreadProcessId( prev, NULL ), |
164 |
int |
FALSE ); |
165 |
get_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx, int *r_hotkey ) |
|
166 |
{ |
if( r_main ) |
167 |
HWND hwnd; |
*r_main = prev; |
168 |
int rc = 1, hotkey = 0; |
|
169 |
|
return fg; |
170 |
if( r_hotkey ) |
} |
171 |
hotkey = *r_hotkey; |
|
172 |
|
|
173 |
hwnd = get_curr_wnd( old_hwnd, hotkey, &ctx->main ); |
int |
174 |
if( hwnd == NULL ) |
get_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx, int *r_hotkey ) |
175 |
return rc; |
{ |
176 |
ctx->focus = hwnd; |
HWND hwnd; |
177 |
clip_clear( ); |
int rc = 1, hotkey = 0; |
178 |
|
|
179 |
AttachThreadInput( GetCurrentThreadId( ), |
if( r_hotkey ) |
180 |
GetWindowThreadProcessId( hwnd, NULL ), |
hotkey = *r_hotkey; |
181 |
TRUE ); |
|
182 |
SetFocus( hwnd ); |
hwnd = get_curr_wnd( old_hwnd, hotkey, &ctx->main ); |
183 |
|
if( hwnd == NULL ) |
184 |
/* First we try to send a simple clipboard copying command and check |
return rc; |
185 |
if the clipboard contains some text. */ |
ctx->focus = hwnd; |
186 |
SendMessage( hwnd, WM_COPY, 0, 0 ); |
clip_clear( ); |
187 |
if( !clip_check( ) ) { |
|
188 |
rc = 0; |
AttachThreadInput( GetCurrentThreadId( ), |
189 |
goto leave; |
GetWindowThreadProcessId( hwnd, NULL ), |
190 |
} |
TRUE ); |
191 |
/* Then the check if the window is an edit control */ |
SetFocus( hwnd ); |
192 |
wnd_msg_em_set_pos( hwnd, 0 ); |
|
193 |
SendMessage( hwnd, WM_COPY, 0, 0 ); |
/* First we try to send a simple clipboard copying command and check |
194 |
if( !clip_check( ) ) { |
if the clipboard contains some text. */ |
195 |
rc = 0; |
SendMessage( hwnd, WM_COPY, 0, 0 ); |
196 |
goto leave; |
if( !clip_check( ) ) { |
197 |
} |
rc = 0; |
198 |
|
goto leave; |
199 |
/* The last try is to send a mark all and copy to clipboard command */ |
} |
200 |
wnd_msg_markall( hwnd ); |
/* Then the check if the window is an edit control */ |
201 |
wnd_msg_copy( hwnd ); |
wnd_msg_em_set_pos( hwnd, 0 ); |
202 |
if( !clip_check( ) ) |
SendMessage( hwnd, WM_COPY, 0, 0 ); |
203 |
rc = 0; |
if( !clip_check( ) ) { |
204 |
|
rc = 0; |
205 |
leave: |
goto leave; |
206 |
AttachThreadInput( GetCurrentThreadId( ), |
} |
207 |
GetWindowThreadProcessId( hwnd, NULL ), |
|
208 |
FALSE ); |
/* The last try is to send a mark all and copy to clipboard command */ |
209 |
if( r_hotkey ) |
wnd_msg_markall (); |
210 |
*r_hotkey = 0; /* reset: we need this in any case */ |
wnd_msg_copy (); |
211 |
|
if( !clip_check( ) ) |
212 |
return rc; |
rc = 0; |
213 |
} /* get_window_contents */ |
|
214 |
|
leave: |
215 |
|
AttachThreadInput( GetCurrentThreadId( ), |
216 |
int |
GetWindowThreadProcessId( hwnd, NULL ), |
217 |
set_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx ) |
FALSE ); |
218 |
{ |
if( r_hotkey ) |
219 |
HWND hwnd, lost; |
*r_hotkey = 0; /* reset: we need this in any case */ |
220 |
|
|
221 |
hwnd = ctx->focus; |
return rc; |
222 |
if( IsIconic( ctx->main ) ) |
} |
223 |
ShowWindow( hwnd, SW_SHOWNORMAL ); |
|
224 |
SetForegroundWindow( hwnd ); |
|
225 |
|
int |
226 |
AttachThreadInput( GetCurrentThreadId( ), |
set_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx ) |
227 |
GetWindowThreadProcessId( hwnd, NULL ), |
{ |
228 |
TRUE ); |
HWND hwnd, lost; |
229 |
|
|
230 |
lost = SetFocus( hwnd ); |
hwnd = ctx->focus; |
231 |
|
if( IsIconic( ctx->main ) ) |
232 |
wnd_msg_em_set_pos( hwnd, 0 ); |
ShowWindow( hwnd, SW_SHOWNORMAL ); |
233 |
wnd_msg_paste( hwnd ); |
SetForegroundWindow( hwnd ); |
234 |
wnd_msg_em_set_pos( hwnd, 1 ); |
|
235 |
|
AttachThreadInput( GetCurrentThreadId( ), |
236 |
AttachThreadInput( GetCurrentThreadId(), |
GetWindowThreadProcessId( hwnd, NULL ), |
237 |
GetWindowThreadProcessId( hwnd, NULL ), |
TRUE ); |
238 |
FALSE ); |
|
239 |
|
lost = SetFocus( hwnd ); |
240 |
SetForegroundWindow( lost ); |
|
241 |
|
wnd_msg_em_set_pos( hwnd, 0 ); |
242 |
return 0; |
wnd_msg_paste (); |
243 |
} /* set_window_contents */ |
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 |
|
} |