1 |
/* wptCurrWnd.cpp - Current window code |
/* wptCurrWnd.cpp - Current window code |
2 |
* Copyright (C) 2001-2004 Timo Schulz |
* Copyright (C) 2001-2004 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* WinPT is free software; you can redistribute it and/or modify |
* 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 |
* 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 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
* (at your option) any later version. |
10 |
* |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
15 |
* |
* |
16 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include <windows.h> |
#ifdef HAVE_CONFIG_H |
22 |
|
#include <config.h> |
23 |
#include "wptW32API.h" |
#endif |
24 |
#include "wptErrors.h" |
|
25 |
|
#include <windows.h> |
26 |
#define KEYDOWN( vcode, scode ) keybd_event( (vcode), (scode), 0, 0 ) |
|
27 |
#define KEYUP( vcode, scode) keybd_event( (vcode), (scode), KEYEVENTF_KEYUP, 0 ) |
#include "wptW32API.h" |
28 |
|
#include "wptErrors.h" |
29 |
|
#include "wptVersion.h" |
30 |
extern HWND PTD_get_curr_hwnd( void ); |
|
31 |
extern BOOL PTD_keyb_send( UINT *keys, UINT n_keys ); |
#define KEYDOWN( vcode, scode ) keybd_event( (vcode), (scode), 0, 0 ) |
32 |
int PTD_is_used( void ); |
#define KEYUP( vcode, scode) keybd_event( (vcode), (scode), KEYEVENTF_KEYUP, 0 ) |
33 |
|
|
34 |
static void |
|
35 |
clip_clear( void ) |
extern "C" HWND PTD_get_curr_hwnd( void ); |
36 |
{ |
extern "C" BOOL PTD_keyb_send( UINT *keys, UINT n_keys ); |
37 |
OpenClipboard( NULL ); |
extern "C" int PTD_is_used( void ); |
38 |
EmptyClipboard( ); |
|
39 |
CloseClipboard( ); |
static void |
40 |
} /* clip_clear */ |
clip_clear (void) |
41 |
|
{ |
42 |
|
OpenClipboard (NULL); |
43 |
static int |
EmptyClipboard (); |
44 |
clip_check( void ) |
CloseClipboard (); |
45 |
{ |
} /* clip_clear */ |
46 |
HANDLE clipmem; |
|
47 |
|
|
48 |
/* Arrrgg: it seems that we've to wait until the remote thread |
static int |
49 |
processed the message. W32 said it does, but I don't think so :-((. */ |
clip_check( void ) |
50 |
Sleep( 400 ); |
{ |
51 |
|
HANDLE clipmem; |
52 |
if( OpenClipboard( NULL ) == FALSE ) |
|
53 |
return WPTERR_CLIP_OPEN; |
/* Arrrgg: it seems that we've to wait until the remote thread |
54 |
clipmem = GetClipboardData( CF_TEXT ); |
processed the message. W32 said it does, but I don't think so :-((. */ |
55 |
if( clipmem == NULL ) { |
Sleep( 400 ); |
56 |
CloseClipboard( ); |
|
57 |
return WPTERR_CLIP_GET; |
if( OpenClipboard( NULL ) == FALSE ) |
58 |
} |
return WPTERR_CLIP_OPEN; |
59 |
CloseClipboard( ); |
clipmem = GetClipboardData( CF_TEXT ); |
60 |
return 0; |
if( clipmem == NULL ) { |
61 |
} /* clip_check */ |
CloseClipboard( ); |
62 |
|
return WPTERR_CLIP_GET; |
63 |
static void |
} |
64 |
wnd_msg_markall( HWND hwnd ) |
CloseClipboard( ); |
65 |
{ |
return 0; |
66 |
unsigned keys[4] = {0}; |
} /* clip_check */ |
67 |
|
|
68 |
if( PTD_is_used() ) { |
|
69 |
keys[0] = VK_CONTROL | 0x8000; |
static void |
70 |
keys[1] = 'A' | 0x8000; |
wnd_msg_markall (void) |
71 |
keys[2] = 'A'; |
{ |
72 |
keys[3] = VK_CONTROL; |
unsigned keys[4] = {0}; |
73 |
PTD_keyb_send( keys, 4 ); |
|
74 |
|
if( PTD_is_used() ) { |
75 |
} |
keys[0] = VK_CONTROL | 0x8000; |
76 |
else { |
keys[1] = 'A' | 0x8000; |
77 |
KEYDOWN( VK_CONTROL, 0x1d ); |
keys[2] = 'A'; |
78 |
KEYDOWN( (BYTE)'A', 0x1e ); |
keys[3] = VK_CONTROL; |
79 |
KEYUP( (BYTE)'A', 0x1e ); |
PTD_keyb_send( keys, 4 ); |
80 |
KEYUP( VK_CONTROL, 0x1d ); |
|
81 |
} |
} |
82 |
} /* msg_wm_markall */ |
else { |
83 |
|
KEYDOWN( VK_CONTROL, 0x1d ); |
84 |
|
KEYDOWN( (BYTE)'A', 0x1e ); |
85 |
static void |
KEYUP( (BYTE)'A', 0x1e ); |
86 |
wnd_msg_paste( HWND hwnd ) |
KEYUP( VK_CONTROL, 0x1d ); |
87 |
{ |
} |
88 |
unsigned keys[4] = {0}; |
} |
89 |
|
|
90 |
if( PTD_is_used() ) { |
|
91 |
keys[0] = VK_CONTROL | 0x8000; |
static void |
92 |
keys[1] = 'V' | 0x8000; |
wnd_msg_paste (void) |
93 |
keys[2] = 'V'; |
{ |
94 |
keys[3] = VK_CONTROL; |
unsigned keys[4] = {0}; |
95 |
PTD_keyb_send( keys, 4 ); |
|
96 |
} |
if( PTD_is_used() ) { |
97 |
else { |
keys[0] = VK_CONTROL | 0x8000; |
98 |
KEYDOWN( VK_CONTROL, 0x1d ); |
keys[1] = 'V' | 0x8000; |
99 |
KEYDOWN( (BYTE)'V', 0x2f ); |
keys[2] = 'V'; |
100 |
KEYUP( (BYTE)'V', 0x2f ); |
keys[3] = VK_CONTROL; |
101 |
KEYUP( VK_CONTROL, 0x1d ); |
PTD_keyb_send( keys, 4 ); |
102 |
} |
} |
103 |
} /* msg_wm_paste */ |
else { |
104 |
|
KEYDOWN( VK_CONTROL, 0x1d ); |
105 |
|
KEYDOWN( (BYTE)'V', 0x2f ); |
106 |
static void |
KEYUP( (BYTE)'V', 0x2f ); |
107 |
wnd_msg_copy( HWND hwnd ) |
KEYUP( VK_CONTROL, 0x1d ); |
108 |
{ |
} |
109 |
unsigned keys[4] = {0}; |
} |
110 |
|
|
111 |
if( PTD_is_used() ) { |
|
112 |
keys[0] = VK_CONTROL | 0x8000; |
static void |
113 |
keys[1] = 'C' | 0x8000; |
wnd_msg_copy (void) |
114 |
keys[2] = 'C'; |
{ |
115 |
keys[3] = VK_CONTROL; |
unsigned keys[4] = {0}; |
116 |
PTD_keyb_send( keys, 4 ); |
|
117 |
} |
if (PTD_is_used ()) { |
118 |
else { |
keys[0] = VK_CONTROL | 0x8000; |
119 |
KEYDOWN( VK_CONTROL, 0x1d ); |
keys[1] = 'C' | 0x8000; |
120 |
KEYDOWN( (BYTE)'C', 0x2e ); |
keys[2] = 'C'; |
121 |
KEYUP( (BYTE)'C', 0x2e ); |
keys[3] = VK_CONTROL; |
122 |
KEYUP( VK_CONTROL, 0x1d ); |
PTD_keyb_send( keys, 4 ); |
123 |
} |
} |
124 |
} /* msg_wm_copy */ |
else { |
125 |
|
KEYDOWN( VK_CONTROL, 0x1d ); |
126 |
|
KEYDOWN( (BYTE)'C', 0x2e ); |
127 |
static inline void |
KEYUP( (BYTE)'C', 0x2e ); |
128 |
wnd_msg_em_set_pos( HWND hwnd, int begin ) |
KEYUP( VK_CONTROL, 0x1d ); |
129 |
{ |
} |
130 |
SendMessage( hwnd, EM_SETSEL, 0, begin? 1 : -1 ); |
} /* msg_wm_copy */ |
131 |
} /* wnd_msg_em_set_pos */ |
|
132 |
|
|
133 |
|
static inline void |
134 |
static HWND |
wnd_msg_em_set_pos( HWND hwnd, int begin ) |
135 |
get_curr_wnd( HWND main, int hotkey, HWND *r_main ) |
{ |
136 |
{ |
SendMessage( hwnd, EM_SETSEL, 0, begin? 1 : -1 ); |
137 |
HWND prev, fg; |
} /* wnd_msg_em_set_pos */ |
138 |
|
|
139 |
if( hotkey ) |
|
140 |
prev = GetForegroundWindow(); |
static HWND |
141 |
else { |
get_curr_wnd( HWND main, int hotkey, HWND *r_main ) |
142 |
if( r_main ) |
{ |
143 |
*r_main = GetNextWindow( main, GW_HWNDNEXT ); |
HWND prev, fg; |
144 |
return PTD_get_curr_hwnd( ); |
|
145 |
} |
if( hotkey ) |
146 |
|
prev = GetForegroundWindow(); |
147 |
SetForegroundWindow( prev ); |
else { |
148 |
|
if( r_main ) |
149 |
AttachThreadInput( GetCurrentThreadId(), |
*r_main = GetNextWindow( main, GW_HWNDNEXT ); |
150 |
GetWindowThreadProcessId( prev, NULL ), |
return PTD_get_curr_hwnd( ); |
151 |
TRUE ); |
} |
152 |
fg = GetFocus(); |
|
153 |
AttachThreadInput( GetCurrentThreadId(), |
SetForegroundWindow( prev ); |
154 |
GetWindowThreadProcessId( prev, NULL ), |
|
155 |
FALSE ); |
AttachThreadInput( GetCurrentThreadId(), |
156 |
|
GetWindowThreadProcessId( prev, NULL ), |
157 |
if( r_main ) |
TRUE ); |
158 |
*r_main = prev; |
fg = GetFocus(); |
159 |
|
AttachThreadInput( GetCurrentThreadId(), |
160 |
return fg; |
GetWindowThreadProcessId( prev, NULL ), |
161 |
} /* get_curr_wnd */ |
FALSE ); |
162 |
|
|
163 |
|
if( r_main ) |
164 |
int |
*r_main = prev; |
165 |
get_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx, int *r_hotkey ) |
|
166 |
{ |
return fg; |
167 |
HWND hwnd; |
} /* get_curr_wnd */ |
168 |
int rc = 1, hotkey = 0; |
|
169 |
|
|
170 |
if( r_hotkey ) |
int |
171 |
hotkey = *r_hotkey; |
get_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx, int *r_hotkey ) |
172 |
|
{ |
173 |
hwnd = get_curr_wnd( old_hwnd, hotkey, &ctx->main ); |
HWND hwnd; |
174 |
if( hwnd == NULL ) |
int rc = 1, hotkey = 0; |
175 |
return rc; |
|
176 |
ctx->focus = hwnd; |
if( r_hotkey ) |
177 |
clip_clear( ); |
hotkey = *r_hotkey; |
178 |
|
|
179 |
AttachThreadInput( GetCurrentThreadId( ), |
hwnd = get_curr_wnd( old_hwnd, hotkey, &ctx->main ); |
180 |
GetWindowThreadProcessId( hwnd, NULL ), |
if( hwnd == NULL ) |
181 |
TRUE ); |
return rc; |
182 |
SetFocus( hwnd ); |
ctx->focus = hwnd; |
183 |
|
clip_clear( ); |
184 |
/* First we try to send a simple clipboard copying command and check |
|
185 |
if the clipboard contains some text. */ |
AttachThreadInput( GetCurrentThreadId( ), |
186 |
SendMessage( hwnd, WM_COPY, 0, 0 ); |
GetWindowThreadProcessId( hwnd, NULL ), |
187 |
if( !clip_check( ) ) { |
TRUE ); |
188 |
rc = 0; |
SetFocus( hwnd ); |
189 |
goto leave; |
|
190 |
} |
/* First we try to send a simple clipboard copying command and check |
191 |
/* Then the check if the window is an edit control */ |
if the clipboard contains some text. */ |
192 |
wnd_msg_em_set_pos( hwnd, 0 ); |
SendMessage( hwnd, WM_COPY, 0, 0 ); |
193 |
SendMessage( hwnd, WM_COPY, 0, 0 ); |
if( !clip_check( ) ) { |
194 |
if( !clip_check( ) ) { |
rc = 0; |
195 |
rc = 0; |
goto leave; |
196 |
goto leave; |
} |
197 |
} |
/* Then the check if the window is an edit control */ |
198 |
|
wnd_msg_em_set_pos( hwnd, 0 ); |
199 |
/* The last try is to send a mark all and copy to clipboard command */ |
SendMessage( hwnd, WM_COPY, 0, 0 ); |
200 |
wnd_msg_markall( hwnd ); |
if( !clip_check( ) ) { |
201 |
wnd_msg_copy( hwnd ); |
rc = 0; |
202 |
if( !clip_check( ) ) |
goto leave; |
203 |
rc = 0; |
} |
204 |
|
|
205 |
leave: |
/* The last try is to send a mark all and copy to clipboard command */ |
206 |
AttachThreadInput( GetCurrentThreadId( ), |
wnd_msg_markall (); |
207 |
GetWindowThreadProcessId( hwnd, NULL ), |
wnd_msg_copy (); |
208 |
FALSE ); |
if( !clip_check( ) ) |
209 |
if( r_hotkey ) |
rc = 0; |
210 |
*r_hotkey = 0; /* reset: we need this in any case */ |
|
211 |
|
leave: |
212 |
return rc; |
AttachThreadInput( GetCurrentThreadId( ), |
213 |
} /* get_window_contents */ |
GetWindowThreadProcessId( hwnd, NULL ), |
214 |
|
FALSE ); |
215 |
|
if( r_hotkey ) |
216 |
int |
*r_hotkey = 0; /* reset: we need this in any case */ |
217 |
set_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx ) |
|
218 |
{ |
return rc; |
219 |
HWND hwnd, lost; |
} /* get_window_contents */ |
220 |
|
|
221 |
hwnd = ctx->focus; |
|
222 |
if( IsIconic( ctx->main ) ) |
int |
223 |
ShowWindow( hwnd, SW_SHOWNORMAL ); |
set_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx ) |
224 |
SetForegroundWindow( hwnd ); |
{ |
225 |
|
HWND hwnd, lost; |
226 |
AttachThreadInput( GetCurrentThreadId( ), |
|
227 |
GetWindowThreadProcessId( hwnd, NULL ), |
hwnd = ctx->focus; |
228 |
TRUE ); |
if( IsIconic( ctx->main ) ) |
229 |
|
ShowWindow( hwnd, SW_SHOWNORMAL ); |
230 |
lost = SetFocus( hwnd ); |
SetForegroundWindow( hwnd ); |
231 |
|
|
232 |
wnd_msg_em_set_pos( hwnd, 0 ); |
AttachThreadInput( GetCurrentThreadId( ), |
233 |
wnd_msg_paste( hwnd ); |
GetWindowThreadProcessId( hwnd, NULL ), |
234 |
wnd_msg_em_set_pos( hwnd, 1 ); |
TRUE ); |
235 |
|
|
236 |
AttachThreadInput( GetCurrentThreadId(), |
lost = SetFocus( hwnd ); |
237 |
GetWindowThreadProcessId( hwnd, NULL ), |
|
238 |
FALSE ); |
wnd_msg_em_set_pos( hwnd, 0 ); |
239 |
|
wnd_msg_paste (); |
240 |
SetForegroundWindow( lost ); |
wnd_msg_em_set_pos( hwnd, 1 ); |
241 |
|
|
242 |
return 0; |
AttachThreadInput( GetCurrentThreadId(), |
243 |
} /* set_window_contents */ |
GetWindowThreadProcessId( hwnd, NULL ), |
244 |
|
FALSE ); |
245 |
|
|
246 |
|
SetForegroundWindow( lost ); |
247 |
|
|
248 |
|
return 0; |
249 |
|
} /* set_window_contents */ |