diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-17 14:43:52 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-17 14:43:52 +0200 |
commit | c60fdf109150312c1ba5e749aff6196f098b2752 (patch) | |
tree | 8452b421ff2b4f2e86d4b11b6b906c6fbbe4a656 | |
parent | 1d041ad5b123e4ecbe69dfa621349f6be9b933d6 (diff) | |
download | EpisodeBrowser-c60fdf109150312c1ba5e749aff6196f098b2752.tar.gz |
Add FileView::Initialized constructor.
-rw-r--r-- | c/data.h | 18 | ||||
-rw-r--r-- | c/datalistview.cpp | 4 | ||||
-rw-r--r-- | c/episodelistview.cpp | 23 | ||||
-rw-r--r-- | c/layout.cpp | 8 | ||||
-rw-r--r-- | c/layout.h | 2 | ||||
-rw-r--r-- | c/main.cpp | 34 |
6 files changed, 48 insertions, 41 deletions
@@ -70,7 +70,23 @@ constexpr inline bool HasVersion<T, std::void_t<typename T::version>> = true; template <typename T> struct FileView { - FileView(const wchar_t* filename, size_t c_) : c(c_) + static FileView Initialized(const wchar_t* filename, size_t c, size_t cInit = 0) + { + bool bExisting = GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES; + FileView fv{filename, c}; + + /* If file didn't exist, initialize it with defaults. */ + if (!bExisting) { + T t; + cInit = cInit? cInit: c; + for (size_t i = 0; i < cInit; i++) + memcpy(fv+i, &t, sizeof(T)); + } + + return fv; + } + + FileView(const wchar_t* filename, size_t c) : c(c) { hf = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); diff --git a/c/datalistview.cpp b/c/datalistview.cpp index 9b3de7a..6d3d79c 100644 --- a/c/datalistview.cpp +++ b/c/datalistview.cpp @@ -29,8 +29,8 @@ DataListView::DataListView(const HWND hWndParent) lvc.cx = 500; ListView_InsertColumn(hWnd, DLVSIVALUE, &lvc); - extern FileView<CfgA> g_fvCfg; - m_height = g_fvCfg.At(0).heightDlv; + extern CfgA& g_cfg; + m_height = g_cfg.heightDlv; } int DataListView::Height() diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index f004f8a..db53be3 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -12,6 +12,8 @@ #include "util.h" #include "win.h" +extern CfgA& g_cfg; + EpisodeListView::EpisodeListView(const HWND hWndParent) : ListView(hWndParent, reinterpret_cast<HMENU>(IDC_EPISODELISTVIEW), 0) { @@ -34,8 +36,7 @@ EpisodeListView::EpisodeListView(const HWND hWndParent) ListView_InsertColumn(hWnd, ELVSIRATING, &lvc); /* Get saved sort-by-column setting. */ - extern FileView<CfgA> g_fvCfg; - m_iSortCol = g_fvCfg->iSortCol; + m_iSortCol = g_cfg.iSortCol; } void EpisodeListView::EnsureFocusVisible() @@ -136,8 +137,7 @@ LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) /* The sign of m_iSortCol decides the sort order. */ m_iSortCol = abs(m_iSortCol) == iCol? -m_iSortCol: iCol; - extern FileView<CfgA> g_fvCfg; - g_fvCfg->iSortCol = m_iSortCol; + g_cfg.iSortCol = m_iSortCol; Sort(); ShowFocus(); return 0; @@ -231,10 +231,9 @@ void EpisodeListView::RestoreFocus() int i, iEpisode, iItem; LVFINDINFO lvfi; extern DataListView* const g_dlv; - extern FileView<CfgA> g_fvCfg; iItem = 0; - iEpisode = g_fvCfg->iFocus; + iEpisode = g_cfg.iFocus; lvfi.flags = LVFI_PARAM; lvfi.lParam = iEpisode; @@ -260,10 +259,9 @@ s: g_dlv->ShowEpisode(iEpisode); void EpisodeListView::SaveFocus() { - extern FileView<CfgA> g_fvCfg; LVITEM lvi = {LVIF_PARAM, -1}; if (FindNextItem(&lvi, LVNI_FOCUSED)) - g_fvCfg->iFocus = lvi.lParam; + g_cfg.iFocus = lvi.lParam; } void EpisodeListView::SetTop(const int iItem) @@ -405,15 +403,14 @@ void EpisodeListView::Update() if (!e.siEp[0]) continue; - extern FileView<CfgA> g_fvCfg; - //if (g_fvCfg->limitScreenwriter[0] && !Pl("episode_data","episode_datum",iEp, - // "Screenwriter",g_fvCfg->limitScreenwriter)) + //if (g_cfg.limitScreenwriter[0] && !Pl("episode_data","episode_datum",iEp, + // "Screenwriter",g_cfg.limitScreenwriter)) // continue; - if (!g_fvCfg->bViewWatched && e.bWatched) + if (!g_cfg.bViewWatched && e.bWatched) continue; - if (!g_fvCfg->bViewTVOriginal && e.bTVOriginal) + if (!g_cfg.bViewTVOriginal && e.bTVOriginal) continue; /* Insert item. */ diff --git a/c/layout.cpp b/c/layout.cpp index 653b675..5b3ff00 100644 --- a/c/layout.cpp +++ b/c/layout.cpp @@ -68,14 +68,14 @@ void DlvDragger::Drag(const int, const int y) const void DlvDragger::Reset() const { + extern CfgA& g_cfg; g_dlv->SetHeight(0); - extern FileView<CfgA> g_fvCfg; - g_fvCfg->heightDlv = 0; + g_cfg.heightDlv = 0; UpdateLayout(); } void DlvDragger::Done() const { - extern FileView<CfgA> g_fvCfg; - g_fvCfg->heightDlv = g_dlv->Height(); + extern CfgA& g_cfg; + g_cfg.heightDlv = g_dlv->Height(); } @@ -3,8 +3,6 @@ #include <windows.h> -#include "datalistview.h" -#include "pl.h" #include "win.h" /* Given main window's width and height, set appropriate positions and @@ -52,7 +52,8 @@ EpisodeListView* g_elv; DlvDragger g_dragDlv; /* File views. */ -FileView<CfgA> g_fvCfg{L"cfg.dat", 1}; +FileView<CfgA> g_fvCfg = FileView<CfgA>::Initialized(L"cfg.dat", 1); +CfgA& g_cfg = g_fvCfg.At(0); /* Optional Windows functions. */ BOOL (*IsThemeActive)(); @@ -229,12 +230,9 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, SetFocus(g_elv->hWnd); /* Set menu item checkmarks according to saved settings. */ - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, - g_fvCfg->bViewWatched? MF_CHECKED: MF_UNCHECKED); - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, - g_fvCfg->bViewTVOriginal? MF_CHECKED: MF_UNCHECKED); - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, - g_fvCfg->limitScreenwriter[0]? MF_UNCHECKED: MF_CHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, g_cfg.bViewWatched? MF_CHECKED: MF_UNCHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, g_cfg.bViewTVOriginal? MF_CHECKED: MF_UNCHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, g_cfg.limitScreenwriter[0]? MF_UNCHECKED: MF_CHECKED); return 0; case WM_CLOSE: @@ -326,13 +324,13 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, /*IDM_FILE_FETCH_DATA*/L"Fetch episode data from the web (may take a few seconds).", /*IDM_FILE_FETCH_SCREENWRITERS*/L"Fetch screenwriters from the web (may take a minute).", /*IDM_FILE_ABOUT*/L"Show information about Episode Browser.", - /*IDM_VIEW_WATCHED*/(g_fvCfg->bViewWatched? + /*IDM_VIEW_WATCHED*/(g_cfg.bViewWatched? L"Click to hide watched episodes.": L"Click to show watched episodes."), - /*IDM_VIEW_TV_ORIGINAL*/(g_fvCfg->bViewTVOriginal? + /*IDM_VIEW_TV_ORIGINAL*/(g_cfg.bViewTVOriginal? L"Click to hide TV original episodes.": L"Click to show TV original episodes."), - /*IDM_VIEW_OTHERS*/(g_fvCfg->limitScreenwriter? + /*IDM_VIEW_OTHERS*/(g_cfg.limitScreenwriter? L"Click to hide episodes by other screenwriters.": L"Click to show episodes by other screenwriters.") }; @@ -415,9 +413,8 @@ void HandleMainMenu(const HWND hWnd, const WORD command) break; case IDM_VIEW_WATCHED: - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, - g_fvCfg->bViewWatched? MF_UNCHECKED: MF_CHECKED); - g_fvCfg->bViewWatched = !g_fvCfg->bViewWatched; + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, g_cfg.bViewWatched? MF_UNCHECKED: MF_CHECKED); + g_cfg.bViewWatched = !g_cfg.bViewWatched; g_elv->Update(); g_elv->EnsureFocusVisible(); /* TODO: Remember last valid focus. In case of @@ -425,24 +422,23 @@ void HandleMainMenu(const HWND hWnd, const WORD command) break; case IDM_VIEW_TV_ORIGINAL: - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, - g_fvCfg->bViewTVOriginal? MF_UNCHECKED: MF_CHECKED); - g_fvCfg->bViewTVOriginal = !g_fvCfg->bViewTVOriginal; + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, g_cfg.bViewTVOriginal? MF_UNCHECKED: MF_CHECKED); + g_cfg.bViewTVOriginal = !g_cfg.bViewTVOriginal; g_elv->Update(); g_elv->EnsureFocusVisible(); break; case IDM_VIEW_OTHERS: - if (g_fvCfg->limitScreenwriter[0]) { /* Show episodes by all screenwriters. */ + if (g_cfg.limitScreenwriter[0]) { /* Show episodes by all screenwriters. */ CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, MF_CHECKED); - g_fvCfg->limitScreenwriter[0] = 0; + g_cfg.limitScreenwriter[0] = 0; } else { /* Hide episodes by other screenwriters than current. */ Mark m; WcharPtr s; LVITEM lvi = {LVIF_PARAM, -1}; if (g_elv->FindNextItem(&lvi, LVNI_FOCUSED) && Pl("episode_data","episode_datum",lvi.lParam,"Screenwriter",&s)) { - Wcscpy(g_fvCfg->limitScreenwriter, s); + Wcscpy(g_cfg.limitScreenwriter, s); CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, MF_UNCHECKED); } } |