1 |
werner |
36 |
/* wptCurrWnd.cpp - Current window code |
2 |
twoaday |
328 |
* Copyright (C) 2001-2004, 2006, 2007 Timo Schulz |
3 |
werner |
36 |
* |
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 |
|
|
|
17 |
|
|
#ifdef HAVE_CONFIG_H |
18 |
|
|
#include <config.h> |
19 |
|
|
#endif |
20 |
|
|
|
21 |
|
|
#include <windows.h> |
22 |
|
|
|
23 |
|
|
#include "wptW32API.h" |
24 |
|
|
#include "wptErrors.h" |
25 |
werner |
57 |
#include "wptVersion.h" |
26 |
werner |
36 |
|
27 |
twoaday |
255 |
#define KEYDOWN(vcode, scode) keybd_event ((vcode), (scode), 0, 0) |
28 |
twoaday |
273 |
#define KEYUP(vcode, scode) keybd_event ((vcode), (scode), KEYEVENTF_KEYUP, 0) |
29 |
werner |
36 |
|
30 |
twoaday |
328 |
|
31 |
|
|
/* PTD prototypes from the DLL. */ |
32 |
twoaday |
255 |
extern "C" HWND PTD_get_curr_hwnd (void); |
33 |
|
|
extern "C" BOOL PTD_keyb_send (UINT *keys, UINT n_keys); |
34 |
|
|
extern "C" int PTD_is_used (void); |
35 |
werner |
36 |
|
36 |
twoaday |
255 |
|
37 |
|
|
/* Clear the clipboard contents. */ |
38 |
werner |
36 |
static void |
39 |
|
|
clip_clear (void) |
40 |
|
|
{ |
41 |
|
|
OpenClipboard (NULL); |
42 |
|
|
EmptyClipboard (); |
43 |
|
|
CloseClipboard (); |
44 |
twoaday |
226 |
} |
45 |
werner |
36 |
|
46 |
|
|
|
47 |
twoaday |
328 |
/* Check if the clipboard returns any text. |
48 |
twoaday |
226 |
Return value: 0 on success. */ |
49 |
werner |
36 |
static int |
50 |
twoaday |
226 |
clip_check (void) |
51 |
werner |
36 |
{ |
52 |
|
|
HANDLE clipmem; |
53 |
|
|
|
54 |
twoaday |
255 |
/* Little backoff time to make sure the clipboard now really |
55 |
|
|
contains all data. */ |
56 |
|
|
Sleep (400); |
57 |
werner |
36 |
|
58 |
twoaday |
255 |
if (OpenClipboard (NULL) == FALSE) |
59 |
werner |
36 |
return WPTERR_CLIP_OPEN; |
60 |
twoaday |
255 |
clipmem = GetClipboardData (CF_TEXT); |
61 |
|
|
if (clipmem == NULL) { |
62 |
|
|
CloseClipboard (); |
63 |
werner |
36 |
return WPTERR_CLIP_GET; |
64 |
|
|
} |
65 |
twoaday |
255 |
CloseClipboard (); |
66 |
werner |
36 |
return 0; |
67 |
twoaday |
226 |
} |
68 |
werner |
36 |
|
69 |
twoaday |
225 |
|
70 |
twoaday |
226 |
/* Fabricate a "select all" keystroke sequence. */ |
71 |
werner |
36 |
static void |
72 |
twoaday |
328 |
window_msg_selectall (void) |
73 |
werner |
36 |
{ |
74 |
twoaday |
328 |
unsigned int keys[4]; |
75 |
werner |
36 |
|
76 |
twoaday |
255 |
if (PTD_is_used ()) { |
77 |
werner |
36 |
keys[0] = VK_CONTROL | 0x8000; |
78 |
|
|
keys[1] = 'A' | 0x8000; |
79 |
|
|
keys[2] = 'A'; |
80 |
|
|
keys[3] = VK_CONTROL; |
81 |
twoaday |
255 |
PTD_keyb_send (keys, 4); |
82 |
werner |
36 |
|
83 |
|
|
} |
84 |
|
|
else { |
85 |
twoaday |
255 |
KEYDOWN (VK_CONTROL, 0x1d); |
86 |
|
|
KEYDOWN ((BYTE)'A', 0x1e); |
87 |
|
|
KEYUP ((BYTE)'A', 0x1e); |
88 |
|
|
KEYUP (VK_CONTROL, 0x1d); |
89 |
werner |
36 |
} |
90 |
twoaday |
225 |
} |
91 |
werner |
36 |
|
92 |
|
|
|
93 |
twoaday |
255 |
/* Fabricate a "paste" keystrok sequence. */ |
94 |
werner |
36 |
static void |
95 |
twoaday |
328 |
window_msg_paste (void) |
96 |
werner |
36 |
{ |
97 |
twoaday |
328 |
unsigned int keys[4]; |
98 |
werner |
36 |
|
99 |
twoaday |
255 |
if (PTD_is_used ()) { |
100 |
werner |
36 |
keys[0] = VK_CONTROL | 0x8000; |
101 |
|
|
keys[1] = 'V' | 0x8000; |
102 |
|
|
keys[2] = 'V'; |
103 |
|
|
keys[3] = VK_CONTROL; |
104 |
twoaday |
255 |
PTD_keyb_send (keys, 4); |
105 |
werner |
36 |
} |
106 |
|
|
else { |
107 |
twoaday |
255 |
KEYDOWN (VK_CONTROL, 0x1d); |
108 |
|
|
KEYDOWN ((BYTE)'V', 0x2f); |
109 |
|
|
KEYUP ((BYTE)'V', 0x2f); |
110 |
|
|
KEYUP (VK_CONTROL, 0x1d); |
111 |
werner |
36 |
} |
112 |
twoaday |
225 |
} |
113 |
werner |
36 |
|
114 |
|
|
|
115 |
twoaday |
255 |
/* Fabricate a "copy" keystroke sequence. */ |
116 |
werner |
36 |
static void |
117 |
twoaday |
328 |
window_msg_copy (void) |
118 |
werner |
36 |
{ |
119 |
twoaday |
328 |
unsigned int keys[4]; |
120 |
werner |
36 |
|
121 |
twoaday |
225 |
if (PTD_is_used ()) { |
122 |
werner |
36 |
keys[0] = VK_CONTROL | 0x8000; |
123 |
|
|
keys[1] = 'C' | 0x8000; |
124 |
|
|
keys[2] = 'C'; |
125 |
|
|
keys[3] = VK_CONTROL; |
126 |
twoaday |
273 |
PTD_keyb_send (keys, 4); |
127 |
werner |
36 |
} |
128 |
|
|
else { |
129 |
twoaday |
273 |
KEYDOWN (VK_CONTROL, 0x1d); |
130 |
|
|
KEYDOWN ((BYTE)'C', 0x2e); |
131 |
|
|
KEYUP ((BYTE)'C', 0x2e); |
132 |
|
|
KEYUP (VK_CONTROL, 0x1d); |
133 |
werner |
36 |
} |
134 |
twoaday |
226 |
} |
135 |
werner |
36 |
|
136 |
|
|
|
137 |
twoaday |
255 |
/* Set the position inside the edit control @hwnd. */ |
138 |
werner |
36 |
static inline void |
139 |
twoaday |
328 |
window_msg_em_set_pos (HWND hwnd, int begin) |
140 |
werner |
36 |
{ |
141 |
twoaday |
255 |
SendMessage (hwnd, EM_SETSEL, 0, begin? 0 : -1); |
142 |
twoaday |
226 |
} |
143 |
werner |
36 |
|
144 |
|
|
|
145 |
twoaday |
255 |
/* Get the current window and return it in @r_main. |
146 |
|
|
Return value: The Window with the keyboard focus. */ |
147 |
werner |
36 |
static HWND |
148 |
twoaday |
328 |
get_current_wnd (HWND main, int use_hotkey, HWND *r_main) |
149 |
werner |
36 |
{ |
150 |
|
|
HWND prev, fg; |
151 |
|
|
|
152 |
twoaday |
255 |
if (use_hotkey) |
153 |
|
|
prev = GetForegroundWindow (); |
154 |
werner |
36 |
else { |
155 |
twoaday |
328 |
*r_main = GetNextWindow (main, GW_HWNDNEXT); |
156 |
|
|
return PTD_get_curr_hwnd (); |
157 |
werner |
36 |
} |
158 |
|
|
|
159 |
twoaday |
255 |
SetForegroundWindow (prev); |
160 |
|
|
AttachThreadInput (GetCurrentThreadId(), |
161 |
|
|
GetWindowThreadProcessId (prev, NULL), |
162 |
|
|
TRUE); |
163 |
|
|
fg = GetFocus (); |
164 |
|
|
AttachThreadInput (GetCurrentThreadId (), |
165 |
|
|
GetWindowThreadProcessId (prev, NULL), |
166 |
|
|
FALSE); |
167 |
werner |
36 |
|
168 |
twoaday |
328 |
*r_main = prev; |
169 |
werner |
36 |
return fg; |
170 |
twoaday |
226 |
} |
171 |
werner |
36 |
|
172 |
|
|
|
173 |
twoaday |
273 |
/* Retrieve the contents of the current window based on |
174 |
|
|
the old window handle @old_hwnd and store the information |
175 |
|
|
in @ctx. |
176 |
|
|
A return value of 0 indicates a problem. */ |
177 |
werner |
36 |
int |
178 |
twoaday |
328 |
copy_window_content (HWND old_hwnd, HWND *r_main, HWND *r_focus, |
179 |
|
|
int use_hotkey) |
180 |
werner |
36 |
{ |
181 |
|
|
HWND hwnd; |
182 |
twoaday |
328 |
int rc; |
183 |
werner |
36 |
|
184 |
twoaday |
328 |
rc = WPTERR_GENERAL; |
185 |
|
|
hwnd = get_current_wnd (old_hwnd, use_hotkey, r_main); |
186 |
twoaday |
255 |
if (!hwnd) |
187 |
werner |
36 |
return rc; |
188 |
twoaday |
328 |
*r_focus = hwnd; |
189 |
twoaday |
255 |
clip_clear (); |
190 |
werner |
36 |
|
191 |
twoaday |
255 |
AttachThreadInput (GetCurrentThreadId (), |
192 |
|
|
GetWindowThreadProcessId (hwnd, NULL), |
193 |
|
|
TRUE); |
194 |
|
|
SetFocus (hwnd); |
195 |
werner |
36 |
/* First we try to send a simple clipboard copying command and check |
196 |
|
|
if the clipboard contains some text. */ |
197 |
twoaday |
255 |
SendMessage (hwnd, WM_COPY, 0, 0); |
198 |
|
|
if (!clip_check ()) { |
199 |
werner |
36 |
rc = 0; |
200 |
|
|
goto leave; |
201 |
|
|
} |
202 |
|
|
/* Then the check if the window is an edit control */ |
203 |
twoaday |
328 |
window_msg_em_set_pos (hwnd, 0); |
204 |
twoaday |
255 |
SendMessage (hwnd, WM_COPY, 0, 0); |
205 |
|
|
if (!clip_check ()) { |
206 |
werner |
36 |
rc = 0; |
207 |
|
|
goto leave; |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
/* The last try is to send a mark all and copy to clipboard command */ |
211 |
twoaday |
328 |
window_msg_selectall (); |
212 |
|
|
window_msg_copy (); |
213 |
twoaday |
255 |
if (!clip_check ()) |
214 |
werner |
36 |
rc = 0; |
215 |
|
|
|
216 |
|
|
leave: |
217 |
twoaday |
255 |
AttachThreadInput (GetCurrentThreadId (), |
218 |
|
|
GetWindowThreadProcessId (hwnd, NULL), |
219 |
|
|
FALSE); |
220 |
werner |
36 |
return rc; |
221 |
twoaday |
226 |
} |
222 |
werner |
36 |
|
223 |
|
|
|
224 |
twoaday |
273 |
/* Restore the clipboard contents to the window handle given |
225 |
|
|
in @ctx and reset the current window to @old_hwnd if possible. */ |
226 |
werner |
36 |
int |
227 |
twoaday |
328 |
paste_window_content (HWND old_hwnd, HWND main, HWND focus) |
228 |
werner |
36 |
{ |
229 |
twoaday |
255 |
HWND hwnd, lost; |
230 |
werner |
36 |
|
231 |
twoaday |
328 |
hwnd = focus; |
232 |
|
|
|
233 |
|
|
/* Restore window when in minimized state. */ |
234 |
|
|
if (IsIconic (main)) |
235 |
twoaday |
255 |
ShowWindow (hwnd, SW_SHOWNORMAL); |
236 |
|
|
SetForegroundWindow (hwnd); |
237 |
werner |
36 |
|
238 |
twoaday |
255 |
AttachThreadInput (GetCurrentThreadId (), |
239 |
|
|
GetWindowThreadProcessId (hwnd, NULL), |
240 |
|
|
TRUE); |
241 |
werner |
36 |
|
242 |
twoaday |
255 |
lost = SetFocus (hwnd); |
243 |
werner |
36 |
|
244 |
twoaday |
328 |
window_msg_em_set_pos (hwnd, 0); |
245 |
|
|
window_msg_paste (); |
246 |
|
|
window_msg_em_set_pos (hwnd, 1); |
247 |
werner |
36 |
|
248 |
twoaday |
273 |
AttachThreadInput (GetCurrentThreadId(), |
249 |
|
|
GetWindowThreadProcessId (hwnd, NULL), |
250 |
|
|
FALSE); |
251 |
werner |
36 |
|
252 |
twoaday |
273 |
SetForegroundWindow (lost); |
253 |
werner |
36 |
return 0; |
254 |
twoaday |
226 |
} |