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