aboutsummaryrefslogtreecommitdiff
path: root/c/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/main.c')
-rw-r--r--c/main.c232
1 files changed, 232 insertions, 0 deletions
diff --git a/c/main.c b/c/main.c
new file mode 100644
index 0000000..abe6044
--- /dev/null
+++ b/c/main.c
@@ -0,0 +1,232 @@
+#include "main.h"
+
+#include <windows.h>
+#include <commctrl.h>
+#include <stdio.h>
+#include <string.h>
+#include <SWI-Prolog.h>
+
+#include "resource.h"
+#include "common.h"
+#include "episodelistview.h"
+#include "main.h"
+
+HFONT g_GUIFont;
+HFONT g_GUIFontBold;
+
+static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
+static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
+static void SetupFonts();
+static int Attach(void);
+
+int WINAPI
+WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
+ LPSTR lpCmdLine, INT nCmdShow)
+{
+ char *rgArgs[2];
+ HWND hWnd;
+ MSG msg;
+ INITCOMMONCONTROLSEX icc;
+ WNDCLASSEX wc;
+
+ /* Initialize Prolog. */
+
+ rgArgs[0] = "episode_browser";
+ rgArgs[1] = NULL;
+
+ if (!PL_initialise(1, rgArgs))
+ PL_halt(1);
+
+ Attach();
+
+ /* Create window. */
+
+ icc.dwSize = sizeof(icc);
+ icc.dwICC = ICC_WIN95_CLASSES;
+ InitCommonControlsEx(&icc);
+
+ SetupFonts();
+
+ wc.cbSize = sizeof(WNDCLASSEX);
+ wc.style = 0;
+ wc.lpfnWndProc = WndProc;
+ wc.cbClsExtra = 0;
+ wc.cbWndExtra = 0;
+ 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);
+
+ RegisterClassEx(&wc);
+
+ hWnd = CreateWindowEx(
+ 0,
+ TEXT("Episode Browser"),
+ TEXT("Episode Browser"),
+ WS_OVERLAPPEDWINDOW,
+ CW_USEDEFAULT, CW_USEDEFAULT, /* Position */
+ 510, 300, /* Size */
+ NULL, /* Parent window */
+ NULL, /* Menu */
+ hInstance,
+ NULL
+ );
+
+ if (!hWnd)
+ return 0;
+
+ ShowWindow(hWnd, nCmdShow);
+
+ while (GetMessage(&msg, NULL, 0, 0) > 0) {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+
+ return 0;
+}
+
+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_COMMAND:
+ switch (LOWORD(wParam)) {
+ case ID_FILE_EXIT:
+ PostMessage(hWnd, WM_CLOSE, 0, 0);
+ break;
+ case ID_FILE_REFRESH:
+ ElvUpdate();
+ break;
+ case ID_FILE_ABOUT:
+ DialogBox(
+ GetModuleHandle(NULL),
+ MAKEINTRESOURCE(IDD_ABOUT),
+ hWnd,
+ AboutDlgProc
+ );
+ break;
+ }
+ break;
+ case WM_CREATE:
+ ElvCreate(hWnd);
+ break;
+ case WM_SIZE:
+ UpdateLayout(hWnd);
+ break;
+ case WM_NOTIFY:
+ switch (((NMHDR *)lParam)->idFrom) {
+ case IDC_EPISODELISTVIEW:
+ return ElvHandleNotify(lParam);
+ }
+ 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_INITDIALOG:
+ return TRUE;
+ 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()
+{
+ int r;
+ term_t t;
+
+ t = PL_new_term_refs(2);
+ r = PL_call_predicate(NULL, PL_Q_NORMAL,
+ PL_predicate("attach", 0, "track_episodes"),
+ t);
+ if (!r) return r;
+
+ r = PL_call_predicate(NULL, PL_Q_NORMAL,
+ PL_predicate("attach", 0, "episode_data"),
+ t);
+ if (!r) return r;
+
+ 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);
+ g_GUIFont = CreateFontIndirect(&m.lfMessageFont);
+ } else {
+ g_GUIFont = GetStockObject(DEFAULT_GUI_FONT);
+ }
+
+ GetObject(g_GUIFont, sizeof(LOGFONT), &lf);
+ lf.lfWeight = FW_BOLD;
+ g_GUIFontBold = CreateFontIndirect(&lf);
+}
+
+/***/
+
+void
+UpdateLayout(HWND hWnd)
+{
+ HWND hListView;
+ int cxColumn;
+ RECT rc;
+ static int cxVScroll = 0;
+ extern int g_SelectedItem;
+
+ if (cxVScroll == 0)
+ cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
+
+ GetClientRect(hWnd, &rc);
+ hListView = GetDlgItem(hWnd, IDC_EPISODELISTVIEW);
+ MoveWindow(hListView, 0, 0,
+ rc.right, rc.bottom,
+ TRUE);
+
+ cxColumn = ListView_GetColumnWidth(hListView, 0);
+ ListView_SetColumnWidth(hListView, 1,
+ rc.right-cxColumn-cxVScroll);
+
+ ListView_EnsureVisible(hListView, g_SelectedItem, TRUE);
+}