From bc4cef92d8efbf97a9215122abc2d7247c287f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 2 Sep 2022 20:16:04 +0200 Subject: Improve Window object. --- c/data.cpp | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) (limited to 'c/data.cpp') diff --git a/c/data.cpp b/c/data.cpp index 1274321..b1c028e 100644 --- a/c/data.cpp +++ b/c/data.cpp @@ -5,12 +5,12 @@ #include #include -#include "resource.h" #include "data.h" #include "episodelistview.h" -#include "main.h" +#include "resource.h" #include "util.h" -#include "win.h" +#include "win32.h" +#include "window.h" UniqueOk RemoteParserCtxt(const wchar_t* wszUrl, const char* szUrl) { @@ -88,9 +88,10 @@ enum Signal : unsigned char ABORT = 1<<2 /* Main -> fetch: exit prematurely! */ }; -void WaitFor(void (*f)(unsigned char*)) +static Window* s_window; + +void WaitFor(Window& window, void (*f)(unsigned char*)) { - extern Window* g_window; static unsigned char sig = READY; static UINT_PTR iTimer = 0; @@ -102,13 +103,13 @@ void WaitFor(void (*f)(unsigned char*)) KillTimer(nullptr, iTimer); i = 0; sig = READY; /* Reset signals. */ - g_window->elv.Update(); /* Reset status bar. */ - EnableMenuItem(GetMenu(g_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_GRAYED); + s_window->elv.Update(); /* Reset status bar. */ + EnableMenuItem(GetMenu(s_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_GRAYED); } else { /* Animate ellipsis in status bar. */ static const wchar_t* text[] = {L".", L"..", L"...", L""}; i = (i+1)%(sizeof(text)/sizeof(*text)); - Status(text[i], 1); + s_window->Status(text[i], 1); } }; @@ -118,7 +119,7 @@ void WaitFor(void (*f)(unsigned char*)) while (!(sig & READY)) Sleep(100); sig = 0; - EnableMenuItem(GetMenu(g_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_ENABLED); + EnableMenuItem(GetMenu(s_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_ENABLED); try { f(&sig); sig |= DONE; @@ -132,7 +133,7 @@ void WaitFor(void (*f)(unsigned char*)) /* Null indicates that any active task should be cancelled. */ if (!f) { sig |= ABORT; - EnableMenuItem(GetMenu(g_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_GRAYED); + EnableMenuItem(GetMenu(s_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_GRAYED); return; } @@ -146,8 +147,9 @@ void WaitFor(void (*f)(unsigned char*)) return; } + s_window = &window; std::thread(procThread, f).detach(); - Status(L".", 1); + s_window->Status(L".", 1); Prefer(iTimer = SetTimer(nullptr, iTimer, 500, procTimer)); } @@ -180,8 +182,6 @@ void FetchData(unsigned char* sig) throw std::runtime_error("could not find remote episode information"); for (int i = 0; i < nodes->nodeNr; i++) { - extern Window* g_window; - if (*sig & ABORT) return; @@ -189,8 +189,8 @@ void FetchData(unsigned char* sig) if (xmlChildElementCount(node) != 8) throw std::runtime_error("unexpected remote data format"); - ElvDataA& e = g_window->fvElv.At(i); - DlvDataA& d = g_window->fvDlv.At(i); + ElvDataA& e = s_window->fvElv.At(i); + DlvDataA& d = s_window->fvDlv.At(i); /* Each datum is contained within a specific cell in * the row. The child element count above ensures that @@ -222,8 +222,6 @@ void FetchData(unsigned char* sig) void FetchScreenwriters(unsigned char* sig) { - extern Window* g_window; - /* Screenwriters are expensive to fetch, so we try to avoid * fetching screenwriters for episodes that already have a * screenwriter. Additionally, in the same session, we don't @@ -231,18 +229,18 @@ void FetchScreenwriters(unsigned char* sig) * already tried to fetch screenwriters. We keep track of * these states using the iLast variable. */ static int iLast = -1; - int iMax = g_window->cfg.cEp-1; + int iMax = s_window->cfg.cEp-1; /* Find the last episode that has a screenwriter. */ if (iLast == -1) - for (size_t i = 0; i < g_window->fvDlv.c; i++) - if (const DlvDataA& d = g_window->fvDlv[i]; !d.date[0]) { + for (size_t i = 0; i < s_window->fvDlv.c; i++) + if (const DlvDataA& d = s_window->fvDlv[i]; !d.date[0]) { iMax = i-1; break; } else if (d.screenwriter[0]) iLast = i; - FINALLY { Status(L""); }; + FINALLY { s_window->Status(L""); }; /* Fetch screenwriters for the rest of the episodes. */ const wchar_t prefix[] = L"https://www.detectiveconanworld.com"; @@ -257,10 +255,10 @@ void FetchScreenwriters(unsigned char* sig) wchar_t msg[48]; Swprintf(msg, L"Fetching screenwriter for episode %d...", iLast+1); - Status(msg); + s_window->Status(msg); /* Retrieve URL for episode's wiki page. */ - DlvDataA& d = g_window->fvDlv[iLast]; + DlvDataA& d = s_window->fvDlv[iLast]; Wcscpy(Buf(url)+Len(prefix), d.wiki); /* Retrieve screenwriter from HTML. */ -- cgit v1.2.3