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

Annotation of /trunk/Src/wptClipEditDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 248 - (hide annotations)
Fri Jul 28 11:11:09 2006 UTC (18 years, 7 months ago) by twoaday
File size: 8194 byte(s)
Prepare 1.0.0pre2 release.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26