AmendHub

Download

jcs

/

wikipedia

/

main.c

 

(View History)

jcs   browser+wikipedia: Support UTF8, article redirections, "View Source" mode Latest amendment: 32 on 2022-09-07

1 /*
2 * Copyright (c) 2022 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 "wikipedia.h"
21 #include "browser.h"
22 #include "focusable.h"
23 #include "util.h"
24
25 MenuHandle apple_menu, file_menu, edit_menu, view_menu;
26 bool quitting = false;
27
28 void handle_menu(long menu_id);
29
30 int
31 main(void)
32 {
33 Handle mbar;
34 EventRecord event;
35 WindowPtr event_win;
36 GrafPtr old_port;
37 struct focusable *focusable;
38 short event_in, n;
39 char key;
40
41 InitGraf(&thePort);
42 InitFonts();
43 FlushEvents(everyEvent, 0);
44 InitWindows();
45 InitMenus();
46 TEInit();
47 InitDialogs(0);
48 InitCursor();
49 MaxApplZone();
50
51 util_init();
52 _atexit(focusables_atexit);
53
54 mbar = GetNewMBar(MBAR_ID);
55 SetMenuBar(mbar);
56 apple_menu = GetMHandle(APPLE_MENU_ID);
57 AddResMenu(apple_menu, 'DRVR');
58 file_menu = GetMHandle(FILE_MENU_ID);
59 edit_menu = GetMHandle(EDIT_MENU_ID);
60 view_menu = GetMHandle(VIEW_MENU_ID);
61 menu_defaults();
62 DrawMenuBar();
63
64 if (_TCPInit() != 0)
65 panic("Failed initializing MacTCP");
66
67 browser_init();
68
69 while (!quitting) {
70 WaitNextEvent(everyEvent, &event, 5L, 0L);
71
72 switch (event.what) {
73 case nullEvent:
74 for (n = 0; n < nfocusables; n++) {
75 if (focusables[n]->idle)
76 focusables[n]->idle(focusables[n], &event);
77 }
78 break;
79 case keyDown:
80 case autoKey:
81 key = event.message & charCodeMask;
82 if ((event.modifiers & cmdKey) != 0)
83 handle_menu(MenuKey(key));
84 else if ((focusable = focusable_focused()) &&
85 focusable->key_down)
86 focusable->key_down(focusable, &event);
87 break;
88 case mouseDown:
89 event_in = FindWindow(event.where, &event_win);
90
91 switch (event_in) {
92 case inMenuBar:
93 handle_menu(MenuSelect(event.where));
94 break;
95 case inSysWindow:
96 SystemClick(&event, event_win);
97 break;
98 case inDrag:
99 if ((focusable = focusable_find(event_win)) != NULL) {
100 if (!focusable_show(focusable))
101 break;
102
103 DragWindow(event_win, event.where, &screenBits.bounds);
104 }
105 break;
106 case inGoAway:
107 if (TrackGoAway(event_win, event.where)) {
108 if ((focusable = focusable_find(event_win)) != NULL)
109 focusable_close(focusable);
110 }
111 break;
112 case inContent:
113 if ((focusable = focusable_find(event_win)) != NULL) {
114 if (!focusable_show(focusable))
115 break;
116 if (focusable->mouse_down)
117 focusable->mouse_down(focusable, &event);
118 }
119 break;
120 }
121 break;
122 case updateEvt:
123 event_win = (WindowPtr)event.message;
124
125 GetPort(&old_port);
126 SetPort(event_win);
127 BeginUpdate(event_win);
128
129 focusable = focusable_find(event_win);
130 if (focusable && focusable->update)
131 focusable->update(focusable, &event);
132
133 EndUpdate(event_win);
134 SetPort(old_port);
135 break;
136 case activateEvt:
137 break;
138 case app4Evt:
139 if (HiWord(event.message) & (1 << 8)) {
140 /* multifinder suspend/resume */
141 switch (event.message & (1 << 0)) {
142 case 0:
143 /* suspend */
144 for (n = 0; n < nfocusables; n++) {
145 if (focusables[n]->suspend)
146 focusables[n]->suspend(focusables[n], &event);
147 }
148 break;
149 case 1:
150 /* resume */
151 for (n = 0; n < nfocusables; n++) {
152 if (focusables[n]->resume)
153 focusables[n]->resume(focusables[n], &event);
154 }
155 break;
156 }
157 }
158 break;
159 }
160 }
161
162 return 0;
163 }
164
165 void
166 handle_menu(long menu_id)
167 {
168 struct focusable *focused;
169 short menu, item;
170
171 menu = HiWord(menu_id);
172 item = LoWord(menu_id);
173
174 if ((focused = focusable_focused()) && focused->menu &&
175 focused->menu(focused, menu, item))
176 goto handled;
177
178 switch (menu) {
179 case APPLE_MENU_ID:
180 switch (item) {
181 case APPLE_MENU_ABOUT_ID:
182 about(PROGRAM_NAME);
183 break;
184 default: {
185 Str255 da;
186 GrafPtr save_port;
187
188 GetItem(apple_menu, LoWord(menu_id), &da);
189 GetPort(&save_port);
190 OpenDeskAcc(da);
191 SetPort(save_port);
192 break;
193 }
194 }
195 break;
196 case FILE_MENU_ID:
197 switch (item) {
198 case FILE_MENU_QUIT_ID:
199 if (focusables_quit())
200 quitting = true;
201 break;
202 }
203 break;
204 }
205
206 handled:
207 HiliteMenu(0);
208 }
209
210 void
211 menu_defaults(void)
212 {
213 DisableItem(edit_menu, EDIT_MENU_CUT_ID);
214 DisableItem(edit_menu, EDIT_MENU_COPY_ID);
215 DisableItem(edit_menu, EDIT_MENU_PASTE_ID);
216 DisableItem(edit_menu, EDIT_MENU_SELECT_ALL_ID);
217 }