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