/[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 105 by twoaday, Wed Nov 30 10:22:00 2005 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-2005 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    #ifdef HAVE_CONFIG_H
21  #include <windows.h>  #include <config.h>
22  #include <stdio.h>  #endif
   
 #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;  
 }  
23    
24    #include <windows.h>
25    #include <stdio.h>
26    
27    #include "wptTypes.h"
28    #include "wptW32API.h"
29    #include "wptVersion.h"
30    #include "wptErrors.h"
31    #include "wptGPG.h"
32    #include "wptNLS.h"
33    #include "resource.h"
34    
35    
36    /* Load the clipboard contents into the text field in @dlg.
37       Return value: 0 on success. */
38    static int
39    load_clipboard (HWND dlg)
40    {
41        HANDLE clipmem;
42        char *cliptext;
43        char *p;
44    
45        CloseClipboard (); /* make sure it is closed. */
46        if (OpenClipboard (NULL) == FALSE) {
47            msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), _("Clipboard"), MB_ERR);
48            return -1;
49        }
50        clipmem = GetClipboardData (CF_TEXT);
51        if (clipmem == NULL) {
52            CloseClipboard ();
53            return -1;
54        }
55        
56        cliptext = (char *) GlobalLock (clipmem);
57        if (cliptext == NULL || !strlen (cliptext)) {    
58            CloseClipboard ();
59            return -1;
60        }
61    
62        p = new char [strlen (cliptext)+2];
63        if (!p)    
64            BUG (0);        
65        strcpy (p, cliptext);
66        
67        GlobalUnlock (clipmem);
68        CloseClipboard ();  
69        SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);    
70        free_if_alloc (p);
71        
72        return 0;
73    }
74    
75    
76    /* Load the clipboard from the selected file. */
77    static int
78    load_clipboard_from_file (HWND dlg)
79    {
80        OPENFILENAME open;
81        FILE *fp;
82        char *p;
83        char file[300] = "";
84        DWORD size;
85        int id;
86    
87        memset (&open, 0, sizeof (open));
88        open.lStructSize = sizeof (OPENFILENAME);      
89        open.hInstance = glob_hinst;            
90        open.lpstrTitle = _("File Open");      
91        open.lpstrFilter = (char *)_("All Files (*.*)\0*.*\0\0");      
92        open.hwndOwner = dlg;          
93        open.lpstrFile = file;                  
94        open.nMaxFile = sizeof (file)-1;                
95        open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
96                
97        if (GetOpenFileName (&open)) {
98            if( (size=get_file_size (file)) > MAX_CLIPTEXT_SIZE) {      
99                id = msg_box (dlg, _("The file you want to add is very large.\n"
100                                     "Still proceed?"), _("Clipboard"),
101                              MB_INFO|MB_YESNO);
102                if (id == IDNO)
103                    return -1;
104            }
105            fp = fopen (file, "rb");
106            if (!fp) {
107                msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN),
108                         _("Clipboard"), MB_ERR);
109                return FALSE;
110            }
111            p = new char[size+1];
112            if (!p)
113                BUG (0);
114            fread (p, 1, size, fp);
115            p[size] = '\0';
116            fclose (fp);
117            SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);        
118            free_if_alloc (p);
119        }
120        return 0;
121    }
122    
123    
124    /* Save the contents of the clipboard to the selected file. */
125    static int
126    save_clipboard_to_file (HWND dlg)
127    {
128        OPENFILENAME save;
129        FILE *fp;
130        char *p=NULL;
131        char file[300] = "";
132        DWORD nbytes;
133        int id;
134    
135        memset (&save, 0, sizeof (save));      
136        save.lStructSize = sizeof (OPENFILENAME);      
137        save.hInstance = glob_hinst;            
138        save.lpstrTitle = _("File Save");      
139        save.lpstrFile = (char *)_("All Files (*.*)\0*.*\0\0");        
140        save.hwndOwner = dlg;                  
141        save.lpstrFile = file;                  
142        save.nMaxFile = sizeof (file) - 1;
143        save.Flags = OFN_OVERWRITEPROMPT;
144                        
145        if( GetSaveFileName( &save ) ) {
146            if( file_exist_check( file ) == 0 ) {      
147                id = log_box (_("Clipboard"), MB_YESNO,
148                              _("\"%s\" already exists.\n"
149                                "Replace existing file?"), file);      
150                if( id == IDNO )    
151                    return FALSE;
152            }
153            fp = fopen (file, "wb");
154            if (!fp) {
155                msg_box (dlg, winpt_strerror (WPTERR_FILE_CREAT),
156                         _("Clipboard"), MB_ERR);
157                return FALSE;
158            }
159            /* XXX: there is a NUL-byte at the end. */
160            nbytes = SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2,
161                                         WM_GETTEXTLENGTH, 0, 0);
162            if (nbytes > 0) {
163                p = new char[nbytes+1];
164                if (!p)
165                    BUG (NULL);
166                GetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes);
167                fwrite (p, 1, nbytes, fp);
168                
169            }          
170            fclose (fp);
171            free_if_alloc (p);
172        }
173        return 0;
174    }
175    
176    
177    /* Dialog box procedure for the clipboard editor. */
178    BOOL CALLBACK
179    clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
180    {
181        switch (msg)  {
182        case WM_INITDIALOG:
183            SetWindowText (dlg, _("Clipboard Editor"));
184            SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy"));
185            SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r"));
186            SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));    
187            SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));
188            SetDlgItemText (dlg, IDC_CLIPEDIT_QUOTE, _("Add quotes"));
189            SetDlgItemText (dlg, IDOK, _("&Exit"));
190    
191            load_clipboard (dlg);
192            center_window (dlg, NULL);
193            SetForegroundWindow (dlg);
194            return TRUE;
195            
196        case WM_SYSCOMMAND:
197            if (LOWORD (wparam) == SC_CLOSE)
198                EndDialog (dlg, TRUE);
199            return TRUE;
200            
201        case WM_COMMAND:
202            switch (LOWORD (wparam)) {
203            case IDOK:              
204                EndDialog (dlg, TRUE);
205                return TRUE;
206                
207            case IDC_CLIPEDIT_SEND:
208                if (IsDlgButtonChecked (dlg, IDC_CLIPEDIT_QUOTE)) {
209                    gpgme_data_t txt;
210                    gpg_data_new_from_clipboard (&txt, 0);
211                    gpg_data_mail_quote (&txt);
212                    gpg_data_release_and_set_clipboard (txt, 0);
213                    SendMessage (dlg, WM_INITDIALOG, 0, 0);
214                    return TRUE;
215                }
216                SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, EM_SETSEL, 0, -1);
217                SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_COPY, 0, 0);
218                return TRUE;
219                
220            case IDC_CLIPEDIT_CLEAR:
221                if (OpenClipboard (NULL) == FALSE) {
222                    msg_box (dlg,  winpt_strerror (WPTERR_CLIP_OPEN),
223                             _("Clipboard"), MB_ERR);
224                    return TRUE;
225                }                  
226                if (EmptyClipboard () == FALSE)            
227                    msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY),
228                             _("Clipboard"), MB_ERR);
229                else
230                    SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, "");
231                CloseClipboard ();
232                return TRUE;
233                
234            case IDC_CLIPEDIT_LOAD:
235                if (!load_clipboard_from_file (dlg)) {
236                    /* XXX: for some files the code produces a crash! */
237                    PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDC_CLIPEDIT_SEND, 0), 0);
238                }
239                return TRUE;
240                
241            case IDC_CLIPEDIT_SAVE:
242                save_clipboard_to_file (dlg);          
243                return TRUE;
244            }
245            break;
246        }
247        
248        return FALSE;
249    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26