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

Annotation of /trunk/Src/wptCurrWnd.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 416 - (hide annotations)
Sun Feb 12 18:29:29 2012 UTC (13 years ago) by twoaday
File size: 5869 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26