AmendHub

Download

jcs

/

amend

/

main.c

 

(View History)

jcs   browser+main: Hopefully fix a bug that was crashing after committing Latest amendment: 113 on 2023-04-17

1 /*
2 * Copyright (c) 2020-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 "browser.h"
22 #include "committer.h"
23 #include "focusable.h"
24 #include "repo.h"
25 #include "settings.h"
26 #include "util.h"
27
28 MenuHandle apple_menu, file_menu, edit_menu, repo_menu, amendment_menu;
29 bool quitting = false;
30
31 bool handle_menu(long menu_id);
32
33 int
34 main(void)
35 {
36 Handle mbar;
37 EventRecord event;
38 WindowPtr event_win;
39 GrafPtr old_port;
40 AppFile finder_file;
41 struct repo *repo;
42 struct focusable *focusable;
43 short event_in, n, finder_action, finder_count;
44 bool did_initial_open = false;
45 char key;
46
47 InitGraf(&thePort);
48 InitFonts();
49 FlushEvents(everyEvent, 0);
50 InitWindows();
51 InitMenus();
52 TEInit();
53 InitDialogs(0);
54 InitCursor();
55 MaxApplZone();
56
57 util_init();
58 settings_load();
59
60 mbar = GetNewMBar(MBAR_ID);
61 SetMenuBar(mbar);
62 apple_menu = GetMHandle(APPLE_MENU_ID);
63 AddResMenu(apple_menu, 'DRVR');
64 file_menu = GetMHandle(FILE_MENU_ID);
65 edit_menu = GetMHandle(EDIT_MENU_ID);
66 repo_menu = GetMHandle(REPO_MENU_ID);
67 amendment_menu = GetMHandle(AMENDMENT_MENU_ID);
68 menu_defaults();
69 DrawMenuBar();
70
71 /* see if we were started by double-clicking a .repo file */
72 CountAppFiles(&finder_action, &finder_count);
73
74 while (!quitting) {
75 WaitNextEvent(everyEvent, &event, 5L, 0L);
76
77 switch (event.what) {
78 case nullEvent:
79 for (n = 0; n < nfocusables; n++) {
80 if (focusables[n]->idle)
81 focusables[n]->idle(focusables[n], &event);
82 }
83 if (!did_initial_open) {
84 if (finder_count) {
85 GetAppFiles(1, &finder_file);
86 ClrAppFiles(1);
87 finder_count = 0;
88 repo = repo_open(&finder_file);
89 } else
90 repo = repo_open(NULL);
91
92 if (repo)
93 browser_init(repo);
94
95 did_initial_open = true;
96 }
97 break;
98 case keyDown:
99 case autoKey:
100 key = event.message & charCodeMask;
101 if ((event.modifiers & cmdKey) != 0) {
102 if (handle_menu(MenuKey(key)))
103 break;
104 }
105 if ((focusable = focusable_focused()) &&
106 focusable->key_down)
107 focusable->key_down(focusable, &event);
108 break;
109 case mouseDown:
110 event_in = FindWindow(event.where, &event_win);
111
112 switch (event_in) {
113 case inMenuBar:
114 handle_menu(MenuSelect(event.where));
115 break;
116 case inSysWindow:
117 SystemClick(&event, event_win);
118 break;
119 case inDrag:
120 if ((focusable = focusable_find(event_win)) != NULL) {
121 if (!focusable_show(focusable))
122 break;
123
124 DragWindow(event_win, event.where, &screenBits.bounds);
125 }
126 break;
127 case inGoAway:
128 if (TrackGoAway(event_win, event.where)) {
129 if ((focusable = focusable_find(event_win)) != NULL)
130 focusable_close(focusable);
131 }
132 break;
133 case inContent:
134 if ((focusable = focusable_find(event_win)) != NULL) {
135 if (!focusable_show(focusable))
136 break;
137 if (focusable->mouse_down)
138 focusable->mouse_down(focusable, &event);
139 }
140 break;
141 }
142 break;
143 case updateEvt:
144 event_win = (WindowPtr)event.message;
145 focusable = focusable_find(event_win);
146 if (focusable && focusable->update) {
147 GetPort(&old_port);
148 SetPort(event_win);
149 BeginUpdate(event_win);
150 focusable->update(focusable, &event);
151 EndUpdate(event_win);
152 SetPort(old_port);
153 }
154 break;
155 case activateEvt:
156 break;
157 case app4Evt:
158 if (HiWord(event.message) & (1 << 8)) {
159 /* multifinder suspend/resume */
160 switch (event.message & (1 << 0)) {
161 case 0:
162 /* suspend */
163 for (n = 0; n < nfocusables; n++) {
164 if (focusables[n]->suspend)
165 focusables[n]->suspend(focusables[n], &event);
166 }
167 break;
168 case 1:
169 /* resume */
170 for (n = 0; n < nfocusables; n++) {
171 if (focusables[n]->resume)
172 focusables[n]->resume(focusables[n], &event);
173 }
174 break;
175 }
176 }
177 break;
178 }
179 }
180
181 return 0;
182 }
183
184 bool
185 handle_menu(long menu_id)
186 {
187 struct focusable *focused;
188 short menu, item;
189 bool ret = true;
190
191 menu = HiWord(menu_id);
192 item = LoWord(menu_id);
193
194 if (menu && item && (focused = focusable_focused()) &&
195 focused->menu && focused->menu(focused, menu, item))
196 goto handled;
197
198 switch (menu) {
199 case APPLE_MENU_ID:
200 switch (item) {
201 case APPLE_MENU_ABOUT_ID:
202 about(PROGRAM_NAME);
203 break;
204 default: {
205 Str255 da;
206 GrafPtr save_port;
207
208 GetItem(apple_menu, LoWord(menu_id), &da);
209 GetPort(&save_port);
210 OpenDeskAcc(da);
211 SetPort(save_port);
212 break;
213 }
214 }
215 break;
216 case FILE_MENU_ID:
217 switch (item) {
218 case FILE_MENU_NEW_ID: {
219 struct repo *repo;
220 if ((repo = repo_create()))
221 browser_init(repo);
222 break;
223 }
224 case FILE_MENU_OPEN_ID: {
225 struct repo *repo;
226 if ((repo = repo_open(NULL)))
227 browser_init(repo);
228 break;
229 }
230 case FILE_MENU_SETTINGS_ID:
231 settings_edit();
232 break;
233 case FILE_MENU_QUIT_ID:
234 if (focusables_quit())
235 quitting = true;
236 break;
237 }
238 break;
239 default:
240 ret = false;
241 }
242
243 handled:
244 HiliteMenu(0);
245 return ret;
246 }
247
248 void
249 menu_defaults(void)
250 {
251 DisableItem(edit_menu, EDIT_MENU_CUT_ID);
252 DisableItem(edit_menu, EDIT_MENU_COPY_ID);
253 DisableItem(edit_menu, EDIT_MENU_PASTE_ID);
254 DisableItem(edit_menu, EDIT_MENU_SELECT_ALL_ID);
255
256 DisableItem(repo_menu, REPO_MENU_ADD_FILE_ID);
257 DisableItem(repo_menu, REPO_MENU_DISCARD_CHANGES_ID);
258 DisableItem(repo_menu, REPO_MENU_APPLY_DIFF_ID);
259
260 DisableItem(amendment_menu, AMENDMENT_MENU_EDIT_ID);
261 DisableItem(amendment_menu, AMENDMENT_MENU_EXPORT_ID);
262 }