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

Diff of /trunk/Src/wptCurrWnd.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 20 by twoaday, Wed Jul 27 11:17:22 2005 UTC revision 57 by werner, Wed Nov 2 11:29:44 2005 UTC
# Line 1  Line 1 
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 HWND PTD_get_curr_hwnd( void );
36  {  extern BOOL PTD_keyb_send( UINT *keys, UINT n_keys );
37      OpenClipboard (NULL);  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() ) {  static void
69          keys[0] = VK_CONTROL | 0x8000;  wnd_msg_markall( HWND hwnd )
70          keys[1] = 'A' | 0x8000;  {
71          keys[2] = 'A';        unsigned keys[4] = {0};
72          keys[3] = VK_CONTROL;    
73          PTD_keyb_send( keys, 4 );      if( PTD_is_used() ) {
74            keys[0] = VK_CONTROL | 0x8000;
75      }          keys[1] = 'A' | 0x8000;
76      else {          keys[2] = 'A';  
77          KEYDOWN( VK_CONTROL, 0x1d );              keys[3] = VK_CONTROL;  
78          KEYDOWN( (BYTE)'A', 0x1e );              PTD_keyb_send( keys, 4 );
79          KEYUP( (BYTE)'A', 0x1e );  
80          KEYUP( VK_CONTROL, 0x1d );            }
81      }      else {
82  } /* msg_wm_markall */          KEYDOWN( VK_CONTROL, 0x1d );    
83            KEYDOWN( (BYTE)'A', 0x1e );    
84            KEYUP( (BYTE)'A', 0x1e );
85  static void          KEYUP( VK_CONTROL, 0x1d );      
86  wnd_msg_paste( HWND hwnd )      }
87  {  } /* msg_wm_markall */
88      unsigned keys[4] = {0};  
89    
90      if( PTD_is_used() ) {  static void
91          keys[0] = VK_CONTROL | 0x8000;  wnd_msg_paste( HWND hwnd )
92          keys[1] = 'V' | 0x8000;  {
93          keys[2] = 'V';      unsigned keys[4] = {0};
94          keys[3] = VK_CONTROL;  
95          PTD_keyb_send( keys, 4 );            if( PTD_is_used() ) {
96      }          keys[0] = VK_CONTROL | 0x8000;
97      else {          keys[1] = 'V' | 0x8000;
98          KEYDOWN( VK_CONTROL, 0x1d );              keys[2] = 'V';
99          KEYDOWN( (BYTE)'V', 0x2f );              keys[3] = VK_CONTROL;
100          KEYUP( (BYTE)'V', 0x2f );                PTD_keyb_send( keys, 4 );      
101          KEYUP( VK_CONTROL, 0x1d );            }
102      }      else {
103  } /* msg_wm_paste */          KEYDOWN( VK_CONTROL, 0x1d );    
104            KEYDOWN( (BYTE)'V', 0x2f );    
105            KEYUP( (BYTE)'V', 0x2f );      
106  static void          KEYUP( VK_CONTROL, 0x1d );      
107  wnd_msg_copy( HWND hwnd )      }
108  {  } /* msg_wm_paste */
109      unsigned keys[4] = {0};  
110    
111      if( PTD_is_used() ) {  static void
112          keys[0] = VK_CONTROL | 0x8000;  wnd_msg_copy( HWND hwnd )
113          keys[1] = 'C' | 0x8000;  {
114          keys[2] = 'C';      unsigned keys[4] = {0};
115          keys[3] = VK_CONTROL;  
116          PTD_keyb_send( keys, 4 );            if( PTD_is_used() ) {
117      }          keys[0] = VK_CONTROL | 0x8000;
118      else {          keys[1] = 'C' | 0x8000;
119          KEYDOWN( VK_CONTROL, 0x1d );          keys[2] = 'C';
120          KEYDOWN( (BYTE)'C', 0x2e );          keys[3] = VK_CONTROL;
121          KEYUP( (BYTE)'C', 0x2e );          PTD_keyb_send( keys, 4 );      
122          KEYUP( VK_CONTROL, 0x1d );            }
123      }      else {
124  } /* msg_wm_copy */          KEYDOWN( VK_CONTROL, 0x1d );
125            KEYDOWN( (BYTE)'C', 0x2e );
126            KEYUP( (BYTE)'C', 0x2e );
127  static inline void          KEYUP( VK_CONTROL, 0x1d );      
128  wnd_msg_em_set_pos( HWND hwnd, int begin )      }
129  {  } /* msg_wm_copy */
130      SendMessage( hwnd, EM_SETSEL, 0, begin? 1 : -1 );  
131  } /* wnd_msg_em_set_pos */  
132    static inline void
133    wnd_msg_em_set_pos( HWND hwnd, int begin )
134  static HWND  {
135  get_curr_wnd( HWND main, int hotkey, HWND *r_main )      SendMessage( hwnd, EM_SETSEL, 0, begin? 1 : -1 );
136  {  } /* wnd_msg_em_set_pos */
137      HWND prev, fg;  
138    
139      if( hotkey )  static HWND
140          prev = GetForegroundWindow();  get_curr_wnd( HWND main, int hotkey, HWND *r_main )
141      else {  {
142          if( r_main )      HWND prev, fg;
143              *r_main = GetNextWindow( main, GW_HWNDNEXT );  
144          return PTD_get_curr_hwnd( );      if( hotkey )
145      }          prev = GetForegroundWindow();
146        else {
147      SetForegroundWindow( prev );          if( r_main )
148                *r_main = GetNextWindow( main, GW_HWNDNEXT );
149      AttachThreadInput( GetCurrentThreadId(),          return PTD_get_curr_hwnd( );
150                          GetWindowThreadProcessId( prev, NULL ),      }
151                          TRUE );  
152      fg = GetFocus();      SetForegroundWindow( prev );
153      AttachThreadInput( GetCurrentThreadId(),  
154                          GetWindowThreadProcessId( prev, NULL ),      AttachThreadInput( GetCurrentThreadId(),
155                          FALSE );                          GetWindowThreadProcessId( prev, NULL ),
156                            TRUE );
157      if( r_main )      fg = GetFocus();
158          *r_main = prev;      AttachThreadInput( GetCurrentThreadId(),
159                            GetWindowThreadProcessId( prev, NULL ),
160      return fg;                          FALSE );
161  } /* get_curr_wnd */  
162        if( r_main )
163            *r_main = prev;
164  int  
165  get_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx, int *r_hotkey )      return fg;
166  {  } /* get_curr_wnd */
167      HWND hwnd;  
168      int rc = 1, hotkey = 0;  
169        int
170      if( r_hotkey )  get_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx, int *r_hotkey )
171          hotkey = *r_hotkey;  {
172                HWND hwnd;
173      hwnd = get_curr_wnd( old_hwnd, hotkey, &ctx->main );      int rc = 1, hotkey = 0;
174      if( hwnd == NULL )      
175          return rc;      if( r_hotkey )
176      ctx->focus = hwnd;          hotkey = *r_hotkey;
177      clip_clear( );          
178                hwnd = get_curr_wnd( old_hwnd, hotkey, &ctx->main );
179      AttachThreadInput( GetCurrentThreadId( ),      if( hwnd == NULL )
180                          GetWindowThreadProcessId( hwnd, NULL ),          return rc;
181                          TRUE );      ctx->focus = hwnd;
182      SetFocus( hwnd );      clip_clear( );
183                    
184      /* First we try to send a simple clipboard copying command and check      AttachThreadInput( GetCurrentThreadId( ),
185         if the clipboard contains some text. */                          GetWindowThreadProcessId( hwnd, NULL ),
186      SendMessage( hwnd, WM_COPY, 0, 0 );                          TRUE );
187      if( !clip_check( ) ) {      SetFocus( hwnd );
188          rc = 0;          
189          goto leave;      /* First we try to send a simple clipboard copying command and check
190      }         if the clipboard contains some text. */
191      /* Then the check if the window is an edit control */      SendMessage( hwnd, WM_COPY, 0, 0 );
192      wnd_msg_em_set_pos( hwnd, 0 );      if( !clip_check( ) ) {
193      SendMessage( hwnd, WM_COPY, 0, 0 );          rc = 0;
194      if( !clip_check( ) ) {          goto leave;
195          rc = 0;      }
196          goto leave;      /* Then the check if the window is an edit control */
197      }      wnd_msg_em_set_pos( hwnd, 0 );
198                SendMessage( hwnd, WM_COPY, 0, 0 );
199      /* The last try is to send a mark all and copy to clipboard command */      if( !clip_check( ) ) {
200      wnd_msg_markall( hwnd );          rc = 0;
201      wnd_msg_copy( hwnd );          goto leave;
202      if( !clip_check( ) )      }
203          rc = 0;          
204        /* The last try is to send a mark all and copy to clipboard command */
205  leave:      wnd_msg_markall( hwnd );
206      AttachThreadInput( GetCurrentThreadId( ),                                            wnd_msg_copy( hwnd );
207                          GetWindowThreadProcessId( hwnd, NULL ),                                  if( !clip_check( ) )
208                          FALSE );          rc = 0;
209      if( r_hotkey )  
210          *r_hotkey = 0; /* reset: we need this in any case */  leave:
211            AttachThreadInput( GetCurrentThreadId( ),                                      
212      return rc;                          GetWindowThreadProcessId( hwnd, NULL ),                            
213  } /* get_window_contents */                          FALSE );
214        if( r_hotkey )
215            *r_hotkey = 0; /* reset: we need this in any case */
216  int      
217  set_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx )      return rc;
218  {  } /* get_window_contents */
219      HWND hwnd, lost;      
220            
221      hwnd = ctx->focus;  int
222      if( IsIconic( ctx->main ) )  set_window_contents( HWND old_hwnd, curr_wnd_ctx *ctx )
223          ShowWindow( hwnd, SW_SHOWNORMAL );  {
224      SetForegroundWindow( hwnd );      HWND hwnd, lost;    
225                    
226      AttachThreadInput( GetCurrentThreadId( ),      hwnd = ctx->focus;
227                          GetWindowThreadProcessId( hwnd, NULL ),      if( IsIconic( ctx->main ) )
228                          TRUE );          ShowWindow( hwnd, SW_SHOWNORMAL );
229                SetForegroundWindow( hwnd );
230      lost = SetFocus( hwnd );          
231        AttachThreadInput( GetCurrentThreadId( ),
232      wnd_msg_em_set_pos( hwnd, 0 );                          GetWindowThreadProcessId( hwnd, NULL ),
233      wnd_msg_paste( hwnd );                          TRUE );
234      wnd_msg_em_set_pos( hwnd, 1 );          
235                lost = SetFocus( hwnd );
236      AttachThreadInput( GetCurrentThreadId(),  
237                          GetWindowThreadProcessId( hwnd, NULL ),      wnd_msg_em_set_pos( hwnd, 0 );
238                          FALSE );      wnd_msg_paste( hwnd );
239                wnd_msg_em_set_pos( hwnd, 1 );
240      SetForegroundWindow( lost );          
241        AttachThreadInput( GetCurrentThreadId(),
242      return 0;                          GetWindowThreadProcessId( hwnd, NULL ),
243  } /* set_window_contents */                          FALSE );
244            
245        SetForegroundWindow( lost );
246    
247        return 0;
248    } /* set_window_contents */

Legend:
Removed from v.20  
changed lines
  Added in v.57

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26