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