30 |
#include "../resource.h" |
#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. */ |
/* Dialog box procedure for the clipboard editor. */ |
170 |
BOOL CALLBACK |
BOOL CALLBACK |
171 |
clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
172 |
{ |
{ |
173 |
HANDLE clipmem; |
switch (msg) { |
|
OPENFILENAME open; |
|
|
FILE * fp; |
|
|
char * cliptext = NULL, * newtext = NULL, * p; |
|
|
char file[512] = ""; |
|
|
int nbytes, id, size; |
|
|
|
|
|
switch ( msg ) { |
|
174 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
|
#ifndef LANG_DE |
|
175 |
SetWindowText (dlg, _("Clipboard Editor")); |
SetWindowText (dlg, _("Clipboard Editor")); |
176 |
SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy")); |
SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy")); |
177 |
SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r")); |
SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r")); |
178 |
SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load")); |
SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load")); |
179 |
SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save")); |
SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save")); |
180 |
#endif |
|
181 |
CloseClipboard (); /* make sure it is closed. */ |
load_clipboard (dlg); |
|
if( OpenClipboard( NULL ) == FALSE ) { |
|
|
msg_box( dlg, winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR ); |
|
|
return FALSE; |
|
|
} |
|
|
clipmem = GetClipboardData (CF_TEXT); |
|
|
if (clipmem == NULL) { |
|
|
center_window (dlg, NULL); |
|
|
SetForegroundWindow (dlg); |
|
|
CloseClipboard (); |
|
|
return FALSE; |
|
|
} |
|
|
cliptext = (char *) GlobalLock (clipmem); |
|
|
if (cliptext == NULL || !strlen (cliptext)) { |
|
|
CloseClipboard (); |
|
|
return FALSE; |
|
|
} |
|
|
p = new char [strlen (cliptext)+2]; |
|
|
if (!p) |
|
|
BUG (0); |
|
|
strcpy (p, cliptext); |
|
|
GlobalUnlock (clipmem); |
|
|
CloseClipboard (); |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p); |
|
|
free_if_alloc (p); |
|
182 |
center_window (dlg, NULL); |
center_window (dlg, NULL); |
183 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
184 |
return TRUE; |
return TRUE; |
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)) { |
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)) { |
|
open.lStructSize = sizeof (OPENFILENAME); |
|
|
open.hInstance = glob_hinst; |
|
|
open.lpstrTitle = _("File Open"); |
|
|
open.lpstrFilter = (char *)_("All Files (*.*)\0*.*\0\0"); |
|
|
open.hwndOwner = dlg; |
|
|
open.lpstrFile = file; |
|
|
open.nMaxFile = sizeof (file)-1; |
|
|
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
|
|
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); |
|
226 |
/* XXX: for some files the code produces a crash! */ |
/* XXX: for some files the code produces a crash! */ |
227 |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDC_CLIPEDIT_SEND, 0), NULL); |
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*.*\0\0"); |
|
|
open.hwndOwner = dlg; |
|
|
open.lpstrFile = file; |
|
|
open.nMaxFile = sizeof (file) - 1; |
|
|
open.Flags = OFN_OVERWRITEPROMPT; |
|
|
|
|
|
if( GetSaveFileName( &open ) ) { |
|
|
if( file_exist_check( file ) == 0 ) { |
|
|
id = log_box (_("Clipboard"), MB_YESNO, |
|
|
_("\"%s\" already exists.\n" |
|
|
"Replace existing file?"), file); |
|
|
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 |
} |
} |