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