/* * Copyright (c) 2021 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include "amend.h" #include "settings.h" #include "tetab.h" #include "util.h" struct settings settings = { 0 }; void settings_load(void) { char *author; author = xGetStringAsChar(STR_AUTHOR_ID); strlcpy(settings.author, author, sizeof(settings.author)); xfree(&author); if (settings.author[0] == '\0') snprintf(settings.author, sizeof(settings.author), "unknown"); settings.tabwidth = (short)xGetStringAsLong(STR_TABWIDTH_ID); if (settings.tabwidth < 1 || settings.tabwidth > 20) settings.tabwidth = 4; settings.max_amendments = (short)xGetStringAsLong(STR_MAX_AMENDMENTS_ID); if (settings.max_amendments < 0) settings.max_amendments = 0; TETabWidth = settings.tabwidth; } void settings_save(void) { Handle res; size_t l; char tmp[8]; res = xGetResource('STR ', STR_AUTHOR_ID); HLock(res); l = strlen(settings.author); xSetHandleSize(res, l + 1); memcpy(*res, settings.author, l + 1); CtoPstr(*res); ChangedResource(res); ReleaseResource(res); res = xGetResource('STR ', STR_TABWIDTH_ID); HLock(res); snprintf(tmp, sizeof(tmp), "%d", settings.tabwidth); l = strlen(tmp); xSetHandleSize(res, l + 1); memcpy(*res, tmp, l + 1); CtoPstr(*res); ChangedResource(res); ReleaseResource(res); res = xGetResource('STR ', STR_MAX_AMENDMENTS_ID); HLock(res); snprintf(tmp, sizeof(tmp), "%d", settings.max_amendments); l = strlen(tmp); xSetHandleSize(res, l + 1); memcpy(*res, tmp, l + 1); CtoPstr(*res); ChangedResource(res); ReleaseResource(res); UpdateResFile(CurResFile()); if (ResError()) warn("UpdateResFile failed! %d", ResError()); TETabWidth = settings.tabwidth; } void settings_edit(void) { Str255 txt; Handle ihandle; short itype, hit; bool done; DialogPtr dlg; Rect irect; dlg = GetNewDialog(SETTINGS_DLOG_ID, nil, (WindowPtr)-1); if (dlg == NULL) err(1, "Can't find settings DLOG %d", SETTINGS_DLOG_ID); GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect); strlcpy((char *)txt, settings.author, sizeof(txt)); CtoPstr(txt); SetIText(ihandle, txt); GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect); snprintf((char *)txt, sizeof(txt), "%d", settings.tabwidth); CtoPstr(txt); SetIText(ihandle, txt); GetDItem(dlg, SETTINGS_MAX_AMENDMENTS_ID, &itype, &ihandle, &irect); snprintf((char *)txt, sizeof(txt), "%d", settings.max_amendments); CtoPstr(txt); SetIText(ihandle, txt); ShowWindow(dlg); dialog_wait: done = false; while (!done) { ModalDialog(nil, &hit); switch (hit) { case SETTINGS_SAVE_ID: done = true; break; case SETTINGS_CANCEL_ID: goto dialog_close; } } GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect); GetIText(ihandle, txt); PtoCstr(txt); if (strlen((char *)&txt) >= sizeof(settings.author)) { warn("Author setting is too long (%ld max)", sizeof(settings.author) - 1); goto dialog_wait; } memcpy(settings.author, txt, strlen((char *)&txt) + 1); GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect); GetIText(ihandle, txt); PtoCstr(txt); settings.tabwidth = atoi((char *)&txt); if (settings.tabwidth < 1) settings.tabwidth = 1; HUnlock(ihandle); GetDItem(dlg, SETTINGS_MAX_AMENDMENTS_ID, &itype, &ihandle, &irect); GetIText(ihandle, txt); PtoCstr(txt); settings.max_amendments = atoi((char *)&txt); if (settings.max_amendments < 0) settings.max_amendments = 0; HUnlock(ihandle); settings_save(); dialog_close: DisposeDialog(dlg); }