Debian Codegraph @

common/core_display.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "core_display.h"
22 #include "core_commands2.h"
23 #include "core_globals.h"
24 #include "core_helpers.h"
25 #include "core_main.h"
26 #include "core_tables.h"
27 #include "core_variables.h"
28 #include "shell.h"
29 #include "shell_spool.h"
30
31
32 /********************/
33 /* HP-42S font data */
34 /********************/
35
36
37 static const unsigned char bigchars[138][5] =
38 {
39 { 0x08, 0x08, 0x2a, 0x08, 0x08 },
40 { 0x22, 0x14, 0x08, 0x14, 0x22 },
41 { 0x10, 0x20, 0x7f, 0x01, 0x01 },
42 { 0x20, 0x40, 0x3e, 0x01, 0x02 },
43 { 0x55, 0x2a, 0x55, 0x2a, 0x55 },
44 { 0x41, 0x63, 0x55, 0x49, 0x63 },
45 { 0x7f, 0x7f, 0x3e, 0x1c, 0x08 },
46 { 0x04, 0x7c, 0x04, 0x7c, 0x04 },
47 { 0x30, 0x48, 0x45, 0x40, 0x20 },
48 { 0x50, 0x58, 0x54, 0x52, 0x51 },
49 { 0x0f, 0x08, 0x00, 0x78, 0x28 },
50 { 0x51, 0x52, 0x54, 0x58, 0x50 },
51 { 0x14, 0x34, 0x1c, 0x16, 0x14 },
52 { 0x20, 0x70, 0xa8, 0x20, 0x3f },
53 { 0x10, 0x20, 0x7f, 0x20, 0x10 },
54 { 0x08, 0x08, 0x2a, 0x1c, 0x08 },
55 { 0x08, 0x1c, 0x2a, 0x08, 0x08 },
56 { 0x7e, 0x20, 0x20, 0x1e, 0x20 },
57 { 0x48, 0x7e, 0x49, 0x41, 0x02 },
58 { 0x00, 0x0e, 0x0a, 0x0e, 0x00 },
59 { 0x78, 0x16, 0x15, 0x16, 0x78 },
60 { 0x7c, 0x0a, 0x11, 0x22, 0x7d },
61 { 0x7c, 0x13, 0x12, 0x13, 0x7c },
62 { 0x60, 0x50, 0x58, 0x64, 0x42 },
63 { 0x3e, 0x2a, 0x2a, 0x22, 0x00 },
64 { 0x7e, 0x09, 0x7f, 0x49, 0x41 },
65 { 0x60, 0x00, 0x60, 0x00, 0x60 },
66 { 0x1f, 0x15, 0x71, 0x50, 0x50 },
67 { 0x3c, 0x43, 0x42, 0x43, 0x3c },
68 { 0x3c, 0x41, 0x40, 0x41, 0x3c },
69 { 0x04, 0x02, 0x01, 0x02, 0x04 },
70 { 0x3c, 0x3c, 0x3c, 0x3c, 0x3c },
71 { 0x00, 0x00, 0x00, 0x00, 0x00 },
72 { 0x00, 0x00, 0x5f, 0x00, 0x00 },
73 { 0x00, 0x07, 0x00, 0x07, 0x00 },
74 { 0x14, 0x7f, 0x14, 0x7f, 0x14 },
75 { 0x24, 0x2a, 0x7f, 0x2a, 0x12 },
76 { 0x23, 0x13, 0x08, 0x64, 0x62 },
77 { 0x36, 0x49, 0x56, 0x20, 0x50 },
78 { 0x00, 0x00, 0x07, 0x00, 0x00 },
79 { 0x00, 0x1c, 0x22, 0x41, 0x00 },
80 { 0x00, 0x41, 0x22, 0x1c, 0x00 },
81 { 0x08, 0x2a, 0x1c, 0x2a, 0x08 },
82 { 0x08, 0x08, 0x3e, 0x08, 0x08 },
83 { 0x00, 0xb0, 0x70, 0x00, 0x00 },
84 { 0x08, 0x08, 0x08, 0x08, 0x00 },
85 { 0x00, 0x60, 0x60, 0x00, 0x00 },
86 { 0x20, 0x10, 0x08, 0x04, 0x02 },
87 { 0x3e, 0x51, 0x49, 0x45, 0x3e },
88 { 0x00, 0x42, 0x7f, 0x40, 0x00 },
89 { 0x62, 0x51, 0x49, 0x49, 0x46 },
90 { 0x22, 0x49, 0x49, 0x49, 0x36 },
91 { 0x18, 0x14, 0x12, 0x7f, 0x10 },
92 { 0x27, 0x45, 0x45, 0x45, 0x39 },
93 { 0x3c, 0x4a, 0x49, 0x49, 0x30 },
94 { 0x01, 0x71, 0x09, 0x05, 0x03 },
95 { 0x36, 0x49, 0x49, 0x49, 0x36 },
96 { 0x06, 0x49, 0x49, 0x29, 0x1e },
97 { 0x00, 0x36, 0x36, 0x00, 0x00 },
98 { 0x00, 0xb6, 0x76, 0x00, 0x00 },
99 { 0x08, 0x14, 0x22, 0x41, 0x00 },
100 { 0x14, 0x14, 0x14, 0x14, 0x14 },
101 { 0x41, 0x22, 0x14, 0x08, 0x00 },
102 { 0x02, 0x01, 0x51, 0x09, 0x06 },
103 { 0x3e, 0x41, 0x5d, 0x55, 0x5e },
104 { 0x7e, 0x09, 0x09, 0x09, 0x7e },
105 { 0x7f, 0x49, 0x49, 0x49, 0x36 },
106 { 0x3e, 0x41, 0x41, 0x41, 0x22 },
107 { 0x7f, 0x41, 0x41, 0x22, 0x1c },
108 { 0x7f, 0x49, 0x49, 0x49, 0x41 },
109 { 0x7f, 0x09, 0x09, 0x09, 0x01 },
110 { 0x3e, 0x41, 0x41, 0x51, 0x72 },
111 { 0x7f, 0x08, 0x08, 0x08, 0x7f },
112 { 0x00, 0x41, 0x7f, 0x41, 0x00 },
113 { 0x30, 0x40, 0x40, 0x40, 0x3f },
114 { 0x7f, 0x08, 0x14, 0x22, 0x41 },
115 { 0x7f, 0x40, 0x40, 0x40, 0x40 },
116 { 0x7f, 0x02, 0x0c, 0x02, 0x7f },
117 { 0x7f, 0x04, 0x08, 0x10, 0x7f },
118 { 0x3e, 0x41, 0x41, 0x41, 0x3e },
119 { 0x7f, 0x09, 0x09, 0x09, 0x06 },
120 { 0x3e, 0x41, 0x51, 0x21, 0x5e },
121 { 0x7f, 0x09, 0x19, 0x29, 0x46 },
122 { 0x26, 0x49, 0x49, 0x49, 0x32 },
123 { 0x01, 0x01, 0x7f, 0x01, 0x01 },
124 { 0x3f, 0x40, 0x40, 0x40, 0x3f },
125 { 0x07, 0x18, 0x60, 0x18, 0x07 },
126 { 0x7f, 0x20, 0x18, 0x20, 0x7f },
127 { 0x63, 0x14, 0x08, 0x14, 0x63 },
128 { 0x03, 0x04, 0x78, 0x04, 0x03 },
129 { 0x61, 0x51, 0x49, 0x45, 0x43 },
130 { 0x00, 0x7f, 0x41, 0x41, 0x00 },
131 { 0x02, 0x04, 0x08, 0x10, 0x20 },
132 { 0x00, 0x41, 0x41, 0x7f, 0x00 },
133 { 0x04, 0x02, 0x7f, 0x02, 0x04 },
134 { 0x80, 0x80, 0x80, 0x80, 0x80 },
135 { 0x00, 0x03, 0x04, 0x00, 0x00 },
136 { 0x20, 0x54, 0x54, 0x54, 0x78 },
137 { 0x7f, 0x44, 0x44, 0x44, 0x38 },
138 { 0x38, 0x44, 0x44, 0x44, 0x44 },
139 { 0x38, 0x44, 0x44, 0x44, 0x7f },
140 { 0x38, 0x54, 0x54, 0x54, 0x58 },
141 { 0x00, 0x08, 0x7e, 0x09, 0x02 },
142 { 0x18, 0xa4, 0xa4, 0xa4, 0x78 },
143 { 0x7f, 0x04, 0x04, 0x04, 0x78 },
144 { 0x00, 0x44, 0x7d, 0x40, 0x00 },
145 { 0x00, 0x40, 0x80, 0x84, 0x7d },
146 { 0x7f, 0x10, 0x28, 0x44, 0x00 },
147 { 0x00, 0x41, 0x7f, 0x40, 0x00 },
148 { 0x7c, 0x04, 0x38, 0x04, 0x7c },
149 { 0x7c, 0x04, 0x04, 0x04, 0x78 },
150 { 0x38, 0x44, 0x44, 0x44, 0x38 },
151 { 0xfc, 0x24, 0x24, 0x24, 0x18 },
152 { 0x18, 0x24, 0x24, 0x24, 0xfc },
153 { 0x7c, 0x08, 0x04, 0x04, 0x04 },
154 { 0x48, 0x54, 0x54, 0x54, 0x24 },
155 { 0x00, 0x04, 0x3f, 0x44, 0x20 },
156 { 0x3c, 0x40, 0x40, 0x40, 0x7c },
157 { 0x1c, 0x20, 0x40, 0x20, 0x1c },
158 { 0x3c, 0x40, 0x30, 0x40, 0x3c },
159 { 0x44, 0x28, 0x10, 0x28, 0x44 },
160 { 0x1c, 0xa0, 0xa0, 0xa0, 0x7c },
161 { 0x44, 0x64, 0x54, 0x4c, 0x44 },
162 { 0x08, 0x36, 0x41, 0x41, 0x00 },
163 { 0x00, 0x00, 0x7f, 0x00, 0x00 },
164 { 0x00, 0x41, 0x41, 0x36, 0x08 },
165 { 0x08, 0x04, 0x08, 0x10, 0x08 },
166 { 0x7f, 0x08, 0x08, 0x08, 0x08 },
167 { 0x28, 0x00, 0x00, 0x00, 0x00 },
168 { 0x04, 0x08, 0x70, 0x08, 0x04 },
169 { 0x5e, 0x61, 0x01, 0x61, 0x5e },
170 { 0x04, 0x04, 0x7c, 0x04, 0x04 },
171 { 0x7c, 0x40, 0x40, 0x40, 0x40 },
172 { 0x78, 0x14, 0x14, 0x14, 0x78 },
173 { 0x7f, 0x41, 0x22, 0x14, 0x08 },
174 { 0x2a, 0x55, 0x2a, 0x14, 0x08 },
175 { 0x08, 0x14, 0x2a, 0x14, 0x22 },
176 { 0x22, 0x14, 0x2a, 0x14, 0x08 },
177 };
178
179 static const unsigned char smallchars[454] =
180 {
181 0x00, 0x00, 0x00,
182 0x5c,
183 0x06, 0x00, 0x06,
184 0x28, 0x7c, 0x28, 0x7c, 0x28,
185 0x08, 0x54, 0x7c, 0x54, 0x20,
186 0x24, 0x10, 0x48,
187 0x30, 0x4c, 0x50, 0x20, 0x50,
188 0x08, 0x04,
189 0x38, 0x44,
190 0x44, 0x38,
191 0x54, 0x38, 0x54,
192 0x10, 0x38, 0x10,
193 0x40, 0x20,
194 0x10, 0x10, 0x10,
195 0x40,
196 0x60, 0x10, 0x0c,
197 0x38, 0x44, 0x38,
198 0x48, 0x7c, 0x40,
199 0x74, 0x54, 0x5c,
200 0x44, 0x54, 0x7c,
201 0x1c, 0x10, 0x7c,
202 0x5c, 0x54, 0x74,
203 0x7c, 0x54, 0x74,
204 0x64, 0x14, 0x0c,
205 0x7c, 0x54, 0x7c,
206 0x5c, 0x54, 0x7c,
207 0x28,
208 0x40, 0x28,
209 0x10, 0x28, 0x44,
210 0x28, 0x28, 0x28,
211 0x44, 0x28, 0x10,
212 0x08, 0x04, 0x54, 0x08,
213 0x38, 0x44, 0x54, 0x58,
214 0x78, 0x14, 0x78,
215 0x7c, 0x54, 0x28,
216 0x38, 0x44, 0x44,
217 0x7c, 0x44, 0x38,
218 0x7c, 0x54, 0x44,
219 0x7c, 0x14, 0x04,
220 0x7c, 0x44, 0x54, 0x74,
221 0x7c, 0x10, 0x7c,
222 0x7c,
223 0x60, 0x40, 0x7c,
224 0x7c, 0x10, 0x28, 0x44,
225 0x7c, 0x40, 0x40,
226 0x7c, 0x08, 0x10, 0x08, 0x7c,
227 0x7c, 0x18, 0x30, 0x7c,
228 0x7c, 0x44, 0x7c,
229 0x7c, 0x14, 0x1c,
230 0x38, 0x44, 0x24, 0x58,
231 0x7c, 0x14, 0x6c,
232 0x48, 0x54, 0x24,
233 0x04, 0x7c, 0x04,
234 0x7c, 0x40, 0x7c,
235 0x1c, 0x60, 0x1c,
236 0x7c, 0x20, 0x10, 0x20, 0x7c,
237 0x6c, 0x10, 0x6c,
238 0x0c, 0x70, 0x0c,
239 0x64, 0x54, 0x4c,
240 0x7c, 0x44,
241 0x0c, 0x10, 0x60,
242 0x44, 0x7c,
243 0x10, 0x08, 0x7c, 0x08, 0x10,
244 0x40, 0x40, 0x40,
245 0x04, 0x08,
246 0x10, 0x6c, 0x44,
247 0x6c,
248 0x44, 0x6c, 0x10,
249 0x10, 0x08, 0x10, 0x20, 0x10,
250 0x54, 0x28, 0x54, 0x28, 0x54,
251 0x10, 0x54, 0x10,
252 0x28, 0x10, 0x28,
253 0x10, 0x20, 0x7c, 0x04, 0x04, 0x04,
254 0x20, 0x40, 0x38, 0x04, 0x08,
255 0x44, 0x6c, 0x54, 0x44,
256 0x08, 0x78, 0x08, 0x78, 0x08,
257 0x50, 0x58, 0x54,
258 0x3c, 0x20, 0x00, 0x78, 0x28,
259 0x54, 0x58, 0x50,
260 0x28, 0x68, 0x38, 0x2c, 0x28,
261 0x10, 0x20, 0x7c, 0x20, 0x10,
262 0x10, 0x10, 0x54, 0x38, 0x10,
263 0x10, 0x38, 0x54, 0x10, 0x10,
264 0x78, 0x20, 0x38, 0x20,
265 0x1c, 0x14, 0x1c,
266 0x1c, 0x08, 0x08,
267 0x60, 0x00, 0x60, 0x00, 0x60,
268 0x60, 0x50, 0x58, 0x64, 0x40,
269 0x74, 0x28, 0x28, 0x74,
270 0x34, 0x48, 0x48, 0x34,
271 0x34, 0x40, 0x40, 0x34,
272 0x7c, 0x12, 0x24, 0x7a,
273 0x50, 0x78, 0x54, 0x04,
274 0x20, 0x54, 0x40, 0x20,
275 0x78, 0x14, 0x7c, 0x54,
276 0x38, 0x38, 0x38,
277 0x70, 0x2c, 0x70,
278 0x7c, 0x7c, 0x38, 0x10,
279 0x30, 0x48, 0x78,
280 0x7c, 0x50, 0x70,
281 0x30, 0x48, 0x48,
282 0x70, 0x50, 0x7c,
283 0x30, 0x68, 0x58,
284 0x10, 0x7c, 0x14,
285 0xb0, 0xa8, 0x78,
286 0x7c, 0x10, 0x70,
287 0x74,
288 0x80, 0xf4,
289 0x7c, 0x10, 0x68,
290 0x7c, 0x40,
291 0x78, 0x08, 0x78, 0x08, 0x70,
292 0x78, 0x08, 0x70,
293 0x38, 0x48, 0x70,
294 0xf8, 0x28, 0x38,
295 0x38, 0x28, 0xf8,
296 0x70, 0x08, 0x08,
297 0x58, 0x58, 0x68,
298 0x08, 0x7c, 0x48,
299 0x38, 0x40, 0x78,
300 0x38, 0x60, 0x38,
301 0x38, 0x40, 0x30, 0x40, 0x38,
302 0x48, 0x30, 0x48,
303 0x98, 0xa0, 0x78,
304 0x68, 0x58, 0x58,
305 0x08, 0x04, 0x08,
306 0x18, 0x60, 0x18,
307 0x58, 0x64, 0x04, 0x64, 0x58,
308 0x7c, 0x44, 0x28, 0x10,
309 0x08, 0x78, 0x08,
310 0x20, 0x70, 0x20, 0x3c,
311 0x7c, 0x54, 0x00, 0x78, 0x48,
312 0x78, 0x40, 0x40,
313 0x70, 0x28, 0x70,
314 0x28, 0x54, 0x28, 0x10,
315 0x10, 0x28, 0x54, 0x28, 0x44,
316 0x44, 0x28, 0x54, 0x28, 0x10,
317 };
318
319 static const short smallchars_offset[137] =
320 {
321 0,
322 3,
323 4,
324 7,
325 12,
326 17,
327 20,
328 25,
329 27,
330 29,
331 31,
332 34,
333 37,
334 39,
335 42,
336 43,
337 46,
338 49,
339 52,
340 55,
341 58,
342 61,
343 64,
344 67,
345 70,
346 73,
347 76,
348 77,
349 79,
350 82,
351 85,
352 88,
353 92,
354 96,
355 99,
356 102,
357 105,
358 108,
359 111,
360 114,
361 118,
362 121,
363 122,
364 125,
365 129,
366 132,
367 137,
368 141,
369 144,
370 147,
371 151,
372 154,
373 157,
374 160,
375 163,
376 166,
377 171,
378 174,
379 177,
380 180,
381 182,
382 185,
383 187,
384 192,
385 195,
386 197,
387 200,
388 201,
389 204,
390 209,
391 214,
392 217,
393 220,
394 226,
395 231,
396 235,
397 240,
398 243,
399 248,
400 251,
401 256,
402 261,
403 266,
404 271,
405 275,
406 278,
407 281,
408 286,
409 291,
410 295,
411 299,
412 303,
413 307,
414 311,
415 315,
416 319,
417 322,
418 325,
419 329,
420 332,
421 335,
422 338,
423 341,
424 344,
425 347,
426 350,
427 353,
428 354,
429 356,
430 359,
431 361,
432 366,
433 369,
434 372,
435 375,
436 378,
437 381,
438 384,
439 387,
440 390,
441 393,
442 398,
443 401,
444 404,
445 407,
446 410,
447 413,
448 418,
449 422,
450 425,
451 429,
452 434,
453 437,
454 440,
455 444,
456 449,
457 454,
458 };
459
460 static const unsigned char smallchars_map[138] =
461 {
462 /* 0 */ 70,
463 /* 1 */ 71,
464 /* 2 */ 72,
465 /* 3 */ 73,
466 /* 4 */ 69,
467 /* 5 */ 74,
468 /* 6 */ 97,
469 /* 7 */ 75,
470 /* 8 */ 93,
471 /* 9 */ 76,
472 /* 10 */ 77,
473 /* 11 */ 78,
474 /* 12 */ 79,
475 /* 13 */ 129,
476 /* 14 */ 80,
477 /* 15 */ 81,
478 /* 16 */ 82,
479 /* 17 */ 83,
480 /* 18 */ 92,
481 /* 19 */ 84,
482 /* 20 */ 96,
483 /* 21 */ 91,
484 /* 22 */ 88,
485 /* 23 */ 87,
486 /* 24 */ 37,
487 /* 25 */ 94,
488 /* 26 */ 86,
489 /* 27 */ 130,
490 /* 28 */ 89,
491 /* 29 */ 90,
492 /* 30 */ 124,
493 /* 31 */ 95,
494 /* 32 */ 0,
495 /* 33 */ 1,
496 /* 34 */ 2,
497 /* 35 */ 3,
498 /* 36 */ 4,
499 /* 37 */ 5,
500 /* 38 */ 6,
501 /* 39 */ 7,
502 /* 40 */ 8,
503 /* 41 */ 9,
504 /* 42 */ 10,
505 /* 43 */ 11,
506 /* 44 */ 12,
507 /* 45 */ 13,
508 /* 46 */ 14,
509 /* 47 */ 15,
510 /* 48 */ 16,
511 /* 49 */ 17,
512 /* 50 */ 18,
513 /* 51 */ 19,
514 /* 52 */ 20,
515 /* 53 */ 21,
516 /* 54 */ 22,
517 /* 55 */ 23,
518 /* 56 */ 24,
519 /* 57 */ 25,
520 /* 58 */ 26,
521 /* 59 */ 27,
522 /* 60 */ 28,
523 /* 61 */ 29,
524 /* 62 */ 30,
525 /* 63 */ 31,
526 /* 64 */ 32,
527 /* 65 */ 33,
528 /* 66 */ 34,
529 /* 67 */ 35,
530 /* 68 */ 36,
531 /* 69 */ 37,
532 /* 70 */ 38,
533 /* 71 */ 39,
534 /* 72 */ 40,
535 /* 73 */ 41,
536 /* 74 */ 42,
537 /* 75 */ 43,
538 /* 76 */ 44,
539 /* 77 */ 45,
540 /* 78 */ 46,
541 /* 79 */ 47,
542 /* 80 */ 48,
543 /* 81 */ 49,
544 /* 82 */ 50,
545 /* 83 */ 51,
546 /* 84 */ 52,
547 /* 85 */ 53,
548 /* 86 */ 54,
549 /* 87 */ 55,
550 /* 88 */ 56,
551 /* 89 */ 57,
552 /* 90 */ 58,
553 /* 91 */ 59,
554 /* 92 */ 60,
555 /* 93 */ 61,
556 /* 94 */ 62,
557 /* 95 */ 63,
558 /* 96 */ 64,
559 /* 97 */ 98,
560 /* 98 */ 99,
561 /* 99 */ 100,
562 /* 100 */ 101,
563 /* 101 */ 102,
564 /* 102 */ 103,
565 /* 103 */ 104,
566 /* 104 */ 105,
567 /* 105 */ 106,
568 /* 106 */ 107,
569 /* 107 */ 108,
570 /* 108 */ 109,
571 /* 109 */ 110,
572 /* 110 */ 111,
573 /* 111 */ 112,
574 /* 112 */ 113,
575 /* 113 */ 114,
576 /* 114 */ 115,
577 /* 115 */ 116,
578 /* 116 */ 117,
579 /* 117 */ 118,
580 /* 118 */ 119,
581 /* 119 */ 120,
582 /* 120 */ 121,
583 /* 121 */ 122,
584 /* 122 */ 123,
585 /* 123 */ 65,
586 /* 124 */ 66,
587 /* 125 */ 67,
588 /* 126 */ 68,
589 /* 127 */ 85,
590 /* 128 */ 26,
591 /* 129 */ 125,
592 /* 130 */ 126,
593 /* 131 */ 128,
594 /* 132 */ 131,
595 /* 133 */ 132,
596 /* 134 */ 127,
597 /* 135 */ 133,
598 /* 136 */ 134,
599 /* 137 */ 135,
600 };
601
602
603 static char display[272];
604
605 static bool is_dirty = false;
606 static int dirty_top, dirty_left, dirty_bottom, dirty_right;
607
608 static int catalogmenu_section[5];
609 static int catalogmenu_rows[5];
610 static int catalogmenu_row[5];
611 static int catalogmenu_item[5][6];
612
613 static int custommenu_length[3][6];
614 static char custommenu_label[3][6][7];
615
616 static arg_struct progmenu_arg[9];
617 static bool progmenu_is_gto[9];
618 static int progmenu_length[6];
619 static char progmenu_label[6][7];
620
621 static int appmenu_exitcallback;
622
623 /* Menu keys that should respond to certain hardware
624 * keyboard keys, in addition to the keymap:
625 * 0:none 1:left 2:shift-left 3:right 4:shift-right 5:del
626 */
627 static char special_key[6] = { 0, 0, 0, 0, 0, 0 };
628
629
630 /*******************************/
631 /* Private function prototypes */
632 /*******************************/
633
634 static void mark_dirty(int top, int left, int bottom, int right);
635 static void fill_rect(int x, int y, int width, int height, int color);
636 static int get_cat_index();
637
638
639 bool persist_display() {
640 for (int i = 0; i < 5; i++) {
641 if (!write_int(catalogmenu_section[i])) return false;
642 if (!write_int(catalogmenu_rows[i])) return false;
643 if (!write_int(catalogmenu_row[i])) return false;
644 for (int j = 0; j < 6; j++)
645 if (!write_int(catalogmenu_item[i][j])) return false;
646 }
647 for (int i = 0; i < 3; i++) {
648 for (int j = 0; j < 6; j++) {
649 if (!write_int(custommenu_length[i][j])) return false;
650 if (fwrite(custommenu_label[i][j], 1, 7, gfile) != 7) return false;
651 }
652 }
653 for (int i = 0; i < 9; i++)
654 if (!write_arg(progmenu_arg + i))
655 return false;
656 for (int i = 0; i < 9; i++)
657 if (!write_bool(progmenu_is_gto[i])) return false;
658 for (int i = 0; i < 6; i++) {
659 if (!write_int(progmenu_length[i])) return false;
660 if (fwrite(progmenu_label[i], 1, 7, gfile) != 7) return false;
661 }
662 if (fwrite(display, 1, 272, gfile) != 272)
663 return false;
664 if (!write_int(appmenu_exitcallback)) return false;
665 if (fwrite(special_key, 1, 6, gfile) != 6)
666 return false;
667 return true;
668 }
669
670 bool unpersist_display(int version) {
671 for (int i = 0; i < 5; i++) {
672 if (!read_int(&catalogmenu_section[i])) return false;
673 if (!read_int(&catalogmenu_rows[i])) return false;
674 if (!read_int(&catalogmenu_row[i])) return false;
675 for (int j = 0; j < 6; j++)
676 if (!read_int(&catalogmenu_item[i][j])) return false;
677 }
678 for (int i = 0; i < 3; i++) {
679 for (int j = 0; j < 6; j++) {
680 if (!read_int(&custommenu_length[i][j])) return false;
681 if (fread(custommenu_label[i][j], 1, 7, gfile) != 7) return false;
682 }
683 }
684 for (int i = 0; i < 9; i++)
685 if (!read_arg(progmenu_arg + i))
686 return false;
687 if (version < 35) {
688 int temp;
689 for (int i = 0; i < 9; i++) {
690 if (!read_int(&temp)) return false;
691 progmenu_is_gto[i] = temp != 0;
692 }
693 } else {
694 for (int i = 0; i < 9; i++)
695 if (!read_bool(&progmenu_is_gto[i])) return false;
696 }
697 for (int i = 0; i < 6; i++) {
698 if (!read_int(&progmenu_length[i])) return false;
699 if (fread(progmenu_label[i], 1, 7, gfile) != 7) return false;
700 }
701 if (fread(display, 1, 272, gfile) != 272)
702 return false;
703 if (!read_int(&appmenu_exitcallback)) return false;
704 if (version >= 44) {
705 if (fread(special_key, 1, 6, gfile) != 6)
706 return false;
707 } else
708 memset(special_key, 0, 6);
709 return true;
710 }
711
712 void clear_display() {
713 int i;
714 for (i = 0; i < 272; i++)
715 display[i] = 0;
716 mark_dirty(0, 0, 16, 131);
717 memset(special_key, 0, 6);
718 }
719
720 void flush_display() {
721 if (!is_dirty)
722 return;
723 shell_blitter(display, 17, dirty_left, dirty_top,
724 dirty_right - dirty_left, dirty_bottom - dirty_top);
725 is_dirty = false;
726 }
727
728 void repaint_display() {
729 shell_blitter(display, 17, 0, 0, 131, 16);
730 }
731
732 void draw_pixel(int x, int y) {
733 display[y * 17 + (x >> 3)] |= 1 << (x & 7);
734 mark_dirty(y, x, y + 1, x + 1);
735 }
736
737 void draw_pattern(phloat dx, phloat dy, const char *pattern, int pattern_width){
738 int x, y, h, v, hmin, hmax, vmin, vmax;
739 x = dx < 0 ? to_int(-floor(-dx + 0.5)) : to_int(floor(dx + 0.5));
740 y = dy < 0 ? to_int(-floor(-dy + 0.5)) : to_int(floor(dy + 0.5));
741 if (x + pattern_width < 1 || x > 131 || y + 8 < 1 || y > 16)
742 return;
743 hmin = x < 1 ? 1 - x : 0;
744 hmax = x + pattern_width > 132 ? 132 - x : pattern_width;
745 vmin = y < 1 ? 1 - y : 0;
746 vmax = y + 8 > 17 ? 17 - y : 8;
747 x--;
748 y--;
749 if (flags.f.agraph_control1) {
750 if (flags.f.agraph_control0)
751 /* dst = dst ^ src */
752 for (h = hmin; h < hmax; h++) {
753 char c = pattern[h] >> vmin;
754 for (v = vmin; v < vmax; v++) {
755 if (c & 1) {
756 int X = h + x;
757 int Y = v + y;
758 display[Y * 17 + (X >> 3)] ^= 1 << (X & 7);
759 }
760 c >>= 1;
761 }
762 }
763 else
764 /* dst = dst & ~src */
765 for (h = hmin; h < hmax; h++) {
766 char c = pattern[h] >> vmin;
767 for (v = vmin; v < vmax; v++) {
768 if (c & 1) {
769 int X = h + x;
770 int Y = v + y;
771 display[Y * 17 + (X >> 3)] &= ~(1 << (X & 7));
772 }
773 c >>= 1;
774 }
775 }
776 } else {
777 if (flags.f.agraph_control0)
778 /* dst = src */
779 for (h = hmin; h < hmax; h++) {
780 char c = pattern[h] >> vmin;
781 for (v = vmin; v < vmax; v++) {
782 int X = h + x;
783 int Y = v + y;
784 if (c & 1)
785 display[Y * 17 + (X >> 3)] |= 1 << (X & 7);
786 else
787 display[Y * 17 + (X >> 3)] &= ~(1 << (X & 7));
788 c >>= 1;
789 }
790 }
791 else
792 /* dst = dst | src */
793 for (h = hmin; h < hmax; h++) {
794 char c = pattern[h] >> vmin;
795 for (v = vmin; v < vmax; v++) {
796 if (c & 1) {
797 int X = h + x;
798 int Y = v + y;
799 display[Y * 17 + (X >> 3)] |= 1 << (X & 7);
800 }
801 c >>= 1;
802 }
803 }
804 }
805 mark_dirty(y + vmin, x + hmin, y + vmax, x + hmax);
806 }
807
808 void fly_goose() {
809 static uint4 lastgoosetime = 0;
810 uint4 goosetime = shell_milliseconds();
811 if (goosetime < lastgoosetime)
812 // shell_millisends() wrapped around
813 lastgoosetime = 0;
814 if (goosetime - 100 < lastgoosetime)
815 /* No goose movements if the most recent one was less than 100 ms
816 * ago; in other words, maximum goose speed is 10 positions/second
817 */
818 return;
819 lastgoosetime = goosetime;
820
821 if (mode_goose < 0) {
822 clear_row(0);
823 mode_goose = (-mode_goose) % 22;
824 draw_char(mode_goose, 0, 6);
825 } else {
826 draw_char(mode_goose, 0, ' ');
827 mode_goose = (mode_goose + 1) % 22;
828 draw_char(mode_goose, 0, 6);
829 }
830 flush_display();
831 }
832
833 void squeak() {
834 if (flags.f.audio_enable)
835 shell_beeper(10);
836 }
837
838 void tone(int n) {
839 if (flags.f.audio_enable)
840 shell_beeper(n);
841 }
842
843
844 static void mark_dirty(int top, int left, int bottom, int right) {
845 if (is_dirty) {
846 if (top < dirty_top)
847 dirty_top = top;
848 if (left < dirty_left)
849 dirty_left = left;
850 if (bottom > dirty_bottom)
851 dirty_bottom = bottom;
852 if (right > dirty_right)
853 dirty_right = right;
854 } else {
855 dirty_top = top;
856 dirty_left = left;
857 dirty_bottom = bottom;
858 dirty_right = right;
859 is_dirty = true;
860 }
861 }
862
863 void draw_char(int x, int y, char c) {
864 int X, Y, h, v;
865 unsigned char uc = (unsigned char) c;
866 if (x < 0 || x >= 22 || y < 0 || y >= 2)
867 return;
868 if (undefined_char(uc) || uc == 138)
869 uc -= 128;
870 X = x * 6;
871 Y = y * 8;
872 for (v = 0; v < 8; v++) {
873 int YY = Y + v;
874 for (h = 0; h < 5; h++) {
875 int XX = X + h;
876 char mask = 1 << (XX & 7);
877 if (bigchars[uc][h] & (1 << v))
878 display[YY * 17 + (XX >> 3)] |= mask;
879 else
880 display[YY * 17 + (XX >> 3)] &= ~mask;
881 }
882 }
883 mark_dirty(Y, X, Y + 8, X + 5);
884 }
885
886 const unsigned char *get_char(char c) {
887 unsigned char uc = (unsigned char) c;
888 if (undefined_char(uc) || uc == 138)
889 uc -= 128;
890 return bigchars[uc];
891 }
892
893 void draw_string(int x, int y, const char *s, int length) {
894 while (length != 0 && x < 22) {
895 draw_char(x++, y, *s++);
896 length--;
897 }
898 }
899
900 static void fill_rect(int x, int y, int width, int height, int color) {
901 int h, v;
902 for (v = y; v < y + height; v++)
903 for (h = x; h < x + width; h++)
904 if (color)
905 display[v * 17 + (h >> 3)] |= 1 << (h & 7);
906 else
907 display[v * 17 + (h >> 3)] &= ~(1 << (h & 7));
908 mark_dirty(y, x, y + height, x + width);
909 }
910
911 static void draw_key(int n, int highlight, int hide_meta,
912 const char *s, int length) {
913 int swidth = 0;
914 int len = 0;
915 int len2;
916 int x, i;
917 int fatdot = 31;
918
919 /* Note: the SST handling code uses a magic value of 2 in prgm_mode
920 * so that we know *not* to suppress menu highlights while stepping. */
921 if (flags.f.prgm_mode == 1)
922 highlight = 0;
923
924 if (highlight) {
925 int f = smallchars_map[fatdot];
926 swidth = smallchars_offset[f + 1] - smallchars_offset[f];
927 }
928
929 while (len < length) {
930 int c, m, cw;
931 c = (unsigned char) s[len++];
932 if (undefined_char(c) || c == 138)
933 if (hide_meta)
934 continue;
935 else
936 c &= 127;
937 if (mode_menu_caps && c >= 'a' && c <= 'z')
938 c -= 32;
939 m = smallchars_map[c];
940 cw = smallchars_offset[m + 1] - smallchars_offset[m];
941 if (swidth != 0)
942 cw++;
943 if (swidth + cw > 19) {
944 len--;
945 break;
946 }
947 swidth += cw;
948 }
949
950 fill_rect(n * 22, 9, 21, 7, 1);
951 x = n * 22 + 10 - swidth / 2;
952 len2 = highlight ? len + 1 : len;
953 for (i = 0; i < len2; i++) {
954 int c, m, o, cw, j;
955 if (i == len)
956 c = fatdot;
957 else
958 c = (unsigned char) s[i];
959 if (undefined_char(c) || c == 138)
960 if (hide_meta)
961 continue;
962 else
963 c &= 127;
964 if (mode_menu_caps && c >= 'a' && c <= 'z')
965 c -= 32;
966 m = smallchars_map[c];
967 o = smallchars_offset[m];
968 cw = smallchars_offset[m + 1] - o;
969 for (j = 0; j < cw; j++) {
970 int b, k;
971 b = smallchars[o + j];
972 for (k = 0; k < 8; k++)
973 if ((b >> k) & 1)
974 display[(k + 8) * 17 + (x >> 3)] &= ~(1 << (x & 7));
975 x++;
976 }
977 x++;
978 }
979 /* No need for mark_dirty(); fill_rect() took care of that already. */
980
981 /* Support for automatically mapping physical cursor left,
982 * cursor right, and delete keys, to menu keys with legends
983 * consisting of arrows, double-head arrows, or the word
984 * DEL.
985 */
986 if (string_equals(s, length, "\20", 1))
987 /* <- */
988 special_key[n] = 1;
989 else if (string_equals(s, length, "<\20", 2)
990 || string_equals(s, length, "^", 1))
991 /* <<- or up */
992 special_key[n] = 2;
993 else if (string_equals(s, length, "\17", 1))
994 /* -> */
995 special_key[n] = 3;
996 else if (string_equals(s, length, "\17>", 2)
997 || string_equals(s, length, "\16", 1))
998 /* ->> or down */
999 special_key[n] = 4;
1000 else if (string_equals(s, length, "DEL", 3))
1001 special_key[n] = 5;
1002 else
1003 special_key[n] = 0;
1004 }
1005
1006 int special_menu_key(int which) {
1007 for (int i = 0; i < 6; i++)
1008 if (special_key[i] == which)
1009 return i + 1;
1010 return 0;
1011 }
1012
1013 void clear_row(int row) {
1014 fill_rect(0, row * 8, 131, 8, 0);
1015 }
1016
1017 static int prgmline2buf(char *buf, int len, int4 line, int highlight,
1018 int cmd, arg_struct *arg, const char *orig_num,
1019 bool shift_left = false,
1020 bool highlight_final_end = true,
1021 char **xstr = NULL) {
1022 int bufptr = 0;
1023 if (line != -1) {
1024 if (line < 10)
1025 char2buf(buf, len, &bufptr, '0');
1026 bufptr += int2string(line, buf + bufptr, len - bufptr);
1027 char h = highlight == 0 ? ' ' : highlight == 2 && prgms[current_prgm].locked ? (char) 135 : 6;
1028 char2buf(buf, len, &bufptr, h);
1029 }
1030
1031 if (line == 0) {
1032 int4 size = core_program_size(current_prgm);
1033 string2buf(buf, len, &bufptr, "{ ", 2);
1034 bufptr += int2string(size, buf + bufptr, len - bufptr);
1035 string2buf(buf, len, &bufptr, "-Byte Prgm }", 12);
1036 } else if (alpha_active() && mode_alpha_entry && highlight) {
1037 int append = entered_string_length > 0 && entered_string[0] == 127;
1038 if (append) {
1039 string2buf(buf, len, &bufptr, "\177\"", 2);
1040 string2buf(buf, len, &bufptr, entered_string + 1,
1041 entered_string_length - 1);
1042 } else {
1043 char2buf(buf, len, &bufptr, '"');
1044 string2buf(buf, len, &bufptr, entered_string, entered_string_length);
1045 }
1046 char2buf(buf, len, &bufptr, '_');
1047 } else if (highlight_final_end && cmd == CMD_END
1048 && current_prgm == prgms_count - 1) {
1049 string2buf(buf, len, &bufptr, ".END.", 5);
1050 } else if (cmd == CMD_NUMBER) {
1051 const char *num;
1052 if (orig_num != NULL)
1053 num = orig_num;
1054 else
1055 num = phloat2program(arg->val_d);
1056 int numlen = (int) strlen(num);
1057 if (bufptr + numlen <= len) {
1058 memcpy(buf + bufptr, num, numlen);
1059 bufptr += numlen;
1060 } else {
1061 if (shift_left) {
1062 buf[0] = 26;
1063 if (numlen >= len - 1) {
1064 memcpy(buf + 1, num + numlen - len + 1, len - 1);
1065 } else {
1066 int off = bufptr + numlen - len;
1067 memmove(buf + 1, buf + off + 1, bufptr - off - 1);
1068 bufptr -= off;
1069 memcpy(buf + bufptr, num, len - bufptr);
1070 }
1071 } else {
1072 memcpy(buf + bufptr, num, len - bufptr - 1);
1073 buf[len - 1] = 26;
1074 }
1075 bufptr = len;
1076 }
1077 } else if (cmd == CMD_STRING) {
1078 int append = arg->length > 0 && arg->val.text[0] == 127;
1079 if (append)
1080 char2buf(buf, len, &bufptr, 127);
1081 char2buf(buf, len, &bufptr, '"');
1082 string2buf(buf, len, &bufptr, arg->val.text + append,
1083 arg->length - append);
1084 char2buf(buf, len, &bufptr, '"');
1085 } else if (cmd == CMD_XSTR && xstr != NULL && bufptr + 7 + arg->length > len) {
1086 *xstr = (char *) malloc(bufptr + 7 + arg->length);
1087 if (*xstr == NULL)
1088 goto normal;
1089 memcpy(*xstr, buf, bufptr);
1090 bufptr += command2buf(*xstr + bufptr, arg->length + 7, cmd, arg);
1091 } else {
1092 normal:
1093 bufptr += command2buf(buf + bufptr, len - bufptr, cmd, arg);
1094 }
1095
1096 return bufptr;
1097 }
1098
1099 void tb_write(textbuf *tb, const char *data, size_t size) {
1100 if (tb->size + size > tb->capacity) {
1101 size_t newcapacity = tb->capacity == 0 ? 1024 : (tb->capacity << 1);
1102 while (newcapacity < tb->size + size)
1103 newcapacity <<= 1;
1104 char *newbuf = (char *) realloc(tb->buf, newcapacity);
1105 if (newbuf == NULL) {
1106 /* Bummer! Let's just append as much as we can */
1107 memcpy(tb->buf + tb->size, data, tb->capacity - tb->size);
1108 tb->size = tb->capacity;
1109 tb->fail = true;
1110 } else {
1111 tb->buf = newbuf;
1112 tb->capacity = newcapacity;
1113 memcpy(tb->buf + tb->size, data, size);
1114 tb->size += size;
1115 }
1116 } else {
1117 memcpy(tb->buf + tb->size, data, size);
1118 tb->size += size;
1119 }
1120 }
1121
1122 void tb_indent(textbuf *tb, int indent) {
1123 for (int i = 0; i < indent; i++)
1124 tb_write(tb, " ", 1);
1125 }
1126
1127 void tb_write_null(textbuf *tb) {
1128 char c = 0;
1129 tb_write(tb, &c, 1);
1130 }
1131
1132 void tb_print_current_program(textbuf *tb) {
1133 int4 pc = 0;
1134 int line = 0;
1135 int cmd;
1136 arg_struct arg;
1137 bool end = false;
1138 char buf[100];
1139 char utf8buf[500];
1140 do {
1141 const char *orig_num;
1142 if (line > 0) {
1143 get_next_command(&pc, &cmd, &arg, 0, &orig_num);
1144 if (cmd == CMD_END)
1145 end = true;
1146 }
1147 char *xstr = NULL;
1148 int len = prgmline2buf(buf, 100, line, cmd == CMD_LBL, cmd, &arg, orig_num, false, false, &xstr);
1149 char *buf2 = xstr == NULL ? buf : xstr;
1150 for (int i = 0; i < len; i++)
1151 if (buf2[i] == 10)
1152 buf2[i] = (char) 138;
1153 int off = 0;
1154 while (len > 0) {
1155 int slen = len <= 100 ? len : 100;
1156 int utf8len = hp2ascii(utf8buf, buf2 + off, slen);
1157 tb_write(tb, utf8buf, utf8len);
1158 off += slen;
1159 len -= slen;
1160 }
1161 tb_write(tb, "\r\n", 2);
1162 free(xstr);
1163 line++;
1164 } while (!end);
1165 }
1166
1167 void display_prgm_line(int row, int line_offset) {
1168 int4 tmppc = pc;
1169 int4 tmpline = pc2line(pc);
1170 int cmd = CMD_NONE;
1171 arg_struct arg;
1172 char buf[44];
1173 int bufptr;
1174 int len = 22;
1175 const char *orig_num;
1176
1177 if (row == -1)
1178 /* This means use both lines; used by SHOW */
1179 len = 44;
1180
1181 if (tmpline != 0) {
1182 if (line_offset == 0)
1183 get_next_command(&tmppc, &cmd, &arg, 0, &orig_num);
1184 else if (line_offset == 1) {
1185 tmppc += get_command_length(current_prgm, tmppc);
1186 tmpline++;
1187 get_next_command(&tmppc, &cmd, &arg, 0, &orig_num);
1188 } else /* line_offset == -1 */ {
1189 tmpline--;
1190 if (tmpline != 0) {
1191 tmppc = line2pc(tmpline);
1192 get_next_command(&tmppc, &cmd, &arg, 0, &orig_num);
1193 }
1194 }
1195 } else {
1196 if (line_offset == 0) {
1197 /* Nothing to do */
1198 } else if (line_offset == 1) {
1199 tmppc = 0;
1200 get_next_command(&tmppc, &cmd, &arg, 0, &orig_num);
1201 tmpline++;
1202 }
1203 /* Should not get offset == -1 when at line 0! */
1204 }
1205
1206 bufptr = prgmline2buf(buf, len, tmpline, line_offset == 0 ? 2 : 0, cmd, &arg, orig_num, row == -1);
1207
1208 if (row == -1) {
1209 clear_display();
1210 if (bufptr <= 22)
1211 draw_string(0, 0, buf, bufptr);
1212 else {
1213 draw_string(0, 0, buf, 22);
1214 draw_string(0, 1, buf + 22, bufptr - 22);
1215 }
1216 } else {
1217 clear_row(row);
1218 draw_string(0, row, buf, bufptr);
1219 }
1220 }
1221
1222 void xlabel2buf(char *buf, int buflen, int *bufptr) {
1223 if (matedit_mode == 2 || matedit_mode == 3) {
1224 for (int i = 0; i < matedit_stack_depth; i++) {
1225 *bufptr += int2string(matedit_stack[i] + 1, buf + *bufptr, buflen - *bufptr);
1226 char2buf(buf, buflen, bufptr, '.');
1227 }
1228 if (matedit_is_list) {
1229 vartype *m;
1230 int err = matedit_get(&m);
1231 if (err != ERR_NONE || ((vartype_list *) m)->size == 0)
1232 char2buf(buf, buflen, bufptr, 'E');
1233 else
1234 *bufptr += int2string(matedit_i + 1, buf + *bufptr, buflen - *bufptr);
1235 } else {
1236 *bufptr += int2string(matedit_i + 1, buf + *bufptr, buflen - *bufptr);
1237 char2buf(buf, buflen, bufptr, ':');
1238 *bufptr += int2string(matedit_j + 1, buf + *bufptr, buflen - *bufptr);
1239 }
1240 char2buf(buf, buflen, bufptr, '=');
1241 } else if (input_length > 0) {
1242 string2buf(buf, buflen, bufptr, input_name, input_length);
1243 char2buf(buf, buflen, bufptr, '?');
1244 } else if (flags.f.big_stack) {
1245 string2buf(buf, buflen, bufptr, "1\200", 2);
1246 } else {
1247 string2buf(buf, buflen, bufptr, "x\200", 2);
1248 }
1249 }
1250
1251 void display_x(int row) {
1252 char buf[22];
1253 int bufptr = 0;
1254
1255 clear_row(row);
1256 xlabel2buf(buf, 22, &bufptr);
1257 vartype *x = sp >= 0 ? stack[sp] : NULL;
1258 if (x != NULL)
1259 bufptr += vartype2string(x, buf + bufptr, 22 - bufptr);
1260 draw_string(0, row, buf, bufptr);
1261 }
1262
1263 void display_y(int row) {
1264 char buf[20];
1265 int len;
1266 clear_row(row);
1267 vartype *y = sp >= 1 ? stack[sp - 1] : NULL;
1268 if (flags.f.big_stack)
1269 draw_string(0, row, "2\200", 2);
1270 else
1271 draw_string(0, row, "\201\200", 2);
1272 if (y != NULL) {
1273 len = vartype2string(y, buf, 20);
1274 if (len > 20) {
1275 draw_string(2, row, buf, 19);
1276 draw_char(21, row, 26);
1277 } else
1278 draw_string(2, row, buf, len);
1279 }
1280 }
1281
1282 void display_incomplete_command(int row) {
1283 char buf[40];
1284 int bufptr = 0;
1285 const command_spec *cmd = &cmd_array[incomplete_command];
1286
1287 if (flags.f.prgm_mode && (cmd->flags & FLAG_IMMED) == 0) {
1288 int line = pc2line(pc);
1289 if (line < 10)
1290 char2buf(buf, 40, &bufptr, '0');
1291 bufptr += int2string(line, buf + bufptr, 40 - bufptr);
1292 char2buf(buf, 40, &bufptr, prgms[current_prgm].locked ? (char) 135 : 6);
1293 }
1294
1295 if (incomplete_command == CMD_ASSIGNb) {
1296 string2buf(buf, 40, &bufptr, "ASSIGN \"", 8);
1297 string2buf(buf, 40, &bufptr, pending_command_arg.val.text,
1298 pending_command_arg.length);
1299 string2buf(buf, 40, &bufptr, "\" TO _", 6);
1300 goto done;
1301 }
1302
1303 if (incomplete_argtype == ARG_MKEY) {
1304 /* KEYG/KEYX */
1305 string2buf(buf, 40, &bufptr, "KEY _", 5);
1306 goto done;
1307 }
1308
1309 if (incomplete_command == CMD_SIMQ)
1310 string2buf(buf, 40, &bufptr, "Number of Unknowns ", 19);
1311 else {
1312 string2buf(buf, 40, &bufptr, cmd->name, cmd->name_length);
1313 char2buf(buf, 40, &bufptr, ' ');
1314 }
1315
1316 if (incomplete_ind)
1317 string2buf(buf, 40, &bufptr, "IND ", 4);
1318 if (incomplete_alpha) {
1319 char2buf(buf, 40, &bufptr, '"');
1320 string2buf(buf, 40, &bufptr, incomplete_str, incomplete_length);
1321 char2buf(buf, 40, &bufptr, '_');
1322 } else {
1323 int d = 1;
1324 int i;
1325 for (i = 0; i < incomplete_length - 1; i++)
1326 d *= 10;
1327 for (i = 0; i < incomplete_maxdigits; i++) {
1328 if (i < incomplete_length) {
1329 char2buf(buf, 40, &bufptr,
1330 (char) ('0' + (incomplete_num / d) % 10));
1331 d /= 10;
1332 } else
1333 char2buf(buf, 40, &bufptr, '_');
1334 }
1335 }
1336
1337 done:
1338 clear_row(row);
1339 if (bufptr <= 22)
1340 draw_string(0, row, buf, bufptr);
1341 else {
1342 draw_char(0, row, 26);
1343 draw_string(1, row, buf + (bufptr - 21), 21);
1344 }
1345 }
1346
1347 void display_error(int error) {
1348 clear_row(0);
1349 int err_len;
1350 const char *err_text;
1351 if (error == -1) {
1352 err_len = lasterr_length;
1353 err_text = lasterr_text;
1354 } else {
1355 err_len = errors[error].length;
1356 err_text = errors[error].text;
1357 }
1358 draw_string(0, 0, err_text, err_len);
1359 flags.f.message = 1;
1360 flags.f.two_line_message = 0;
1361 if (!flags.f.prgm_mode && (flags.f.trace_print || flags.f.normal_print) && flags.f.printer_exists)
1362 print_text(err_text, err_len, true);
1363 }
1364
1365 void display_command(int row) {
1366 char buf[22];
1367 int bufptr = 0;
1368 const command_spec *cmd = &cmd_array[pending_command];
1369 int catsect;
1370 int hide = pending_command == CMD_VMEXEC
1371 || pending_command == CMD_PMEXEC
1372 || (pending_command == CMD_XEQ
1373 && xeq_invisible
1374 && get_front_menu() == MENU_CATALOG
1375 && ((catsect = get_cat_section()) == CATSECT_PGM
1376 || catsect == CATSECT_PGM_ONLY));
1377
1378 if (pending_command >= CMD_ASGN01 && pending_command <= CMD_ASGN18)
1379 string2buf(buf, 22, &bufptr, "ASSIGN ", 7);
1380 else if (!hide) {
1381 if (pending_command == CMD_SIMQ)
1382 string2buf(buf, 22, &bufptr, "Number of Unknowns ", 19);
1383 else {
1384 string2buf(buf, 22, &bufptr, cmd->name, cmd->name_length);
1385 char2buf(buf, 22, &bufptr, ' ');
1386 }
1387 }
1388
1389 if (cmd->argtype == ARG_NONE)
1390 goto done;
1391
1392 if (pending_command_arg.type == ARGTYPE_IND_NUM
1393 || pending_command_arg.type == ARGTYPE_IND_STK
1394 || pending_command_arg.type == ARGTYPE_IND_STR)
1395 string2buf(buf, 22, &bufptr, "IND ", 4);
1396
1397 if (pending_command_arg.type == ARGTYPE_NUM
1398 || pending_command_arg.type == ARGTYPE_IND_NUM) {
1399 int d = 1, i;
1400 int leadingzero = 1;
1401 for (i = 0; i < pending_command_arg.length - 1; i++)
1402 d *= 10;
1403 for (i = 0; i < pending_command_arg.length; i++) {
1404 int digit = (pending_command_arg.val.num / d) % 10;
1405 if (digit != 0 || i >= pending_command_arg.length - 2)
1406 leadingzero = 0;
1407 if (!leadingzero)
1408 char2buf(buf, 22, &bufptr, (char) ('0' + digit));
1409 d /= 10;
1410 }
1411 } else if (pending_command_arg.type == ARGTYPE_STK
1412 || pending_command_arg.type == ARGTYPE_IND_STK) {
1413 string2buf(buf, 22, &bufptr, "ST ", 3);
1414 char2buf(buf, 22, &bufptr, pending_command_arg.val.stk);
1415 } else if (pending_command_arg.type == ARGTYPE_STR
1416 || pending_command_arg.type == ARGTYPE_IND_STR) {
1417 char2buf(buf, 22, &bufptr, '"');
1418 string2buf(buf, 22, &bufptr, pending_command_arg.val.text,
1419 pending_command_arg.length);
1420 char2buf(buf, 22, &bufptr, '"');
1421 } else if (pending_command_arg.type == ARGTYPE_LBLINDEX) {
1422 int labelindex = pending_command_arg.val.num;
1423 if (labels[labelindex].length == 0)
1424 if (labelindex == labels_count - 1)
1425 string2buf(buf, 22, &bufptr, ".END.", 5);
1426 else
1427 string2buf(buf, 22, &bufptr, "END", 3);
1428 else {
1429 char2buf(buf, 22, &bufptr, '"');
1430 string2buf(buf, 22, &bufptr, labels[labelindex].name,
1431 labels[labelindex].length);
1432 char2buf(buf, 22, &bufptr, '"');
1433 }
1434 } else if (pending_command_arg.type == ARGTYPE_XSTR) {
1435 char2buf(buf, 22, &bufptr, '"');
1436 string2buf(buf, 22, &bufptr, pending_command_arg.val.xstr,
1437 pending_command_arg.length);
1438 char2buf(buf, 22, &bufptr, '"');
1439 } else /* ARGTYPE_LCLBL */ {
1440 char2buf(buf, 22, &bufptr, pending_command_arg.val.lclbl);
1441 }
1442
1443 if (pending_command >= CMD_ASGN01 && pending_command <= CMD_ASGN18) {
1444 int keynum = pending_command - CMD_ASGN01 + 1;
1445 string2buf(buf, 22, &bufptr, " TO ", 4);
1446 char2buf(buf, 22, &bufptr, (char) ('0' + keynum / 10));
1447 char2buf(buf, 22, &bufptr, (char) ('0' + keynum % 10));
1448 }
1449
1450 done:
1451 clear_row(row);
1452 draw_string(0, row, buf, bufptr);
1453 }
1454
1455 static int set_appmenu(int menuid, bool exitall) {
1456 if (mode_appmenu != MENU_NONE && appmenu_exitcallback != 0) {
1457 /* We delegate the set_menu() call to the callback,
1458 * but only once. If the callback wants to stay active,
1459 * it will have to call set_appmenu_callback() itself
1460 * to reinstate itself.
1461 */
1462 int cb = appmenu_exitcallback;
1463 appmenu_exitcallback = 0;
1464 /* NOTE: I don't use a 'traditional' callback pointer
1465 * because appmenu_exitcallback has to be persistable,
1466 * and pointers to code do not have that property.
1467 */
1468 switch (cb) {
1469 case 1: return appmenu_exitcallback_1(menuid, exitall);
1470 case 2: return appmenu_exitcallback_2(menuid, exitall);
1471 case 3: return appmenu_exitcallback_3(menuid, exitall);
1472 case 4: return appmenu_exitcallback_4(menuid, exitall);
1473 case 5: return appmenu_exitcallback_5(menuid, exitall);
1474 default: return ERR_INTERNAL_ERROR;
1475 }
1476 } else {
1477 mode_appmenu = menuid;
1478 appmenu_exitcallback = 0;
1479 return ERR_NONE;
1480 }
1481 }
1482
1483 void draw_varmenu() {
1484 arg_struct arg;
1485 int saved_prgm, prgm;
1486 int4 pc, pc2;
1487 int command, i, row, key;
1488 int num_mvars = 0;
1489
1490 if (mode_appmenu != MENU_VARMENU)
1491 return;
1492 arg.type = ARGTYPE_STR;
1493 arg.length = varmenu_length;
1494 for (i = 0; i < arg.length; i++)
1495 arg.val.text[i] = varmenu[i];
1496 if (!find_global_label(&arg, &prgm, &pc)) {
1497 set_appmenu(MENU_NONE, false);
1498 varmenu_length = 0;
1499 return;
1500 }
1501 saved_prgm = current_prgm;
1502 current_prgm = prgm;
1503 pc += get_command_length(prgm, pc);
1504 pc2 = pc;
1505 while (get_next_command(&pc, &command, &arg, 0, NULL), command == CMD_MVAR)
1506 num_mvars++;
1507 if (num_mvars == 0) {
1508 current_prgm = saved_prgm;
1509 set_appmenu(MENU_NONE, false);
1510 varmenu_length = 0;
1511 return;
1512 }
1513
1514 varmenu_rows = (num_mvars + 5) / 6;
1515 if (varmenu_row >= varmenu_rows)
1516 varmenu_row = varmenu_rows - 1;
1517 set_annunciators(varmenu_rows > 1, -1, -1, -1, -1, -1);
1518
1519 row = 0;
1520 key = 0;
1521 while (get_next_command(&pc2, &command, &arg, 0, NULL), command == CMD_MVAR) {
1522 if (row == varmenu_row) {
1523 varmenu_labellength[key] = arg.length;
1524 for (i = 0; i < arg.length; i++)
1525 varmenu_labeltext[key][i] = arg.val.text[i];
1526 draw_key(key, 0, 0, arg.val.text, arg.length);
1527 }
1528 if (key++ == 5) {
1529 if (row++ == varmenu_row)
1530 break;
1531 else
1532 key = 0;
1533 }
1534 }
1535 current_prgm = saved_prgm;
1536 while (key < 6) {
1537 varmenu_labellength[key] = 0;
1538 draw_key(key, 0, 0, "", 0);
1539 key++;
1540 }
1541 }
1542
1543 static int fcn_cat[] = {
1544 CMD_ABS, CMD_ACOS, CMD_ACOSH, CMD_ADV, CMD_AGRAPH, CMD_AIP,
1545 CMD_ALENG, CMD_ALL, CMD_ALLSIGMA, CMD_AND, CMD_AOFF, CMD_AON,
1546 CMD_ARCL, CMD_AROT, CMD_ASHF, CMD_ASIN, CMD_ASINH, CMD_ASSIGNa,
1547 CMD_ASTO, CMD_ATAN, CMD_ATANH, CMD_ATOX, CMD_AVIEW, CMD_BASEADD,
1548 CMD_BASESUB, CMD_BASEMUL, CMD_BASEDIV, CMD_BASECHS, CMD_BEEP, CMD_BEST,
1549 CMD_BINM, CMD_BIT_T, CMD_BST, CMD_CF, CMD_CLA, CMD_CLALLa,
1550 CMD_CLD, CMD_CLKEYS, CMD_CLLCD, CMD_CLMENU, CMD_CLP, CMD_CLRG,
1551 CMD_CLST, CMD_CLV, CMD_CLX, CMD_CLSIGMA, CMD_COMB, CMD_COMPLEX,
1552 CMD_CORR, CMD_COS, CMD_COSH, CMD_CPXRES, CMD_CPX_T, CMD_CROSS,
1553 CMD_CUSTOM, CMD_DECM, CMD_DEG, CMD_DEL, CMD_DELAY, CMD_DELR,
1554 CMD_DET, CMD_DIM, CMD_DIM_T, CMD_DOT, CMD_DSE, CMD_EDIT,
1555 CMD_EDITN, CMD_END, CMD_ENG, CMD_ENTER, CMD_EXITALL, CMD_EXPF,
1556 CMD_E_POW_X, CMD_E_POW_X_1, CMD_FC_T, CMD_FCC_T, CMD_FCSTX, CMD_FCSTY,
1557 CMD_FIX, CMD_FNRM, CMD_FP, CMD_FS_T, CMD_FSC_T, CMD_GAMMA,
1558 CMD_GETKEY, CMD_GETM, CMD_GRAD, CMD_GROW, CMD_GTO, CMD_HEXM,
1559 CMD_HMSADD, CMD_HMSSUB, CMD_I_ADD, CMD_I_SUB, CMD_INDEX, CMD_INPUT,
1560 CMD_INSR, CMD_INTEG, CMD_INVRT, CMD_IP, CMD_ISG, CMD_J_ADD,
1561 CMD_J_SUB, CMD_KEYASN, CMD_KEYG, CMD_KEYX, CMD_LASTX, CMD_LBL,
1562 CMD_LCLBL, CMD_LINF, CMD_LINSIGMA, CMD_LIST, CMD_LN, CMD_LN_1_X,
1563 CMD_LOG, CMD_LOGF, CMD_MAN, CMD_MAT_T, CMD_MEAN, CMD_MENU,
1564 CMD_MOD, CMD_MVAR, CMD_FACT, CMD_NEWMAT, CMD_NORM, CMD_NOT,
1565 CMD_OCTM, CMD_OFF, CMD_OLD, CMD_ON, CMD_OR, CMD_PERM,
1566 CMD_PGMINT, CMD_PGMSLV, CMD_PI, CMD_PIXEL, CMD_POLAR, CMD_POSA,
1567 CMD_PRA, CMD_PRLCD, CMD_POFF, CMD_PROMPT, CMD_PON, CMD_PRP,
1568 CMD_PRSTK, CMD_PRUSR, CMD_PRV, CMD_PRX, CMD_PRSIGMA, CMD_PSE,
1569 CMD_PUTM, CMD_PWRF, CMD_QUIET, CMD_RAD, CMD_RAN, CMD_RCL,
1570 CMD_RCL_ADD, CMD_RCL_SUB, CMD_RCL_MUL, CMD_RCL_DIV, CMD_RCLEL, CMD_RCLIJ,
1571 CMD_RDXCOMMA, CMD_RDXDOT, CMD_REALRES, CMD_REAL_T, CMD_RECT, CMD_RND,
1572 CMD_RNRM, CMD_ROTXY, CMD_RSUM, CMD_RTN, CMD_SWAP_R, CMD_RUP,
1573 CMD_RDN, CMD_SCI, CMD_SDEV, CMD_SEED, CMD_SF, CMD_SIGN,
1574 CMD_SIN, CMD_SINH, CMD_SIZE, CMD_SLOPE, CMD_SOLVE, CMD_SQRT,
1575 CMD_SST, CMD_STO, CMD_STO_ADD, CMD_STO_SUB, CMD_STO_MUL, CMD_STO_DIV,
1576 CMD_STOEL, CMD_STOIJ, CMD_STOP, CMD_STR_T, CMD_SUM, CMD_TAN,
1577 CMD_TANH, CMD_TONE, CMD_TRACE, CMD_TRANS, CMD_UVEC, CMD_VARMENU,
1578 CMD_VIEW, CMD_WMEAN, CMD_WRAP, CMD_X_SWAP, CMD_SWAP, CMD_X_LT_0,
1579 CMD_X_LT_Y, CMD_X_LE_0, CMD_X_LE_Y, CMD_X_EQ_0, CMD_X_EQ_Y, CMD_X_NE_0,
1580 CMD_X_NE_Y, CMD_X_GT_0, CMD_X_GT_Y, CMD_X_GE_0, CMD_X_GE_Y, CMD_XEQ,
1581 CMD_XOR, CMD_XTOA, CMD_SQUARE, CMD_YINT, CMD_Y_POW_X, CMD_INV,
1582 CMD_10_POW_X, CMD_ADD, CMD_SUB, CMD_MUL, CMD_DIV, CMD_CHS,
1583 CMD_SIGMAADD, CMD_SIGMASUB, CMD_SIGMAREG, CMD_SIGMAREG_T, CMD_TO_DEC, CMD_TO_DEG,
1584 CMD_TO_HMS, CMD_TO_HR, CMD_TO_OCT, CMD_TO_POL, CMD_TO_RAD, CMD_TO_REC,
1585 CMD_LEFT, CMD_UP, CMD_DOWN, CMD_RIGHT, CMD_PERCENT, CMD_PERCENT_CH,
1586 CMD_FIND, CMD_MAX, CMD_MIN, CMD_NULL, CMD_NULL, CMD_NULL
1587 };
1588
1589 static int ext_time_cat[] = {
1590 CMD_ADATE, CMD_ATIME, CMD_ATIME24, CMD_CLK12, CMD_CLK24, CMD_DATE,
1591 CMD_DATE_PLUS, CMD_DDAYS, CMD_DMY, CMD_DOW, CMD_MDY, CMD_TIME,
1592 CMD_YMD, CMD_NULL, CMD_NULL, CMD_NULL, CMD_NULL, CMD_NULL
1593 };
1594
1595 static int ext_xfcn_cat[] = {
1596 CMD_ANUM, CMD_RCLFLAG, CMD_STOFLAG, CMD_X_SWAP_F, CMD_NULL, CMD_NULL
1597 };
1598
1599 static int ext_base_cat[] = {
1600 CMD_SL, CMD_SR, CMD_RL, CMD_RR, CMD_RLC, CMD_RRC,
1601 CMD_LJ, CMD_ASR, CMD_RLN, CMD_RRN, CMD_RLCN, CMD_RRCN,
1602 CMD_SB, CMD_CB, CMD_B_T, CMD_NUM_B, CMD_MASKL, CMD_MASKR,
1603 CMD_SC, CMD_CC, CMD_C_T, CMD_NULL, CMD_NULL, CMD_NULL,
1604 CMD_SLN, CMD_SRN, CMD_ASRN, CMD_RJ, CMD_NULL, CMD_NULL,
1605 CMD_N_TO_BS, CMD_BS_TO_N, CMD_N_TO_BD, CMD_BD_TO_N, CMD_N_TO_BQ, CMD_BQ_TO_N,
1606 CMD_N_TO_DS, CMD_DS_TO_N, CMD_N_TO_DD, CMD_DD_TO_N, CMD_N_TO_DQ, CMD_DQ_TO_N,
1607 CMD_BRESET, CMD_BSIGNED, CMD_BWRAP, CMD_WSIZE, CMD_WSIZE_T, CMD_A_THRU_F_2,
1608 CMD_DECINT, CMD_NULL, CMD_HEXSEP, CMD_DECSEP, CMD_OCTSEP, CMD_BINSEP
1609 };
1610
1611 static int ext_prgm_cat[] = {
1612 CMD_CPXMAT_T, CMD_CSLD_T, CMD_ERRMSG, CMD_ERRNO, CMD_FUNC, CMD_GETKEY1,
1613 CMD_GETKEYA, CMD_LSTO, CMD_LASTO, CMD_LCLV, CMD_NOP, CMD_PGMMENU,
1614 CMD_PGMVAR, CMD_RTNERR, CMD_RTNNO, CMD_RTNYES, CMD_SKIP, CMD_SST_UP,
1615 CMD_SST_RT, CMD_TYPE_T, CMD_VARMNU1, -2 /* 0? */, -3 /* X? */, CMD_NULL
1616 };
1617
1618 static int ext_str_cat[] = {
1619 CMD_APPEND, CMD_C_TO_N, CMD_EXTEND, CMD_HEAD, CMD_LENGTH, CMD_TO_LIST,
1620 CMD_FROM_LIST, CMD_LIST_T, CMD_LXASTO, CMD_NEWLIST, CMD_N_TO_C, CMD_N_TO_S,
1621 CMD_NN_TO_S, CMD_POS, CMD_REV, CMD_SUBSTR, CMD_S_TO_N, CMD_XASTO,
1622 CMD_XSTR, CMD_XVIEW, CMD_NULL, CMD_NULL, CMD_NULL, CMD_NULL
1623 };
1624
1625 static int ext_stk_cat[] = {
1626 CMD_4STK, CMD_DEPTH, CMD_DROP, CMD_DROPN, CMD_DUP, CMD_DUPN,
1627 CMD_L4STK, CMD_LNSTK, CMD_NSTK, CMD_PICK, CMD_RUPN, CMD_RDNN,
1628 CMD_UNPICK, CMD_NULL, CMD_NULL, CMD_NULL, CMD_NULL, CMD_NULL
1629 };
1630
1631 static int ext_stat_cat[] = {
1632 CMD_SX, CMD_SX2, CMD_SY, CMD_SY2, CMD_SXY, CMD_SN,
1633 CMD_SLNX, CMD_SLNX2, CMD_SLNY, CMD_SLNY2, CMD_SLNXLNY, CMD_SXLNY,
1634 CMD_SYLNX, CMD_NULL, CMD_NULL, CMD_NULL, CMD_NULL, CMD_NULL
1635 };
1636
1637 #if defined(ANDROID) || defined(IPHONE)
1638 #ifdef FREE42_FPTEST
1639 static int ext_misc_cat[] = {
1640 CMD_A2LINE, CMD_A2PLINE, CMD_CAPS, CMD_C_LN_1_X, CMD_C_E_POW_X_1, CMD_DYNAMIC,
1641 CMD_FMA, CMD_GETLI, CMD_GETMI, CMD_HEIGHT, CMD_IDENT, CMD_LOCK,
1642 CMD_MIXED, CMD_PCOMPLX, CMD_PRREG, CMD_PUTLI, CMD_PUTMI, CMD_RCOMPLX,
1643 CMD_STATIC, CMD_STRACE, CMD_UNLOCK, CMD_WIDTH, CMD_X2LINE, CMD_ACCEL,
1644 CMD_LOCAT, CMD_HEADING, CMD_FPTEST, CMD_NULL, CMD_NULL, CMD_NULL
1645 };
1646 #define MISC_CAT_ROWS 5
1647 #else
1648 static int ext_misc_cat[] = {
1649 CMD_A2LINE, CMD_A2PLINE, CMD_CAPS, CMD_C_LN_1_X, CMD_C_E_POW_X_1, CMD_DYNAMIC,
1650 CMD_FMA, CMD_GETLI, CMD_GETMI, CMD_HEIGHT, CMD_IDENT, CMD_LOCK,
1651 CMD_MIXED, CMD_PCOMPLX, CMD_PRREG, CMD_PUTLI, CMD_PUTMI, CMD_RCOMPLX,
1652 CMD_STATIC, CMD_STRACE, CMD_UNLOCK, CMD_WIDTH, CMD_X2LINE, CMD_ACCEL,
1653 CMD_LOCAT, CMD_HEADING, CMD_NULL, CMD_NULL, CMD_NULL, CMD_NULL
1654 };
1655 #define MISC_CAT_ROWS 5
1656 #endif
1657 #else
1658 #ifdef FREE42_FPTEST
1659 static int ext_misc_cat[] = {
1660 CMD_A2LINE, CMD_A2PLINE, CMD_CAPS, CMD_C_LN_1_X, CMD_C_E_POW_X_1, CMD_DYNAMIC,
1661 CMD_FMA, CMD_GETLI, CMD_GETMI, CMD_HEIGHT, CMD_IDENT, CMD_LOCK,
1662 CMD_MIXED, CMD_PCOMPLX, CMD_PRREG, CMD_PUTLI, CMD_PUTMI, CMD_RCOMPLX,
1663 CMD_STATIC, CMD_STRACE, CMD_UNLOCK, CMD_WIDTH, CMD_X2LINE, CMD_FPTEST
1664 };
1665 #define MISC_CAT_ROWS 4
1666 #else
1667 static int ext_misc_cat[] = {
1668 CMD_A2LINE, CMD_A2PLINE, CMD_CAPS, CMD_C_LN_1_X, CMD_C_E_POW_X_1, CMD_DYNAMIC,
1669 CMD_FMA, CMD_GETLI, CMD_GETMI, CMD_HEIGHT, CMD_IDENT, CMD_LOCK,
1670 CMD_MIXED, CMD_PCOMPLX, CMD_PRREG, CMD_PUTLI, CMD_PUTMI, CMD_RCOMPLX,
1671 CMD_STATIC, CMD_STRACE, CMD_UNLOCK, CMD_WIDTH, CMD_X2LINE, CMD_NULL
1672 };
1673 #define MISC_CAT_ROWS 4
1674 #endif
1675 #endif
1676
1677 static int ext_0_cmp_cat[] = {
1678 CMD_0_EQ_NN, CMD_0_NE_NN, CMD_0_LT_NN, CMD_0_GT_NN, CMD_0_LE_NN, CMD_0_GE_NN
1679 };
1680
1681 static int ext_x_cmp_cat[] = {
1682 CMD_X_EQ_NN, CMD_X_NE_NN, CMD_X_LT_NN, CMD_X_GT_NN, CMD_X_LE_NN, CMD_X_GE_NN
1683 };
1684
1685 static void draw_catalog() {
1686 int catsect = get_cat_section();
1687 int catindex = get_cat_index();
1688 if (catsect == CATSECT_TOP) {
1689 draw_top:
1690 draw_key(0, 0, 0, "FCN", 3);
1691 draw_key(1, 0, 0, "PGM", 3);
1692 draw_key(2, 0, 0, "REAL", 4);
1693 draw_key(3, 0, 0, "CPX", 3);
1694 draw_key(4, 0, 0, "MAT", 3);
1695 draw_key(5, 0, 0, "MEM", 3);
1696 mode_updown = true;
1697 set_annunciators(1, -1, -1, -1, -1, -1);
1698 } else if (catsect == CATSECT_EXT_1) {
1699 draw_key(0, 0, 0, "TIME", 4);
1700 draw_key(1, 0, 0, "XFCN", 4);
1701 draw_key(2, 0, 0, "BASE", 4);
1702 draw_key(3, 0, 0, "PRGM", 4);
1703 draw_key(4, 0, 0, "STR", 3);
1704 draw_key(5, 0, 0, "STK", 3);
1705 mode_updown = true;
1706 set_annunciators(1, -1, -1, -1, -1, -1);
1707 } else if (catsect == CATSECT_EXT_2) {
1708 draw_key(0, 0, 0, "STAT", 4);
1709 draw_key(1, 0, 0, "MISC", 4);
1710 draw_key(2, 0, 0, "", 0);
1711 draw_key(3, 0, 0, "", 0);
1712 draw_key(4, 0, 0, "", 0);
1713 draw_key(5, 0, 0, "", 0);
1714 mode_updown = true;
1715 set_annunciators(1, -1, -1, -1, -1, -1);
1716 } else if (catsect == CATSECT_PGM
1717 || catsect == CATSECT_PGM_ONLY
1718 || catsect == CATSECT_PGM_SOLVE
1719 || catsect == CATSECT_PGM_INTEG
1720 || catsect == CATSECT_PGM_MENU) {
1721 /* Show menu of alpha labels */
1722 int lcount = 0;
1723 int i, j, k = -1;
1724 if (catsect == CATSECT_PGM || catsect == CATSECT_PGM_ONLY) {
1725 int lastprgm = -1;
1726 for (i = 0; i < labels_count; i++) {
1727 if (labels[i].length > 0 || labels[i].prgm != lastprgm)
1728 lcount++;
1729 lastprgm = labels[i].prgm;
1730 }
1731 } else /* CATSECT_PGM_SOLVE, CATSECT_PGM_INTEG, CATSECT_PGM_MENU */ {
1732 for (i = 0; i < labels_count; i++)
1733 if (label_has_mvar(i))
1734 lcount++;
1735 }
1736 catalogmenu_rows[catindex] = (lcount + 5) / 6;
1737 if (catalogmenu_row[catindex] >= catalogmenu_rows[catindex])
1738 catalogmenu_row[catindex] = catalogmenu_rows[catindex] - 1;
1739 j = -1;
1740 for (i = labels_count - 1; i >= 0; i--) {
1741 bool show_this_label;
1742 if (catsect == CATSECT_PGM || catsect == CATSECT_PGM_ONLY) {
1743 show_this_label = labels[i].length > 0 || i == 0
1744 || labels[i - 1].prgm != labels[i].prgm;
1745 } else {
1746 show_this_label = label_has_mvar(i);
1747 }
1748 if (show_this_label) {
1749 j++;
1750 if (j / 6 == catalogmenu_row[catindex]) {
1751 int len = labels[i].length;
1752 k = j % 6;
1753 if (len == 0) {
1754 if (i == labels_count - 1)
1755 draw_key(k, 0, 0, ".END.", 5);
1756 else
1757 draw_key(k, 0, 0, "END", 3);
1758 } else
1759 draw_key(k, 0, 0, labels[i].name, labels[i].length);
1760 catalogmenu_item[catindex][k] = i;
1761 if (k == 5)
1762 break;
1763 }
1764 }
1765 }
1766 while (k < 5) {
1767 draw_key(++k, 0, 0, "", 0);
1768 catalogmenu_item[catindex][k] = -1;
1769 }
1770 mode_updown = catalogmenu_rows[catindex] > 1;
1771 set_annunciators(mode_updown, -1, -1, -1, -1, -1);
1772 } else if (catsect == CATSECT_FCN
1773 || catsect >= CATSECT_EXT_TIME && catsect <= CATSECT_EXT_X_CMP) {
1774 int *subcat;
1775 int subcat_rows;
1776 switch (catsect) {
1777 case CATSECT_FCN: subcat = fcn_cat; subcat_rows = 43; break;
1778 case CATSECT_EXT_TIME: subcat = ext_time_cat; subcat_rows = 3; break;
1779 case CATSECT_EXT_XFCN: subcat = ext_xfcn_cat; subcat_rows = 1; break;
1780 case CATSECT_EXT_BASE: subcat = ext_base_cat; subcat_rows = 9; break;
1781 case CATSECT_EXT_PRGM: subcat = ext_prgm_cat; subcat_rows = 4; break;
1782 case CATSECT_EXT_STR: subcat = ext_str_cat; subcat_rows = 4; break;
1783 case CATSECT_EXT_STK: subcat = ext_stk_cat; subcat_rows = 3; break;
1784 case CATSECT_EXT_STAT: subcat = ext_stat_cat; subcat_rows = 3; break;
1785 case CATSECT_EXT_MISC: subcat = ext_misc_cat; subcat_rows = MISC_CAT_ROWS; break;
1786 case CATSECT_EXT_0_CMP: subcat = ext_0_cmp_cat; subcat_rows = 1; break;
1787 case CATSECT_EXT_X_CMP: subcat = ext_x_cmp_cat; subcat_rows = 1; break;
1788 }
1789
1790 int desired_row = catalogmenu_row[catindex];
1791 if (desired_row >= subcat_rows)
1792 desired_row = 0;
1793 for (int i = 0; i < 6; i++) {
1794 int cmd = subcat[desired_row * 6 + i];
1795 catalogmenu_item[catindex][i] = cmd;
1796 if (cmd == -2)
1797 draw_key(i, 0, 1, "0?", 2);
1798 else if (cmd == -3)
1799 draw_key(i, 0, 1, "X?", 2);
1800 else
1801 draw_key(i, 0, 1, cmd_array[cmd].name,
1802 cmd_array[cmd].name_length);
1803 }
1804 catalogmenu_rows[catindex] = subcat_rows;
1805 mode_updown = subcat_rows > 1;
1806 set_annunciators(mode_updown ? 1 : 0, -1, -1, -1, -1, -1);
1807 } else {
1808 int vcount = 0;
1809 int i, j, k = -1;
1810 int show_real = 1;
1811 int show_str = 1;
1812 int show_cpx = 1;
1813 int show_mat = 1;
1814 int show_list = 1;
1815
1816 switch (catsect) {
1817 case CATSECT_REAL:
1818 case CATSECT_REAL_ONLY:
1819 show_cpx = show_mat = show_list = 0; break;
1820 case CATSECT_CPX:
1821 show_real = show_str = show_mat = show_list = 0; break;
1822 case CATSECT_MAT:
1823 case CATSECT_MAT_ONLY:
1824 show_real = show_str = show_cpx = show_list = 0; break;
1825 case CATSECT_MAT_LIST:
1826 case CATSECT_MAT_LIST_ONLY:
1827 show_real = show_str = show_cpx = 0; break;
1828 case CATSECT_LIST_STR_ONLY:
1829 show_real = show_cpx = show_mat = 0; break;
1830 case CATSECT_LIST:
1831 case CATSECT_LIST_ONLY:
1832 show_real = show_str = show_cpx = show_mat = 0; break;
1833 }
1834
1835 for (i = 0; i < vars_count; i++) {
1836 int type = vars[i].value->type;
1837 if ((vars[i].flags & (VAR_HIDDEN | VAR_PRIVATE)) != 0)
1838 continue;
1839 switch (type) {
1840 case TYPE_REAL:
1841 if (show_real) vcount++;
1842 break;
1843 case TYPE_STRING:
1844 if (show_str) vcount++;
1845 break;
1846 case TYPE_COMPLEX:
1847 if (show_cpx) vcount++;
1848 break;
1849 case TYPE_REALMATRIX:
1850 case TYPE_COMPLEXMATRIX:
1851 if (show_mat) vcount++;
1852 break;
1853 case TYPE_LIST:
1854 if (show_list) vcount++;
1855 break;
1856 }
1857 }
1858 if (vcount == 0) {
1859 /* We should only get here if the 'plainmenu' catalog is
1860 * in operation; the other catalogs only operate during
1861 * command entry mode, or are label catalogs -- so in those
1862 * cases, it is possible to prevent empty catalogs from
1863 * being displayed *in advance* (i.e., check if any real
1864 * variables exist before enabling MENU_CATALOG with
1865 * catalogmenu_section = CATSECT_REAL, etc.).
1866 * When a catalog becomes empty while displayed, we move
1867 * to the top level silently. The "No XXX Variables" message
1868 * is only displayed if the user actively tries to enter
1869 * an empty catalog section.
1870 */
1871 set_cat_section(CATSECT_TOP);
1872 goto draw_top;
1873 }
1874
1875 catalogmenu_rows[catindex] = (vcount + 5) / 6;
1876 if (catalogmenu_row[catindex] >= catalogmenu_rows[catindex])
1877 catalogmenu_row[catindex] = catalogmenu_rows[catindex] - 1;
1878 j = -1;
1879 for (i = vars_count - 1; i >= 0; i--) {
1880 if ((vars[i].flags & (VAR_HIDDEN | VAR_PRIVATE)) != 0)
1881 continue;
1882 int type = vars[i].value->type;
1883 switch (type) {
1884 case TYPE_REAL:
1885 if (show_real) break; else continue;
1886 case TYPE_STRING:
1887 if (show_str) break; else continue;
1888 case TYPE_COMPLEX:
1889 if (show_cpx) break; else continue;
1890 case TYPE_REALMATRIX:
1891 case TYPE_COMPLEXMATRIX:
1892 if (show_mat) break; else continue;
1893 case TYPE_LIST:
1894 if (show_list) break; else continue;
1895 default:
1896 continue;
1897 }
1898 j++;
1899 if (j / 6 == catalogmenu_row[catindex]) {
1900 k = j % 6;
1901 draw_key(k, 0, 0, vars[i].name, vars[i].length);
1902 catalogmenu_item[catindex][k] = i;
1903 if (k == 5)
1904 break;
1905 }
1906 }
1907 while (k < 5) {
1908 draw_key(++k, 0, 0, "", 0);
1909 catalogmenu_item[catindex][k] = -1;
1910 }
1911 mode_updown = catalogmenu_rows[catindex] > 1;
1912 set_annunciators(mode_updown, -1, -1, -1, -1, -1);
1913 }
1914 }
1915
1916 void display_mem() {
1917 uint8 bytes = shell_get_mem();
1918 char buf[20];
1919 int buflen;
1920 clear_display();
1921 draw_string(0, 0, "Available Memory:", 17);
1922 buflen = ulong2string(bytes, buf, 20);
1923 draw_string(0, 1, buf, buflen);
1924 draw_string(buflen + 1, 1, "Bytes", 5);
1925 flush_display();
1926 }
1927
1928 static int procrustean_phloat2string(phloat d, char *buf, int buflen) {
1929 char tbuf[100];
1930 int tbuflen = phloat2string(d, tbuf, 100, 0, 0, 3,
1931 flags.f.thousands_separators, MAX_MANT_DIGITS);
1932 if (tbuflen <= buflen) {
1933 memcpy(buf, tbuf, tbuflen);
1934 return tbuflen;
1935 }
1936 if (flags.f.thousands_separators) {
1937 tbuflen = phloat2string(d, tbuf, 100, 0, 0, 3, 0, MAX_MANT_DIGITS);
1938 if (tbuflen <= buflen) {
1939 memcpy(buf, tbuf, tbuflen);
1940 return tbuflen;
1941 }
1942 }
1943 int epos = 0;
1944 while (epos < tbuflen && tbuf[epos] != 24)
1945 epos++;
1946 if (epos == tbuflen) {
1947 int dpos = buflen - 2;
1948 char dec = flags.f.decimal_point ? '.' : ',';
1949 while (dpos >= 0 && tbuf[dpos] != dec)
1950 dpos--;
1951 if (dpos != -1) {
1952 memcpy(buf, tbuf, buflen - 1);
1953 buf[buflen - 1] = 26;
1954 return buflen;
1955 }
1956 tbuflen = phloat2string(d, tbuf, 100, 0, MAX_MANT_DIGITS - 1, 1, 0, MAX_MANT_DIGITS);
1957 epos = 0;
1958 int zero_since = -1;
1959 while (epos < tbuflen && tbuf[epos] != 24) {
1960 if (tbuf[epos] == '0') {
1961 if (zero_since == -1)
1962 zero_since = epos;
1963 } else {
1964 zero_since = -1;
1965 }
1966 epos++;
1967 }
1968 if (zero_since != -1) {
1969 memmove(tbuf + zero_since, tbuf + epos, tbuflen - epos);
1970 tbuflen -= epos - zero_since;
1971 epos = zero_since;
1972 }
1973 if (tbuflen <= buflen) {
1974 memcpy(buf, tbuf, tbuflen);
1975 return tbuflen;
1976 }
1977 }
1978 int expsize = tbuflen - epos;
1979 memcpy(buf, tbuf, buflen - expsize - 1);
1980 buf[buflen - expsize - 1] = 26;
1981 memcpy(buf + buflen - expsize, tbuf + epos, expsize);
1982 return buflen;
1983 }
1984
1985 void show() {
1986 if (flags.f.prgm_mode)
1987 display_prgm_line(-1, 0);
1988 else if (alpha_active()) {
1989 clear_display();
1990 if (reg_alpha_length <= 22)
1991 draw_string(0, 0, reg_alpha, reg_alpha_length);
1992 else {
1993 draw_string(0, 0, reg_alpha, 22);
1994 draw_string(0, 1, reg_alpha + 22, reg_alpha_length - 22);
1995 }
1996 } else {
1997 char buf[45];
1998 int bufptr;
1999 vartype *rx;
2000 int x_type;
2001 if (sp >= 0) {
2002 rx = stack[sp];
2003 x_type = rx->type;
2004 } else {
2005 x_type = TYPE_NULL;
2006 }
2007 clear_display();
2008 switch (x_type) {
2009 case TYPE_REAL: {
2010 bufptr = phloat2string(((vartype_real *) rx)->x, buf, 45,
2011 2, 0, 3,
2012 flags.f.thousands_separators, MAX_MANT_DIGITS);
2013 if (bufptr == 45)
2014 bufptr = phloat2string(((vartype_real *) rx)->x, buf,
2015 44, 2, 0, 3, 0, MAX_MANT_DIGITS);
2016 show_one_or_two_lines:
2017 if (bufptr <= 22)
2018 draw_string(0, 0, buf, bufptr);
2019 else {
2020 draw_string(0, 0, buf, 22);
2021 draw_string(0, 1, buf + 22, bufptr - 22);
2022 }
2023 break;
2024 }
2025 case TYPE_STRING: {
2026 vartype_string *s = (vartype_string *) rx;
2027 bufptr = 0;
2028 char2buf(buf, 44, &bufptr, '"');
2029 string2buf(buf, 44, &bufptr, s->txt(), s->length);
2030 if (bufptr < 44)
2031 char2buf(buf, 44, &bufptr, '"');
2032 goto show_one_or_two_lines;
2033 }
2034 case TYPE_COMPLEX: {
2035 vartype_complex *c = (vartype_complex *) rx;
2036 phloat x, y;
2037 if (flags.f.polar) {
2038 generic_r2p(c->re, c->im, &x, &y);
2039 if (p_isinf(x))
2040 x = POS_HUGE_PHLOAT;
2041 } else {
2042 x = c->re;
2043 y = c->im;
2044 }
2045 bufptr = procrustean_phloat2string(x, buf, 22);
2046 draw_string(0, 0, buf, bufptr);
2047 bufptr = procrustean_phloat2string(y, buf, 21);
2048 if (flags.f.polar) {
2049 draw_char(0, 1, 23);
2050 draw_string(1, 1, buf, bufptr);
2051 } else {
2052 if (buf[0] == '-') {
2053 draw_string(0, 1, "-i", 2);
2054 draw_string(2, 1, buf + 1, bufptr - 1);
2055 } else {
2056 draw_char(0, 1, 'i');
2057 draw_string(1, 1, buf, bufptr);
2058 }
2059 }
2060 break;
2061 }
2062 case TYPE_REALMATRIX: {
2063 vartype_realmatrix *rm = (vartype_realmatrix *) rx;
2064 phloat *d = rm->array->data;
2065 bufptr = vartype2string(rx, buf, 22);
2066 draw_string(0, 0, buf, bufptr);
2067 draw_string(0, 1, "1:1=", 4);
2068 bufptr = 0;
2069 if (rm->array->is_string[0] != 0) {
2070 char *text;
2071 int4 len;
2072 get_matrix_string(rm, 0, &text, &len);
2073 char2buf(buf, 18, &bufptr, '"');
2074 string2buf(buf, 18, &bufptr, text, len);
2075 if (bufptr < 18)
2076 char2buf(buf, 18, &bufptr, '"');
2077 } else {
2078 bufptr = phloat2string(*d, buf, 18,
2079 0, 0, 3,
2080 flags.f.thousands_separators);
2081 }
2082 draw_string(4, 1, buf, bufptr);
2083 break;
2084 }
2085 case TYPE_COMPLEXMATRIX: {
2086 vartype_complexmatrix *cm = (vartype_complexmatrix *) rx;
2087 vartype_complex c;
2088 bufptr = vartype2string(rx, buf, 22);
2089 draw_string(0, 0, buf, bufptr);
2090 draw_string(0, 1, "1:1=", 4);
2091 c.type = TYPE_COMPLEX;
2092 c.re = cm->array->data[0];
2093 c.im = cm->array->data[1];
2094 bufptr = vartype2string((vartype *) &c, buf, 18);
2095 draw_string(4, 1, buf, bufptr);
2096 break;
2097 }
2098 }
2099 }
2100 flush_display();
2101 }
2102
2103 void redisplay() {
2104 int menu_id;
2105 int avail_rows = 2;
2106 int i;
2107
2108 #if defined(ANDROID) || defined(IPHONE)
2109 static bool popup_keyboard_visible;
2110 bool popup_kb = core_alpha_menu();
2111 if (mode_popup_unknown || popup_keyboard_visible != popup_kb) {
2112 mode_popup_unknown = false;
2113 popup_keyboard_visible = popup_kb;
2114 shell_show_alpha_keyboard(popup_kb);
2115 }
2116 #endif
2117
2118 if (mode_clall) {
2119 clear_display();
2120 draw_string(0, 0, "Clear All Memory?", 17);
2121 draw_key(0, 0, 0, "YES", 3);
2122 draw_key(1, 0, 0, "", 0);
2123 draw_key(2, 0, 0, "", 0);
2124 draw_key(3, 0, 0, "", 0);
2125 draw_key(4, 0, 0, "", 0);
2126 draw_key(5, 0, 0, "NO", 2);
2127 flush_display();
2128 return;
2129 }
2130
2131 if (flags.f.two_line_message)
2132 return;
2133 if (flags.f.message)
2134 clear_row(1);
2135 else
2136 clear_display();
2137
2138 if (mode_commandmenu != MENU_NONE)
2139 menu_id = mode_commandmenu;
2140 else if (mode_alphamenu != MENU_NONE)
2141 menu_id = mode_alphamenu;
2142 else if (mode_transientmenu != MENU_NONE)
2143 menu_id = mode_transientmenu;
2144 else if (mode_plainmenu != MENU_NONE)
2145 menu_id = mode_plainmenu;
2146 else if (mode_appmenu != MENU_NONE)
2147 menu_id = mode_appmenu;
2148 else
2149 menu_id = MENU_NONE;
2150 if (menu_id == MENU_CATALOG) {
2151 draw_catalog();
2152 avail_rows = 1;
2153 } else if (menu_id == MENU_VARMENU) {
2154 draw_varmenu();
2155 if (varmenu_length == 0) {
2156 redisplay();
2157 return;
2158 }
2159 avail_rows = 1;
2160 } else if (menu_id == MENU_CUSTOM1 || menu_id == MENU_CUSTOM2
2161 || menu_id == MENU_CUSTOM3) {
2162 int r = menu_id - MENU_CUSTOM1;
2163 if (flags.f.local_label
2164 && !(mode_command_entry && incomplete_argtype == ARG_CKEY)) {
2165 for (i = 0; i < 5; i++) {
2166 char c = (r == 0 ? (mode_menu_caps || mode_menu_static || !mode_shift ? 'A' : 'a') : 'F') + i;
2167 draw_key(i, 0, 0, &c, 1);
2168 }
2169 draw_key(5, 0, 0, mode_menu_static || !mode_shift ? "XEQ" : "GTO", 3);
2170 } else {
2171 for (i = 0; i < 6; i++) {
2172 draw_key(i, 0, 1, custommenu_label[r][i],
2173 custommenu_length[r][i]);
2174 }
2175 }
2176 avail_rows = 1;
2177 } else if (menu_id == MENU_PROGRAMMABLE) {
2178 for (i = 0; i < 6; i++)
2179 draw_key(i, 0, 0, progmenu_label[i], progmenu_length[i]);
2180 avail_rows = 1;
2181 } else if (menu_id != MENU_NONE) {
2182 const menu_spec *m = menus + menu_id;
2183 for (i = 0; i < 6; i++) {
2184 const menu_item_spec *mi = m->child + i;
2185 if (mi->menuid == MENU_NONE || (mi->menuid & 0x3000) == 0)
2186 draw_key(i, 0, 0, mi->title, mi->title_length);
2187 else {
2188 int cmd_id = mi->menuid & 0xfff;
2189 const command_spec *cmd = &cmd_array[cmd_id];
2190 int is_flag = (mi->menuid & 0x2000) != 0;
2191 if (is_flag) {
2192 /* Take a closer look at the command ID and highlight
2193 * the menu item if appropriate -- that is, clear 'is_flag'
2194 * if highlighting is *not* appropriate
2195 */
2196 switch (cmd_id) {
2197 case CMD_FIX:
2198 is_flag = flags.f.fix_or_all
2199 && !flags.f.eng_or_all;
2200 break;
2201 case CMD_SCI:
2202 is_flag = !flags.f.fix_or_all
2203 && !flags.f.eng_or_all;
2204 break;
2205 case CMD_ENG:
2206 is_flag = !flags.f.fix_or_all
2207 && flags.f.eng_or_all;
2208 break;
2209 case CMD_ALL:
2210 is_flag = flags.f.fix_or_all
2211 && flags.f.eng_or_all;
2212 break;
2213 case CMD_RDXDOT:
2214 is_flag = flags.f.decimal_point;
2215 break;
2216 case CMD_RDXCOMMA:
2217 is_flag = !flags.f.decimal_point;
2218 break;
2219 case CMD_DEG:
2220 is_flag = !flags.f.rad && !flags.f.grad;
2221 break;
2222 case CMD_RAD:
2223 is_flag = flags.f.rad;
2224 break;
2225 case CMD_GRAD:
2226 is_flag = !flags.f.rad && flags.f.grad;
2227 break;
2228 case CMD_POLAR:
2229 is_flag = flags.f.polar;
2230 break;
2231 case CMD_RECT:
2232 is_flag = !flags.f.polar;
2233 break;
2234 case CMD_QUIET:
2235 is_flag = !flags.f.audio_enable;
2236 break;
2237 case CMD_CPXRES:
2238 is_flag = !flags.f.real_result_only;
2239 break;
2240 case CMD_REALRES:
2241 is_flag = flags.f.real_result_only;
2242 break;
2243 case CMD_KEYASN:
2244 is_flag = !flags.f.local_label;
2245 break;
2246 case CMD_LCLBL:
2247 is_flag = flags.f.local_label;
2248 break;
2249 case CMD_BSIGNED:
2250 is_flag = flags.f.base_signed;
2251 break;
2252 case CMD_BWRAP:
2253 is_flag = flags.f.base_wrap;
2254 break;
2255 case CMD_DECINT:
2256 is_flag = mode_dec_int;
2257 break;
2258 case CMD_BINSEP:
2259 is_flag = mode_bin_sep;
2260 break;
2261 case CMD_OCTSEP:
2262 is_flag = mode_oct_sep;
2263 break;
2264 case CMD_DECSEP:
2265 is_flag = mode_dec_sep;
2266 break;
2267 case CMD_HEXSEP:
2268 is_flag = mode_hex_sep;
2269 break;
2270 case CMD_MDY:
2271 is_flag = !flags.f.ymd && !flags.f.dmy;
2272 break;
2273 case CMD_DMY:
2274 is_flag = !flags.f.ymd && flags.f.dmy;
2275 break;
2276 case CMD_YMD:
2277 is_flag = flags.f.ymd;
2278 break;
2279 case CMD_CLK12:
2280 is_flag = !mode_time_clk24;
2281 break;
2282 case CMD_CLK24:
2283 is_flag = mode_time_clk24;
2284 break;
2285 case CMD_4STK:
2286 is_flag = !flags.f.big_stack;
2287 break;
2288 case CMD_NSTK:
2289 is_flag = flags.f.big_stack;
2290 break;
2291 case CMD_CAPS:
2292 is_flag = mode_menu_caps;
2293 break;
2294 case CMD_MIXED:
2295 is_flag = !mode_menu_caps;
2296 break;
2297 case CMD_STATIC:
2298 is_flag = mode_menu_static;
2299 break;
2300 case CMD_DYNAMIC:
2301 is_flag = !mode_menu_static;
2302 break;
2303 case CMD_PON:
2304 is_flag = flags.f.printer_exists;
2305 break;
2306 case CMD_POFF:
2307 is_flag = !flags.f.printer_exists;
2308 break;
2309 case CMD_MAN:
2310 is_flag = !flags.f.trace_print
2311 && !flags.f.normal_print;
2312 break;
2313 case CMD_NORM:
2314 is_flag = !flags.f.trace_print
2315 && flags.f.normal_print;
2316 break;
2317 case CMD_TRACE:
2318 is_flag = flags.f.trace_print
2319 && !flags.f.normal_print;
2320 break;
2321 case CMD_STRACE:
2322 is_flag = flags.f.trace_print
2323 && flags.f.normal_print;
2324 break;
2325 case CMD_ALLSIGMA:
2326 is_flag = flags.f.all_sigma;
2327 break;
2328 case CMD_LINSIGMA:
2329 is_flag = !flags.f.all_sigma;
2330 break;
2331 case CMD_LINF:
2332 is_flag = flags.f.lin_fit;
2333 break;
2334 case CMD_LOGF:
2335 is_flag = flags.f.log_fit;
2336 break;
2337 case CMD_EXPF:
2338 is_flag = flags.f.exp_fit;
2339 break;
2340 case CMD_PWRF:
2341 is_flag = flags.f.pwr_fit;
2342 break;
2343 case CMD_WRAP:
2344 is_flag = !flags.f.grow;
2345 break;
2346 case CMD_GROW:
2347 is_flag = flags.f.grow;
2348 break;
2349 case CMD_BINM:
2350 is_flag = get_base() == 2;
2351 break;
2352 case CMD_OCTM:
2353 is_flag = get_base() == 8;
2354 break;
2355 case CMD_DECM:
2356 is_flag = get_base() == 10;
2357 break;
2358 case CMD_HEXM:
2359 is_flag = get_base() == 16;
2360 break;
2361 }
2362 }
2363 int scmd = CMD_NONE;
2364 if (!mode_menu_static && mode_shift) {
2365 switch (menu_id) {
2366 case MENU_TOP_FCN: {
2367 switch (i) {
2368 case 0: scmd = CMD_SIGMASUB; break;
2369 case 1: scmd = CMD_Y_POW_X; break;
2370 case 2: scmd = CMD_SQUARE; break;
2371 case 3: scmd = CMD_10_POW_X; break;
2372 case 4: scmd = CMD_E_POW_X; break;
2373 case 5: scmd = CMD_GTO; break;
2374 }
2375 break;
2376 }
2377 case MENU_PGM_FCN1: {
2378 if (i == 5)
2379 scmd = CMD_GTO;
2380 break;
2381 }
2382 case MENU_STAT1: {
2383 if (i == 0)
2384 scmd = CMD_SIGMASUB;
2385 break;
2386 }
2387 case MENU_BASE2: {
2388 if (i == 0)
2389 scmd = CMD_SLN;
2390 else if (i == 1)
2391 scmd = CMD_SRN;
2392 break;
2393 }
2394 case MENU_BASE3: {
2395 if (i == 0)
2396 scmd = CMD_RJ;
2397 else if (i == 1)
2398 scmd = CMD_ASRN;
2399 break;
2400 }
2401 }
2402 }
2403 if (scmd == CMD_NONE)
2404 draw_key(i, is_flag, 1, cmd->name, cmd->name_length);
2405 else
2406 draw_key(i, is_flag, 1, cmd_array[scmd].name, cmd_array[scmd].name_length);
2407 }
2408 }
2409 avail_rows = 1;
2410 }
2411
2412 if (!flags.f.prgm_mode &&
2413 (mode_command_entry || pending_command != CMD_NONE)) {
2414 int cmd_row;
2415 if (menu_id == MENU_NONE) {
2416 cmd_row = 1;
2417 avail_rows = 1;
2418 } else {
2419 cmd_row = 0;
2420 avail_rows = 0;
2421 }
2422 if (mode_command_entry)
2423 display_incomplete_command(cmd_row);
2424 else
2425 display_command(cmd_row);
2426 }
2427
2428 if (!alpha_active() && !flags.f.prgm_mode) {
2429 if (avail_rows == 1) {
2430 if (!flags.f.message)
2431 display_x(0);
2432 } else if (avail_rows == 2) {
2433 if (!flags.f.message)
2434 display_y(0);
2435 display_x(1);
2436 }
2437 } else if (flags.f.prgm_mode && avail_rows != 0) {
2438 if (mode_command_entry) {
2439 if (avail_rows == 1)
2440 display_incomplete_command(0);
2441 else {
2442 display_prgm_line(0, -1);
2443 display_incomplete_command(1);
2444 }
2445 } else {
2446 if (avail_rows == 1) {
2447 if (!flags.f.message)
2448 display_prgm_line(0, 0);
2449 } else if (avail_rows == 2) {
2450 if (!flags.f.message) {
2451 if (pc == -1)
2452 prgm_highlight_row = 0;
2453 else {
2454 if (prgms[current_prgm].is_end(pc))
2455 prgm_highlight_row = 1;
2456 }
2457 if (prgm_highlight_row == 0) {
2458 display_prgm_line(0, 0);
2459 display_prgm_line(1, 1);
2460 } else {
2461 display_prgm_line(0, -1);
2462 display_prgm_line(1, 0);
2463 }
2464 } else
2465 display_prgm_line(1, 0);
2466 }
2467 }
2468 } else if (alpha_active() && avail_rows != 0 && !flags.f.message) {
2469 int avail = mode_alpha_entry ? 21 : 22;
2470 if (reg_alpha_length <= avail) {
2471 draw_string(0, 0, reg_alpha, reg_alpha_length);
2472 if (mode_alpha_entry)
2473 draw_char(reg_alpha_length, 0, '_');
2474 } else {
2475 avail--;
2476 draw_char(0, 0, 26);
2477 draw_string(1, 0, reg_alpha + reg_alpha_length - avail, avail);
2478 if (mode_alpha_entry)
2479 draw_char(21, 0, '_');
2480 }
2481 }
2482
2483 flush_display();
2484 }
2485
2486 void print_display() {
2487 shell_print(NULL, 0, display, 17, 0, 0, 131, 16);
2488 }
2489
2490 struct prp_data_struct {
2491 char buf[100];
2492 int len;
2493 int saved_prgm;
2494 int cmd;
2495 arg_struct arg;
2496 int4 line;
2497 int4 pc;
2498 int4 lines;
2499 int width;
2500 int first;
2501 bool trace;
2502 bool normal;
2503 bool full_xstr;
2504 };
2505
2506 static prp_data_struct *prp_data;
2507 static int print_program_worker(bool interrupted);
2508
2509 int print_program(int prgm_index, int4 pc, int4 lines, bool normal) {
2510 prp_data_struct *dat = (prp_data_struct *) malloc(sizeof(prp_data_struct));
2511 if (dat == NULL)
2512 return ERR_INSUFFICIENT_MEMORY;
2513
2514 set_annunciators(-1, -1, 1, -1, -1, -1);
2515 dat->len = 0;
2516 dat->saved_prgm = current_prgm;
2517 dat->cmd = CMD_NONE;
2518 dat->line = pc2line(pc);
2519 dat->pc = pc;
2520 dat->lines = lines;
2521 dat->width = flags.f.double_wide_print ? 12 : 24;
2522 dat->first = 1;
2523 if (normal) {
2524 dat->trace = false;
2525 dat->normal = true;
2526 dat->full_xstr = false;
2527 } else {
2528 dat->trace = flags.f.trace_print;
2529 dat->normal = flags.f.normal_print;
2530 dat->full_xstr = true;
2531 }
2532
2533 current_prgm = prgm_index;
2534 prp_data = dat;
2535
2536 if (normal) {
2537 /* Printing just one line for NORM and TRACE mode;
2538 * we don't do the 'interruptible' thing in this case.
2539 */
2540 int err;
2541 while ((err = print_program_worker(false)) == ERR_INTERRUPTIBLE);
2542 return err;
2543 } else {
2544 print_text(NULL, 0, true);
2545 mode_interruptible = print_program_worker;
2546 mode_stoppable = true;
2547 return ERR_INTERRUPTIBLE;
2548 }
2549 }
2550
2551 static int print_program_worker(bool interrupted) {
2552 prp_data_struct *dat = prp_data;
2553 int printed = 0;
2554
2555 if (interrupted)
2556 goto done;
2557
2558 do {
2559 const char *orig_num;
2560 if (dat->line == 0)
2561 dat->pc = 0;
2562 else
2563 get_next_command(&dat->pc, &dat->cmd, &dat->arg, 0, &orig_num);
2564
2565 char *xstr = NULL;
2566 if (dat->trace) {
2567 if (dat->cmd == CMD_LBL || dat->first) {
2568 if (dat->len > 0) {
2569 print_lines(dat->buf, dat->len, true);
2570 printed = 1;
2571 }
2572 if (!dat->first)
2573 print_text(NULL, 0, true);
2574 dat->first = 0;
2575 dat->buf[0] = ' ';
2576 dat->len = 1 + prgmline2buf(dat->buf + 1, 100 - 1, dat->line,
2577 dat->cmd == CMD_LBL, dat->cmd,
2578 &dat->arg, orig_num);
2579 if (dat->cmd == CMD_LBL || dat->cmd == CMD_END
2580 || dat->lines == 1) {
2581 print_lines(dat->buf, dat->len, true);
2582 printed = 1;
2583 dat->len = 0;
2584 }
2585 } else {
2586 int i, len2;
2587 if (dat->len > 0) {
2588 dat->buf[dat->len++] = ' ';
2589 dat->buf[dat->len++] = ' ';
2590 }
2591 len2 = prgmline2buf(dat->buf + dat->len, 100 - dat->len, -1, 0,
2592 dat->cmd, &dat->arg, orig_num,
2593 false, true, dat->full_xstr ? &xstr : NULL);
2594 if (dat->len > 0 && dat->len + len2 > dat->width) {
2595 /* Break line before current instruction */
2596 print_lines(dat->buf, dat->len - 2, true);
2597 printed = 1;
2598 if (xstr == NULL) {
2599 for (i = 0; i < len2; i++)
2600 dat->buf[i] = dat->buf[dat->len + i];
2601 dat->len = len2;
2602 } else {
2603 goto print_xstr;
2604 }
2605 } else if (xstr != NULL) {
2606 print_xstr:
2607 int plen = (len2 / dat->width) * dat->width;
2608 print_lines(xstr, plen, true);
2609 memcpy(dat->buf, xstr + plen, len2 - plen);
2610 dat->len = len2 - plen;
2611 } else
2612 dat->len += len2;
2613 if (dat->lines == 1 || dat->cmd == CMD_END) {
2614 print_lines(dat->buf, dat->len, true);
2615 printed = 1;
2616 } else if (dat->len >= dat->width) {
2617 len2 = (dat->len / dat->width) * dat->width;
2618 print_lines(dat->buf, len2, true);
2619 printed = 1;
2620 for (i = len2; i < dat->len; i++)
2621 dat->buf[i - len2] = dat->buf[i];
2622 dat->len -= len2;
2623 }
2624 }
2625 } else {
2626 dat->len = prgmline2buf(dat->buf, 100, dat->line,
2627 dat->cmd == CMD_LBL, dat->cmd, &dat->arg,
2628 orig_num, false, true,
2629 dat->full_xstr ? &xstr : NULL);
2630 char *buf2 = xstr == NULL ? dat->buf : xstr;
2631 if (dat->normal) {
2632 /* In normal mode, programs are printed right-justified;
2633 * we pad the instuctions to a minimum of 8 characters so
2634 * the listing won't look too ragged.
2635 * First, find the beginning of the instruction -- it starts
2636 * right after the first space or 'goose' (6) character.
2637 */
2638 int p = 0;
2639 while (buf2[p] != ' ' && buf2[p] != 6)
2640 p++;
2641 while (dat->len < p + 9)
2642 buf2[dat->len++] = ' ';
2643 /* Insert blank line above LBLs */
2644 if (dat->cmd == CMD_LBL && !dat->first)
2645 print_text(NULL, 0, true);
2646 dat->first = 0;
2647 }
2648 print_lines(buf2, dat->len, !dat->normal);
2649 printed = 1;
2650 }
2651 free(xstr);
2652 dat->line++;
2653 dat->lines--;
2654
2655 } while (!printed);
2656
2657 if (dat->lines != 0 && dat->cmd != CMD_END)
2658 return ERR_INTERRUPTIBLE;
2659
2660 done:
2661 current_prgm = dat->saved_prgm;
2662 free(dat);
2663 set_annunciators(-1, -1, 0, -1, -1, -1);
2664 return ERR_STOP;
2665 }
2666
2667 void print_program_line(int prgm_index, int4 pc) {
2668 print_program(prgm_index, pc, 1, true);
2669 }
2670
2671 int command2buf(char *buf, int len, int cmd, const arg_struct *arg) {
2672 int bufptr = 0;
2673
2674 int4 xrom_arg;
2675 if ((cmd_array[cmd].code1 & 0xf8) == 0xa0 && (cmd_array[cmd].flags & FLAG_HIDDEN) != 0) {
2676 xrom_arg = (cmd_array[cmd].code1 << 8) | cmd_array[cmd].code2;
2677 cmd = CMD_XROM;
2678 } else if (cmd == CMD_XROM) {
2679 if (arg->type == ARGTYPE_NUM) {
2680 xrom_arg = arg->val.num;
2681 } else {
2682 string2buf(buf, len, &bufptr, "XROM 0x", 7);
2683 for (int i = 0; i < arg->length; i++) {
2684 char2buf(buf, len, &bufptr, "0123456789abcdef"[(arg->val.text[i] >> 4) & 15]);
2685 char2buf(buf, len, &bufptr, "0123456789abcdef"[arg->val.text[i] & 15]);
2686 }
2687 return bufptr;
2688 }
2689 }
2690
2691 const command_spec *cmdspec = &cmd_array[cmd];
2692 if (cmd >= CMD_ASGN01 && cmd <= CMD_ASGN18) {
2693 string2buf(buf, len, &bufptr, "ASSIGN ", 7);
2694 } else {
2695 for (int i = 0; i < cmdspec->name_length; i++) {
2696 int c = (unsigned char) cmdspec->name[i];
2697 if (undefined_char(c))
2698 c &= 127;
2699 char2buf(buf, len, &bufptr, c);
2700 }
2701 }
2702
2703 if (cmd == CMD_XROM) {
2704 int n = xrom_arg & 0x7ff;
2705 int rom = n >> 6;
2706 int instr = n & 63;
2707 char2buf(buf, len, &bufptr, ' ');
2708 char2buf(buf, len, &bufptr, '0' + rom / 10);
2709 char2buf(buf, len, &bufptr, '0' + rom % 10);
2710 char2buf(buf, len, &bufptr, ',');
2711 char2buf(buf, len, &bufptr, '0' + instr / 10);
2712 char2buf(buf, len, &bufptr, '0' + instr % 10);
2713 } else if (cmdspec->argtype != ARG_NONE) {
2714 if (cmdspec->name_length > 0)
2715 char2buf(buf, len, &bufptr, ' ');
2716 if (arg->type == ARGTYPE_IND_NUM
2717 || arg->type == ARGTYPE_IND_STK
2718 || arg->type == ARGTYPE_IND_STR)
2719 string2buf(buf, len, &bufptr, "IND ", 4);
2720 if (arg->type == ARGTYPE_NUM
2721 || arg->type == ARGTYPE_IND_NUM) {
2722 int digits = arg->type == ARGTYPE_IND_NUM ? 2
2723 : cmdspec->argtype == ARG_NUM9 ? 1
2724 : 2;
2725 int d = 1, i;
2726 for (i = 0; i < digits - 1; i++)
2727 d *= 10;
2728 while (arg->val.num >= d * 10) {
2729 d *= 10;
2730 digits++;
2731 }
2732 for (i = 0; i < digits; i++) {
2733 char2buf(buf, len, &bufptr,
2734 (char) ('0' + (arg->val.num / d) % 10));
2735 d /= 10;
2736 }
2737 } else if (arg->type == ARGTYPE_STK
2738 || arg->type == ARGTYPE_IND_STK) {
2739 string2buf(buf, len, &bufptr, "ST ", 3);
2740 char2buf(buf, len, &bufptr, arg->val.stk);
2741 } else if (arg->type == ARGTYPE_STR
2742 || arg->type == ARGTYPE_IND_STR) {
2743 char2buf(buf, len, &bufptr, '"');
2744 string2buf(buf, len, &bufptr, arg->val.text,
2745 arg->length);
2746 char2buf(buf, len, &bufptr, '"');
2747 } else if (arg->type == ARGTYPE_LCLBL) {
2748 char2buf(buf, len, &bufptr, arg->val.lclbl);
2749 } else if (arg->type == ARGTYPE_LBLINDEX) {
2750 label_struct *lbl = labels + arg->val.num;
2751 if (lbl->length == 0) {
2752 if (arg->val.num == labels_count - 1)
2753 string2buf(buf, len, &bufptr, ".END.", 5);
2754 else
2755 string2buf(buf, len, &bufptr, "END", 3);
2756 } else {
2757 char2buf(buf, len, &bufptr, '"');
2758 string2buf(buf, len, &bufptr, lbl->name, lbl->length);
2759 char2buf(buf, len, &bufptr, '"');
2760 }
2761 } else if (arg->type == ARGTYPE_XSTR) {
2762 char2buf(buf, len, &bufptr, '"');
2763 string2buf(buf, len, &bufptr, arg->val.xstr,
2764 arg->length);
2765 char2buf(buf, len, &bufptr, '"');
2766 } else /* ARGTYPE_COMMAND; for backward compatibility only */ {
2767 const command_spec *cs = &cmd_array[arg->val.cmd];
2768 char2buf(buf, len, &bufptr, '"');
2769 string2buf(buf, len, &bufptr, cs->name, cs->name_length);
2770 char2buf(buf, len, &bufptr, '"');
2771 }
2772 }
2773 if (cmd >= CMD_ASGN01 && cmd <= CMD_ASGN18) {
2774 int keynum = cmd - CMD_ASGN01 + 1;
2775 string2buf(buf, len, &bufptr, " TO ", 4);
2776 char2buf(buf, len, &bufptr, (char) ('0' + keynum / 10));
2777 char2buf(buf, len, &bufptr, (char) ('0' + keynum % 10));
2778 }
2779
2780 return bufptr;
2781 }
2782
2783 static int get_cat_index() {
2784 if (mode_commandmenu != MENU_NONE)
2785 return 0;
2786 else if (mode_alphamenu != MENU_NONE)
2787 return 1;
2788 else if (mode_transientmenu != MENU_NONE)
2789 return 2;
2790 else if (mode_plainmenu != MENU_NONE)
2791 return 3;
2792 else if (mode_appmenu != MENU_NONE)
2793 return 4;
2794 else
2795 return -1;
2796 }
2797
2798 void set_menu(int level, int menuid) {
2799 int err = set_menu_return_err(level, menuid, false);
2800 if (err != ERR_NONE) {
2801 display_error(err);
2802 flush_display();
2803 }
2804 }
2805
2806 int set_menu_return_err(int level, int menuid, bool exitall) {
2807 int err;
2808
2809 switch (level) {
2810 case MENULEVEL_COMMAND:
2811 mode_commandmenu = menuid;
2812 goto lbl_03;
2813 case MENULEVEL_ALPHA:
2814 mode_alphamenu = menuid;
2815 goto lbl_02;
2816 case MENULEVEL_TRANSIENT:
2817 mode_transientmenu = menuid;
2818 goto lbl_01;
2819 case MENULEVEL_PLAIN:
2820 mode_plainmenu = menuid;
2821 goto lbl_00;
2822 case MENULEVEL_APP:
2823 err = set_appmenu(menuid, exitall);
2824 if (err != ERR_NONE)
2825 return err;
2826 }
2827 mode_plainmenu = MENU_NONE;
2828 lbl_00: mode_transientmenu = MENU_NONE;
2829 lbl_01: mode_alphamenu = MENU_NONE;
2830 lbl_02: mode_commandmenu = MENU_NONE;
2831 lbl_03:
2832
2833 int newmenu = get_front_menu();
2834 if (newmenu != MENU_NONE) {
2835 if (newmenu == MENU_CATALOG) {
2836 int index = get_cat_index();
2837 mode_updown = index != -1 && catalogmenu_rows[index] > 1;
2838 } else if (newmenu == MENU_PROGRAMMABLE) {
2839 /* The programmable menu's up/down annunciator is on if the UP
2840 * and/or DOWN keys have been assigned to.
2841 * This is something the original HP-42S doesn't do, but I couldn't
2842 * resist this little improvement, perfect compatibility be damned.
2843 * In my defense, the Programming Examples and Techniques book,
2844 * bottom of page 34, does state that this should work.
2845 * Can't say whether the fact that it doesn't work on the real
2846 * HP-42S is a bug, or whether the coders and the documentation
2847 * writers just had a misunderstanding.
2848 */
2849 mode_updown = progmenu_arg[6].type != ARGTYPE_NONE
2850 || progmenu_arg[7].type != ARGTYPE_NONE;
2851 } else {
2852 /* The up/down annunciator for catalogs depends on how many
2853 * items they contain; this is handled in draw_catalog().
2854 */
2855 mode_updown = newmenu == MENU_VARMENU
2856 ? varmenu_rows > 1
2857 : menus[newmenu].next != MENU_NONE;
2858 }
2859 } else
2860 mode_updown = false;
2861 set_annunciators(mode_updown, -1, -1, -1, -1, -1);
2862 return ERR_NONE;
2863 }
2864
2865 void set_appmenu_exitcallback(int callback_id) {
2866 appmenu_exitcallback = callback_id;
2867 }
2868
2869 void set_plainmenu(int menuid, const char *name, int length) {
2870 if (name != NULL)
2871 print_menu_trace(name, length);
2872
2873 mode_commandmenu = MENU_NONE;
2874 mode_alphamenu = MENU_NONE;
2875 mode_transientmenu = MENU_NONE;
2876
2877 if (menuid == mode_plainmenu) {
2878 mode_plainmenu_sticky = 1;
2879 redisplay();
2880 } else if (menuid == MENU_CUSTOM1
2881 || menuid == MENU_CUSTOM2
2882 || menuid == MENU_CUSTOM3) {
2883 mode_plainmenu = menuid;
2884 mode_plainmenu_sticky = 1;
2885 redisplay();
2886 mode_updown = 1;
2887 set_annunciators(1, -1, -1, -1, -1, -1);
2888 } else {
2889 /* Even if it's a different menu than the current one, it should
2890 * still stick if it belongs to the same group.
2891 */
2892 if (menuid != MENU_NONE && mode_plainmenu != MENU_NONE) {
2893 int menu1 = mode_plainmenu;
2894 int menu2 = menuid;
2895 int parent;
2896 while ((parent = menus[menu1].parent) != MENU_NONE)
2897 menu1 = parent;
2898 while ((parent = menus[menu2].parent) != MENU_NONE)
2899 menu2 = parent;
2900 if (menu1 == menu2)
2901 mode_plainmenu_sticky = 1;
2902 else if (menus[menu1].next == MENU_NONE)
2903 mode_plainmenu_sticky = 0;
2904 else {
2905 int m = menu1;
2906 mode_plainmenu_sticky = 0;
2907 do {
2908 m = menus[m].next;
2909 if (m == menu2) {
2910 mode_plainmenu_sticky = 1;
2911 break;
2912 }
2913 } while (m != menu1);
2914 }
2915 } else
2916 mode_plainmenu_sticky = 0;
2917 if (!mode_plainmenu_sticky) {
2918 mode_plainmenu = menuid;
2919 if (mode_plainmenu == MENU_CATALOG)
2920 set_cat_section(CATSECT_TOP);
2921 redisplay();
2922 }
2923 mode_updown = mode_plainmenu == MENU_CATALOG
2924 || mode_plainmenu != MENU_NONE
2925 && menus[mode_plainmenu].next != MENU_NONE;
2926 set_annunciators(mode_updown, -1, -1, -1, -1, -1);
2927 }
2928 }
2929
2930 void set_catalog_menu(int section) {
2931 mode_commandmenu = MENU_CATALOG;
2932 move_cat_row(0);
2933 if (section == CATSECT_VARS_ONLY && incomplete_command == CMD_HEAD)
2934 section = CATSECT_LIST_STR_ONLY;
2935 set_cat_section(section);
2936 switch (section) {
2937 case CATSECT_TOP:
2938 case CATSECT_FCN:
2939 case CATSECT_PGM:
2940 case CATSECT_PGM_ONLY:
2941 case CATSECT_EXT_1:
2942 case CATSECT_EXT_TIME:
2943 case CATSECT_EXT_XFCN:
2944 case CATSECT_EXT_BASE:
2945 case CATSECT_EXT_PRGM:
2946 case CATSECT_EXT_STR:
2947 case CATSECT_EXT_STK:
2948 case CATSECT_EXT_2:
2949 case CATSECT_EXT_STAT:
2950 case CATSECT_EXT_MISC:
2951 case CATSECT_EXT_0_CMP:
2952 case CATSECT_EXT_X_CMP:
2953 return;
2954 case CATSECT_REAL:
2955 case CATSECT_REAL_ONLY:
2956 if (!vars_exist(CATSECT_REAL))
2957 mode_commandmenu = MENU_NONE;
2958 return;
2959 case CATSECT_CPX:
2960 if (!vars_exist(CATSECT_CPX))
2961 mode_commandmenu = MENU_NONE;
2962 return;
2963 case CATSECT_MAT:
2964 case CATSECT_MAT_ONLY:
2965 if (!vars_exist(CATSECT_MAT))
2966 mode_commandmenu = MENU_NONE;
2967 return;
2968 case CATSECT_MAT_LIST:
2969 case CATSECT_MAT_LIST_ONLY:
2970 if (!vars_exist(CATSECT_MAT_LIST))
2971 mode_commandmenu = MENU_NONE;
2972 return;
2973 case CATSECT_LIST_STR_ONLY:
2974 if (!vars_exist(CATSECT_LIST_STR_ONLY))
2975 mode_commandmenu = MENU_NONE;
2976 return;
2977 case CATSECT_LIST:
2978 case CATSECT_LIST_ONLY:
2979 if (!vars_exist(CATSECT_LIST))
2980 mode_commandmenu = MENU_NONE;
2981 return;
2982 case CATSECT_VARS_ONLY:
2983 if (!vars_exist(-1))
2984 mode_commandmenu = MENU_NONE;
2985 return;
2986 case CATSECT_PGM_SOLVE:
2987 case CATSECT_PGM_INTEG:
2988 case CATSECT_PGM_MENU:
2989 default:
2990 mode_commandmenu = MENU_NONE;
2991 return;
2992 }
2993 }
2994
2995 int get_front_menu() {
2996 if (mode_commandmenu != MENU_NONE)
2997 return mode_commandmenu;
2998 if (mode_alphamenu != MENU_NONE)
2999 return mode_alphamenu;
3000 if (mode_transientmenu != MENU_NONE)
3001 return mode_transientmenu;
3002 if (mode_plainmenu != MENU_NONE)
3003 return mode_plainmenu;
3004 return mode_appmenu;
3005 }
3006
3007 void set_cat_section(int section) {
3008 int index = get_cat_index();
3009 if (index != -1)
3010 catalogmenu_section[index] = section;
3011 }
3012
3013 int get_cat_section() {
3014 int index = get_cat_index();
3015 if (index != -1)
3016 return catalogmenu_section[index];
3017 else
3018 return CATSECT_TOP;
3019 }
3020
3021 void move_cat_row(int direction) {
3022 int index = get_cat_index();
3023 if (index == -1)
3024 return;
3025 if (direction == 0)
3026 catalogmenu_row[index] = 0;
3027 else if (direction == -1) {
3028 catalogmenu_row[index]--;
3029 if (catalogmenu_row[index] < 0)
3030 catalogmenu_row[index] = catalogmenu_rows[index] - 1;
3031 } else {
3032 catalogmenu_row[index]++;
3033 if (catalogmenu_row[index] >= catalogmenu_rows[index])
3034 catalogmenu_row[index] = 0;
3035 }
3036 }
3037
3038 void set_cat_row(int row) {
3039 int index = get_cat_index();
3040 if (index == -1)
3041 return;
3042 catalogmenu_row[index] = row;
3043 }
3044
3045 int get_cat_row() {
3046 int index = get_cat_index();
3047 if (index == -1)
3048 return 0;
3049 else
3050 return catalogmenu_row[index];
3051 }
3052
3053 int get_cat_item(int menukey) {
3054 int index = get_cat_index();
3055 if (index == -1)
3056 return -1;
3057 else
3058 return catalogmenu_item[index][menukey];
3059 }
3060
3061 void update_catalog() {
3062 int *the_menu;
3063 if (mode_commandmenu != MENU_NONE)
3064 the_menu = &mode_commandmenu;
3065 else if (mode_alphamenu != MENU_NONE)
3066 the_menu = &mode_alphamenu;
3067 else if (mode_transientmenu != MENU_NONE)
3068 the_menu = &mode_transientmenu;
3069 else if (mode_plainmenu != MENU_NONE)
3070 the_menu = &mode_plainmenu;
3071 else if (mode_appmenu != MENU_NONE)
3072 the_menu = &mode_appmenu;
3073 else
3074 return;
3075 if (*the_menu != MENU_CATALOG)
3076 return;
3077 switch (get_cat_section()) {
3078 case CATSECT_TOP:
3079 case CATSECT_FCN:
3080 case CATSECT_EXT_1:
3081 case CATSECT_EXT_TIME:
3082 case CATSECT_EXT_XFCN:
3083 case CATSECT_EXT_BASE:
3084 case CATSECT_EXT_PRGM:
3085 case CATSECT_EXT_STR:
3086 case CATSECT_EXT_STK:
3087 case CATSECT_EXT_2:
3088 case CATSECT_EXT_STAT:
3089 case CATSECT_EXT_MISC:
3090 case CATSECT_EXT_0_CMP:
3091 case CATSECT_EXT_X_CMP:
3092 return;
3093 case CATSECT_PGM:
3094 case CATSECT_PGM_ONLY:
3095 break;
3096 case CATSECT_REAL:
3097 if (!vars_exist(CATSECT_REAL))
3098 set_cat_section(CATSECT_TOP);
3099 break;
3100 case CATSECT_CPX:
3101 if (!vars_exist(CATSECT_CPX))
3102 set_cat_section(CATSECT_TOP);
3103 break;
3104 case CATSECT_MAT:
3105 if (!vars_exist(CATSECT_MAT))
3106 set_cat_section(CATSECT_TOP);
3107 break;
3108 case CATSECT_MAT_LIST:
3109 if (!vars_exist(CATSECT_MAT_LIST))
3110 set_cat_section(CATSECT_TOP);
3111 break;
3112 case CATSECT_LIST:
3113 if (!vars_exist(CATSECT_LIST))
3114 set_cat_section(CATSECT_TOP);
3115 break;
3116 case CATSECT_REAL_ONLY:
3117 if (!vars_exist(CATSECT_REAL)) {
3118 *the_menu = MENU_NONE;
3119 redisplay();
3120 return;
3121 }
3122 break;
3123 case CATSECT_MAT_ONLY:
3124 if (!vars_exist(CATSECT_MAT)) {
3125 *the_menu = MENU_NONE;
3126 redisplay();
3127 return;
3128 }
3129 break;
3130 case CATSECT_MAT_LIST_ONLY:
3131 if (!vars_exist(CATSECT_MAT_LIST)) {
3132 *the_menu = MENU_NONE;
3133 redisplay();
3134 return;
3135 }
3136 break;
3137 case CATSECT_LIST_STR_ONLY:
3138 if (!vars_exist(CATSECT_LIST_STR_ONLY)) {
3139 *the_menu = MENU_NONE;
3140 redisplay();
3141 return;
3142 }
3143 break;
3144 case CATSECT_LIST_ONLY:
3145 if (!vars_exist(CATSECT_LIST)) {
3146 *the_menu = MENU_NONE;
3147 redisplay();
3148 return;
3149 }
3150 break;
3151 case CATSECT_VARS_ONLY:
3152 if (!vars_exist(-1)) {
3153 *the_menu = MENU_NONE;
3154 redisplay();
3155 return;
3156 }
3157 break;
3158 case CATSECT_PGM_SOLVE:
3159 case CATSECT_PGM_INTEG:
3160 case CATSECT_PGM_MENU:
3161 if (!mvar_prgms_exist()) {
3162 *the_menu = MENU_NONE;
3163 display_error(ERR_NO_MENU_VARIABLES);
3164 redisplay();
3165 return;
3166 }
3167 break;
3168 }
3169 draw_catalog();
3170 }
3171
3172 void clear_custom_menu() {
3173 int row, key;
3174 for (row = 0; row < 3; row++)
3175 for (key = 0; key < 6; key++)
3176 custommenu_length[row][key] = 0;
3177 }
3178
3179 void assign_custom_key(int keynum, const char *name, int length) {
3180 int row = (keynum - 1) / 6;
3181 int key = (keynum - 1) % 6;
3182 int i;
3183 custommenu_length[row][key] = length;
3184 for (i = 0; i < length; i++)
3185 custommenu_label[row][key][i] = name[i];
3186 }
3187
3188 void get_custom_key(int keynum, char *name, int *length) {
3189 int row = (keynum - 1) / 6;
3190 int key = (keynum - 1) % 6;
3191 string_copy(name, length, custommenu_label[row][key],
3192 custommenu_length[row][key]);
3193 }
3194
3195 void clear_prgm_menu() {
3196 int i;
3197 for (i = 0; i < 9; i++)
3198 progmenu_arg[i].type = ARGTYPE_NONE;
3199 for (i = 0; i < 6; i++)
3200 progmenu_length[i] = 0;
3201 }
3202
3203 void assign_prgm_key(int keynum, bool is_gto, const arg_struct *arg) {
3204 int length, i;
3205 keynum--;
3206 progmenu_arg[keynum] = (arg_struct) *arg;
3207 progmenu_is_gto[keynum] = is_gto;
3208 length = reg_alpha_length;
3209 if (keynum < 6) {
3210 if (length > 7)
3211 length = 7;
3212 for (i = 0; i < length; i++)
3213 progmenu_label[keynum][i] = reg_alpha[i];
3214 progmenu_length[keynum] = length;
3215 }
3216 }
3217
3218 void do_prgm_menu_key(int keynum) {
3219 int err, oldprgm;
3220 int4 oldpc;
3221 keynum--;
3222 if (keynum == 8)
3223 set_menu(MENULEVEL_PLAIN, MENU_NONE);
3224 if (progmenu_arg[keynum].type == ARGTYPE_NONE) {
3225 if (keynum < 6)
3226 pending_command = CMD_NULL;
3227 else if (keynum == 8)
3228 pending_command = CMD_CANCELLED;
3229 return;
3230 }
3231 if ((flags.f.trace_print || flags.f.normal_print) && flags.f.printer_exists)
3232 print_command(progmenu_is_gto[keynum] ? CMD_GTO : CMD_XEQ,
3233 &progmenu_arg[keynum]);
3234 oldprgm = current_prgm;
3235 oldpc = pc;
3236 set_running(true);
3237 progmenu_arg[keynum].target = -1; /* Force docmd_gto() to search */
3238 err = docmd_gto(&progmenu_arg[keynum]);
3239 if (err != ERR_NONE) {
3240 set_running(false);
3241 display_error(err);
3242 flush_display();
3243 return;
3244 }
3245 if (!progmenu_is_gto[keynum]) {
3246 err = push_rtn_addr(oldprgm, oldpc == -1 ? 0 : oldpc);
3247 if (err != ERR_NONE) {
3248 current_prgm = oldprgm;
3249 pc = oldpc;
3250 set_running(false);
3251 display_error(err);
3252 flush_display();
3253 return;
3254 } else
3255 save_csld();
3256 }
3257 }