-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkiesconv.c
More file actions
289 lines (284 loc) · 8.64 KB
/
kiesconv.c
File metadata and controls
289 lines (284 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <windows.h>
#include <commctrl.h>
#include "SysToolX.h"
#include "resource/kiesconv.h"
#include "kiesdmod.h"
TCHAR *OpenSaveDialogEx(HWND wnd, DWORD msk, TCHAR *name, int savedlg) {
TCHAR buf[1024], *result, *s;
DWORD i, l;
result = NULL;
l = 0;
buf[0] = 0;
// only 4 bits
msk &= 0xF;
// any files always present
msk |= 1 << (IDS_MSK_ANY - 1);
// add strings
for (i = 0; msk; msk >>= 1) {
i++;
// bit is set?
if (msk & 1) {
s = LangLoadString(i);
if (s) {
lstrcpyn(&buf[l], s, 1024 - l);
l += lstrlen(s);
FreeMem(s);
}
}
// buf string too short
if (l >= 1024) { break; }
}
// replace '|' with nulls
for (s = buf; *s; s++) {
if (*s == TEXT('|')) {
*s = 0;
}
}
// get default extension
for (s = buf; *s; s++);
for (s++; *s; s++) {
// no multiextension
if (*s == ';') {
s = NULL;
break;
}
}
if (s) {
for (s--; *s; s--) {
if (*s == TEXT('.')) {
break;
}
if ((*s == TEXT('*')) || (*s == TEXT('?')) || (*s == TEXT(';'))) {
s = NULL;
break;
}
}
if (s) { s++; }
}
result = OpenSaveDialog(wnd, buf, s, name, savedlg);
return(result);
}
TCHAR *BaseName(TCHAR *name) {
TCHAR *s;
// sanity check
if (name) {
for (s = name; *s; s++) {
if ((*s == TEXT('\\')) || (*s == TEXT('/'))) {
name = &s[1];
}
}
}
return(name);
}
void ChangeFileExt(TCHAR *name, TCHAR *ext) {
TCHAR *s;
// sanity check
if (name && ext) {
// basename
name = BaseName(name);
// files with the dot in a start of a name
for (; (*name == TEXT('.')); name++);
for (s = NULL; *name; name++) {
if (*name == TEXT('.')) {
s = name;
}
}
lstrcpy(s ? s : name, ext);
}
}
BOOL CALLBACK DlgPrc(HWND wnd, UINT msg, WPARAM wparm, LPARAM lparm) {
BOOL result;
DWORD i;
TCHAR *s, *d, ext[4];
DRAWITEMSTRUCT *dis;
// v1.1 drag'n'drop files
HANDLE dh;
result = FALSE;
switch (msg) {
// v1.1 drag'n'drop files
case WM_DROPFILES:
dh = (HANDLE) wparm;
// get filename length
i = DragQueryFile(dh, 0, NULL, 0);
// not empty
if (i) {
// macros allocated i+1 characters
s = STR_ALLOC(i);
// memory allocated
if (s) {
// get filename
DragQueryFile(dh, 0, s, i + 1);
// set as file to open
SetDlgItemText(wnd, IDC_FSRC, s);
// post message
PostMessage(wnd, WM_COMMAND, MAKELONG(IDC_OPEN, BN_CLICKED), 0);
// free buffer
FreeMem(s);
}
}
break;
case WM_INITDIALOG:
// v1.1 drag'n'drop files
DragAcceptFiles(wnd, TRUE);
// add icons
SendMessage(wnd, WM_SETICON, ICON_BIG , (LPARAM) LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICN)));
SendMessage(wnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICN)));
// diable buttons
DialogEnableWindow(wnd, IDC_SAVE, FALSE);
DialogEnableWindow(wnd, IDOK, FALSE);
// set focus (because _DEF_PUSHBUTTON
// also must be Post not Send or didn't work at all
// https://blogs.msdn.microsoft.com/oldnewthing/20040802-00/?p=38283
//PostMessage(wnd, WM_NEXTDLGCTL, (WPARAM) GetDlgItem(wnd, IDC_OPEN), TRUE);
// file from command line?
// this message must be after focus selecting
s = (TCHAR *) lparm;
if (s) {
SetDlgItemText(wnd, IDC_FSRC, s);
// add message to window message queue - emulate "Open..." click
// note that lparam (handle of control) must be null - used as flag
PostMessage(wnd, WM_COMMAND, MAKELONG(IDC_OPEN, BN_CLICKED), 0);
}
// update controls
result = TRUE;
break;
case WM_COMMAND:
if (HIWORD(wparm) == BN_CLICKED) {
result = TRUE;
switch (LOWORD(wparm)) {
case IDC_OPEN:
// lparam are zero if got here from WM_INITDIALOG
s = lparm ? OpenSaveDialogEx(wnd, 1|2|4, NULL, 0) : GetWndText(GetDlgItem(wnd, IDC_FSRC));
// init edit control
if (!lparm) { SetDlgItemText(wnd, IDC_FSRC, (TCHAR *) &lparm); }
if (s) {
if (IsKiesFile(s)) {
// set dialog text
SetDlgItemText(wnd, IDC_FSRC, s);
// scroll to the end so the filename can be visible
SendDlgItemMessage(wnd, IDC_FSRC, EM_SETSEL, 0, -1);
// also generate and fill output filename
// cur extension (if any)
i = TEXT('.');
ChangeFileExt(s, (TCHAR *)&i);
SetDlgItemText(wnd, IDC_FDST, s);
// select all (carret = end)
SendDlgItemMessage(wnd, IDC_FDST, EM_SETSEL, 0, -1);
// unselect all (carret stays intact)
SendDlgItemMessage(wnd, IDC_FDST, EM_SETSEL, -1, 0);
// add new extension
ext[0] = TEXT('x');
ext[1] = TEXT('m');
ext[2] = TEXT('l');
ext[3] = 0;
SendDlgItemMessage(wnd, IDC_FDST, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) ext);
// and scroll too
SendDlgItemMessage(wnd, IDC_FDST, EM_SETSEL, 0, -1);
// enable buttons
DialogEnableWindow(wnd, IDC_SAVE, TRUE);
DialogEnableWindow(wnd, IDOK, TRUE);
// set focus to "Convert" button if goes from the commandline
if (!lparm) {
PostMessage(wnd, WM_NEXTDLGCTL, (WPARAM) GetDlgItem(wnd, IDOK), TRUE);
}
} else {
// show error message
MsgBox(wnd, MAKEINTRESOURCE(IDS_ERR_BAD), MB_ICONERROR);
}
FreeMem(s);
}
break;
case IDC_SAVE:
d = GetWndText(GetDlgItem(wnd, IDC_FDST));
s = OpenSaveDialogEx(wnd, 8, d, 1);
if (d) { FreeMem(d); }
if (s) {
// set dialog text
SetDlgItemText(wnd, IDC_FDST, s);
// scroll to the end so the filename can be visible
SendDlgItemMessage(wnd, IDC_FDST, EM_SETSEL, 0, -1);
FreeMem(s);
}
break;
case IDOK:
s = GetWndText(GetDlgItem(wnd, IDC_FSRC));
d = GetWndText(GetDlgItem(wnd, IDC_FDST));
if (s && d && *s && *d) {
i = DecodeKiesFile(s, d);
MsgBox(wnd, MAKEINTRESOURCE(i + IDS_ERR_OCS), i ? MB_ICONERROR : MB_ICONINFORMATION);
}
if (d) { FreeMem(d); }
if (s) { FreeMem(s); }
break;
// exit from program
case IDCANCEL:
EndDialog(wnd, 0);
break;
case IDC_SITE:
// get control text
s = GetWndText(GetDlgItem(wnd, LOWORD(wparm)));
if (s) {
// save original pointer
d = s;
// find link splitter
for (; *s; s++) {
// found it
if (*s == TEXT('|')) {
break;
}
}
// found?
if (*s == TEXT('|')) {
// remove space if any
for (s++; *s == TEXT(' '); s++);
}
// open link
if (*s) {
URLOpenLink(wnd, s);
}
// free memory
FreeMem(d);
}
break;
}
}
break;
// IDC_SITE
case WM_DRAWITEM:
dis = (DRAWITEMSTRUCT *) lparm;
if (dis && (dis->CtlID == IDC_SITE)) {
s = GetWndText(dis->hwndItem);
if (s) {
SetTextColor(dis->hDC, RGB(0, 0, 0xFF));
SetBkMode(dis->hDC, TRANSPARENT);
DrawText(dis->hDC, s, -1, &dis->rcItem, DT_LEFT | DT_TOP | DT_SINGLELINE);
FreeMem(s);
result = TRUE;
}
}
break;
case WM_SETCURSOR:
if (((HWND) wparm) == GetDlgItem(wnd, IDC_SITE)) {
SetCursor(LoadCursor(NULL, IDC_HAND));
SetWindowLongPtr(wnd, DWLP_MSGRESULT, TRUE);
result = TRUE;
}
break;
}
return(result);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
TCHAR **argv, *openfile;
DWORD argc;
InitCommonControls();
openfile = NULL;
argv = GetCmdLineArgs(&argc);
if (argv) {
if (argc == 2) { openfile = GetFullFilePath(argv[1]); }
FreeMem(argv);
}
DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DLG), 0, &DlgPrc, (LPARAM) openfile);
if (openfile) { FreeMem(openfile); }
ExitProcess(0);
return(0);
}