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

Annotation of /trunk/Src/wptCurrWnd.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 414 - (hide annotations)
Sat Feb 11 16:45:17 2012 UTC (13 years ago) by twoaday
File size: 5883 byte(s)
2012-02-11  Timo Schulz  <twoaday@gmx.net>

        * wptCurrWnd.cpp (get_current_wnd): store thread ID
        pair to avoid side-effects.
        (paste_window_content): Likewise.
        (copy_window_content): Likewise.
				        					

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     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 340 if (PTD_is_hook_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 340 PTD_keyboard_send_keys (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 340 if (PTD_is_hook_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 340 PTD_keyboard_send_keys (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 340 if (PTD_is_hook_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 340 PTD_keyboard_send_keys (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 twoaday 414 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 twoaday 414 if (!use_hotkey) {
151 twoaday 328 *r_main = GetNextWindow (main, GW_HWNDNEXT);
152 twoaday 340 return PTD_get_current_window ();
153 werner 36 }
154 twoaday 340
155 twoaday 414 HWND prev = GetForegroundWindow ();
156     DWORD src_thread = GetCurrentThreadId();
157     DWORD dst_thread = GetWindowThreadProcessId (prev, NULL);
158    
159     SetForegroundWindow (prev);
160     AttachThreadInput (src_thread, dst_thread, TRUE);
161     HWND fg = GetFocus ();
162     AttachThreadInput (src_thread, dst_thread, FALSE);
163 werner 36
164 twoaday 328 *r_main = prev;
165 werner 36 return fg;
166 twoaday 226 }
167 werner 36
168    
169 twoaday 273 /* Retrieve the contents of the current window based on
170     the old window handle @old_hwnd and store the information
171     in @ctx.
172     A return value of 0 indicates a problem. */
173 werner 36 int
174 twoaday 340 copy_window_content (HWND old_hwnd, HWND *r_main, HWND *r_focus, int use_hotkey)
175 twoaday 414 {
176     int rc = WPTERR_GENERAL;
177     HWND hwnd = get_current_wnd (old_hwnd, use_hotkey, r_main);
178 twoaday 255 if (!hwnd)
179 werner 36 return rc;
180 twoaday 328 *r_focus = hwnd;
181 twoaday 255 clip_clear ();
182 twoaday 414
183     DWORD src_thread = GetCurrentThreadId ();
184     DWORD dst_thread = GetWindowThreadProcessId (hwnd, NULL);
185    
186     AttachThreadInput (src_thread, dst_thread, TRUE);
187 twoaday 255 SetFocus (hwnd);
188 werner 36 /* First we try to send a simple clipboard copying command and check
189     if the clipboard contains some text. */
190 twoaday 255 SendMessage (hwnd, WM_COPY, 0, 0);
191     if (!clip_check ()) {
192 twoaday 340 rc = WPTERR_SUCESS;
193 werner 36 goto leave;
194     }
195     /* Then the check if the window is an edit control */
196 twoaday 328 window_msg_em_set_pos (hwnd, 0);
197 twoaday 255 SendMessage (hwnd, WM_COPY, 0, 0);
198     if (!clip_check ()) {
199 twoaday 340 rc = WPTERR_SUCESS;
200 werner 36 goto leave;
201     }
202    
203     /* The last try is to send a mark all and copy to clipboard command */
204 twoaday 328 window_msg_selectall ();
205     window_msg_copy ();
206 twoaday 255 if (!clip_check ())
207 twoaday 340 rc = WPTERR_SUCESS;
208 werner 36
209     leave:
210 twoaday 414 AttachThreadInput (src_thread, dst_thread, FALSE);
211 werner 36 return rc;
212 twoaday 226 }
213 werner 36
214    
215 twoaday 273 /* Restore the clipboard contents to the window handle given
216     in @ctx and reset the current window to @old_hwnd if possible. */
217 werner 36 int
218 twoaday 328 paste_window_content (HWND old_hwnd, HWND main, HWND focus)
219 twoaday 414 {
220     HWND hwnd = focus;
221 twoaday 328
222     /* Restore window when in minimized state. */
223     if (IsIconic (main))
224 twoaday 255 ShowWindow (hwnd, SW_SHOWNORMAL);
225     SetForegroundWindow (hwnd);
226 werner 36
227 twoaday 414 DWORD src_thread = GetCurrentThreadId();
228     DWORD dst_thread = GetWindowThreadProcessId (hwnd, NULL);
229    
230     AttachThreadInput (src_thread, dst_thread, TRUE);
231     HWND lost = SetFocus (hwnd);
232 twoaday 328 window_msg_em_set_pos (hwnd, 0);
233     window_msg_paste ();
234 twoaday 414 window_msg_em_set_pos (hwnd, 1);
235     AttachThreadInput (src_thread, dst_thread, FALSE);
236 werner 36
237 twoaday 273 SetForegroundWindow (lost);
238 werner 36 return 0;
239 twoaday 226 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26