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