jcs
/subtext
/amendments
/70
util: Sync with upstream
jcs made amendment 70 over 2 years ago
--- util.c Wed Jan 26 10:58:59 2022
+++ util.c Mon Feb 7 22:26:21 2022
@@ -18,7 +18,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-
+#include <GestaltEqu.h>
+#include <Script.h>
+#include <SetUpA4.h>
#include "util.h"
/* ALRT resources */
@@ -847,6 +849,80 @@ FSReadLine(short frefnum, char *buf, size_t buflen)
/*
+ * Gestalt functions
+ */
+char *
+gestalt_machine_type(void)
+{
+ short error;
+ long resp;
+
+ error = Gestalt(gestaltMachineType, &resp);
+ if (error)
+ return NULL;
+ switch (resp) {
+ case gestaltClassic:
+ return "Macintosh 128K";
+ case gestaltMacXL:
+ return "Macintosh XL";
+ case gestaltMac512KE:
+ return "Macintosh 512Ke";
+ case gestaltMacPlus:
+ return "Macintosh Plus";
+ case gestaltMacSE:
+ return "Macintosh SE";
+ case gestaltMacII:
+ return "Macintosh II";
+ case gestaltMacIIx:
+ return "Macintosh IIx";
+ case gestaltMacIIcx:
+ return "Macintosh IIcx";
+ case gestaltMacSE030:
+ return "Macintosh SE/30";
+ case gestaltPortable:
+ return "Macintosh Portable";
+ case gestaltMacIIci:
+ return "Macintosh IIci";
+ case gestaltMacIIfx:
+ return "Macintosh IIfx";
+ case gestaltMacClassic:
+ return "Macintosh Classic";
+ case gestaltMacIIsi:
+ return "Macintosh IIsi";
+ case gestaltMacLC:
+ return "Macintosh LC";
+ }
+
+ return NULL;
+}
+
+char *
+get_version(bool long_version)
+{
+ static char vers_s[256] = { 0 };
+ char *v;
+ VersRecHndl vers;
+ short len;
+
+ vers = (VersRecHndl)GetResource('vers', 128);
+ if (!vers)
+ return "?.?";
+
+ HLock(vers);
+ v = (char *)&(*vers)->shortVersion;
+ len = v[0];
+ if (long_version) {
+ v += len + 1;
+ len = v[0];
+ }
+ memcpy(vers_s, v, len + 1);
+ ReleaseResource(vers);
+
+ PtoCstr(vers_s);
+ return vers_s;
+}
+
+/*
* General Mac-specific GUI functions
*/
@@ -961,8 +1037,8 @@ UpdateScrollbarForTE(ControlHandle control, TEHandle t
vlines = (ter->viewRect.bottom - ter->viewRect.top) / fheight;
telines = ter->nLines;
/* telines is inaccurate if the last line doesn't have any chars */
- if (telines >= vlines)
- telines++;
+ //if (telines >= vlines)
+ // telines++;
max = telines - vlines + 1;
if (max < 1)
max = 1;
@@ -981,7 +1057,13 @@ UpdateScrollbarForTE(ControlHandle control, TEHandle t
}
}
}
- SetCtlMax(control, max);
+
+ /*
+ * Avoid SetCtlMax because it will redraw and then SetCtlValue will
+ * redraw again, which can cause a jump if we're trying to keep the
+ * scrollbar position in the same place (like at the bottom).
+ */
+ (*control)->contrlMax = max;
SetCtlValue(control, val);
HUnlock(te);
@@ -1057,9 +1139,22 @@ TrackMouseDownInControl(ControlHandle control, short p
pascal bool
ModalDialogFilter(DialogPtr dlg, EventRecord *event, short *hit)
{
+ WindowPtr event_win;
+ short event_in;
char key;
switch (event->what) {
+ case mouseDown:
+ event_in = FindWindow(event->where, &event_win);
+
+ switch (event_in) {
+ case inGoAway:
+ if (TrackGoAway(dlg, event->where)) {
+ *hit = -1;
+ return true;
+ }
+ }
+ break;
case keyDown:
key = event->message & charCodeMask;
@@ -1082,7 +1177,7 @@ ModalDialogFilter(DialogPtr dlg, EventRecord *event, s
return false;
} else if (key == 13 || key == 3) {
/* OK button */
- *hit = 1;
+ *hit = OK;
return true;
}
@@ -1090,4 +1185,93 @@ ModalDialogFilter(DialogPtr dlg, EventRecord *event, s
}
return false;
-}
+}
+
+static short _password_dialog_ditl_id = -1;
+static char *_password_dialog_storage = NULL;
+static size_t _password_dialog_storage_len = 0;
+
+void
+PasswordDialogFieldFilterSetup(short ditl_id, char *storage, size_t len)
+{
+ _password_dialog_ditl_id = ditl_id;
+ _password_dialog_storage = storage;
+ _password_dialog_storage_len = len;
+
+ memset(_password_dialog_storage, 0, len);
+}
+
+pascal bool
+PasswordDialogFieldFilter(DialogPtr dlg, EventRecord *event, short *hit)
+{
+ DialogPeek dlgp;
+ WindowPtr event_win;
+ short event_in, sel_start, sel_end;
+ char key;
+
+ dlgp = (DialogPeek)dlg;
+ if (dlgp->editField == _password_dialog_ditl_id - 1) {
+ sel_start = (*(dlgp->textH))->selStart;
+ sel_end = (*(dlgp->textH))->selEnd;
+
+ switch (event->what) {
+ case keyDown:
+ case autoKey:
+ key = event->message & charCodeMask;
+ if (event->modifiers & cmdKey) {
+ /* TODO: implement DlgPaste for cmd+v? */
+ event->what = nullEvent;
+ return false;
+ }
+
+ if (key == 8) {
+ /* backspace */
+ if (sel_start == sel_end && sel_start > 0)
+ memmove(_password_dialog_storage + sel_start - 1,
+ _password_dialog_storage + sel_start,
+ _password_dialog_storage_len - sel_start - 1);
+ else if (sel_start != sel_end)
+ memmove(_password_dialog_storage + sel_start,
+ _password_dialog_storage + sel_end,
+ _password_dialog_storage_len - sel_end - 1);
+ } else if (sel_start >= _password_dialog_storage_len) {
+ event->what = nullEvent;
+ return false;
+ } else if (key >= ' ' && key <= '~') {
+ if (sel_start != sel_end)
+ /* delete selection before making space for new char */
+ memmove(_password_dialog_storage + sel_start,
+ _password_dialog_storage + sel_end,
+ _password_dialog_storage_len - sel_end - 1);
+ memmove(_password_dialog_storage + sel_start + 1,
+ _password_dialog_storage + sel_start,
+ _password_dialog_storage_len - sel_start - 1);
+ _password_dialog_storage[sel_start] = key;
+ event->message = '•';
+ }
+ _password_dialog_storage[(*(dlgp->textH))->teLength + 1] = '\0';
+ sel_start = 0;
+ break;
+ }
+ }
+
+ return ModalDialogFilter(dlg, event, hit);
+}
+
+void
+PasswordDialogFieldFinish(void)
+{
+ _password_dialog_ditl_id = -1;
+ _password_dialog_storage = NULL;
+ _password_dialog_storage_len = 0;
+}
+
+/* (*(some_te))->caretHook = NullCaretHook; */
+pascal void
+NullCaretHook(void)
+{
+ asm {
+ move.l (a7)+,d0
+ rts
+ }
+}
--- util.h Sat Jan 29 16:52:36 2022
+++ util.h Mon Feb 7 21:18:17 2022
@@ -70,20 +70,20 @@ typedef struct stat {
unsigned char st_flags;
};
-void *xmalloc(size_t);
-void *xmalloczero(size_t);
-void *xcalloc(size_t, size_t);
-void *xrealloc(void *src, size_t size);
-void *xmallocarray(size_t nmemb, size_t size);
-void *xreallocarray(void *, size_t, size_t);
-char *xstrdup(const char *);
+void * xmalloc(size_t);
+void * xmalloczero(size_t);
+void * xcalloc(size_t, size_t);
+void * xrealloc(void *src, size_t size);
+void * xmallocarray(size_t nmemb, size_t size);
+void * xreallocarray(void *, size_t, size_t);
+char * xstrdup(const char *);
short getline(char *str, size_t len, char **ret);
size_t strlcpy(char *dst, const char *src, size_t dsize);
size_t strlcat(char *dst, const char *src, size_t dsize);
-const char *ordinal(unsigned short n);
+const char * ordinal(unsigned short n);
long strpos_quoted(char *str, char c);
-char *OSTypeToString(OSType type);
+char * OSTypeToString(OSType type);
unsigned long xorshift32(void);
@@ -100,12 +100,11 @@ void progress(char *format, ...);
Handle xNewHandle(size_t size);
Handle xGetResource(ResType type, short id);
StringHandle xGetString(short id);
-char *xGetStringAsChar(short id);
+char * xGetStringAsChar(short id);
long xGetStringAsLong(short id);
void xSetHandleSize(Handle h, Size s);
-short getpath(short vRefNum, Str255 fileName, Str255 *ret,
- bool include_file);
+short getpath(short vRefNum, Str255 fileName, Str255 *ret, bool include_file);
bool FIsDir(Str255 path);
short stat(char *path, struct stat *sb);
short FStat(Str255 path, struct stat *sb);
@@ -113,6 +112,9 @@ OSErr copy_file(Str255 source, Str255 dest, bool overw
OSErr copy_file_contents(short source_ref, short dest_ref);
OSErr FSReadLine(short frefnum, char *buf, size_t buflen);
+char * gestalt_machine_type(void);
+char * get_version(bool long_version);
+
short FontHeight(short font_id, short size);
void DrawGrowIconOnly(WindowPtr win);
short TEGetWidth(short off, TEHandle te);
@@ -121,5 +123,11 @@ void SetTrackControlTE(TEHandle te);
pascal void TrackMouseDownInControl(ControlHandle control, short part);
pascal bool ModalDialogFilter(DialogPtr dlg, EventRecord *event,
short *hit);
+void PasswordDialogFieldFilterSetup(short ditl_id, char *storage,
+ size_t len);
+pascal bool PasswordDialogFieldFilter(DialogPtr dlg, EventRecord *event,
+ short *hit);
+void PasswordDialogFieldFinish(void);
+pascal void NullCaretHook(void);
#endif