jcs
/amend
/amendments
/143
util: Add GetSystemSubfolder, outline default dialog buttons
jcs made amendment 143 over 2 years ago
--- util.c Fri Feb 16 13:55:31 2024
+++ util.c Wed Mar 20 21:27:59 2024
@@ -19,13 +19,15 @@
/* THINK C Project must have a header of "#include <MacHeaders>" */
+#include <GestaltEqu.h>
+#include <Folders.h>
+#include <Script.h>
+#include <SetUpA4.h>
+#include <Traps.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <GestaltEqu.h>
-#include <Script.h>
-#include <SetUpA4.h>
#include "util.h"
/* ALRT resources */
@@ -521,15 +523,13 @@ vwarn(short alert_func, const char *format, va_list ap
Rect bounds;
short hit, itype;
WindowPtr win, dialog;
- Handle ihandle;
+ Handle ihandle, okhandle;
Rect irect;
progress(NULL);
-
GetPort(&win);
vsnprintf(err_str, ERROR_STRING_SIZE, format, ap);
-
ParamText(CtoPstr(err_str), "\p", "\p", "\p");
center_in_screen(320, 110, false, &bounds);
@@ -555,7 +555,9 @@ vwarn(short alert_func, const char *format, va_list ap
ShowWindow(dialog);
+ GetDItem(dialog, ok, &itype, &okhandle, &irect);
for (;;) {
+ outline_button((ControlHandle)okhandle);
ModalDialog(0, &hit);
if (hit == ok)
break;
@@ -647,9 +649,11 @@ short
ask(const char *format, ...)
{
Rect bounds;
- short hit;
WindowPtr win, dialog;
va_list ap;
+ Handle ihandle;
+ Rect irect;
+ short hit, itype;
GetPort(&win);
@@ -669,7 +673,9 @@ ask(const char *format, ...)
(WindowPtr)-1L, false, 0, ask_ditl_h);
ShowWindow(dialog);
+ GetDItem(dialog, ok, &itype, &ihandle, &irect);
for (;;) {
+ outline_button((ControlHandle)ihandle);
ModalDialog(0, &hit);
if (hit == 1 || hit == 2)
break;
@@ -803,6 +809,23 @@ centered_sfput_dialog(void)
return p;
}
+void
+outline_button(ControlHandle button)
+{
+ PenState pen;
+ Rect rect;
+
+ SetPort((*button)->contrlOwner);
+ GetPenState(&pen);
+ PenNormal();
+
+ rect = (*button)->contrlRect;
+ InsetRect(&rect, -4, -4);
+ PenSize(3, 3);
+ FrameRoundRect(&rect, (rect.bottom - rect.top) / 2,
+ (rect.bottom - rect.top) / 2);
+ SetPenState(&pen);
+}
/*
* General Mac-specific non-GUI functions
@@ -1213,6 +1236,72 @@ FSReadLine(short frefnum, char *buf, size_t buflen)
return total_read;
}
+TrapType
+GetTrapType(unsigned long theTrap)
+{
+ if (BitAnd(theTrap, 0x0800) > 0)
+ return ToolTrap;
+
+ return OSTrap;
+}
+
+bool
+TrapAvailable(unsigned long trap)
+{
+ TrapType trapType = ToolTrap;
+ unsigned long numToolBoxTraps;
+
+ if (NGetTrapAddress(_InitGraf, ToolTrap) ==
+ NGetTrapAddress(0xAA6E, ToolTrap))
+ numToolBoxTraps = 0x200;
+ else
+ numToolBoxTraps = 0x400;
+
+ trapType = GetTrapType(trap);
+ if (trapType == ToolTrap) {
+ trap = BitAnd(trap, 0x07FF);
+ if (trap >= numToolBoxTraps)
+ trap = _Unimplemented;
+ }
+
+ return (NGetTrapAddress(trap, trapType) !=
+ NGetTrapAddress(_Unimplemented, ToolTrap));
+}
+
+void
+GetSystemFolder(short *vRefNumP, long *dirIDP)
+{
+ SysEnvRec info;
+ long wdProcID;
+
+ SysEnvirons(1, &info);
+ if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
+ *vRefNumP = 0;
+ *dirIDP = 0;
+ }
+}
+
+void
+GetSystemSubfolder(OSType folder, bool create, short *vRefNumP, long *dirIDP)
+{
+ bool hasFolderMgr = false;
+ long feature;
+
+ if (TrapAvailable(_GestaltDispatch) &&
+ Gestalt(gestaltFindFolderAttr, &feature) == noErr)
+ hasFolderMgr = true;
+
+ if (!hasFolderMgr) {
+ GetSystemFolder(vRefNumP, dirIDP);
+ return;
+ }
+
+ if (FindFolder(kOnSystemDisk, folder, create, vRefNumP,
+ dirIDP) != noErr) {
+ *vRefNumP = 0;
+ *dirIDP = 0;
+ }
+}
/*
* Gestalt functions
--- util.h Thu Mar 7 17:27:58 2024
+++ util.h Wed Mar 20 17:11:47 2024
@@ -40,12 +40,12 @@
} \
}
-#define CHARS_TO_LONG(a,b,c,d) (unsigned long)(\
+#define BYTES_TO_LONG(a,b,c,d) (unsigned long)(\
((unsigned long)((unsigned char)(a)) << 24) | \
((unsigned long)((unsigned char)(b)) << 16) | \
((unsigned long)((unsigned char)(c)) << 8) | \
(unsigned long)((unsigned char)(d)) )
-#define CHARS_TO_SHORT(a,b) (unsigned short)(\
+#define BYTES_TO_SHORT(a,b) (unsigned short)(\
((unsigned short)((unsigned char)(a)) << 8) | \
(unsigned short)((unsigned char)(b)) )
@@ -109,6 +109,7 @@ void window_rect(WindowPtr win, Rect *ret);
void center_in_screen(short width, short height, bool titlebar, Rect *b);
Point centered_sfget_dialog(void);
Point centered_sfput_dialog(void);
+void outline_button(ControlHandle button);
Handle xNewHandle(size_t size);
Handle xGetResource(ResType type, short id);
@@ -124,6 +125,11 @@ short FStat(Str255 path, struct stat *sb);
OSErr copy_file(Str255 source, Str255 dest, bool overwrite);
OSErr copy_file_contents(short source_ref, short dest_ref);
OSErr FSReadLine(short frefnum, char *buf, size_t buflen);
+TrapType GetTrapType(unsigned long theTrap);
+bool TrapAvailable(unsigned long trap);
+void GetSystemFolder(short *vRefNumP, long *dirIDP);
+void GetSystemSubfolder(OSType folder, bool create, short *vRefNumP,
+ long *dirIDP);
char * gestalt_machine_type(void);
char * get_version(bool long_version);