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