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