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

Contents of /trunk/Src/wptClipEditDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 225 - (show annotations)
Tue Jun 6 13:37:59 2006 UTC (18 years, 8 months ago) by twoaday
File size: 6986 byte(s)


1 /* wptClipEditDlg.cpp - Clipboard Editor dialog
2 * Copyright (C) 2000-2006 Timo Schulz
3 *
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 #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
48 /* Load the clipboard contents into the text field in @dlg.
49 Return value: 0 on success. */
50 static int
51 load_clipboard (HWND dlg)
52 {
53 HANDLE clipmem;
54 char *cliptext;
55 char *p;
56
57 CloseClipboard (); /* make sure it is closed. */
58 if (OpenClipboard (NULL) == FALSE) {
59 msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), _("Clipboard"), MB_ERR);
60 return -1;
61 }
62 clipmem = GetClipboardData (CF_TEXT);
63 if (clipmem == NULL) {
64 CloseClipboard ();
65 return -1;
66 }
67
68 cliptext = (char *) GlobalLock (clipmem);
69 if (cliptext == NULL || !strlen (cliptext)) {
70 CloseClipboard ();
71 return -1;
72 }
73
74 p = new char [strlen (cliptext)+2];
75 if (!p)
76 BUG (0);
77 strcpy (p, cliptext);
78
79 GlobalUnlock (clipmem);
80 CloseClipboard ();
81 SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);
82 free_if_alloc (p);
83
84 return 0;
85 }
86
87
88 /* Load the clipboard from the selected file. */
89 static int
90 load_clipboard_from_file (HWND dlg)
91 {
92 OPENFILENAME open;
93 FILE *fp;
94 char *p;
95 char file[300] = "";
96 DWORD size;
97 int id;
98
99 memset (&open, 0, sizeof (open));
100 open.lStructSize = sizeof (OPENFILENAME);
101 open.hInstance = glob_hinst;
102 open.lpstrTitle = _("File Open");
103 open.lpstrFilter = "All Files (*.*)\0*.*\0\0";
104 open.hwndOwner = dlg;
105 open.lpstrFile = file;
106 open.nMaxFile = sizeof (file)-1;
107 open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
108
109 if (GetOpenFileName (&open)) {
110 if( (size=get_file_size (file)) > MAX_CLIPTEXT_SIZE) {
111 id = msg_box (dlg, _("The file you want to add is very large.\n"
112 "Still proceed?"), _("Clipboard"),
113 MB_INFO|MB_YESNO);
114 if (id == IDNO)
115 return -1;
116 }
117 fp = fopen (file, "rb");
118 if (!fp) {
119 msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN),
120 _("Clipboard"), MB_ERR);
121 return FALSE;
122 }
123 p = new char[size+1];
124 if (!p)
125 BUG (0);
126 memset (p, 0, size+1);
127 size = fread (p, 1, size, fp);
128 fclose (fp);
129 SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);
130 free_if_alloc (p);
131 }
132 return 0;
133 }
134
135
136 /* Save the contents of the clipboard to the selected file. */
137 static int
138 save_clipboard_to_file (HWND dlg)
139 {
140 OPENFILENAME save;
141 FILE *fp;
142 char *p=NULL;
143 char file[300] = "";
144 DWORD nbytes;
145 int id;
146
147 memset (&save, 0, sizeof (save));
148 save.lStructSize = sizeof (OPENFILENAME);
149 save.hInstance = glob_hinst;
150 save.lpstrTitle = _("File Save");
151 save.lpstrFile = (char *)"All Files (*.*)\0*.*\0\0";
152 save.hwndOwner = dlg;
153 save.lpstrFile = file;
154 save.nMaxFile = sizeof (file) - 1;
155 save.Flags = OFN_OVERWRITEPROMPT;
156
157 if (GetSaveFileName (&save)) {
158 if (file_exist_check (file) == 0) {
159 id = log_box (_("Clipboard"), MB_YESNO,
160 _("\"%s\" already exists.\n"
161 "Replace existing file?"), file);
162 if( id == IDNO )
163 return FALSE;
164 }
165 fp = fopen (file, "wb");
166 if (!fp) {
167 msg_box (dlg, winpt_strerror (WPTERR_FILE_CREAT),
168 _("Clipboard"), MB_ERR);
169 return FALSE;
170 }
171 /* XXX: there is a NUL-byte at the end. */
172 nbytes = SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2,
173 WM_GETTEXTLENGTH, 0, 0);
174 if (nbytes > 0) {
175 p = new char[nbytes+1];
176 if (!p)
177 BUG (NULL);
178 GetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes);
179 fwrite (p, 1, nbytes, fp);
180
181 }
182 fclose (fp);
183 free_if_alloc (p);
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 switch (msg) {
194 case WM_INITDIALOG:
195 SetWindowText (dlg, _("Clipboard Editor"));
196 SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy"));
197 SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r"));
198 SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));
199 SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));
200 SetDlgItemText (dlg, IDC_CLIPEDIT_QUOTE, _("Add quotes"));
201 SetDlgItemText (dlg, IDOK, _("&Close"));
202
203 load_clipboard (dlg);
204 center_window (dlg, NULL);
205 SetForegroundWindow (dlg);
206 return TRUE;
207
208 case WM_HELP:
209 html_help_dispatch (lparam, "winpt.chm::winpt_texts.txt", help_arr);
210 break;
211
212 case WM_COMMAND:
213 switch (LOWORD (wparam)) {
214 case IDCANCEL:
215 EndDialog (dlg, FALSE);
216 return TRUE;
217
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 CheckDlgButton (dlg, IDC_CLIPEDIT_QUOTE, BST_UNCHECKED);
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 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26