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