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

Diff of /trunk/Src/wptClipEditDlg.cpp

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

revision 24 by twoaday, Sat Oct 8 10:43:08 2005 UTC revision 328 by twoaday, Fri Sep 25 16:07:38 2009 UTC
# Line 1  Line 1 
1  /* wptClipEditDlg.cpp - Clipboard Editor dialog  /* wptClipEditDlg.cpp - Clipboard Editor dialog
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2006 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  #ifdef HAVE_CONFIG_H
17   * along with WinPT; if not, write to the Free Software Foundation,  #include <config.h>
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  #endif
  */  
   
 #include <windows.h>  
 #include <stdio.h>  
   
 #include "wptTypes.h"  
 #include "wptW32API.h"  
 #include "wptVersion.h"  
 #include "wptErrors.h"  
 #include "wptGPG.h"  
 #include "wptNLS.h"  
 #include "../resource.h"  
   
   
 /* Dialog box procedure for the clipboard editor. */  
 BOOL CALLBACK  
 clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  
 {  
     HANDLE clipmem;  
     OPENFILENAME open;  
     FILE * fp;  
     char * cliptext = NULL, * newtext = NULL,  * p;  
     char file[512] = "";  
     int nbytes, id, size;  
       
     switch ( msg )  {  
     case WM_INITDIALOG:  
         #ifndef LANG_DE  
         SetWindowText (dlg, _("Clipboard Editor"));  
         SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy"));  
         SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r"));  
         SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));      
         SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));  
         #endif  
         CloseClipboard (); /* make sure it is closed. */  
         if( OpenClipboard( NULL ) == FALSE ) {  
             msg_box( dlg, winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR );  
             return FALSE;  
         }  
         clipmem = GetClipboardData (CF_TEXT);  
         if (clipmem == NULL) {  
             center_window (dlg, NULL);  
             SetForegroundWindow (dlg);  
             CloseClipboard ();  
             return FALSE;  
         }  
         cliptext = (char *) GlobalLock (clipmem);  
         if (cliptext == NULL || !strlen (cliptext)) {  
             CloseClipboard ();  
             return FALSE;  
         }  
         p = new char [strlen (cliptext)+2];  
         if (!p)  
             BUG (0);  
         strcpy (p, cliptext);  
         GlobalUnlock (clipmem);  
         CloseClipboard ();  
         SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);  
         free_if_alloc (p);  
         center_window (dlg, NULL);  
         SetForegroundWindow (dlg);  
         return TRUE;  
           
     case WM_SYSCOMMAND:  
         if (LOWORD (wparam) == SC_CLOSE)  
             EndDialog (dlg, TRUE);  
         return FALSE;  
           
     case WM_COMMAND:  
         switch (LOWORD (wparam)) {  
         case IDOK:                
             EndDialog (dlg, TRUE);  
             return TRUE;  
               
         case IDC_CLIPEDIT_SEND:  
             if (IsDlgButtonChecked (dlg, IDC_CLIPEDIT_QUOTE)) {  
                 gpgme_data_t txt;  
                 gpg_data_new_from_clipboard (&txt, 0);  
                 gpg_data_mail_quote (&txt);  
                 gpg_data_release_and_set_clipboard (txt, 0);  
                 SendMessage (dlg, WM_INITDIALOG, 0, 0);  
                 return TRUE;  
             }  
             SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, EM_SETSEL, 0, -1);  
             SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_COPY, 0, 0);  
             return TRUE;  
               
         case IDC_CLIPEDIT_CLEAR:  
             if( OpenClipboard( NULL ) == FALSE ) {        
                 msg_box( dlg,  winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR );  
                 return FALSE;  
             }                    
             if (EmptyClipboard () == FALSE) {  
                 CloseClipboard ();  
                 msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY), _("Clipboard"), MB_ERR);  
                 return FALSE;  
             }  
             CloseClipboard ();  
             SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, "");  
             return TRUE;  
               
         case IDC_CLIPEDIT_LOAD:  
             memset (&open, 0, sizeof (open));  
             open.lStructSize = sizeof (OPENFILENAME);  
             open.hInstance = glob_hinst;  
             open.lpstrTitle = _("File Open");  
             open.lpstrFilter = (char *)_("All Files (*.*)\0*.*\0\0");  
             open.hwndOwner = dlg;  
             open.lpstrFile = file;        
             open.nMaxFile = sizeof (file)-1;      
             open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;  
             if (GetOpenFileName (&open)) {  
                 if( (size=get_file_size( file )) > MAX_CLIPTEXT_SIZE ) {  
                     id = msg_box( dlg, _("The file you want to add is very large.\n"  
                                         "Still proceed?"), _("Clipboard"), MB_INFO|MB_YESNO );  
                     if( id == IDNO )  
                         return FALSE;  
                 }  
                 fp = fopen( file, "rb" );  
                 if (!fp) {  
                     msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), _("Clipboard"), MB_ERR);  
                     return FALSE;  
                 }  
                 p = new char[size+1];  
                 if (!p)  
                     BUG (0);  
                 fread (p, 1, size, fp);  
                 p[size] = '\0';  
                 fclose (fp);  
                 SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);  
                 free_if_alloc (p);  
                 /* XXX: for some files the code produces a crash! */  
                 PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDC_CLIPEDIT_SEND, 0), NULL);  
                 return TRUE;  
             }  
             break;  
               
         case IDC_CLIPEDIT_SAVE:  
             memset (&open, 0, sizeof (open));  
             open.lStructSize = sizeof (OPENFILENAME);  
             open.hInstance = glob_hinst;  
             open.lpstrTitle = _("File Save");  
             open.lpstrFile = (char *)_("All Files (*.*)\0*.*\0\0");  
             open.hwndOwner = dlg;        
             open.lpstrFile = file;        
             open.nMaxFile = sizeof (file) - 1;  
             open.Flags = OFN_OVERWRITEPROMPT;  
               
             if( GetSaveFileName( &open ) ) {  
                 if( file_exist_check( file ) == 0 ) {  
                     id = log_box (_("Clipboard"), MB_YESNO,  
                                   _("\"%s\" already exists.\n"  
                                   "Replace existing file?"), file);  
                     if( id == IDNO )  
                         return FALSE;  
                 }  
                 fp = fopen( file, "wb" );  
                 if( !fp ) {  
                     msg_box( dlg, winpt_strerror( WPTERR_FILE_CREAT ), _("Clipboard"), MB_ERR );  
                     return FALSE;                
                 }  
                 nbytes = SendDlgItemMessage( dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_GETTEXTLENGTH, 0, 0 );  
                 if( nbytes > 0 ) {  
                     p = new char[nbytes+1];              
                     if( !p )  
                         BUG( NULL );  
                     GetDlgItemText( dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes );  
                     fwrite( p, 1, nbytes, fp );  
                 }            
                 fclose( fp );    
                 free_if_alloc( p );  
             }  
             break;  
         }  
         break;  
     }  
       
     return FALSE;  
 }  
19    
20    #include <windows.h>
21    #include <stdio.h>
22    
23    #include "wptTypes.h"
24    #include "wptW32API.h"
25    #include "wptVersion.h"
26    #include "wptErrors.h"
27    #include "wptGPG.h"
28    #include "wptNLS.h"
29    #include "resource.h"
30    #include "wptCommonCtl.h"
31    #include "wptContext.h"
32    #include "wptKeylist.h"
33    
34    
35    /* XXX: it would be really nice to have tempest prevention fonts. */
36    
37    void verify_get_clip_info (gpgme_signature_t sig,
38                               char **r_header, char **r_footer);
39    
40    
41    /* Load the clipboard contents into the text field in @dlg.
42       Optinally @header and @footer can be used to set a header and footer.
43       Return value: 0 on success. */
44    static int
45    load_clipboard (HWND dlg, const char *header, const char *footer)
46    {
47        HANDLE clipmem;
48        char *cliptext;
49        char *p, *pp;
50    
51        CloseClipboard (); /* make sure it is closed. */
52        if (OpenClipboard (NULL) == FALSE) {
53            msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), _("Clipboard"), MB_ERR);
54            return -1;
55        }
56        clipmem = GetClipboardData (CF_TEXT);
57        if (clipmem == NULL) {
58            CloseClipboard ();
59            return -1;
60        }
61        
62        cliptext = (char *) GlobalLock (clipmem);
63        if (cliptext == NULL || !strlen (cliptext)) {    
64            CloseClipboard ();
65            return -1;
66        }
67    
68        p = new char [strlen (cliptext)+2];
69        if (!p)    
70            BUG (0);        
71        strcpy (p, cliptext);
72        
73        GlobalUnlock (clipmem);
74        CloseClipboard ();
75    
76        if (header && footer) {
77            const char *fmt = "%s\r\n%s\r\n%s";
78            int len;
79    
80            len = strlen (header) + strlen (footer) + strlen (fmt) + strlen (p);
81            pp = new char[len + 1];
82            if (!pp)
83                BUG (0);
84            _snprintf (pp, len, fmt, header, p, footer);
85            SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, pp);
86            free_if_alloc (pp);
87        }
88        else
89            SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);
90        free_if_alloc (p);
91        return 0;
92    }
93    
94    
95    /* Load the clipboard from the selected file. */
96    static int
97    load_clipboard_from_file (HWND dlg)
98    {
99        OPENFILENAME open;
100        FILE *fp;
101        char *p;
102        char file[MAX_PATH+64] = "";
103        DWORD size;
104        int id;
105    
106        memset (&open, 0, sizeof (open));
107        open.lStructSize = sizeof (OPENFILENAME);
108        open.hInstance = glob_hinst;
109        open.lpstrTitle = _("File Open");
110        open.lpstrFilter = "All Files (*.*)\0*.*\0\0";
111        open.hwndOwner = dlg;          
112        open.lpstrFile = file;                  
113        open.nMaxFile = DIM (file)-1;                  
114        open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
115                
116        if (GetOpenFileName (&open)) {
117            if( (size=get_file_size (file)) > MAX_CLIPTEXT_SIZE) {
118                id = msg_box (dlg, _("The file you want to add is very large.\n"
119                                     "Continue?"), _("Clipboard"),
120                              MB_INFO|MB_YESNO);
121                if (id == IDNO)
122                    return -1;
123            }
124            fp = fopen (file, "rb");
125            if (!fp) {
126                msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN),
127                         _("Clipboard"), MB_ERR);
128                return -1;
129            }
130            p = new char[size+1];
131            if (!p)
132                BUG (0);
133            memset (p, 0, size+1);
134            size = fread (p, 1, size, fp);
135            fclose (fp);
136            SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);        
137            free_if_alloc (p);
138        }
139        return 0;
140    }
141    
142    
143    /* Save the contents of the clipboard to the selected file. */
144    static int
145    save_clipboard_to_file (HWND dlg)
146    {
147        OPENFILENAME save;
148        FILE *fp;
149        char *p=NULL;
150        char file[300] = "";
151        DWORD nbytes;
152    
153        memset (&save, 0, sizeof (save));      
154        save.lStructSize = sizeof (OPENFILENAME);      
155        save.hInstance = glob_hinst;            
156        save.lpstrTitle = _("File Save");      
157        save.lpstrFile = (char*)"All Files (*.*)\0*.*\0\0";
158        save.hwndOwner = dlg;
159        save.lpstrFile = file;
160        save.nMaxFile = DIM (file) - 1;
161        save.Flags = OFN_OVERWRITEPROMPT;
162    
163        if (GetSaveFileName (&save)) {
164            fp = fopen (file, "wb");
165            if (!fp) {
166                msg_box (dlg, winpt_strerror (WPTERR_FILE_CREAT),
167                         _("Clipboard"), MB_ERR);
168                return -1;
169            }
170            nbytes = SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2,
171                                         WM_GETTEXTLENGTH, 0, 0);
172            if (nbytes > 0) {
173                p = new char[nbytes+1];
174                if (!p)
175                    BUG (NULL);
176                GetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes);
177                fwrite (p, 1, nbytes, fp);
178                
179            }          
180            fclose (fp);
181            free_if_alloc (p);
182            msg_box (dlg, _("Data successfully written to file."),
183                     _("Clipboard"), MB_OK);
184        }
185        return 0;
186    }
187    
188    
189    /* Dialog box procedure for the clipboard editor. */
190    BOOL CALLBACK
191    clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
192    {
193        HMENU menu;
194        char *head=NULL, *foot=NULL;
195        gpgme_data_t txt;
196    
197        switch (msg)  {
198        case WM_INITDIALOG:        
199            /* If there is a param, we expect a signature and initialize
200               the header and footer variable accordingly. */
201            if (lparam)
202                verify_get_clip_info ((gpgme_signature_t)lparam, &head, &foot);
203            load_clipboard (dlg, head, foot);
204            free_if_alloc (head);
205            free_if_alloc (foot);
206    
207            menu = LoadMenu (glob_hinst, (LPCTSTR)IDR_WINPT_CLIPEDIT);
208          
209            set_menu_text_bypos (menu, 0, _("&File"));
210            set_menu_text_bypos (menu, 1, _("&Edit"));
211                                
212            set_menu_text (menu, ID_CLIPEDIT_COPY, _("&Copy"));
213            set_menu_text (menu, ID_CLIPEDIT_CLEAR, _("Clea&r"));
214            set_menu_text (menu, ID_CLIPEDIT_QUOTE, _("&Quote"));
215            set_menu_text (menu, ID_CLIPEDIT_OPEN, _("&Open..."));  
216            set_menu_text (menu, ID_CLIPEDIT_SAVE, _("&Save..."));
217            set_menu_text (menu, ID_CLIPEDIT_PASTE, _("&Paste"));
218            set_menu_text (menu, ID_CLIPEDIT_ENC, _("&Encrypt"));
219            set_menu_text (menu, ID_CLIPEDIT_DEC, _("&Decrypt"));
220            set_menu_text (menu, ID_CLIPEDIT_QUIT, _("&Exit"));
221            SetMenu (dlg, menu);
222            SetWindowText (dlg, _("Clipboard Editor"));
223            SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy"));
224            center_window (dlg, NULL);
225            SetForegroundWindow (dlg);
226            return TRUE;
227            
228        case WM_COMMAND:
229            switch (LOWORD (wparam)) {
230            case IDCANCEL:
231                EndDialog (dlg, FALSE);
232                return TRUE;
233    
234            case IDOK:
235            case ID_CLIPEDIT_QUIT:
236                EndDialog (dlg, TRUE);
237                return TRUE;
238                
239            case ID_CLIPEDIT_QUOTE:
240                SendMessage (dlg, WM_COMMAND, ID_CLIPEDIT_COPY, 0);
241                if (gpg_data_new_from_clipboard (&txt, 0))
242                    return TRUE;
243                gpg_data_mail_quote (&txt);
244                gpg_data_release_to_clipboard (txt, 0);
245                load_clipboard (dlg, head, foot);
246                return TRUE;
247    
248            case ID_CLIPEDIT_COPY:
249            case IDC_CLIPEDIT_SEND:
250                SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, EM_SETSEL, 0, -1);
251                SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_COPY, 0, 0);
252                SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, EM_SETSEL, -1, 0);
253                return TRUE;
254                
255            case ID_CLIPEDIT_CLEAR:
256                if (OpenClipboard (NULL) == FALSE) {
257                    msg_box (dlg,  winpt_strerror (WPTERR_CLIP_OPEN),
258                             _("Clipboard"), MB_ERR);
259                    return TRUE;
260                }                  
261                if (EmptyClipboard () == FALSE)            
262                    msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY),
263                             _("Clipboard"), MB_ERR);
264                else
265                    SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, "");
266                CloseClipboard ();
267                return TRUE;
268    
269            case ID_CLIPEDIT_PASTE:
270                load_clipboard (dlg, NULL, NULL);
271                break;
272                
273            case ID_CLIPEDIT_OPEN:
274                if (!load_clipboard_from_file (dlg))
275                    PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDC_CLIPEDIT_SEND, 0), 0);
276                return TRUE;
277                
278            case ID_CLIPEDIT_SAVE:
279                save_clipboard_to_file (dlg);          
280                return TRUE;
281    
282            case ID_CLIPEDIT_ENC:
283                SendMessage (dlg, WM_COMMAND, ID_CLIPEDIT_COPY, 0);
284                SendMessage (glob_hwnd, WM_COMMAND, ID_WINPT_ENCRYPT, 0);
285                load_clipboard (dlg, NULL, NULL);
286                SetForegroundWindow (dlg);
287                break;
288    
289            case ID_CLIPEDIT_DEC:
290                SendMessage (glob_hwnd, WM_COMMAND, ID_WINPT_DECRYPT_VERIFY, 0);
291                load_clipboard (dlg, NULL, NULL);
292                SetForegroundWindow (dlg);
293                break;
294            }
295            break;
296        }
297        
298        return FALSE;
299    }

Legend:
Removed from v.24  
changed lines
  Added in v.328

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26