Debian Codegraph @

mac.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 /* (C) 1986, 1987, 1988 Ken Mitchum. This code is intended only for use with Jove. */
9
10 /* This code is not needed for MacOS X onwards, i.e any modern Mac,
11 * on which Jove's classic UNIX/POSIX code works well, as of 4.17.
12 * All #ifdef MAC is will be removed in a future version of Jove,
13 * unless someone volunteers to test it.
14 *
15 * This code was last tested since 1995 December, when D. Hugh Redelmeier
16 * hacked on the code to make it work on Think C 5.0 under System 7.1.
17 *
18 * Obligatory excuses:
19 * - Hugh is not a Mac expert
20 * - Think C 5.0 is quite obsolete (1991)
21 * - The only goal was to get the code working, not working well.
22 *
23 * Known issues:
24 * - the keyboard routines were designed for the Mac Plus keyboard.
25 * + "Command" is taken as "Control" and ` is taken as ESC.
26 * + There should be support for a distinct Command keymap
27 * with Mac-like default bindings.
28 * - "macify" ought to be extended to find-file and perhaps
29 * other commands
30 * - perhaps there are newer MacOS facilities that ought to be
31 * exploited. Apple Events?
32 * - the hacky way the command keystrokes are described in About Jove
33 * ought to be improved.
34 * - Highlighting ought to be supported.
35 * - Mouse support ought to be better. For example, selecting text
36 * ought to be at least as well done as under XTerm!
37 * - [supposition] Because double-clicking is supported, nothing
38 * is done for a single click until the double-click timeout
39 * happens. Since the double-click action is a superset of
40 * the single-click action, the single click action ought to
41 * be immediately performed and then augmented if a double-click
42 * happens.
43 * - see also comments containing ???
44 */
45
46 #include "tune.h"
47
48 #ifdef MAC /* the body is the rest of this file */
49
50 #include "jove.h"
51
52 #include <Controls.h>
53 #include <Desk.h>
54 #include <Dialogs.h>
55 #include <Errors.h>
56 #include <Events.h>
57 #include <Files.h>
58 #include <Fonts.h>
59 #include <Lists.h>
60 #include <LoMem.h>
61 #include <Menus.h>
62 #include <Quickdraw.h>
63 #include <Resources.h>
64 #include <SegLoad.h>
65 #include <StandardFile.h>
66 #include <ToolUtils.h>
67 #include <Types.h>
68 #include <Windows.h>
69
70 #include <pascal.h>
71
72 #include "mac.h"
73 #include "ask.h"
74 #include "chars.h"
75 #include "disp.h"
76 #include "extend.h"
77 #include "fp.h" /* for flushscreen() */
78 #include "commands.h"
79 #include "fmt.h"
80 #include "marks.h"
81 #include "misc.h"
82 #include "move.h"
83 #include "screen.h"
84 #include "scandir.h"
85 #include "term.h"
86 #include "vars.h"
87 #include "version.h"
88 #include "wind.h"
89
90 extern struct menu Menus[NMENUS]; /* from menumaps.txt => menumaps.c */
91
92 private EventRecord the_Event;
93
94 private void SetBounds proto((void));
95 private void Set_std proto((void));
96 private void Reset_std proto((void));
97 private jbool findtext proto((void));
98
99
100 /* tn.h Modified for variable screen size 11/21/87. K. Mitchum */
101
102 #define SCREENSIZE (wc->w_rows * ROWSIZE)
103 #define FONT monaco
104 #define TEXTSIZE 9
105
106 #define HEIGHT 11
107 #define WIDTH 6
108 #define DESCENT 2
109 #define TWIDTH CO * WIDTH
110 #define THEIGHT LI * HEIGHT
111
112 /* window specs */
113
114 #define SCROLLWIDTH 16 /* width of scroll bar control in pixels */
115 #define WINDWIDTH (wc->w_width - SCROLLWIDTH + 1) /* local coordinates */
116 #define WINDHEIGHT (wc->w_height) /* local coordinates */
117 #define MAXROW ILI
118 #define MAXCOL (CO - 1)
119
120
121 /* for keyboard routines */
122 #define MCHARS 32 /* length of circular buffer -- must be a power of two */
123 #define NCHMASK (MCHARS - 1) /* mask for modulo MCHARS */
124
125
126 /***************************************************/
127
128 private void
129 putcurs proto((unsigned row, unsigned col, jbool vis)),
130 curset proto((jbool desired)),
131 dellines proto((int n, int bot)),
132 inslines proto((int n, int bot));
133
134 private Rect LimitRect; /* bounds we can't move past */
135
136 struct wind_config {
137 int w_width; /* pixel width of the Mac window */
138 int w_height;
139 int w_rows; /* rows of characters which fit the window */
140 int w_cols;
141 } wc_std, wc_user, *wc;
142
143 private WindowPtr theScreen;
144
145 jbool
146 Windchange,
147 EventCmd,
148 Keyonly,
149 Bufchange,
150 Modechange;
151
152 jbool
153 Macmode = NO; /* VAR: use Mac file selector */
154
155 /* Initialization Routines. */
156
157 void
158 getTERM()
159 {
160 }
161
162 /* For each binding, mark the command with the binding.
163 * We use it for "About Jove ...".
164 * ??? This is faster than using find_binds, but it has problems:
165 * - it only notes the last binding of each command
166 * - it only reflects the three listed keymaps
167 * - it requires a wart on struct cmd
168 * - it is MAC-only
169 */
170
171 private void
172 InitMapBinds(km, kmc)
173 data_obj **km;
174 char kmc;
175 {
176 ZXchar i;
177
178 for (i = 0; i < NCHARS; i++) {
179 if (*km != NULL && obj_type(*km) == COMMAND) {
180 struct cmd *c = (struct cmd *) *km;
181
182 c->c_map = kmc;
183 c->c_key = i;
184 }
185 km += 1;
186 }
187 }
188
189 private void
190 InitBinds()
191 {
192 InitMapBinds(MainKeys, F_MAINMAP);
193 InitMapBinds(EscKeys, F_PREF1MAP);
194 InitMapBinds(CtlxKeys, F_PREF2MAP);
195 }
196
197 private WindowPtr window;
198 private Rect r;
199 private CursHandle cross;
200
201 private void InitSysMenu proto((void));
202
203 void
204 InitEvents()
205 {
206 window = theScreen;
207 InitSysMenu();
208 SetRect(&r, window->portRect.left,
209 window->portRect.top,
210 window->portRect.right - SCROLLWIDTH,
211 window->portRect.bottom - SCROLLWIDTH);
212 cross = GetCursor(crossCursor);
213 }
214
215 private void tn_init proto((void));
216 private int getdir proto((void));
217
218 void
219 MacInit()
220 {
221 tn_init();
222 getdir();
223 strcpy(TmpDir, gethome());
224 strcpy(ShareDir, TmpDir);
225 InitBinds();
226 }
227
228 void
229 ttysetattr(n)
230 jbool n; /* also used as subscript! */
231 {
232 }
233
234 /* Surrogate unix-style file i/o routines for Jove. These replace the
235 * routines distributed in the libraries. They work with Jove, but may
236 * not be general enough for other purposes.
237 */
238
239 #define NFILES 10
240
241 private int cur_vol; /* Disk or volume number */
242 private long cur_dir; /* Directory number */
243 private int cur_vref; /* ugh.. Vref for volume + directory */
244
245 struct ftab {
246 jbool inuse;
247 int refnum; /* Mac file reference number */
248 } ft[NFILES];
249
250 private void
251 fsetup(p)
252 HParmBlkPtr p;
253 {
254 byte_zero(p, sizeof(HParamBlockRec));
255 p->fileParam.ioVRefNum = cur_vol;
256 p->fileParam.ioDirID = cur_dir;
257 /* p->fileParam.ioFVersNum = 0; */
258 }
259
260 private void
261 isetup(p)
262 IOParam *p;
263 {
264 byte_zero(p, sizeof(IOParam));
265 p->ioVRefNum = cur_vol;
266 }
267
268
269 /* Kludge to convert Macintosh error codes to something like Unix. */
270
271 private int
272 cvt_err(err) /* some of these don't make sense... */
273 int err;
274 {
275 switch(err) {
276 case noErr:
277 errno = 0;
278 return 0;
279 case dirFulErr:
280 case dskFulErr:
281 errno = ENOSPC;
282 break;
283 /* case nsvErr: */
284 /* case mFulErr: */
285 /* case tmfoErr: */
286 /* case fnfErr: */
287 default:
288 errno = ENOENT;
289 break;
290 case ioErr:
291 errno = EIO;
292 break;
293 case bdNamErr:
294 case opWrErr:
295 case paramErr:
296 errno = EINVAL;
297 break;
298 case fnOpnErr: /* dubious... */
299 case rfNumErr:
300 errno = EBADF;
301 break;
302 case eofErr: /* ditto */
303 case posErr:
304 errno = /* no longer defined: ESPIPE */ EIO;
305 break;
306 case wPrErr:
307 errno = EROFS;
308 break;
309 case fLckdErr:
310 case permErr:
311 errno = EACCES;
312 break;
313 case fBsyErr:
314 errno = EBUSY;
315 break;
316 case dupFNErr:
317 errno = EEXIST;
318 break;
319 case gfpErr:
320 case volOffLinErr:
321 case volOnLinErr:
322 case nsDrvErr:
323 errno = ENODEV;
324 break;
325 case noMacDskErr:
326 case extFSErr:
327 errno = EIO;
328 break;
329 case fsRnErr:
330 case badMDBErr:
331 case wrPermErr:
332 errno = /* no longer defined: EPERM */ EACCES;
333 break;
334 }
335 return -1;
336 }
337
338 private StringPtr
339 cvt_fnm(file)
340 const char *file;
341 {
342 static char nm[255];
343 char *t;
344
345 if (*file == '/') {
346 strcpy(nm, file + 1); /* full path */
347 } else {
348 if (strchr(file + 1, '/') != NULL)
349 strcpy(nm, "/"); /* make a partial pathname */
350 else
351 nm[0] = '\0';
352 strcat(nm, file);
353 }
354 for (t = nm; (t = strchr(t, '/')) != NULL; )
355 *t++ = ':';
356 return CtoPstr(nm);
357 }
358
359 private int do_creat proto((HParmBlkPtr p, StringPtr nm));
360
361 int
362 creat(name, perm)
363 const char *name;
364 jmode_t perm; /* perm is irrelevant on a Mac */
365 {
366 int fd, err;
367 StringPtr nm;
368 HParamBlockRec p;
369
370 nm = cvt_fnm(name); /* convert filename to Mac type name */
371 for (fd = 0; ft[fd].inuse; fd++) {
372 if (fd == NFILES-1) {
373 errno = EMFILE;
374 return -1;
375 }
376 }
377 fsetup(&p); /* try to delete it, whether it is there or not. */
378 p.fileParam.ioNamePtr = nm;
379 if ((err = PBHDelete(&p, 0)) != noErr && err != fnfErr)
380 return cvt_err(err);
381
382 if (do_creat(&p, nm) != 0)
383 return -1;
384
385 ft[fd].inuse = YES;
386 ft[fd].refnum = p.ioParam.ioRefNum;
387 return fd + 1;
388 }
389
390 #ifdef USE_PROTOTYPES
391 int
392 open(const char *path, int flags, ...)
393 #else
394 int
395 open(path, flags)
396 const char *path;
397 int flags;
398 #endif
399 {
400 int fd, err;
401 StringPtr nm;
402 HParamBlockRec p;
403
404 nm = cvt_fnm(path); /* convert filename to Mac type name */
405 for (fd = 0; ft[fd].inuse; fd++) {
406 if (fd == NFILES-1) {
407 errno = EMFILE;
408 return -1;
409 }
410 }
411 fsetup(&p);
412 switch (flags & 03) {
413 case O_RDONLY:
414 p.ioParam.ioPermssn = fsRdPerm;
415 break;
416 case O_WRONLY:
417 p.ioParam.ioPermssn = fsWrPerm;
418 break;
419 case O_RDWR:
420 p.ioParam.ioPermssn = fsRdWrPerm;
421 break;
422 }
423 p.ioParam.ioNamePtr = nm;
424 p.ioParam.ioMisc = 0;
425 if ((err = PBHOpen(&p, 0)) != noErr)
426 return cvt_err(err);
427
428 ft[fd].refnum = p.ioParam.ioRefNum;
429 p.ioParam.ioPosMode = fsFromStart;
430 p.ioParam.ioPosOffset = 0;
431 if ((err = PBSetFPos((ParamBlockRec *) &p, 0)) != noErr)
432 return cvt_err(err);
433
434 ft[fd].inuse = YES;
435 errno = 0;
436 return fd + 1;
437 }
438
439 private int
440 do_creat(p, nm)
441 HParmBlkPtr p;
442 StringPtr nm;
443 {
444 int err;
445
446 fsetup(p);
447 p->fileParam.ioNamePtr = nm;
448 if ((err = PBHCreate(p, 0)) != noErr)
449 return cvt_err(err);
450
451 fsetup(p);
452 p->fileParam.ioNamePtr = nm;
453 p->fileParam.ioFDirIndex = 0;
454 if ((err = PBHGetFInfo(p, 0)) != noErr)
455 return cvt_err(err);
456
457 p->fileParam.ioDirID = cur_dir;
458 p->fileParam.ioFlFndrInfo.fdType = 'TEXT';
459 p->fileParam.ioFlFndrInfo.fdCreator = 'JV01';
460 p->fileParam.ioFlFndrInfo.fdFlags = 0;
461 p->fileParam.ioFVersNum = 0;
462 if ((err = PBHSetFInfo(p, 0)) != noErr)
463 return cvt_err(err);
464
465 fsetup(p);
466 p->ioParam.ioNamePtr = nm;
467 p->ioParam.ioPermssn = fsRdWrPerm;
468 p->ioParam.ioMisc = 0;
469 if (cvt_err(PBHOpen(p, 0)))
470 return -1;
471
472 return 0;
473 }
474
475
476 int
477 close(fd)
478 int fd;
479 {
480 int err;
481 HParamBlockRec p;
482
483 if (!ft[--fd].inuse) {
484 errno = EBADF;
485 return -1;
486 }
487
488 fsetup(&p);
489 p.ioParam.ioRefNum = ft[fd].refnum;
490 ft[fd].inuse = NO;
491 if (cvt_err(PBClose((ParamBlockRec *) &p, 0)) < 0)
492 return -1;
493
494 fsetup(&p);
495 p.ioParam.ioNamePtr = NULL;
496 if (cvt_err(PBFlushVol((ParamBlockRec *) &p, 0)) < 0)
497 return -1;
498
499 return 0;
500 }
501
502 private JSSIZE_T con_read proto((char *buf, size_t size));
503
504 /* Raw UNIX-like read */
505
506 JSSIZE_T
507 read(fd, ubuf, n)
508 int fd;
509 UnivPtr ubuf;
510 size_t n;
511 {
512 char *buf = ubuf; /* char * is more useful */
513 int err;
514 IOParam p;
515
516 if (fd == 0) {
517 return con_read(buf, n);
518 } else {
519 if (!ft[--fd].inuse) {
520 errno = EBADF;
521 return -1;
522 }
523 isetup(&p);
524 p.ioRefNum = ft[fd].refnum;
525 p.ioBuffer = buf;
526 p.ioReqCount = n;
527 p.ioPosMode = fsFromMark;
528 p.ioPosOffset = 0;
529 if ((err = PBRead((ParamBlockRec *)&p, 0)) != noErr && err != eofErr)
530 return cvt_err(err);
531
532 errno = 0;
533 return p.ioActCount;
534 }
535 }
536
537 /* Raw UNIX-like write */
538
539 JSSIZE_T
540 write(fd, ubuf, n)
541 int fd;
542 UnivConstPtr ubuf;
543 size_t n;
544 {
545 const char *buf = ubuf; /* char * is more convenient */
546
547 if (fd == 0) {
548 writetext((unsigned char *)buf, n);
549 return n;
550 } else {
551 IOParam p;
552 int err;
553 const char *ebuf = buf + n;
554
555 if (!ft[--fd].inuse) {
556 errno = EBADF;
557 return -1;
558 }
559 isetup(&p);
560 p.ioRefNum = ft[fd].refnum;
561 p.ioPosMode = fsFromMark;
562 p.ioReqCount = n;
563 p.ioBuffer = (Ptr)buf;
564 p.ioPosOffset = 0L; /* bidirectional */
565 if ((err = PBWrite((ParamBlockRec *)&p, 0)) != noErr)
566 return cvt_err(err);
567
568 return p.ioActCount;
569 }
570 }
571
572 long
573 lseek(fd, offset, whence)
574 int fd;
575 off_t offset;
576 int whence;
577 {
578 int err;
579 long cur_mark, leof, new_mark;
580 IOParam p;
581
582 if (!ft[--fd].inuse) {
583 errno = EBADF;
584 return -1;
585 }
586
587 isetup(&p);
588 p.ioRefNum = ft[fd].refnum;
589 if ((err = PBGetFPos((ParamBlockRec *)&p, 0)) != noErr)
590 return cvt_err(err);
591
592 cur_mark = p.ioPosOffset;
593 isetup(&p);
594 p.ioRefNum = ft[fd].refnum;
595 if ((err = PBGetEOF((ParamBlockRec *)&p, 0)) != noErr)
596 return cvt_err(err);
597
598 leof = (long) p.ioMisc;
599 switch(whence) {
600 case 0:
601 new_mark = offset;
602 break;
603 case 1:
604 new_mark = offset + cur_mark;
605 break;
606 case 2:
607 new_mark = offset + leof;
608 break;
609 default:
610 errno = EINVAL;
611 return -1;
612 }
613 if (new_mark > leof) {
614 /* need more space in file -- grow it */
615 isetup(&p);
616 p.ioRefNum = ft[fd].refnum;
617 p.ioMisc = (Ptr) new_mark;
618 if ((err = PBSetEOF((ParamBlockRec *)&p, 0)) != noErr)
619 return cvt_err(err);
620 }
621 isetup(&p);
622 p.ioRefNum = ft[fd].refnum;
623 p.ioPosOffset = new_mark;
624 p.ioPosMode = fsFromStart;
625 if ((err = PBSetFPos((ParamBlockRec *)&p, 0)) != noErr)
626 return cvt_err(err);
627
628 errno = 0;
629 return p.ioPosOffset;
630 }
631
632 /* delete file, if it exists */
633
634 int
635 unlink(name)
636 const char *name;
637 {
638 int fd, err;
639 HParamBlockRec p;
640
641 fsetup(&p);
642 p.fileParam.ioNamePtr = cvt_fnm(name);
643 if ((err = PBHDelete(&p, 0)) != noErr && err != fnfErr)
644 return cvt_err(err);
645
646 return 0;
647 }
648
649 /* Console read routine */
650
651 private ZXchar rawgetc proto((void));
652
653 private JSSIZE_T
654 con_read(buf, size)
655 char *buf;
656 size_t size;
657 {
658 size_t n;
659 ZXchar p;
660
661
662 n = 0;
663 do {
664 p = rawgetc();
665 *buf++ = p;
666 n++;
667 } while (rawchkc() && n <= size);
668 return n;
669 }
670
671 void
672 dobell(n) /* declared in term.h */
673 int n;
674 {
675 while (--n >= 0)
676 SysBeep(5);
677 flushscreen();
678 }
679
680 /* Simplified stat() routine emulates what is needed most. */
681
682 int
683 stat(fname, buf)
684 const char *fname;
685 struct stat *buf;
686 {
687 CInfoPBRec p;
688 StringPtr nm;
689
690 nm = cvt_fnm(fname);
691 byte_zero(&p, sizeof(CInfoPBRec));
692 p.hFileInfo.ioCompletion = 0;
693 p.hFileInfo.ioNamePtr = nm;
694 p.hFileInfo.ioFVersNum = 0;
695 p.hFileInfo.ioFDirIndex = 0;
696 p.hFileInfo.ioVRefNum = cur_vol;
697 p.hFileInfo.ioDirID = cur_dir;
698
699 switch (PBGetCatInfo(&p, 0)) {
700 case noErr:
701 errno = 0;
702 buf->st_dev = p.hFileInfo.ioVRefNum + 1; /* don't want 0 */
703 buf->st_ino = p.hFileInfo.ioDirID;
704 buf->st_size = p.hFileInfo.ioFlLgLen;
705 buf->st_mtime = p.hFileInfo.ioFlMdDat;
706 buf->st_mode = (p.hFileInfo.ioFlAttrib & 0x10) ? S_IFDIR : S_IFREG;
707 return 0;
708 case nsvErr:
709 case paramErr:
710 case bdNamErr:
711 case fnfErr:
712 errno = ENOENT;
713 break;
714 case ioErr:
715 errno = EIO;
716 break;
717 default:
718 errno = ENOENT;
719 break;
720 }
721 return -1;
722 }
723
724 /* Directory related routines. Jove keeps track of the true Volume (disk)
725 * number and directory number, and avoids "Working Directory Reference
726 * Numbers", which are confusing.
727 */
728 private int
729 getdir() /* call this only once, during startup. */
730 {
731 WDPBRec p;
732
733 p.ioCompletion = 0;
734 p.ioNamePtr = NULL;
735 if (PBHGetVol(&p, 0) != noErr)
736 return -1; /* BIG trouble (but caller never checks returned value!) */
737
738 cur_vol = p.ioWDVRefNum;
739 cur_dir = p.ioWDDirID;
740 SFSaveDisk = 0 - cur_vol; /* these are for SF dialogs */
741 CurDirStore = cur_dir;
742 return 0;
743 }
744
745 private int
746 setdir(vol, dir)
747 int vol;
748 long dir;
749 {
750 WDPBRec p;
751
752 p.ioCompletion = 0;
753 p.ioNamePtr = NULL;
754 p.ioVRefNum = vol;
755 p.ioWDDirID = dir;
756 if (PBHSetVol(&p, 0) != noErr)
757 return -1;
758
759 cur_vol = vol;
760 cur_dir = dir;
761 SFSaveDisk = 0 - vol; /* these are for SF dialogs */
762 CurDirStore = dir;
763 return 0;
764 }
765
766 private jbool
767 lookupdir(dir, d)
768 const char *dir; /* UNIX-like pathname for directory */
769 CInfoPBPtr d; /* info from directory */
770 {
771 char
772 nm[FILESIZE + 1],
773 *t;
774
775 if (strcmp(dir, ".") == 0)
776 getcwd(nm, sizeof(nm) - 1);
777 else
778 strcpy(nm, dir);
779
780 for (t = nm; (t = strchr(t, '/')) != NULL; )
781 *t++ = ':';
782
783 t = nm; /* get rid of initial slashes */
784 while (*t == ':')
785 t++;
786
787 strcat(t, ":"); /* force trailing ':', signifying directory */
788
789 byte_zero(d, sizeof(*d));
790 /* d->dirInfo.ioCompletion = 0; */
791 d->dirInfo.ioNamePtr = CtoPstr(t);
792 d->dirInfo.ioVRefNum = cur_vol;
793 /* d->dirInfo.ioFDirIndex = 0; */
794 /* d->dirInfo.ioDrDirID = 0; */
795 PBGetCatInfo(d, 0);
796 return d->dirInfo.ioResult == noErr
797 && (d->dirInfo.ioFlAttrib & 0x10) != 0;
798 }
799
800 int
801 chdir(dir)
802 const char *dir;
803 {
804 CInfoPBRec d;
805
806 if (strcmp(dir, "/") == 0 /* There is no root... */
807 || !lookupdir(dir, &d)
808 || setdir(d.dirInfo.ioVRefNum, d.dirInfo.ioDrDirID) < 0)
809 return -1;
810
811 return 0;
812 }
813
814 /* Scandir returns the number of entries or -1 if the directory cannot
815 * be opened or malloc fails.
816 * Note: if we ever support RECOVER, this code will have to be moved
817 * to scandir.c
818 */
819 int
820 jscandir(dir, nmptr, qualify, sorter)
821 const char *dir;
822 char ***nmptr;
823 jbool (*qualify) ptrproto((char *));
824 int (*sorter) ptrproto((UnivConstPtr, UnivConstPtr));
825 {
826 long DirID;
827 char **ourarray;
828 unsigned int nalloc = 10,
829 nentries = 0,
830 index = 1;
831
832 if (strcmp(dir, "/") == 0) {
833 /* we are enumerating volumes */
834 DirID = 0;
835 } else {
836 /* we are enumerating the contents of a volume or directory */
837 CInfoPBRec d;
838
839 if (!lookupdir(dir, &d))
840 return -1;
841
842 DirID = d.dirInfo.ioDrDirID;
843 }
844
845 ourarray = (char **) emalloc(nalloc * sizeof (char *));
846 for (;;) {
847 Str32 name; /* 31 is limit, but we might add a '/' */
848
849 if (DirID == 0) {
850 /* we are enumerating volumes */
851 ParamBlockRec d;
852
853 byte_zero(&d, sizeof(d));
854 d.volumeParam.ioCompletion = 0;
855 d.volumeParam.ioNamePtr = name;
856 d.volumeParam.ioVRefNum = 0;
857 d.volumeParam.ioVolIndex = index++;
858 if (PBGetVInfo(&d, 0) != noErr)
859 break; /* we are done, then */
860
861 PtoCstr(name);
862 #ifdef DIRECTORY_ADD_SLASH
863 /* I *think* this has got to be a volume */
864 strcat((char *)name, "/");
865 #endif
866
867 } else {
868 /* we are enumerating the contents of a volume or directory */
869 CInfoPBRec d;
870
871 byte_zero(&d, sizeof(d));
872 d.dirInfo.ioCompletion = 0;
873 d.dirInfo.ioNamePtr = name;
874 d.dirInfo.ioVRefNum = cur_vol;
875 d.dirInfo.ioFDirIndex = index++;
876 d.dirInfo.ioDrDirID = DirID; /* .ioDirID == .ioDrDirID */
877 if (PBGetCatInfo(&d, 0) != noErr)
878 break; /* we are done, then */
879
880 PtoCstr(name);
881 #ifdef DIRECTORY_ADD_SLASH
882 if (d.dirInfo.ioFlAttrib & 0x10) /* see Inside Mac IV-122 */
883 strcat((char *)name, "/");
884 #endif
885 }
886 if (qualify != NULL && !(*qualify)((char *) name))
887 continue;
888
889 /* note: test ensures one space left in ourarray for NULL */
890 if (nentries+1 == nalloc)
891 ourarray = (char **) erealloc((char *) ourarray, (nalloc += 10) * sizeof (char *));
892 ourarray[nentries++] = copystr((char *)name);
893 }
894 ourarray[nentries] = NULL;
895
896 if (sorter != NULL)
897 qsort((char *) ourarray, nentries, sizeof (char **), sorter);
898 *nmptr = ourarray;
899
900 return nentries;
901 }
902
903
904 char *
905 getcwd(buf, size)
906 char *buf;
907 size_t size;
908 {
909 CInfoPBRec d;
910 Str31 nm;
911 char *p = buf + size; /* build from right */
912
913 if (p == buf)
914 return NULL; /* not even room for NUL */
915
916 *--p = '\0';
917
918 for (d.dirInfo.ioDrDirID = cur_dir; ; d.dirInfo.ioDrDirID = d.dirInfo.ioDrParID) {
919 d.dirInfo.ioCompletion = 0;
920 d.dirInfo.ioNamePtr = nm;
921 d.dirInfo.ioVRefNum = cur_vol;
922 d.dirInfo.ioFDirIndex = -1;
923 PBGetCatInfo(&d, 0);
924 if (d.dirInfo.ioResult != noErr)
925 return NULL;
926
927 if (p - buf <= Length(nm))
928 return NULL; /* insufficient room for / and name */
929
930 p -= Length(nm);
931 memcpy((UnivPtr)p, (UnivPtr) (nm+1), Length(nm));
932 *--p = '/';
933
934 if (d.dirInfo.ioDrDirID == 2)
935 break; /* home directory */
936 }
937 /* strcpy(buf, p) won't do because the destination overlaps the source */
938 memmove((UnivPtr)buf, (UnivPtr)p, buf + size - p); /* left justify */
939 return buf;
940 }
941
942 char *
943 gethome() /* this will be startup directory */
944 {
945 static char *ret = NULL;
946 char space[FILESIZE];
947
948 if (ret == NULL)
949 ret = copystr(getcwd(space, sizeof(space)));
950 return ret;
951 }
952
953
954
955 /* Routines that put up and manipulate the "About Jove" dialog. */
956
957
958 /* (ORIGINALLY IN) about_j.c. */
959
960
961 #define DLOGNAME "\pABOUT_JDLOG"
962
963 #define DONE_ITEM 1
964 #define LIST_ITEM 2
965
966
967 #define DWIDTH 460 /* there should be an easy way to get this */
968 #define DHEIGHT 240 /* from the resource file! */
969
970 WindowPtr makedisplay();
971 ListHandle makelist();
972
973
974 private WindowPtr theWindow;
975 private ListHandle theList;
976 private Rect theListRect;
977 private EventRecord theEvent;
978
979
980
981 private void
982 do_list proto((void)),
983 do_events proto((void));
984
985 private WindowPtr
986 makedisplay proto((void));
987
988 private ListHandle
989 makelist proto((void));
990
991 private void
992 about_j()
993 {
994 WindowPtr OldWindow;
995
996 GetPort(&OldWindow);
997
998 if ((theWindow = makedisplay()) == 0)
999 return;
1000
1001 SetPort(theWindow);
1002 if (theList = makelist()) {
1003 LActivate(1, theList);
1004 do_list();
1005 ShowWindow(theWindow);
1006 do_events();
1007 }
1008 SetPort(OldWindow);
1009 LDispose(theList);
1010 DisposDialog(theWindow);
1011 }
1012
1013
1014 private WindowPtr
1015 makedisplay()
1016 {
1017 static short dlogid = 0;
1018
1019 DialogPtr theDialog;
1020 Handle theHandle;
1021 Handle theResource;
1022 Str255 buf;
1023 ResType resType;
1024 short itemType;
1025 Rect theRect;
1026 short dh, dv; /* to center dialog on the screen */
1027 Str255 nostring;
1028
1029 if (dlogid == 0) {
1030 if ((theResource = GetNamedResource('DLOG', DLOGNAME)) == NULL)
1031 return (WindowPtr)NULL;
1032
1033 itemType = 'DLOG';
1034 GetResInfo(theResource, &dlogid, &resType, buf);
1035 }
1036
1037 theDialog = GetNewDialog(dlogid, 0L, (WindowPtr) -1);
1038 nostring[0] = 0; /* set length of Pascal String to 0 */
1039 ParamText(
1040 "\pMacJove - Copyright (C) 1986-1999 J. Payne, K. Gegenfurtner,",
1041 "\pK. Mitchum. Portions (C) THINK Technologies, Inc.",
1042 nostring, nostring);
1043
1044 dh = qd.screenBits.bounds.left + (qd.screenBits.bounds.right - DWIDTH) / 2;
1045 dv = qd.screenBits.bounds.top + (qd.screenBits.bounds.bottom - DHEIGHT) / 2;
1046 MoveWindow((WindowPtr)theDialog, dh, dv, 0);
1047 ShowWindow((WindowPtr)theDialog);
1048
1049
1050 GetDItem(theDialog, LIST_ITEM, &itemType, &theHandle, &theRect);
1051 theListRect = theRect;
1052 theListRect.right -= 15;
1053 ((WindowPtr)theDialog)->txFont = FONT;
1054 ((WindowPtr)theDialog)->txSize = TEXTSIZE;
1055
1056 return (WindowPtr) theDialog;
1057 }
1058
1059 private void
1060 do_display() /* draw necessary controls, lines */
1061 {
1062 Rect rViewF; /* framing rect for list */
1063 int offset;
1064
1065 rViewF = theListRect;
1066
1067 rViewF.left--;
1068 rViewF.top--;
1069 rViewF.right++;
1070 rViewF.bottom++;
1071 FrameRect(&rViewF);
1072
1073 DrawControls(theWindow);
1074 }
1075
1076 private ListHandle
1077 makelist()
1078 {
1079 Point csize;
1080 Rect dataBounds, rView; /* list boundaries */
1081
1082 csize.h = csize.v = 0;
1083 SetRect(&dataBounds, 0, 0, 1, 0);
1084 return LNew(&theListRect, &dataBounds, csize, 0, theWindow, 0, 0, 0, 1);
1085 }
1086
1087 private void
1088 printbind(f, buf)
1089 const struct cmd *f;
1090 char *buf;
1091 {
1092 char c;
1093
1094 if (f->c_map == 0 || (c = f->c_key) == 0x7f) {
1095 strcpy(buf, " ");
1096 return;
1097 }
1098 switch(f->c_map) {
1099 case F_MAINMAP:
1100 strcpy(buf, " ");
1101 break;
1102
1103 case F_PREF1MAP:
1104 strcpy(buf, " ESC ");
1105 break;
1106
1107 case F_PREF2MAP:
1108 strcpy(buf, " ^X ");
1109 break;
1110 }
1111 if (c < ' ') {
1112 buf[5] = '^'; /* control char */
1113 c |= 0x40;
1114 } else {
1115 buf[5] = ' ';
1116 }
1117 if ('a' <= c && c <= 'z')
1118 c &= 0x5f;
1119 buf[6] = c;
1120 buf[7] = ' ';
1121 buf[8] = '\0';
1122 }
1123
1124 private void
1125 do_list()
1126 {
1127 int row, col;
1128 const struct cmd *f;
1129 char buf[255];
1130 Point theCell;
1131
1132 theCell.h = 0;
1133
1134 for (f = commands, row = 0; f->Name; f++, row++) {
1135 LAddRow(1, row, theList);
1136 theCell.v = row;
1137
1138 printbind(f, buf);
1139 strcat(buf, f->Name);
1140 LSetCell(buf, strlen(buf), theCell, theList);
1141 }
1142 }
1143
1144
1145
1146 private pascal Boolean
1147 ProcFilter(theDialog, event, itemHit)
1148 DialogPtr theDialog;
1149 EventRecord *event;
1150 short *itemHit;
1151 {
1152 theEvent = *event;
1153 if (theEvent.what == keyDown && theEvent.message & charCodeMask == '\r') {
1154 *itemHit = 1;
1155 return YES;
1156 }
1157 if (theEvent.what == activateEvt && (WindowPtr) theEvent.message == theWindow) {
1158 LDoDraw(1, theList);
1159 LActivate(1, theList);
1160 }
1161 if (theEvent.what == updateEvt && (WindowPtr) theEvent.message == theWindow) {
1162 BeginUpdate(theWindow);
1163 do_display();
1164 DrawDialog(theWindow);
1165 LUpdate(theWindow->visRgn, theList);
1166 EndUpdate(theWindow);
1167 }
1168
1169 return NO;
1170 }
1171
1172
1173 void
1174 do_events()
1175 {
1176 short item;
1177 jbool done = NO;
1178 Point p;
1179
1180 while (!done) {
1181 ModalDialog(ProcFilter, &item);
1182 switch(item) {
1183 case DONE_ITEM:
1184 done = YES;
1185 /* ??? fall through? -- DHR */
1186 case LIST_ITEM:
1187 p = theEvent.where;
1188 GlobalToLocal(&p);
1189 LClick(p, theEvent.modifiers, theList);
1190 break;
1191 }
1192 }
1193 }
1194
1195 /* Window and Control related routines. */
1196
1197 /* (ORIGINALLY IN) tcon.c.
1198 * control handler routines for Jove. K. Mitchum 12/86
1199 */
1200
1201 #define MINC 0
1202 #define MAXC 100
1203 #define INITC 0
1204 #define EVENTLIST (mDownMask | keyDownMask )
1205
1206 private Point p;
1207 private jbool wc_adjust proto((int, int, struct wind_config *, int));
1208
1209 private void
1210 MakeScrollBar proto((Window *w)),
1211 AdjustScrollBar proto((Window *w)),
1212 drawfluff proto((void));
1213
1214 void
1215 docontrols() /* called from redisplay routines */
1216 {
1217 Window *w;
1218 int top;
1219
1220 w = fwind;
1221 top = 0;
1222 do {
1223 if (w->w_control != NULL)
1224 HideControl(w->w_control);
1225 w = w->w_next;
1226 } while (w != fwind);
1227 w = fwind;
1228 do {
1229 w->w_topline = top;
1230 if (w->w_control != NULL)
1231 AdjustScrollBar(w);
1232 else
1233 MakeScrollBar(w);
1234 ShowControl(w->w_control);
1235 top += w->w_height;
1236 w = w->w_next;
1237 } while (w != fwind);
1238 Windchange = NO;
1239 drawfluff();
1240 }
1241
1242
1243 private void
1244 MakeScrollBar(w) /* set up control */
1245 Window *w;
1246 {
1247 Rect BarRect;
1248 int wheight, wtop;
1249 WindowPtr window = theScreen;
1250
1251 wheight = w->w_height;
1252 wtop = w->w_topline;
1253 SetRect(&BarRect, window->portRect.right - SCROLLWIDTH + 1,
1254 window->portRect.top -2 + wtop * HEIGHT,
1255 window->portRect.right +1,
1256 window->portRect.top + ((wheight + wtop) * HEIGHT + 1));
1257 w->w_control = NewControl(window, &BarRect, "\psbar", 1, INITC,
1258 MINC, MAXC, scrollBarProc, (long)w);
1259 }
1260
1261 private void
1262 AdjustScrollBar(w) /* redo existing control */
1263 Window *w;
1264 {
1265 ControlHandle handle = w->w_control;;
1266
1267 if (handle != NULL) {
1268 int wtop = w->w_topline;
1269 int wheight = w->w_height;
1270 WindowPtr window = (*handle)->contrlOwner;
1271
1272 SizeControl(handle, SCROLLWIDTH, wheight * HEIGHT + 1);
1273
1274 MoveControl(handle, window->portRect.right - SCROLLWIDTH + 1,
1275 window->portRect.top - 1 + wtop * HEIGHT);
1276 }
1277 }
1278
1279 private int ltoc proto((void)); /* calculate ctlvalue for line position */
1280
1281 void
1282 SetScrollBar(w) /* set value of the bar */
1283 Window *w;
1284 {
1285 SetCtlValue(w->w_control, ltoc());
1286 }
1287
1288 private void
1289 drawfluff() /* draw controls and dividers */
1290 {
1291 Window *w = fwind;
1292
1293 DrawControls(theScreen);
1294 DrawGrowIcon(theScreen);
1295 }
1296
1297 void
1298 RemoveScrollBar(w)
1299 Window *w;
1300 {
1301 if (w->w_control != NULL)
1302 DisposeControl(w->w_control);
1303 w->w_control = NULL;
1304 }
1305
1306 private pascal void
1307 DScroll(control, part)
1308 ControlHandle control;
1309 int part;
1310 {
1311 DownScroll();
1312 redisplay();
1313 }
1314
1315 private pascal void
1316 UScroll(control, part)
1317 ControlHandle control;
1318 int part;
1319 {
1320 UpScroll();
1321 redisplay();
1322 }
1323
1324 private pascal void
1325 NPage(control, part)
1326 ControlHandle control;
1327 int part;
1328 { NextPage();
1329 redisplay();
1330 }
1331
1332 private pascal void
1333 PPage(control, part)
1334 ControlHandle control;
1335 int part;
1336 { PrevPage();
1337 redisplay();
1338 }
1339
1340 private long npos; /* number of lines in buffer */
1341
1342 private int
1343 ltoc() /* calculate ctlvalue for line position */
1344 {
1345 long ipos = LinesTo(curbuf->b_first, curline) + 1;
1346
1347 npos = ipos + LinesTo(curline, (LinePtr)NULL) - 1;
1348 return (int) ((ipos * MAXC) / npos);
1349 }
1350
1351 private LinePtr
1352 ctol(ctlv) /* find buffer line for ctlvalue */
1353 int ctlv;
1354 {
1355 return next_line(curbuf->b_first, (long) ((npos * ctlv)/MAXC));
1356 }
1357
1358 private void
1359 doWind(event, window)
1360 EventRecord *event;
1361 WindowPtr window;
1362 {
1363 p = event->where;
1364 GlobalToLocal(&p);
1365
1366 if (event->what == mouseDown) {
1367 ControlHandle whichControl;
1368 Window
1369 *jwind,
1370 *cwind;
1371 jbool notcurwind = NO;
1372 int cpart; /* control part */
1373
1374 if ((cpart = FindControl(p, window, &whichControl)) == 0)
1375 return;
1376
1377 if ((jwind = (Window *) (*whichControl)->contrlRfCon) != curwind) {
1378 notcurwind = YES;
1379 cwind = curwind;
1380 SetWind(jwind);
1381 }
1382 switch (cpart) {
1383 case inUpButton:
1384 TrackControl(whichControl, p, (ProcPtr) DScroll);
1385 break;
1386 case inDownButton:
1387 TrackControl(whichControl, p, (ProcPtr) UScroll);
1388 break;
1389 case inPageUp:
1390 TrackControl(whichControl, p, (ProcPtr) PPage);
1391 break;
1392 case inPageDown:
1393 TrackControl(whichControl, p, (ProcPtr) NPage);
1394 break;
1395 case inThumb:
1396 if (TrackControl(whichControl, p, (ProcPtr)NULL)) {
1397 int newval = GetCtlValue(whichControl);
1398
1399 if (newval == MAXC)
1400 Eof();
1401 else if (newval == MINC)
1402 Bof();
1403 else
1404 SetLine(ctol(newval));
1405 }
1406 break;
1407 }
1408 if (notcurwind) {
1409 SetWind(cwind);
1410 redisplay();
1411 }
1412 redisplay(); /* again, to set the cursor */
1413 } else {
1414 if (findtext())
1415 redisplay();
1416 }
1417 }
1418
1419 #define std_state(w) (*((WStateData **)((WindowPeek)((w)))->dataHandle))->stdState
1420 #define user_state(w) (*((WStateData **)((WindowPeek)((w)))->dataHandle))->userState
1421
1422 private void
1423 doDrag(event, window)
1424 EventRecord *event;
1425 WindowPtr window;
1426 {
1427 Rect old_std = std_state(window);
1428
1429 DragWindow(window, event->where, &LimitRect);
1430 if (wc == &wc_std) {
1431 wc_user = wc_std;
1432 user_state(theScreen) = std_state(theScreen);
1433 ZoomWindow(window, 7, 1);
1434 wc = &wc_user;
1435 Reset_std();
1436 }
1437 }
1438
1439 private void
1440 doGrow(event, window)
1441 EventRecord *event;
1442 WindowPtr window;
1443 {
1444 long size;
1445
1446 /* zero means user didn't change anything */
1447 if ((size = GrowWindow(window, event->where, &LimitRect)) != 0) {
1448 if (wc == &wc_std) {
1449 wc_user = wc_std;
1450 user_state(theScreen) = std_state(theScreen);
1451 ZoomWindow(window, 7, 1);
1452 wc = &wc_user;
1453 Reset_std();
1454 }
1455 if (wc_adjust(LoWord(size), HiWord(size), wc, 0)) {
1456 EraseRect(&window->portRect);
1457 SizeWindow(window, wc->w_width, wc->w_height, YES);
1458 win_reshape(0); /* no signals here... */
1459 }
1460 }
1461 }
1462
1463 private void
1464 doZoomIn(event, window)
1465 EventRecord *event;
1466 WindowPtr window;
1467 {
1468 if (TrackBox(window, event->where, 7)) {
1469 EraseRect(&window->portRect);
1470 ZoomWindow(window, 7, 1);
1471 wc = &wc_user;
1472 win_reshape(0); /* we do our own toggle, not ZoomWindow() */
1473 }
1474 }
1475
1476 private void
1477 doZoomOut(event, window)
1478 EventRecord *event;
1479 WindowPtr window;
1480 {
1481 if (TrackBox(window, event->where, 8)) {
1482 EraseRect(&window->portRect);
1483 ZoomWindow(window, 8, 1);
1484 wc = &wc_std;
1485 win_reshape(0); /* we do our own toggle, not ZoomWindow() */
1486 }
1487 }
1488
1489 private void
1490 doGoAway(event, window)
1491 EventRecord *event;
1492 WindowPtr window;
1493 {
1494 if (TrackGoAway(window, event->where))
1495 Leave();
1496 }
1497
1498 private Window *
1499 rtowind(row) /* return jove window row is in */
1500 int row;
1501 {
1502 Window *w = fwind;
1503
1504 do {
1505 if ((w->w_topline <= row) && ((w->w_height + w->w_topline) > row))
1506 return w;
1507
1508 w = w->w_next;
1509 } while (w != fwind);
1510 return NULL;
1511 }
1512
1513 private LinePtr
1514 windtol(w, row) /* return line for row in window */
1515 Window *w;
1516 int row;
1517 {
1518 LinePtr l = w->w_top;
1519
1520 while (row-- && l != NULL)
1521 l = l->l_next;
1522 return l;
1523 }
1524
1525 private int ptoxy proto((Point, int *, int *)); /* convert Point to terminal x, y coordinate */
1526
1527 private jbool
1528 findtext() /* locate and move the point to match the mouse */
1529 {
1530 int row, col;
1531 int offset;
1532 long ticks;
1533 EventRecord event;
1534 Window *w;
1535 LinePtr l;
1536
1537 ticks = Ticks;
1538 ptoxy(p, &row, &col);
1539 if ((w = rtowind(row)) == NULL)
1540 return NO;
1541
1542 if (w != curwind)
1543 SetWind(w);
1544 offset = PhysScreen[row].s_offset; /* account for horizontal scrolling and */
1545 offset += SIWIDTH(offset) + W_NUMWIDTH(w); /* line number */
1546 row -= w->w_topline; /* now have row number in window */
1547 if (row >= w->w_height -1)
1548 return NO;
1549
1550 if ((l = windtol(w, row)) == NULL)
1551 return NO;
1552
1553 if (l->l_dline == NULL_DADDR)
1554 return NO;
1555
1556 this_cmd = LINECMD;
1557 SetLine(l); /* Curline is in linebuf now */
1558 col -= offset;
1559 if (col < 0)
1560 col = 0;
1561 curchar = how_far(curline, col);
1562 do {
1563 if (GetNextEvent(mUpMask, &event) && (event.when < ticks + DoubleTime)) {
1564 set_mark();
1565 break;
1566 }
1567 } while ((Ticks - ticks) < DoubleTime);
1568 return YES;
1569 }
1570
1571
1572 private int
1573 ptoxy(p, row, col) /* convert Point to terminal x, y coordinate */
1574 Point p;
1575 int *row, *col;
1576 {
1577 *row = (p.v / HEIGHT);
1578 *col = (p.h / WIDTH );
1579 if ((*row > MAXROW) || (*col > MAXCOL))
1580 return JMP_ERROR;
1581
1582 return 0;
1583 }
1584
1585 /* Event-related routines. The Event loop is CheckEvents(), and is called
1586 * whenever a console read occurs or a call to charp(). During certain
1587 * activities, such as ask(), etc. non-keyboard events are ignored.
1588 * This is set by the variable Keyonly. As an update or activate event
1589 * generates a call to redisplay(), it is important that redisplay() and
1590 * related routines NOT check for keyboard characters.
1591 */
1592
1593 /* (ORIGINALLY IN) tevent.c
1594 * event handler for Jove. K Mitchum 12/86
1595 */
1596
1597 #define SYS_ID 100
1598 #define NOFUNC ((void (*) ptrproto((EventRecord *event)))NULL)
1599 #define NEVENTS 16
1600
1601 private void
1602 doMouse proto((EventRecord *event)),
1603 dokeyDown proto((EventRecord *event)),
1604 doUpdate proto((EventRecord *event)),
1605 doActivate proto((EventRecord *event));
1606
1607 private void p_refresh proto((void));
1608
1609 private MenuHandle SysMenu;
1610
1611 private void (*eventlist[]) ptrproto((EventRecord *event)) =
1612 {
1613 NOFUNC, /* nullEvent */
1614 doMouse, /* mouseDown */
1615 doMouse, /* mouseUp */
1616 dokeyDown, /* keyDown */
1617 NOFUNC, /* keyUp */
1618 dokeyDown, /* autoKey */
1619 doUpdate, /* updateEvt */
1620 NOFUNC, /* diskEvt */
1621 doActivate, /* activateEvt */
1622 NOFUNC, /* not used */
1623 NOFUNC, /* networkEvt = 10 */
1624 NOFUNC, /* driverEvt */
1625 NOFUNC, /* app1Evt */
1626 NOFUNC, /* app2Evt */
1627 NOFUNC, /* app3Evt */
1628 NOFUNC /* app4Ev */
1629 };
1630
1631
1632 private void
1633 SetBufMenu proto((void)),
1634 MarkModes proto((void));
1635
1636 private void
1637 CheckEvents()
1638 {
1639 EventRecord theEvent;
1640 static long time = 0;
1641
1642 static void (*fptr) ptrproto((EventRecord *event));
1643
1644 if (FrontWindow() == window) {
1645 Point Mousep;
1646
1647 GetMouse(&Mousep);
1648 if (PtInRect(Mousep, &r))
1649 SetCursor(*cross);
1650 else
1651 SetCursor(&qd.arrow);
1652 }
1653
1654 SystemTask();
1655 if (EventCmd && !Keyonly)
1656 return;
1657
1658 if (Bufchange)
1659 SetBufMenu();
1660 if (Modechange)
1661 MarkModes();
1662 while (GetNextEvent(everyEvent, &theEvent)) {
1663 if ((theEvent.what < NEVENTS) && (fptr = eventlist[theEvent.what])) {
1664 (*fptr)(&theEvent);
1665 }
1666 SystemTask();
1667 }
1668 if (TimeDisplayed && (Ticks - time) > 3600) {
1669 time = Ticks;
1670 UpdModLine = YES;
1671 redisplay();
1672 }
1673 }
1674
1675 private void InitLocalMenus proto((void));
1676
1677 private void
1678 InitSysMenu()
1679 {
1680 SysMenu = NewMenu(SYS_ID, "\p\24");
1681 AppendMenu(SysMenu, "\pAbout Jove");
1682 AddResMenu(SysMenu, 'DRVR');
1683 InsertMenu(SysMenu, 0);
1684 InitLocalMenus();
1685 DrawMenuBar();
1686 }
1687
1688 private void
1689 doWind proto((EventRecord *event, WindowPtr window)),
1690 doGoAway proto((EventRecord *event, WindowPtr window)),
1691 doSysMenu proto((EventRecord *event, WindowPtr window)),
1692 doSysClick proto((EventRecord *event, WindowPtr window)),
1693 doDrag proto((EventRecord *event, WindowPtr window)),
1694 doGrow proto((EventRecord *event, WindowPtr window)),
1695 doZoomIn proto((EventRecord *event, WindowPtr window)),
1696 doZoomOut proto((EventRecord *event, WindowPtr window));
1697
1698 #define NMEVENTS 9
1699
1700 private void (*mouselist[]) ptrproto((EventRecord *event, WindowPtr window)) =
1701 {
1702 (void (*) ptrproto((EventRecord *event, WindowPtr window)))NULL, /* inDesk */
1703 doSysMenu, /* inMenuBar */
1704 doSysClick, /* inSysWindow */
1705 doWind, /* inContent */
1706 doDrag, /* inDrag */
1707 doGrow, /* inGrow */
1708 doGoAway, /* inGoAway */
1709 doZoomIn, /* inZoomIn */
1710 doZoomOut /* inZoomOut */
1711 };
1712
1713
1714 private void
1715 doMouse(event)
1716 EventRecord *event;
1717 {
1718 if (Keyonly) {
1719 if (event->what == mouseDown)
1720 SysBeep(2);
1721 } else {
1722 WindowPtr theWindow;
1723 int wpart = FindWindow(event->where, &theWindow);
1724 void (*fptr) ptrproto((EventRecord *event, WindowPtr window));
1725
1726 if (wpart < NMEVENTS && (fptr = mouselist[wpart]) != NULL)
1727 (*fptr)(event, theWindow);
1728 }
1729 }
1730
1731 private void ProcMenu proto((int menuno, int itemno));
1732
1733 private void
1734 doSysMenu(event, window)
1735 EventRecord *event;
1736 WindowPtr window;
1737 {
1738 long result = MenuSelect(event->where);
1739 int Menu = (result >> 16) & 0xffff;
1740 int Item = result & 0xffff;
1741
1742 if (Item == 0)
1743 return; /* no choice made */
1744
1745 if (Menu == SYS_ID) { /* apple menu */
1746 Str255 Name;
1747 GrafPtr Port;
1748
1749 if (Item == 1) {
1750 about_j();
1751 } else {
1752 GetItem(SysMenu, Item, Name);
1753 GetPort(&Port);
1754 OpenDeskAcc(Name);
1755 SetPort(Port);
1756 }
1757 } else {
1758 ProcMenu(Menu, Item);
1759 }
1760 HiliteMenu(0);
1761 EventCmd = YES;
1762 menus_on();
1763 }
1764
1765 private void
1766 doSysClick(event, window)
1767 EventRecord *event;
1768 WindowPtr window;
1769 {
1770 SystemClick(event, window);
1771 }
1772
1773
1774 private void
1775 doUpdate(event)
1776 EventRecord *event;
1777 {
1778 WindowPtr
1779 theWindow = (WindowPtr) event->message,
1780 oldPort;
1781
1782 GetPort(&oldPort);
1783 SetPort(theWindow);
1784 BeginUpdate(theWindow);
1785 p_refresh();
1786 drawfluff();
1787 EndUpdate(theWindow);
1788 SetPort(oldPort);
1789 }
1790
1791 private void
1792 doActivate(event)
1793 EventRecord *event;
1794 {
1795 WindowPtr theWindow = (WindowPtr) event->message;
1796 ControlHandle control;
1797 int hilite;
1798
1799 SetPort(theWindow);
1800 hilite = (event->modifiers & activeFlag)? 0 : 255;
1801 for (control = (ControlHandle) (((WindowPeek) theWindow)->controlList)
1802 ; (control != 0); control = (*control)->nextControl)
1803 {
1804 HiliteControl(control, hilite);
1805 }
1806 }
1807
1808 /* Keyboard routines. */
1809
1810 /* Keycodes (from Inside MacIntosh I-251). This table is ONLY used when
1811 * we are trying to make the Option key work as a Meta key. When we are
1812 * doing this, the system-supplied character is wrong, so we retranslate
1813 * the key code to a character code.
1814 *
1815 * Since we only use this table when the character generated by an
1816 * option-modified key is greater than DEL, and since the Option
1817 * modifier does not so affect keypad keys, we need not provide for
1818 * them in this table.
1819 *
1820 * ??? This may need to be updated to reflect keyboards newer than the Mac+!
1821 */
1822
1823 #define NOKEY '?'
1824
1825 private char nsh_keycodes[] = {
1826 'a','s','d','f','h', /* 00 - 04 */
1827 'g','z','x','c','v', /* 05 - 09 */
1828 NOKEY,'b','q','w','e', /* 0A - 0E */
1829 'r','y','t','1','2', /* 0F - 13 */
1830 '3','4','6','5','=', /* 14 - 18 */
1831 '9','7','-','8','0', /* 19 - 1D */
1832 ']','O','u','[','i', /* 1E - 22 */
1833 'p',CR,'l','j','\'', /* 23 - 27 */
1834 'k',';','\\',',','/', /* 28 - 2C */
1835 'n','m','.','\t',NOKEY, /* 2D - 31 */
1836 '`',DEL /* 32 - 33*/
1837 };
1838
1839 private char sh_keycodes[] = {
1840 'A','S','D','F','H', /* 00 - 04 */
1841 'G','Z','X','C','V', /* 05 - 09 */
1842 NOKEY,'B','Q','W','E', /* 0A - 0E */
1843 'R','Y','T','!','@', /* 0F - 13 */
1844 '#','$','^','%','+', /* 14 - 18 */
1845 '(','&','_','*',')', /* 19 - 1D */
1846 '}','O','U','{','I', /* 1E - 22 */
1847 'P',CR,'L','J','\'', /* 23 - 27 */
1848 'K',';','|','<','?', /* 28 - 2C */
1849 'N','M','>','\t',NOKEY, /* 2D - 31 */
1850 '~',DEL /* 32 - 33 */
1851 };
1852
1853 /* (ORIGINALLY IN) tkey.c
1854 * keyboard routines for Macintosh. K Mitchum 12/86
1855 */
1856
1857 jmp_buf auxjmp;
1858
1859 private nchars = 0;
1860 private char charbuf[MCHARS];
1861
1862 /* The following kludges a meta key out of the option key by
1863 * sending an escape sequence back to the dispatch routines. This is
1864 * not elegant but it works, and doesn't alter escape sequences for
1865 * those that prefer them. To remap the control or meta keys,
1866 * see mackeys.h.
1867 */
1868
1869 private void
1870 dokeyDown(event)
1871 EventRecord *event;
1872 {
1873 unsigned mods;
1874 int c;
1875 static int cptr = 0;
1876
1877 if (MCHARS - nchars < 2)
1878 return;
1879
1880 c = event->message & charCodeMask;
1881
1882 mods = event->modifiers;
1883
1884 if (MetaKey && (mods & optionKey)) {
1885 /* Treat the Option key as a Meta key.
1886 * We have to "undo" the normal option key effect.
1887 * This means that, if the character is greater than DEL
1888 * and the code is known to our table, we retranslate the
1889 * code into a character.
1890 * This seems pretty dubious. I wonder if "KeyTrans" would
1891 * be a better tool.
1892 */
1893 int code = (event->message & keyCodeMask) >> 8;
1894
1895 if (c > DEL && code < elemsof(sh_keycodes))
1896 c = ((mods & shiftKey)? sh_keycodes : nsh_keycodes)[code];
1897
1898 /* jam an ESC prefix */
1899 charbuf[cptr++] = ESC;
1900 cptr &= NCHMASK;
1901 nchars++;
1902 }
1903
1904 if (mods & (cmdKey | controlKey)) {
1905 /* control key (command key is treated as a control key too) */
1906 if (c == '@' || c == '2' || c == ' ')
1907 c = '\0'; /* so we have a null char */
1908 if (c != '`')
1909 c = CTL(c); /* make a control char */
1910 } else if (c == '`') {
1911 c = ESC; /* for those used to escapes */
1912 }
1913
1914 charbuf[cptr++] = c;
1915 cptr &= NCHMASK;
1916 nchars++;
1917 }
1918
1919 private ZXchar
1920 rawgetc()
1921 {
1922 static int cptr = 0;
1923 ZXchar c;
1924
1925 if (EventCmd) {
1926 longjmp(auxjmp, 1);
1927 /* NOTREACHED */
1928 }
1929
1930
1931 while (nchars <= 0) {
1932 nchars = 0;
1933 if (EventCmd) {
1934 longjmp(auxjmp, 1);
1935 /* NOTREACHED */
1936 }
1937
1938 CheckEvents(); /* ugh! WAIT for a character */
1939 }
1940 nchars--;
1941 c = ZXRC(charbuf[cptr++]);
1942 cptr &= NCHMASK; /* zero if necessary */
1943 return c;
1944 }
1945
1946 jbool
1947 rawchkc()
1948 {
1949 if (EventCmd) {
1950 longjmp(auxjmp, 1);
1951 /* NOTREACHED */
1952 }
1953
1954 if (nchars == 0)
1955 CheckEvents(); /* this should NOT be necessary! */
1956 return nchars > 0;
1957 }
1958
1959 /* Routines for calling the standard file dialogs, when macify is YES.
1960 * If the user changes the directory using the file dialogs, Jove's notion
1961 * of the current directory is updated.
1962 */
1963
1964
1965 /* (ORIGINALLY IN) tmacf.c. K. Mitchum 12/86.
1966 * Macify routines for jove.
1967 */
1968
1969 int CurrentVol; /* see tfile.c */
1970
1971 #define TYPES (-1)
1972
1973 private Point px = {100, 100};
1974 private unsigned char pmess[] = "\pSave file as: ";
1975
1976 private pascal Boolean
1977 Ffilter(p)
1978 ParmBlkPtr p;
1979 {
1980 Boolean r;
1981 char *name;
1982
1983 if (p->fileParam.ioFlFndrInfo.fdType == 'APPL')
1984 return YES;
1985
1986 /* Filter out our tempfiles.
1987 * ??? the test doesn't check to see if the directories match.
1988 */
1989 name = PtoCstr(p->fileParam.ioNamePtr);
1990 r = strcmp(name, ".joveXXX") == 0
1991 #ifdef ABBREV
1992 || strcmp(name, ".jabbXXX") == 0
1993 #endif
1994 #ifdef RECOVER
1995 || strcmp(name, ".jrecXXX") == 0
1996 #endif
1997 ;
1998 CtoPstr(name);
1999 return r;
2000 }
2001
2002 private void
2003 check_dir()
2004 {
2005 if (cur_vol != 0 - SFSaveDisk || cur_dir != CurDirStore) {
2006 char space[FILESIZE];
2007
2008 setdir(0 - SFSaveDisk, CurDirStore);
2009 UpdModLine = YES; /* make sure jove knows the change */
2010 Modechange = YES;
2011 setCWD(getcwd(space, sizeof(space)));
2012 }
2013 }
2014
2015 char *
2016 gfile(namebuf) /* return a filename to get */
2017 char *namebuf;
2018 {
2019 SFReply frec;
2020 char ans[FILESIZE];
2021
2022 SFSaveDisk = 0 - cur_vol; /* in case a Desk Accessory changed them */
2023 CurDirStore = cur_dir;
2024 SFGetFile(px, 0L, Ffilter, TYPES, 0L, 0L, &frec);
2025 check_dir(); /* see if any change, set if so */
2026 if (frec.good) {
2027 EventRecord theEvent;
2028
2029 do; while (GetNextEvent(updateMask, &theEvent) == 0);
2030 doUpdate(&theEvent);
2031 strcpy(ans, PtoCstr(frec.fName));
2032 CtoPstr((char *)frec.fName);
2033 PathParse(ans, namebuf);
2034 return namebuf;
2035 }
2036 return NULL;
2037 }
2038
2039 char *
2040 pfile(namebuf)
2041 char *namebuf;
2042 {
2043 SFReply frec;
2044 StringPtr nm;
2045
2046 SFSaveDisk = 0 - cur_vol; /* in case a Desk Accessory changed them */
2047 CurDirStore = cur_dir;
2048 strncpy(namebuf, filename(curbuf), FILESIZE-1);
2049 nm = cvt_fnm(namebuf);
2050 SFPutFile(px, pmess, nm, 0L, &frec);
2051 check_dir(); /* see if any change, set if so */
2052 if (frec.good) {
2053 EventRecord theEvent;
2054 char *h, *p;
2055
2056 do; while (GetNextEvent(updateMask, &theEvent) == 0);
2057 doUpdate(&theEvent);
2058 h = PtoCstr(frec.fName);
2059 while (*h == ':')
2060 h++; /* convert to unix style */
2061 for (p = h; (p = strchr(p, ':')) != NULL; )
2062 *p++ = '/';
2063 PathParse(h, namebuf);
2064 return namebuf;
2065 }
2066 return NULL;
2067 }
2068
2069
2070 /* getArgs() returns an argument list based on documents clicked on by the user. */
2071
2072 int
2073 getArgs(avp)
2074 char ***avp;
2075 {
2076 int argc, old_vol;
2077 short nargs, type;
2078 long old_dir;
2079 char **argv;
2080 char *pathname;
2081 AppFile p;
2082 WDPBRec d;
2083
2084 old_vol = cur_vol;
2085 old_dir = cur_dir;
2086
2087 CountAppFiles(&type, &nargs);
2088 if (nargs > 0) { /* files to open... */
2089 argv = (char **) emalloc((nargs + 2) * sizeof(char *));
2090 for (argc = 1; argc <= nargs; argc++) {
2091 GetAppFiles(argc, &p);
2092 if (type == 0) {
2093 char space[FILESIZE];
2094
2095 PtoCstr((StringPtr)p.fName);
2096 d.ioCompletion = 0;
2097 d.ioNamePtr = NULL;
2098 d.ioVRefNum = p.vRefNum;
2099 d.ioWDIndex = 0;
2100 PBGetWDInfo(&d, 0);
2101 cur_vol = d.ioWDVRefNum;
2102 cur_dir = d.ioWDDirID;
2103 pathname = getcwd(space, sizeof(space));
2104 argv[argc] = emalloc(strlen((char *)p.fName) + strlen(pathname) + 2);
2105 strcpy(argv[argc], pathname);
2106 strcat(argv[argc], "/");
2107 strcat(argv[argc], (char *)p.fName);
2108 }
2109 ClrAppFiles(argc);
2110 }
2111 if (type != 0)
2112 argc = 1;
2113 } else {
2114 argv = (char **) emalloc(2 * sizeof(char*));
2115 argc = 1;
2116 }
2117 argv[0] = "jove";
2118
2119 argv[argc] = NULL;
2120 *avp = argv;
2121 cur_dir = old_dir;
2122 cur_vol = old_vol;
2123 return argc;
2124 }
2125
2126
2127 /* Menu routines. The menus items are set up in a similar manner as keys, and
2128 * are bound prior to runtime. See menumaps.txt, which must be run through
2129 * setmaps. Unlike keys, menu items may be bound to variables, and to
2130 * buffers. Buffer binding is only done at runtime.
2131 */
2132
2133 private void
2134 InitMenu proto((struct menu *M)),
2135 make_edits proto((int menu));
2136
2137 private void
2138 InitLocalMenus()
2139 {
2140 int i;
2141
2142 for (i = 0; i < NMENUS; i++) {
2143 InitMenu(&Menus[i]);
2144 if (i == 0)
2145 make_edits(Menus[i].menu_id + 1);
2146 }
2147 }
2148
2149 private void
2150 InitMenu(M)
2151 struct menu *M;
2152 {
2153 int i;
2154 StringPtr ps;
2155
2156 if (M->menu_id == 0)
2157 return;
2158
2159 M->Mn = NewMenu(M->menu_id, ps=CtoPstr(M->Name));
2160 PtoCstr(ps);
2161
2162 for (i = 0; i < NMENUITEMS; i++) {
2163 data_obj *d = M->m[i];
2164
2165 if (d == NULL)
2166 break; /* last item... */
2167
2168 switch (d->Type & TYPEMASK) {
2169 case STRING:
2170 AppendMenu(M->Mn, ps=CtoPstr(d->Name));
2171 PtoCstr(ps);
2172 break;
2173 case VARIABLE:
2174 AppendMenu(M->Mn, ps=CtoPstr(d->Name));
2175 PtoCstr(ps);
2176 if ((((struct variable *)d)->v_flags & V_TYPEMASK) == V_BOOL
2177 && *(jbool *)(((struct variable *)d)->v_value))
2178 CheckItem(M->Mn, i + 1, YES);
2179 break;
2180 case COMMAND:
2181 AppendMenu(M->Mn, ps=CtoPstr(d->Name));
2182 PtoCstr(ps);
2183 break;
2184 }
2185 }
2186 InsertMenu(M->Mn, 0);
2187 }
2188
2189 private void MacSetVar proto((struct variable *vp, int mnu, int itm));
2190
2191 private void
2192 ProcMenu(menuno, itemno)
2193 int menuno, itemno;
2194 {
2195 int i;
2196 data_obj *d;
2197
2198 for (i = 0; i < NMENUS; i++) {
2199 if (Menus[i].menu_id == menuno) {
2200 itemno--;
2201 d = Menus[i].m[itemno];
2202 switch(d->Type & TYPEMASK) {
2203 case COMMAND:
2204 ExecCmd((data_obj *) d);
2205 break;
2206 case BUFFER:
2207 SetABuf(curbuf);
2208 tiewind(curwind, (Buffer *) d);
2209 SetBuf((Buffer *) d);
2210 break;
2211 case VARIABLE:
2212 MacSetVar((struct variable *) d, i, itemno);
2213 break;
2214 }
2215 break;
2216 }
2217 }
2218 }
2219
2220
2221 private void
2222 make_edits(menu) /* add dummy edit menu */
2223 int menu;
2224 {
2225 MenuHandle M;
2226 int item;
2227 char *fname;
2228
2229 M = NewMenu((menu), "\pEdit");
2230 AppendMenu(M,
2231 "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear;Select All;(-;Show Clipboard");
2232 InsertMenu(M, 0);
2233 DisableItem(M, 0);
2234 }
2235
2236 void
2237 menus_off()
2238 {
2239 int i;
2240
2241 if (Keyonly || EventCmd)
2242 return;
2243
2244 #ifdef MENU_DISABLE /* NOBODY likes this, but it's here if you want it... */
2245 DisableItem(SysMenu, 0);
2246 for (i = 0; i < NMENUS; i++)
2247 if (Menus[i].Mn)
2248 DisableItem(Menus[i].Mn, 0);
2249 DrawMenuBar();
2250 #endif
2251 Keyonly = YES;
2252 }
2253
2254 void
2255 menus_on()
2256 {
2257 int i;
2258
2259 if (!Keyonly)
2260 return;
2261
2262 #ifdef MENU_DISABLE
2263 EnableItem(SysMenu, 0);
2264 for (i = 0; i < NMENUS; i++)
2265 if (Menus[i].Mn)
2266 EnableItem(Menus[i].Mn, 0);
2267 DrawMenuBar();
2268 #endif
2269 Keyonly = NO;
2270 }
2271
2272 private char *
2273 BufMPrint(b, i)
2274 Buffer *b;
2275 int i;
2276 {
2277 char *p;
2278 char *nm = filename(b);
2279 char t[35];
2280
2281 if (strlen(nm) > 30) {
2282 strcpy(t, "...");
2283 strcat(t, nm + strlen(nm) - 30);
2284 } else {
2285 strcpy(t, nm);
2286 }
2287 nm = t;
2288 while (*nm) {
2289 switch(*nm) { /* ugh... these are metacharacter for Menus */
2290 case '/':
2291 *nm = ':';
2292 break;
2293 case '^':
2294 case '!':
2295 case '<':
2296 case '(':
2297 case ';':
2298 *nm = '.';
2299 break; /* that will confuse everybody */
2300 }
2301 nm++;
2302 }
2303 p = sprint("%-2d %-11s \"%-s\"", i, b->b_name, t);
2304 return p;
2305 }
2306
2307 private void
2308 SetBufMenu()
2309 {
2310 Buffer *b;
2311 int i, j, stop;
2312 struct menu *M;
2313
2314 Bufchange = NO;
2315 for (i = 0; i < NMENUS; i++) {
2316 if (strcmp(Menus[i].Name, "Buffer") == 0) {
2317 M = &Menus[i];
2318 for (j = 0; j < NMENUITEMS; j++) {
2319 data_obj *d = Menus[i].m[j];
2320
2321 if (d == NULL)
2322 break;
2323
2324 if ((d->Type & TYPEMASK) == BUFFER) {
2325 for (i = j, b = world; i < NMENUITEMS && b != NULL; i++, b = b->b_next) {
2326
2327 if (M->m[i] == NULL)
2328 AppendMenu(M->Mn, CtoPstr(BufMPrint(b, i-j+1))); /* add the item */
2329 else
2330 SetItem(M->Mn, i + 1, CtoPstr(BufMPrint(b, i-j+1))); /* or change it */
2331 M->m[i] = (data_obj *) b;
2332 }
2333 stop = i;
2334 /* out of buffers? */
2335 for (; i < NMENUITEMS && M->m[i]; i++) {
2336 DelMenuItem(M->Mn, stop + 1); /* take off last item */
2337 M->m[i] = NULL;
2338 }
2339 break;
2340 }
2341 }
2342 break;
2343 }
2344 }
2345 }
2346
2347 private void
2348 MacSetVar(vp, mnu, itm) /* Set a variable from the menu */
2349 struct variable *vp;
2350 int mnu, itm;
2351 {
2352 if ((vp->v_flags & V_TYPEMASK) == V_BOOL) {
2353 /* toggle the value */
2354 *((jbool *) vp->v_value) = !*((jbool *) vp->v_value);
2355 MarkVar(vp, mnu, itm);
2356 } else {
2357 char prompt[128];
2358
2359 swritef(prompt, sizeof(prompt), "Set %s: ", vp->Name);
2360 vset_aux(vp, prompt);
2361 }
2362 }
2363
2364 private void
2365 MarkModes()
2366 {
2367 int mnu, itm;
2368 data_obj *d;
2369
2370 Modechange = NO;
2371 for (mnu = 0; mnu < NMENUS; mnu++) {
2372 for (itm = 0; itm < NMENUITEMS; itm++) {
2373 if ((d = Menus[mnu].m[itm]) == NULL)
2374 break;
2375
2376 if ((d->Type & (MAJOR_MODE | MINOR_MODE))
2377 || ((d->Type & TYPEMASK) == BUFFER))
2378 {
2379 jbool checked;
2380
2381 if (d->Type & (MAJOR_MODE))
2382 checked = curbuf->b_major == (d->Type >> 8);
2383 else if (d->Type & (MINOR_MODE))
2384 checked = (curbuf->b_minor & (d->Type >> 8)) != 0;
2385 else
2386 checked = d == (data_obj *) curbuf;
2387 CheckItem(Menus[mnu].Mn, itm + 1, checked);
2388 }
2389 }
2390 }
2391 }
2392
2393 void
2394 MarkVar(vp, mnu, itm) /* mark a boolean menu item */
2395 const struct variable *vp;
2396 int mnu, itm;
2397 {
2398 if (mnu == -1) { /* we don't know the item... slow */
2399 for (mnu = 0; ; mnu++) {
2400 if (mnu >= NMENUS)
2401 return; /* not found */
2402
2403 for (itm = 0; (itm < NMENUITEMS); itm++) {
2404 if ((struct variable *) (Menus[mnu].m[itm]) == vp)
2405 break;
2406 }
2407 if (itm < NMENUITEMS)
2408 break;
2409 }
2410 }
2411 CheckItem(Menus[mnu].Mn, itm + 1, *(jbool *)vp->v_value);
2412 }
2413
2414 /* Screen routines and driver. The Macinitosh Text Edit routines are not utilized,
2415 * as they are slow and cumbersome for a terminal emulator. Instead, direct QuickDraw
2416 * calls are used. The fastest output is obtained writing a line at a time, rather
2417 * than on a character basis, so the major output routine is writechr(), which takes
2418 * a pascal-style string as an argument. See do_sputc() in screen.c.
2419 */
2420
2421 void
2422 Placur(line, col)
2423 int line, col;
2424 {
2425 CapCol = col;
2426 CapLine = line;
2427 putcurs(line, col, YES);
2428 }
2429
2430 void
2431 NPlacur(line, col)
2432 int line, col;
2433 {
2434 CapCol = col;
2435 CapLine = line;
2436 putcurs(line, col, NO);
2437 }
2438
2439 void
2440 i_lines(top, bottom, num)
2441 int top, bottom, num;
2442 {
2443 Placur(bottom - num + 1, 0);
2444 dellines(num, bottom);
2445 Placur(top, 0);
2446 inslines(num, bottom);
2447 }
2448
2449 void
2450 d_lines(top, bottom, num)
2451 int top, bottom, num;
2452 {
2453 Placur(top, 0);
2454 dellines(num, bottom);
2455 Placur(bottom + 1 - num, 0);
2456 inslines(num, bottom);
2457 }
2458
2459 /* (ORIGINALLY IN) tn.c
2460 * Window driver for MacIntosh using windows.
2461 * K. Mitchum 9/86
2462 */
2463
2464 /*#define VARFONT*/
2465 #ifdef VARFONT
2466 private height, width, theight, twidth, descent;
2467 #else
2468 # define height HEIGHT
2469 # define width WIDTH
2470 # define theight THEIGHT
2471 # define twidth TWIDTH
2472 # define descent DESCENT
2473 #endif
2474
2475 private int trow, tcol;
2476 private jbool cursvis;
2477 #ifdef NEVER
2478 private jbool insert;
2479 #endif
2480 private Rect cursor_rect;
2481 private char *p_scr, *p_curs; /* physical screen and cursor */
2482 private int p_size;
2483
2484 private Rect vRect;
2485 private WindowRecord myWindowRec;
2486
2487 #define active() SetPort(theScreen)
2488 #define maxadjust(r) OffsetRect((r), 0, 2)
2489
2490 private char *
2491 conv_p_curs(row, col)
2492 int row,
2493 col;
2494 {
2495 return p_scr + (row * (CO)) + col;
2496 }
2497
2498 #ifdef NEVER
2499 private void
2500 INSmode(new)
2501 jbool new;
2502 {
2503 insert = new;
2504 }
2505 #endif
2506
2507 void
2508 SO_effect(new)
2509 jbool new;
2510 {
2511 theScreen->txMode = new? notSrcCopy : srcCopy;
2512 }
2513
2514 private void init_slate proto((void));
2515
2516 private void
2517 tn_init()
2518 {
2519 #ifdef NEVER
2520 INSmode(NO);
2521 #endif
2522 init_slate();
2523 SO_off();
2524 ShowPen();
2525 }
2526
2527 void
2528 clr_page() /* clear and home function */
2529 {
2530 Rect r;
2531
2532 memset(p_scr, ' ', p_size);
2533 active();
2534 SetRect(&r, 0, 0, WINDWIDTH, WINDHEIGHT);
2535 EraseRect(&r);
2536 putcurs(0, 0, NO); /* ??? "NO" guess by DHR */
2537 drawfluff();
2538 }
2539
2540 private void
2541 putcurs(row, col, vis)
2542 unsigned row, col;
2543 jbool vis;
2544 {
2545 active();
2546 curset(NO);
2547 trow = row;
2548 tcol = col;
2549 curset(vis);
2550 }
2551
2552 private void
2553 curset(invert)
2554 jbool invert;
2555 {
2556 int
2557 colpix = tcol * width,
2558 rowpix = trow * height;
2559
2560 if (trow == MAXROW)
2561 rowpix += 2; /* leave space for 2 pixel rule */
2562 p_curs = conv_p_curs(trow, tcol);
2563 MoveTo(colpix, rowpix + height - descent);
2564 DrawChar(*p_curs);
2565 cursvis = invert;
2566 if (invert) {
2567 SetRect(&cursor_rect, colpix, rowpix,
2568 colpix + width - 1, rowpix + height - 1);
2569 InvertRect(&cursor_rect);
2570 }
2571 MoveTo(colpix, rowpix + height - descent);
2572 }
2573
2574 void
2575 clr_eoln()
2576 {
2577 Rect r;
2578
2579 active();
2580 SetRect(&r, tcol * width, trow * height, WINDWIDTH, (trow +1) * height);
2581 if (trow == MAXROW)
2582 maxadjust(&r);
2583 EraseRect(&r);
2584 memset(p_curs, ' ', CO - tcol);
2585 curset(YES);
2586 }
2587
2588 #ifdef NEVER
2589 private void
2590 delchars()
2591 {
2592 Rect r;
2593 RgnHandle updateRgn;
2594
2595 active();
2596 curset(NO);
2597 updateRgn = NewRgn();
2598 SetRect(&r, tcol * width, trow * height, twidth - width, (trow+1) * height);
2599 if (trow == MAXROW)
2600 maxadjust(&r);
2601 ScrollRect(&r, -width, 0, updateRgn);
2602 DisposeRgn(updateRgn);
2603 BlockMove(p_curs + 1, p_curs, (long) (MAXCOL - tcol));
2604 *conv_p_curs(trow, MAXCOL) = ' ';
2605 curset(YES);
2606 }
2607 #endif /* NEVER */
2608
2609 private void
2610 dellines(n, bot)
2611 int n, bot;
2612 {
2613 RgnHandle updateRgn = NewRgn();
2614 Rect r;
2615 long len;
2616
2617 active();
2618 curset(NO);
2619 SetRect(&r, 0, ((trow) * height), WINDWIDTH, ((bot + 1) * height));
2620 ScrollRect(&r, 0, 0 - (n * height), updateRgn);
2621 DisposeRgn(updateRgn);
2622 len = ((bot - trow - n + 1) * CO);
2623 BlockMove(conv_p_curs(trow + n, 0), conv_p_curs(trow, 0), len);
2624 memset(conv_p_curs(bot - n + 1, 0), ' ', n * CO);
2625 putcurs(trow, 0, YES); /* ??? "YES" guess by DHR */
2626 }
2627
2628 private void
2629 inslines(n, bot)
2630 int n, bot;
2631 {
2632 RgnHandle updateRgn = NewRgn();
2633 Rect r;
2634 long len;
2635
2636 active();
2637 curset(NO);
2638 SetRect(&r, 0, trow * height, WINDWIDTH, (bot +1) * height);
2639 ScrollRect(&r, 0, (n * height), updateRgn);
2640 DisposeRgn(updateRgn);
2641 len = ((bot - trow - n +1) * CO);
2642 BlockMove(conv_p_curs(trow, 0), conv_p_curs(trow + n, 0), len);
2643 memset(conv_p_curs(trow, 0), ' ', (n * CO));
2644 putcurs(trow, 0, YES); /* ??? "YES" guess by DHR */
2645 }
2646
2647 void
2648 writetext(str, len)
2649 const unsigned char *str;
2650 size_t len;
2651 {
2652 active();
2653 curset(NO);
2654 #ifdef NEVER
2655 if (insert) {
2656 RgnHandle updateRgn = NewRgn();
2657 Rect r;
2658
2659 SetRect(&r, tcol * width, trow * height, twidth - width * len, (trow +1) * height -1);
2660 if (trow == MAXROW)
2661 maxadjust(&r);
2662 ScrollRect(&r, width * len, 0, updateRgn);
2663 DisposeRgn(updateRgn);
2664 }
2665 #endif
2666 DrawText(str, (short)0, (short)len);
2667 #ifdef NEVER
2668 if (insert)
2669 BlockMove(p_curs, p_curs + len, (long) (CO - tcol - len));
2670 #endif
2671 memcpy((UnivPtr)p_curs, (UnivPtr)str, len);
2672 putcurs(trow, tcol+len <= MAXCOL? tcol+len : MAXCOL, YES); /* ??? "YES" guess by DHR */
2673 }
2674
2675 private Rect myBoundsRect;
2676
2677 private void
2678 init_slate()
2679 {
2680 FontInfo f;
2681
2682 char *Name = "Jove ";
2683 char *Title;
2684
2685 InitGraf(&qd.thePort);
2686 InitWindows();
2687 InitCursor();
2688 InitFonts();
2689 InitMenus();
2690 InitDialogs((ProcPtr)NULL); /* no restart proc */
2691
2692 /* figure limiting rectangle for window moves */
2693 SetRect(&LimitRect,
2694 qd.screenBits.bounds.left + 3,
2695 qd.screenBits.bounds.top + 20,
2696 qd.screenBits.bounds.right - 3,
2697 qd.screenBits.bounds.bottom -3);
2698
2699 Set_std();
2700 SetBounds();
2701
2702 /* initialize char array for updates */
2703 p_scr = emalloc(p_size = wc_std.w_cols * wc_std.w_rows); /* only once */
2704 p_curs = p_scr;
2705
2706 Title = sprint("%s%s", Name, jversion);
2707 theScreen = NewWindow(&myWindowRec, &myBoundsRect, CtoPstr(Title),
2708 1, 8, (WindowPtr) -1, 1, 0L);
2709
2710 /* figure an initial window configuration and adjust it */
2711 wc = &wc_std;
2712 wc_user = wc_std; /* initially, only one configuration to toggle */
2713 user_state(theScreen) = std_state(theScreen);
2714 SetPort(theScreen);
2715
2716 theScreen->txFont = FONT;
2717 theScreen->txSize = TEXTSIZE;
2718
2719 #ifdef VARFONT
2720 GetFontInfo(&f);
2721 height = f.ascent+f.descent+f.leading;
2722 width = f.widMax;
2723 twidth = width * wc->w_cols;
2724 theight = height * wc->w_rows;
2725 descent = f.descent;
2726 #endif
2727
2728 theScreen->txMode = srcCopy;
2729 theScreen->pnMode = patCopy;
2730 PenNormal();
2731 }
2732
2733 private void
2734 p_refresh()
2735 {
2736 int lineno;
2737
2738 for (lineno = 0; lineno < LI; lineno++) {
2739 char *curs = conv_p_curs(lineno, 0);
2740
2741 MoveTo(0, (lineno+1) * height - descent + (lineno == MAXROW? 2 : 0));
2742 /* The following kludgy line is to get SO right. It depends on:
2743 * - !defined(HIGHLIGHTING)
2744 * - this routine not being called at an inauspicious time
2745 * i.e. in the middle of a SO output.
2746 * - the fact that the last line will non-SO so that the text
2747 * mode will be left non-SO.
2748 */
2749 SO_effect(Screen[lineno].s_effects);
2750 DrawText(curs, (short)0, (short)CO);
2751 }
2752 curset(cursvis);
2753 }
2754
2755
2756 private jbool
2757 wc_adjust(w, h, wcf, init) /* adjust window config to look nice */
2758 int w, h;
2759 struct wind_config *wcf;
2760 int init;
2761 {
2762 static int LIMIT_R, LIMIT_C;
2763 int rows, cols;
2764
2765 if (init) {
2766 LIMIT_R = (h - 4) / HEIGHT;
2767 LIMIT_C = (w - SCROLLWIDTH - 1) / WIDTH + 1;
2768 }
2769 if ((w < WIDTH * 40) ||(h < HEIGHT * 10) /* too small */
2770 || ((rows = (h - 4) / HEIGHT) > LIMIT_R) /* too big */
2771 || ((cols = (w - SCROLLWIDTH - 1) / WIDTH + 1) > LIMIT_C))
2772 return NO;
2773
2774 wcf->w_rows = rows;
2775 wcf->w_cols = cols;
2776 wcf->w_width = wcf->w_cols * WIDTH + 1 + SCROLLWIDTH;
2777 wcf->w_height = wcf->w_rows * HEIGHT + 4;
2778 return YES;
2779 }
2780
2781 private int
2782 getCO() /* so that jove knows params */
2783 {
2784 return wc->w_cols;
2785 }
2786
2787 private int
2788 getLI()
2789 {
2790 return wc->w_rows;
2791 }
2792
2793 void
2794 ttsize()
2795 {
2796 /* ??? We really ought to wait until the screen is big enough:
2797 * at least three lines high (one line each for buffer, mode,
2798 * and message) and at least twelve columns wide (eight for
2799 * line number, one for content, two for overflow indicators,
2800 * and one blank at end).
2801 */
2802 /* ??? This should be made more like UNIX version */
2803 CO = getCO();
2804 if (CO > MAXCOLS)
2805 CO = MAXCOLS;
2806 LI = getLI();
2807 Windchange = YES;
2808 clr_page();
2809 ILI = LI - 1;
2810 }
2811
2812 private void
2813 SetBounds()
2814 {
2815 SetRect(&myBoundsRect,
2816 qd.screenBits.bounds.left + 3,
2817 qd.screenBits.bounds.top + 40,
2818 qd.screenBits.bounds.left + 3 + wc_std.w_width,
2819 qd.screenBits.bounds.top + 40 + wc_std.w_height);
2820 }
2821
2822 private void
2823 Set_std()
2824 {
2825 (void) wc_adjust(qd.screenBits.bounds.right - qd.screenBits.bounds.left - 6,
2826 qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - 42,
2827 &wc_std, 1);
2828 }
2829
2830 private void
2831 Reset_std()
2832 {
2833 Set_std();
2834 std_state(theScreen) = myBoundsRect;
2835 }
2836 #endif /* MAC */