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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
#include <windows.h>
#include <commctrl.h>
#include <uxtheme.h>
#include <SWI-Prolog.h>
#include "resource.h"
#include "defs.h"
HFONT HfNormal;
HFONT HfBold;
HMENU HPopupMenu;
HWND HFocus;
HWND HWnd;
int IDPI = -1;
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
static int Attach(void);
static void SetupFonts(void);
static void UpdateTheme(void);
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, INT nCmdShow)
{
char *argv[2];
HWND hWnd;
LPTSTR tszErr;
MSG msg;
INITCOMMONCONTROLSEX icc;
WNDCLASSEX wc;
/* Initialize Prolog. */
argv[0] = "EpisodeBrowser";
argv[1] = NULL;
tszErr = TEXT("Could not initialize Prolog.");
if (!PL_initialise(1, argv)) goto f;
tszErr = TEXT("Could not attach databases.");
if (!Attach()) goto f;
/* Initialize common controls, load menu and register window class. */
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_WIN95_CLASSES;
tszErr = TEXT("Could not initialize common controls.");
if (!InitCommonControlsEx(&icc)) goto f;
HPopupMenu = LoadMenu(NULL, MAKEINTRESOURCE(IDR_POPUPMENU));
HPopupMenu = GetSubMenu(HPopupMenu, 0);
memset(&wc, 0, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.lpszClassName = TEXT("Episode Browser");
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
tszErr = TEXT("Could not register window class.");
if (!RegisterClassEx(&wc)) goto f;
/* Create window. */
hWnd = CreateWindowEx(
0,
TEXT("Episode Browser"),
TEXT("Episode Browser"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0,
NULL, NULL, hInstance, NULL
);
tszErr = TEXT("Could not create main window.");
if (!hWnd) goto f;
SetWindowPos(hWnd, NULL, -1, -1, Dpi(510), Dpi(400), SWP_NOMOVE);
SetupFonts();
ShowWindow(hWnd, nCmdShow);
while (GetMessage(&msg, NULL, 0, 0) > 0) {
if (IsDialogMessage(hWnd, &msg)) continue;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
PL_halt(0);
return 0;
f: MessageBox(NULL, tszErr, TEXT("Error"), MB_ICONERROR);
PL_halt(1);
return 1;
}
LRESULT CALLBACK
WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
UpdateLayout();
break;
case WM_GETMINMAXINFO:
{
LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
extern HWND HDlv;
lpMMI->ptMinTrackSize.y = LvHeight(HDlv)+80;
break;
}
case WM_CREATE:
{
extern HWND HElv;
HWnd = hWnd;
DlvCreate();
ElvCreate();
UpdateTheme();
SetFocus(HElv);
break;
}
case WM_THEMECHANGED:
UpdateTheme();
break;
case 0x02E0: /* WM_DPICHANGED */
{
LPRECT lpr;
lpr = (LPRECT)lParam;
IDPI = HIWORD(wParam);
SetWindowPos(hWnd, NULL,
lpr->left, lpr->top,
lpr->right-lpr->left,
lpr->bottom-lpr->top,
SWP_NOZORDER | SWP_NOACTIVATE);
UpdateLayout();
break;
}
case WM_ACTIVATE:
switch (wParam) {
case WA_INACTIVE:
HFocus = GetFocus();
break;
case WA_ACTIVE:
case WA_CLICKACTIVE:
{
term_t t;
SetFocus(HFocus);
t = T(0);
P("track_episodes","update_tracked_episodes",0,t);
ElvRedraw();
}
}
break;
case WM_NOTIFY:
switch (((LPNMHDR)lParam)->idFrom) {
case IDC_EPISODELISTVIEW:
return ElvHandleNotify(lParam);
}
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_FILE_EXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
case ID_FILE_REFRESH:
ElvUpdate();
break;
case ID_FILE_RESET:
{
extern HWND HElv;
ListView_SetItemState(HElv, -1, LVIF_STATE, LVIS_SELECTED);
ElvSelectRecent();
break;
}
case ID_FILE_ABOUT:
DialogBox(
GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_ABOUT),
hWnd,
AboutDlgProc
);
break;
case ID_WATCH:
case ID_TOGGLE:
case ID_FORGET:
case ID_LOOKUP:
{
LVITEM lvi;
term_t t;
extern HWND HElv;
/* Look through selected items, applying the
* selected command to each one. */
lvi.mask = LVIF_PARAM;
lvi.iItem = -1;
t = T(1);
while ((lvi.iItem = ListView_GetNextItem(
HElv, lvi.iItem, LVNI_SELECTED)) != -1) {
if (!ListView_GetItem(HElv, &lvi)) goto b;
PI(t,lvi.lParam) goto b;
switch (LOWORD(wParam)) {
case ID_WATCH:
P("local_episode","open_episode",1,t);
break;
case ID_TOGGLE:
P("track_episodes","toggle_episode",1,t);
ElvRedraw();
break;
case ID_FORGET:
P("track_episodes","forget_episode",1,t);
P("track_episodes","update_tracked_episodes",0,t);
ElvRedraw();
break;
case ID_LOOKUP:
P("episode_data","retract_episode",1,t);
ElvUpdateName(&lvi);
ElvRedraw();
DlvShowEpisode(lvi.lParam);
break;
}
}
b: break;
}
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
INT_PTR CALLBACK
AboutDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_CLOSE:
EndDialog(hWnd, IDOK);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
EndDialog(hWnd, IDOK);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
/***/
/* Attach persistent databases. */
int
Attach()
{
term_t t;
t = T(0);
P("track_episodes","attach",0,t) return 0;
P("episode_data","attach",0,t) return 0;
return 1;
}
void
SetupFonts()
{
HMODULE hModule;
LOGFONT lf;
hModule = LoadLibrary(TEXT("User32.dll"));
if (hModule && GetProcAddress(hModule, "SystemParametersInfoW")) {
NONCLIENTMETRICS m;
m.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
sizeof(NONCLIENTMETRICS), &m, 0);
HfNormal = CreateFontIndirect(&m.lfMessageFont);
FreeLibrary(hModule);
} else
HfNormal = GetStockObject(DEFAULT_GUI_FONT);
GetObject(HfNormal, sizeof(LOGFONT), &lf);
lf.lfWeight = FW_BOLD;
HfBold = CreateFontIndirect(&lf);
}
/***/
void
UpdateLayout()
{
HWND hElv, hDlv;
int cxColumn, cyDlv;
RECT rc;
static int cxVScroll = 0;
GetClientRect(HWnd, &rc);
if (!cxVScroll)
cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
/* Resize data list view. */
hDlv = GetDlgItem(HWnd, IDC_DATALISTVIEW);
cyDlv = rc.bottom - LvHeight(hDlv);
MoveWindow(hDlv, 0, cyDlv, rc.right, rc.bottom, TRUE);
cxColumn = ListView_GetColumnWidth(hDlv, 0);
ListView_SetColumnWidth(hDlv, 1, rc.right-cxColumn-cxVScroll-4);
/* Resize episode list view. */
hElv = GetDlgItem(HWnd, IDC_EPISODELISTVIEW);
MoveWindow(hElv, 0, 0, rc.right, cyDlv+1, TRUE);
cxColumn = ListView_GetColumnWidth(hElv, 0);
ListView_SetColumnWidth(hElv, 1, rc.right-cxColumn-cxVScroll-4);
}
/* Try to style application according to current Windows theme. */
void
UpdateTheme()
{
int bThemeActive;
extern HWND HElv, HDlv;
static int bThemes = -1;
switch (bThemes) {
case 0:
return;
case -1:
{
HMODULE hModule;
hModule = LoadLibrary(TEXT("uxtheme.dll"));
bThemes = hModule && GetProcAddress(hModule,"SetWindowTheme");
}
}
bThemeActive = IsThemeActive();
LvSetTheme(HElv, bThemeActive);
LvSetTheme(HDlv, bThemeActive);
}
|