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