Download
jcs
/amend
/settings.c
(View History)
jcs *: Lots of little fixes and dead variable removal | Latest amendment: 110 on 2023-02-06 |
1 | /* |
2 | * Copyright (c) 2021 joshua stein <jcs@jcs.org> |
3 | * |
4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above |
6 | * copyright notice and this permission notice appear in all copies. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | */ |
16 | |
17 | #include <stdio.h> |
18 | #include <string.h> |
19 | |
20 | #include "amend.h" |
21 | #include "settings.h" |
22 | #include "tetab.h" |
23 | #include "util.h" |
24 | |
25 | struct settings settings = { 0 }; |
26 | |
27 | void |
28 | settings_load(void) |
29 | { |
30 | char *author; |
31 | |
32 | author = xGetStringAsChar(STR_AUTHOR_ID); |
33 | strlcpy(settings.author, author, sizeof(settings.author)); |
34 | xfree(&author); |
35 | if (settings.author[0] == '\0') |
36 | snprintf(settings.author, sizeof(settings.author), "unknown"); |
37 | |
38 | settings.tabwidth = (short)xGetStringAsLong(STR_TABWIDTH_ID); |
39 | if (settings.tabwidth < 1 || settings.tabwidth > 20) { |
40 | warn("Bogus tabwidth resource %d", settings.tabwidth); |
41 | settings.tabwidth = 4; |
42 | } |
43 | |
44 | TETabWidth = settings.tabwidth; |
45 | } |
46 | |
47 | void |
48 | settings_save(void) |
49 | { |
50 | Handle res; |
51 | size_t l; |
52 | char tmp[8]; |
53 | |
54 | res = xGetResource('STR ', STR_AUTHOR_ID); |
55 | HLock(res); |
56 | l = strlen(settings.author); |
57 | xSetHandleSize(res, l + 1); |
58 | memcpy(*res, settings.author, l + 1); |
59 | CtoPstr(*res); |
60 | ChangedResource(res); |
61 | ReleaseResource(res); |
62 | |
63 | res = xGetResource('STR ', STR_TABWIDTH_ID); |
64 | HLock(res); |
65 | snprintf(tmp, sizeof(tmp), "%d", settings.tabwidth); |
66 | l = strlen(tmp); |
67 | xSetHandleSize(res, l + 1); |
68 | memcpy(*res, tmp, l + 1); |
69 | CtoPstr(*res); |
70 | ChangedResource(res); |
71 | ReleaseResource(res); |
72 | |
73 | UpdateResFile(CurResFile()); |
74 | if (ResError()) |
75 | warn("UpdateResFile failed! %d", ResError()); |
76 | |
77 | TETabWidth = settings.tabwidth; |
78 | } |
79 | |
80 | void |
81 | settings_edit(void) |
82 | { |
83 | Str255 txt; |
84 | Handle ihandle; |
85 | short itype, hit; |
86 | bool done; |
87 | DialogPtr dlg; |
88 | Rect irect; |
89 | |
90 | dlg = GetNewDialog(SETTINGS_DLOG_ID, nil, (WindowPtr)-1); |
91 | if (dlg == NULL) |
92 | err(1, "Can't find settings DLOG %d", SETTINGS_DLOG_ID); |
93 | |
94 | GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect); |
95 | strlcpy((char *)txt, settings.author, sizeof(txt)); |
96 | CtoPstr(txt); |
97 | SetIText(ihandle, txt); |
98 | |
99 | GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect); |
100 | snprintf((char *)txt, sizeof(txt), "%d", settings.tabwidth); |
101 | CtoPstr(txt); |
102 | SetIText(ihandle, txt); |
103 | |
104 | ShowWindow(dlg); |
105 | |
106 | dialog_wait: |
107 | done = false; |
108 | while (!done) { |
109 | ModalDialog(nil, &hit); |
110 | switch (hit) { |
111 | case SETTINGS_SAVE_ID: |
112 | done = true; |
113 | break; |
114 | case SETTINGS_CANCEL_ID: |
115 | goto dialog_close; |
116 | case SETTINGS_AUTHOR_ID: |
117 | break; |
118 | case SETTINGS_TABWIDTH_ID: |
119 | break; |
120 | } |
121 | } |
122 | |
123 | GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect); |
124 | GetIText(ihandle, txt); |
125 | PtoCstr(txt); |
126 | if (strlen((char *)&txt) >= sizeof(settings.author)) { |
127 | warn("Author setting is too long (%ld max)", |
128 | sizeof(settings.author) - 1); |
129 | goto dialog_wait; |
130 | } |
131 | |
132 | memcpy(settings.author, txt, strlen((char *)&txt) + 1); |
133 | |
134 | GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect); |
135 | GetIText(ihandle, txt); |
136 | PtoCstr(txt); |
137 | settings.tabwidth = atoi((char *)&txt); |
138 | HUnlock(ihandle); |
139 | |
140 | settings_save(); |
141 | |
142 | dialog_close: |
143 | DisposeDialog(dlg); |
144 | } |