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 |
* |
* |
30 |
#include "../resource.h" |
#include "../resource.h" |
31 |
|
|
32 |
|
|
33 |
BOOL CALLBACK |
/* Load the clipboard contents into the text field in @dlg. |
34 |
clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
Return value: 0 on success. */ |
35 |
|
static int |
36 |
|
load_clipboard (HWND dlg) |
37 |
{ |
{ |
38 |
HANDLE clipmem; |
HANDLE clipmem; |
39 |
OPENFILENAME open; |
char *cliptext; |
40 |
FILE * fp; |
char *p; |
41 |
char * cliptext = NULL, * newtext = NULL, * p; |
|
42 |
char file[512] = ""; |
CloseClipboard (); /* make sure it is closed. */ |
43 |
int nbytes, id, size; |
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 |
switch ( msg ) { |
cliptext = (char *) GlobalLock (clipmem); |
54 |
case WM_INITDIALOG: |
if (cliptext == NULL || !strlen (cliptext)) { |
55 |
#ifndef LANG_DE |
CloseClipboard (); |
56 |
SetWindowText (dlg, _("Clipboard Editor")); |
return -1; |
57 |
SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy")); |
} |
58 |
SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r")); |
|
59 |
SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load")); |
p = new char [strlen (cliptext)+2]; |
60 |
SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save")); |
if (!p) |
61 |
#endif |
BUG (0); |
62 |
CloseClipboard (); /* make sure it is closed. */ |
strcpy (p, cliptext); |
63 |
if( OpenClipboard( NULL ) == FALSE ) { |
|
64 |
msg_box( dlg, winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR ); |
GlobalUnlock (clipmem); |
65 |
return FALSE; |
CloseClipboard (); |
66 |
} |
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p); |
67 |
clipmem = GetClipboardData (CF_TEXT); |
free_if_alloc (p); |
68 |
if (clipmem == NULL) { |
|
69 |
center_window (dlg); |
return 0; |
70 |
SetForegroundWindow (dlg); |
} |
71 |
CloseClipboard (); |
|
72 |
return FALSE; |
|
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 |
cliptext = (char *) GlobalLock (clipmem); |
fp = fopen (file, "rb"); |
102 |
if (cliptext == NULL || !strlen (cliptext)) { |
if (!fp) { |
103 |
CloseClipboard (); |
msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), _("Clipboard"), MB_ERR); |
104 |
return FALSE; |
return FALSE; |
105 |
} |
} |
106 |
p = new char [strlen (cliptext)+2]; |
p = new char[size+1]; |
107 |
if (!p) |
if (!p) |
108 |
BUG (0); |
BUG (0); |
109 |
strcpy (p, cliptext); |
fread (p, 1, size, fp); |
110 |
GlobalUnlock (clipmem); |
p[size] = '\0'; |
111 |
CloseClipboard (); |
fclose (fp); |
112 |
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p); |
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); |
free_if_alloc (p); |
164 |
center_window (dlg); |
} |
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 |
|
SetWindowText (dlg, _("Clipboard Editor")); |
176 |
|
SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy")); |
177 |
|
SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r")); |
178 |
|
SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load")); |
179 |
|
SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save")); |
180 |
|
|
181 |
|
load_clipboard (dlg); |
182 |
|
center_window (dlg, NULL); |
183 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
184 |
return TRUE; |
return TRUE; |
185 |
|
|
186 |
case WM_SYSCOMMAND: |
case WM_SYSCOMMAND: |
187 |
if (LOWORD (wparam) == SC_CLOSE) |
if (LOWORD (wparam) == SC_CLOSE) |
188 |
EndDialog (dlg, TRUE); |
EndDialog (dlg, TRUE); |
189 |
return FALSE; |
return TRUE; |
190 |
|
|
191 |
case WM_COMMAND: |
case WM_COMMAND: |
192 |
switch (LOWORD (wparam)) { |
switch (LOWORD (wparam)) { |
197 |
case IDC_CLIPEDIT_SEND: |
case IDC_CLIPEDIT_SEND: |
198 |
if (IsDlgButtonChecked (dlg, IDC_CLIPEDIT_QUOTE)) { |
if (IsDlgButtonChecked (dlg, IDC_CLIPEDIT_QUOTE)) { |
199 |
gpgme_data_t txt; |
gpgme_data_t txt; |
200 |
gpgme_data_new_from_clipboard (&txt); |
gpg_data_new_from_clipboard (&txt, 0); |
201 |
gpgme_data_mail_quote (&txt); |
gpg_data_mail_quote (&txt); |
202 |
gpgme_data_release_and_set_clipboard (txt); |
gpg_data_release_and_set_clipboard (txt, 0); |
203 |
SendMessage (dlg, WM_INITDIALOG, 0, 0); |
SendMessage (dlg, WM_INITDIALOG, 0, 0); |
204 |
return TRUE; |
return TRUE; |
205 |
} |
} |
208 |
return TRUE; |
return TRUE; |
209 |
|
|
210 |
case IDC_CLIPEDIT_CLEAR: |
case IDC_CLIPEDIT_CLEAR: |
211 |
if( OpenClipboard( NULL ) == FALSE ) { |
if (OpenClipboard (NULL) == FALSE) { |
212 |
msg_box( dlg, winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR ); |
msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), |
213 |
return FALSE; |
_("Clipboard"), MB_ERR); |
214 |
|
return TRUE; |
215 |
} |
} |
216 |
if (EmptyClipboard () == FALSE) { |
if (EmptyClipboard () == FALSE) |
217 |
CloseClipboard (); |
msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY), |
218 |
msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY), _("Clipboard"), MB_ERR); |
_("Clipboard"), MB_ERR); |
219 |
return FALSE; |
else |
220 |
} |
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, ""); |
221 |
CloseClipboard (); |
CloseClipboard (); |
|
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, ""); |
|
222 |
return TRUE; |
return TRUE; |
223 |
|
|
224 |
case IDC_CLIPEDIT_LOAD: |
case IDC_CLIPEDIT_LOAD: |
225 |
memset (&open, 0, sizeof (open)); |
if (!load_clipboard_from_file (dlg)) { |
226 |
open.lStructSize = sizeof (OPENFILENAME); |
/* XXX: for some files the code produces a crash! */ |
227 |
open.hInstance = glob_hinst; |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDC_CLIPEDIT_SEND, 0), NULL); |
|
open.lpstrTitle = _("File Open"); |
|
|
open.lpstrFilter = (char *)_("All Files (*.*)\0*.*"); |
|
|
open.hwndOwner = dlg; |
|
|
open.lpstrFile = file; |
|
|
open.nMaxFile = sizeof (file)-1; |
|
|
open.Flags = 0; |
|
|
if (GetOpenFileName (&open)) { |
|
|
if( (size=get_file_size( file )) > MAX_CLIPTEXT_SIZE ) { |
|
|
id = msg_box( dlg, _("The file you want to add is very large.\n" |
|
|
"Still proceed?"), _("Clipboard"), MB_INFO|MB_YESNO ); |
|
|
if( id == IDNO ) |
|
|
return FALSE; |
|
|
} |
|
|
fp = fopen( file, "rb" ); |
|
|
if (!fp) { |
|
|
msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), _("Clipboard"), MB_ERR); |
|
|
return FALSE; |
|
|
} |
|
|
p = new char[size+1]; |
|
|
if (!p) |
|
|
BUG (0); |
|
|
fread( p, 1, size, fp ); |
|
|
p[size] = '\0'; |
|
|
fclose( fp ); |
|
|
SetDlgItemText( dlg, IDC_CLIPEDIT_CLIPTEXT2, p ); |
|
|
free_if_alloc( p ); |
|
|
PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDC_CLIPEDIT_SEND, 0), NULL ); |
|
|
return TRUE; |
|
228 |
} |
} |
229 |
break; |
return TRUE; |
230 |
|
|
231 |
case IDC_CLIPEDIT_SAVE: |
case IDC_CLIPEDIT_SAVE: |
232 |
memset (&open, 0, sizeof (open)); |
save_clipboard_to_file (dlg); |
233 |
open.lStructSize = sizeof OPENFILENAME; |
return TRUE; |
|
open.hInstance = glob_hinst; |
|
|
open.lpstrTitle = _("File Save"); |
|
|
open.lpstrFile = (char *)_("All Files (*.*)\0*.*"); |
|
|
open.hwndOwner = dlg; |
|
|
open.lpstrFile = file; |
|
|
open.nMaxFile = sizeof (file) - 1; |
|
|
|
|
|
if( GetSaveFileName( &open ) ) { |
|
|
if( file_exist_check( file ) == 0 ) { |
|
|
id = msg_box( dlg, _("File already exists!\nOverwrite?"), |
|
|
_("Clipboard"), MB_INFO|MB_YESNO ); |
|
|
if( id == IDNO ) |
|
|
return FALSE; |
|
|
} |
|
|
fp = fopen( file, "wb" ); |
|
|
if( !fp ) { |
|
|
msg_box( dlg, winpt_strerror( WPTERR_FILE_CREAT ), _("Clipboard"), MB_ERR ); |
|
|
return FALSE; |
|
|
} |
|
|
nbytes = SendDlgItemMessage( dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_GETTEXTLENGTH, 0, 0 ); |
|
|
if( nbytes > 0 ) { |
|
|
p = new char[nbytes+1]; |
|
|
if( !p ) |
|
|
BUG( NULL ); |
|
|
GetDlgItemText( dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes ); |
|
|
fwrite( p, 1, nbytes, fp ); |
|
|
} |
|
|
fclose( fp ); |
|
|
free_if_alloc( p ); |
|
|
} |
|
|
break; |
|
234 |
} |
} |
235 |
break; |
break; |
236 |
} |
} |
237 |
|
|
238 |
return FALSE; |
return FALSE; |
239 |
} /* clip_edit_dlg_proc */ |
} |
240 |
|
|