From a411a7391bb879db8da398f067245a13c7fa7aee Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 25 Jul 2018 23:10:36 +0300 Subject: major code cleaning of all font-related structures --- src/mir_app/src/FontOptions.cpp | 122 ++++++++++++++++++++-------------------- src/mir_app/src/FontService.cpp | 41 +++----------- src/mir_app/src/chat_opts.cpp | 4 +- 3 files changed, 73 insertions(+), 94 deletions(-) (limited to 'src/mir_app') diff --git a/src/mir_app/src/FontOptions.cpp b/src/mir_app/src/FontOptions.cpp index 9c89f7d287..26e6708bff 100644 --- a/src/mir_app/src/FontOptions.cpp +++ b/src/mir_app/src/FontOptions.cpp @@ -126,13 +126,12 @@ void UpdateFontSettings(FontIDW *font_id, FontSettingsW *fontsettings); void UpdateColourSettings(ColourIDW *colour_id, COLORREF *colour); void UpdateEffectSettings(EffectIDW *effect_id, FONTEFFECT* effectsettings); -static void WriteLine(FILE *out, const char pszText[]) +static int CompareObj(const MBaseFontObjectW *p1, const MBaseFontObjectW *p2) { - fputs(pszText, out); - fputc('\n', out); + return strcmp(p1->dbSettingsGroup, p2->dbSettingsGroup); } -static BOOL ExportSettings(HWND hwndDlg, const wchar_t *filename, OBJLIST& flist, OBJLIST& clist, OBJLIST& elist) +static BOOL ExportSettings(HWND hwndDlg, const wchar_t *filename, OBJLIST &flist, OBJLIST &clist, OBJLIST &elist) { FILE *out = _wfopen(filename, L"w"); if (out == nullptr) { @@ -145,66 +144,69 @@ static BOOL ExportSettings(HWND hwndDlg, const wchar_t *filename, OBJLISTdbSettingsGroup); + LIST arObjects(100, CompareObj); + for (auto &it : flist) + arObjects.insert(it); + for (auto &it : clist) + arObjects.insert(it); + for (auto &it : elist) + arObjects.insert(it); + + for (auto &it : arObjects) { + mir_snprintf(buff, "\n[%s]", it->dbSettingsGroup); if (mir_strcmp(buff, header) != 0) { strncpy(header, buff, _countof(header)); - WriteLine(out, buff); + fputs(buff, out); + fputc('\n', out); } - fprintf(out, (F->flags & FIDF_APPENDNAME) ? "%sName=s%S\n" : "%s=s%S\n", F->prefix, F->value.szFace); + FontInternal *F = (FontInternal*)it; + if (flist.indexOf(F) != -1) { + fprintf(out, (F->flags & FIDF_APPENDNAME) ? "%sName=s%S\n" : "%s=s%S\n", F->setting, F->value.szFace); - int iFontSize; - if (F->flags & FIDF_SAVEACTUALHEIGHT) { - SIZE size; - LOGFONT lf; - CreateFromFontSettings(&F->value, &lf); - HFONT hFont = CreateFontIndirect(&lf); + int iFontSize; + if (F->flags & FIDF_SAVEACTUALHEIGHT) { + LOGFONT lf; + CreateFromFontSettings(&F->value, &lf); + HFONT hFont = CreateFontIndirect(&lf); - HDC hdc = GetDC(hwndDlg); - HFONT hOldFont = (HFONT)SelectObject(hdc, hFont); - GetTextExtentPoint32(hdc, L"_W", 2, &size); - ReleaseDC(hwndDlg, hdc); - SelectObject(hdc, hOldFont); - DeleteObject(hFont); + HDC hdc = GetDC(hwndDlg); + HFONT hOldFont = (HFONT)SelectObject(hdc, hFont); - iFontSize = size.cy; - } - else if (F->flags & FIDF_SAVEPOINTSIZE) { - HDC hdc = GetDC(hwndDlg); - iFontSize = (BYTE)-MulDiv(F->value.size, 72, GetDeviceCaps(hdc, LOGPIXELSY)); - ReleaseDC(hwndDlg, hdc); - } - else iFontSize = F->value.size; - fprintf(out, "%sSize=b%d\n", F->prefix, iFontSize); + SIZE size; + GetTextExtentPoint32(hdc, L"_W", 2, &size); + ReleaseDC(hwndDlg, hdc); + SelectObject(hdc, hOldFont); + DeleteObject(hFont); - fprintf(out, "%sSty=b%d\n", F->prefix, F->value.style); - fprintf(out, "%sSet=b%d\n", F->prefix, F->value.charset); - fprintf(out, "%sCol=d%d\n", F->prefix, F->value.colour); + iFontSize = size.cy; + } + else if (F->flags & FIDF_SAVEPOINTSIZE) { + HDC hdc = GetDC(hwndDlg); + iFontSize = (BYTE)-MulDiv(F->value.size, 72, GetDeviceCaps(hdc, LOGPIXELSY)); + ReleaseDC(hwndDlg, hdc); + } + else iFontSize = F->value.size; + fprintf(out, "%sSize=b%d\n", F->setting, iFontSize); - if (F->flags & FIDF_NOAS) - fprintf(out, "%sAs=w%d\n", F->prefix, 0x00FF); + fprintf(out, "%sSty=b%d\n", F->setting, F->value.style); + fprintf(out, "%sSet=b%d\n", F->setting, F->value.charset); + fprintf(out, "%sCol=d%d\n", F->setting, F->value.colour); - fprintf(out, "%sFlags=w%d\n", F->prefix, F->flags); - } + if (F->flags & FIDF_NOAS) + fprintf(out, "%sAs=w%d\n", F->setting, 0x00FF); - header[0] = 0; - for (auto &C : clist) { - mir_snprintf(buff, "\n[%s]", C->dbSettingsGroup); - if (mir_strcmp(buff, header) != 0) { - strncpy_s(header, buff, _TRUNCATE); - WriteLine(out, buff); + fprintf(out, "%sFlags=w%d\n", F->setting, F->flags); + continue; } - fprintf(out, "%s=d%d\n", C->setting, (DWORD)C->value); - } - header[0] = 0; - for (auto &E : elist) { - mir_snprintf(buff, "\n[%s]", E->dbSettingsGroup); - if (mir_strcmp(buff, header) != 0) { - strncpy_s(header, buff, _TRUNCATE); - WriteLine(out, buff); + ColourInternal *C = (ColourInternal*)it; + if (clist.indexOf(C) != -1) { + fprintf(out, "%s=d%d\n", C->setting, (DWORD)C->value); + continue; } + + EffectInternal *E = (EffectInternal*)it; fprintf(out, "%sEffect=b%d\n", E->setting, E->value.effectIndex); fprintf(out, "%sEffectCol1=d%d\n", E->setting, E->value.baseColour); fprintf(out, "%sEffectCol2=d%d\n", E->setting, E->value.secondaryColour); @@ -496,9 +498,9 @@ static void sttSaveFontData(HWND hwndDlg, FontInternal &F) char str[128]; if (F.flags & FIDF_APPENDNAME) - mir_snprintf(str, "%sName", F.prefix); + mir_snprintf(str, "%sName", F.setting); else - strncpy_s(str, F.prefix, _TRUNCATE); + strncpy_s(str, F.setting, _TRUNCATE); if (db_set_ws(0, F.dbSettingsGroup, str, F.value.szFace)) { char buff[1024]; @@ -506,7 +508,7 @@ static void sttSaveFontData(HWND hwndDlg, FontInternal &F) db_set_s(0, F.dbSettingsGroup, str, buff); } - mir_snprintf(str, "%sSize", F.prefix); + mir_snprintf(str, "%sSize", F.setting); if (F.flags & FIDF_SAVEACTUALHEIGHT) { SIZE size; CreateFromFontSettings(&F.value, &lf); @@ -527,17 +529,17 @@ static void sttSaveFontData(HWND hwndDlg, FontInternal &F) } else db_set_b(0, F.dbSettingsGroup, str, F.value.size); - mir_snprintf(str, "%sSty", F.prefix); + mir_snprintf(str, "%sSty", F.setting); db_set_b(0, F.dbSettingsGroup, str, F.value.style); - mir_snprintf(str, "%sSet", F.prefix); + mir_snprintf(str, "%sSet", F.setting); db_set_b(0, F.dbSettingsGroup, str, F.value.charset); - mir_snprintf(str, "%sCol", F.prefix); + mir_snprintf(str, "%sCol", F.setting); db_set_dw(0, F.dbSettingsGroup, str, F.value.colour); if (F.flags & FIDF_NOAS) { - mir_snprintf(str, "%sAs", F.prefix); + mir_snprintf(str, "%sAs", F.setting); db_set_w(0, F.dbSettingsGroup, str, (WORD)0x00FF); } - mir_snprintf(str, "%sFlags", F.prefix); + mir_snprintf(str, "%sFlags", F.setting); db_set_w(0, F.dbSettingsGroup, str, (WORD)F.flags); } @@ -1196,10 +1198,10 @@ int OptInit(WPARAM wParam, LPARAM) ///////////////////////////////////////////////////////////////////////////////////////// -static FontInternal* sttFindFont(OBJLIST &fonts, char *module, char *prefix) +static FontInternal* sttFindFont(OBJLIST &fonts, char *module, char *setting) { for (auto &F : fonts) - if (!mir_strcmp(F->dbSettingsGroup, module) && !mir_strcmp(F->prefix, prefix)) + if (!mir_strcmp(F->dbSettingsGroup, module) && !mir_strcmp(F->setting, setting)) return F; return nullptr; diff --git a/src/mir_app/src/FontService.cpp b/src/mir_app/src/FontService.cpp index 333c1a7176..dce937b2be 100644 --- a/src/mir_app/src/FontService.cpp +++ b/src/mir_app/src/FontService.cpp @@ -49,13 +49,9 @@ void ConvertFontSettings(FontSettings *fs, FontSettingsW *fsw) bool ConvertFontID(FontID *fid, FontIDW *fidw) { - if (fid->cbSize != sizeof(FontID)) - return false; - memset(fidw, 0, sizeof(FontIDW)); - fidw->cbSize = sizeof(FontIDW); strncpy_s(fidw->dbSettingsGroup, fid->dbSettingsGroup, _TRUNCATE); - strncpy_s(fidw->prefix, fid->prefix, _TRUNCATE); + strncpy_s(fidw->setting, fid->setting, _TRUNCATE); fidw->flags = fid->flags; fidw->order = fid->order; ConvertFontSettings(&fid->deffontsettings, &fidw->deffontsettings); @@ -70,11 +66,6 @@ bool ConvertFontID(FontID *fid, FontIDW *fidw) bool ConvertColourID(ColourID *cid, ColourIDW *cidw) { - if (cid->cbSize != sizeof(ColourID)) - return false; - - cidw->cbSize = sizeof(ColourIDW); - strncpy_s(cidw->dbSettingsGroup, cid->dbSettingsGroup, _TRUNCATE); strncpy_s(cidw->setting, cid->setting, _TRUNCATE); cidw->flags = cid->flags; @@ -88,11 +79,6 @@ bool ConvertColourID(ColourID *cid, ColourIDW *cidw) bool ConvertEffectID(EffectID *eid, EffectIDW *eidw) { - if (eid->cbSize != sizeof(EffectID)) - return false; - - eidw->cbSize = sizeof(EffectIDW); - strncpy_s(eidw->dbSettingsGroup, eid->dbSettingsGroup, _TRUNCATE); strncpy_s(eidw->setting, eid->setting, _TRUNCATE); eidw->flags = eid->flags; @@ -228,7 +214,7 @@ void UpdateFontSettings(FontIDW *font_id, FontSettingsW *fontsettings) { LOGFONT lf; COLORREF colour; - if (GetFontSettingFromDB(font_id->dbSettingsGroup, font_id->prefix, &lf, &colour, font_id->flags) && (font_id->flags & FIDF_DEFAULTVALID)) { + if (GetFontSettingFromDB(font_id->dbSettingsGroup, font_id->setting, &lf, &colour, font_id->flags) && (font_id->flags & FIDF_DEFAULTVALID)) { CreateFromFontSettings(&font_id->deffontsettings, &lf); colour = font_id->deffontsettings.colour; } @@ -247,20 +233,17 @@ void UpdateFontSettings(FontIDW *font_id, FontSettingsW *fontsettings) static int sttRegisterFontWorker(FontIDW *font_id, HPLUGIN pPlugin) { - if (font_id->cbSize != sizeof(FontIDW)) - return -1; - for (auto &F : font_id_list) if (!mir_wstrcmp(F->group, font_id->group) && !mir_wstrcmp(F->name, font_id->name) && !(F->flags & FIDF_ALLOWREREGISTER)) return 1; char idstr[256]; - mir_snprintf(idstr, "%sFlags", font_id->prefix); + mir_snprintf(idstr, "%sFlags", font_id->setting); db_set_dw(0, font_id->dbSettingsGroup, idstr, font_id->flags); FontInternal* newItem = new FontInternal; memset(newItem, 0, sizeof(FontInternal)); - memcpy(newItem, font_id, font_id->cbSize); + memcpy(newItem, font_id, sizeof(FontIDW)); newItem->pPlugin = pPlugin; if (!mir_wstrcmp(newItem->deffontsettings.szFace, L"MS Shell Dlg")) { @@ -299,7 +282,7 @@ static COLORREF sttGetFontWorker(const wchar_t *wszGroup, const wchar_t *wszName for (auto &F : font_id_list) { if (!wcsncmp(F->name, wszName, _countof(F->name)) && !wcsncmp(F->group, wszGroup, _countof(F->group))) { - if (GetFontSettingFromDB(F->dbSettingsGroup, F->prefix, lf, &colour, F->flags) && (F->flags & FIDF_DEFAULTVALID)) { + if (GetFontSettingFromDB(F->dbSettingsGroup, F->setting, lf, &colour, F->flags) && (F->flags & FIDF_DEFAULTVALID)) { CreateFromFontSettings(&F->deffontsettings, lf); colour = F->deffontsettings.colour; } @@ -354,9 +337,6 @@ void UpdateColourSettings(ColourIDW *colour_id, COLORREF *colour) static INT_PTR sttRegisterColourWorker(ColourIDW *colour_id, HPLUGIN pPlugin) { - if (colour_id->cbSize != sizeof(ColourIDW)) - return -1; - for (auto &C : colour_id_list) if (!mir_wstrcmp(C->group, colour_id->group) && !mir_wstrcmp(C->name, colour_id->name)) return 1; @@ -446,9 +426,6 @@ void UpdateEffectSettings(EffectIDW *effect_id, FONTEFFECT *effectsettings) static int sttRegisterEffectWorker(EffectIDW *effect_id, HPLUGIN pPlugin) { - if (effect_id->cbSize != sizeof(EffectIDW)) - return -1; - for (auto &E : effect_id_list) if (!mir_wstrcmp(E->group, effect_id->group) && !mir_wstrcmp(E->name, effect_id->name)) return 1; @@ -542,23 +519,23 @@ int LoadFontserviceModule(void) hColourReloadEvent = CreateHookableEvent(ME_COLOUR_RELOAD); // create generic fonts - FontIDW fontid = { sizeof(fontid) }; + FontIDW fontid = {}; strncpy(fontid.dbSettingsGroup, "Fonts", sizeof(fontid.dbSettingsGroup)); wcsncpy_s(fontid.group, LPGENW("General"), _TRUNCATE); wcsncpy_s(fontid.name, LPGENW("Headers"), _TRUNCATE); fontid.flags = FIDF_APPENDNAME | FIDF_NOAS | FIDF_SAVEPOINTSIZE | FIDF_ALLOWEFFECTS | FIDF_CLASSHEADER; - strncpy(fontid.prefix, "Header", _countof(fontid.prefix)); + strncpy_s(fontid.setting, "Header", _TRUNCATE); g_plugin.addFont(&fontid); wcsncpy_s(fontid.name, LPGENW("Generic text"), _TRUNCATE); fontid.flags = FIDF_APPENDNAME | FIDF_NOAS | FIDF_SAVEPOINTSIZE | FIDF_ALLOWEFFECTS | FIDF_CLASSGENERAL; - strncpy(fontid.prefix, "Generic", _countof(fontid.prefix)); + strncpy_s(fontid.setting, "Generic", _TRUNCATE); g_plugin.addFont(&fontid); wcsncpy_s(fontid.name, LPGENW("Small text"), _TRUNCATE); fontid.flags = FIDF_APPENDNAME | FIDF_NOAS | FIDF_SAVEPOINTSIZE | FIDF_ALLOWEFFECTS | FIDF_CLASSSMALL; - strncpy(fontid.prefix, "Small", _countof(fontid.prefix)); + strncpy_s(fontid.setting, "Small", _TRUNCATE); g_plugin.addFont(&fontid); // do last for silly dyna plugin diff --git a/src/mir_app/src/chat_opts.cpp b/src/mir_app/src/chat_opts.cpp index 6850899677..30145b13b3 100644 --- a/src/mir_app/src/chat_opts.cpp +++ b/src/mir_app/src/chat_opts.cpp @@ -135,7 +135,7 @@ void RegisterFonts(void) SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lfDefault), &lfDefault, FALSE); - FontIDW fontid = { sizeof(fontid) }; + FontIDW fontid = {}; fontid.flags = FIDF_ALLOWREREGISTER | FIDF_DEFAULTVALID | FIDF_NEEDRESTART; wcsncpy_s(fontid.backgroundGroup, g_szFontGroup, _TRUNCATE); wcsncpy_s(fontid.group, g_szFontGroup, _TRUNCATE); @@ -145,7 +145,7 @@ void RegisterFonts(void) strncpy_s(fontid.dbSettingsGroup, CHATFONT_MODULE, _TRUNCATE); wcsncpy_s(fontid.name, FO.szDescr, _TRUNCATE); - mir_snprintf(fontid.prefix, "Font%d", index); + mir_snprintf(fontid.setting, "Font%d", index); fontid.order = index; switch (i) { -- cgit v1.2.3