/[winpt]/trunk/Src/wptCurrWnd.cpp
ViewVC logotype

Contents of /trunk/Src/wptCurrWnd.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 5799 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26