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

Legend:
Removed from v.32  
changed lines
  Added in v.174

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26