jcs
/wikipedia
/amendments
/29
util: Sync upstream, add appicon_note()
This is note() but shows the app's main icon (ICN# 128) instead of a
default icon. This makes it useful for an about box.
jcs made amendment 29 over 2 years ago
--- main.c Thu Sep 1 09:33:48 2022
+++ main.c Tue Sep 6 18:54:34 2022
@@ -22,7 +22,7 @@
#include "focusable.h"
#include "util.h"
-MenuHandle file_menu, edit_menu;
+MenuHandle apple_menu, file_menu, edit_menu, view_menu;
bool quitting = false;
void handle_menu(long menu_id);
@@ -31,7 +31,6 @@ int
main(void)
{
Handle mbar;
- MenuHandle apple_menu;
EventRecord event;
WindowPtr event_win;
GrafPtr old_port;
@@ -58,6 +57,7 @@ main(void)
AddResMenu(apple_menu, 'DRVR');
file_menu = GetMHandle(FILE_MENU_ID);
edit_menu = GetMHandle(EDIT_MENU_ID);
+ view_menu = GetMHandle(VIEW_MENU_ID);
menu_defaults();
DrawMenuBar();
@@ -182,7 +182,7 @@ handle_menu(long menu_id)
VersRecHndl vers;
char vers_s[255];
char short_vers[255] = { 0 };
- short vlen;
+ short vlen, n;
if ((vers = (VersRecHndl)GetResource('vers', 1))) {
/*
@@ -196,10 +196,26 @@ handle_menu(long menu_id)
PtoCstr(short_vers);
snprintf(vers_s, sizeof(vers_s), "%s %s", PROGRAM_NAME,
short_vers);
+ for (n = 0; n < sizeof(vers_s); n++) {
+ if (vers_s[n] == '©') {
+ vers_s[n - 1] = '\r';
+ break;
+ }
+ }
ReleaseResource(vers);
- note("%s", vers_s);
+ appicon_note("%s", vers_s);
} else
warnx("Can't find version number!");
+ break;
+ }
+ default: {
+ Str255 da;
+ GrafPtr save_port;
+
+ GetItem(apple_menu, LoWord(menu_id), &da);
+ GetPort(&save_port);
+ OpenDeskAcc(da);
+ SetPort(save_port);
break;
}
}
--- util.c Tue Sep 6 12:34:51 2022
+++ util.c Tue Sep 6 18:57:47 2022
@@ -35,29 +35,31 @@
static char err_str[ERROR_STRING_SIZE];
/* basic DITL with an ok button (1), text area (2), and icon (3) */
+#define ALERT_DITL_OK 1
#define ALERT_DITL_ICON 3
static const char alert_ditl[] = {
- 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
- 0x00, 0xE6, 0x00, 0x5A, 0x01, 0x20, 0x04, 0x02,
- 0x4F, 0x4B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A,
- 0x00, 0x32, 0x00, 0x40, 0x01, 0x21, 0x08, 0x02,
- 0x5E, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A,
- 0x00, 0x0A, 0x00, 0x2A, 0x00, 0x2A, 0xA0, 0x02,
- 0x00, 0x02
+ 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E,
+ 0x00, 0xFA, 0x00, 0x64, 0x01, 0x34, 0x04, 0x02,
+ 0x4F, 0x4B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D,
+ 0x00, 0x4E, 0x00, 0x41, 0x01, 0x36, 0x08, 0x02,
+ 0x5E, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D,
+ 0x00, 0x17, 0x00, 0x2D, 0x00, 0x37, 0xA0, 0x02,
+ 0x00, 0x01
};
static Handle alert_ditl_h = NULL;
+/* ICN# to show for APPICON_ALERT */
+#define APPICON_ICN_ID 128
+
/* DITL with a Yes button (1), No button (2), text (3), and icon (4) */
static const char ask_ditl[] = {
- 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
- 0x00, 0xE6, 0x00, 0x5A, 0x01, 0x20, 0x04, 0x03,
- 0x59, 0x65, 0x73, 0x21, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x46, 0x00, 0xA0, 0x00, 0x5A, 0x00, 0xDA,
- 0x04, 0x02, 0x4E, 0x6F, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x0A, 0x00, 0x32, 0x00, 0x41, 0x01, 0x22,
- 0x08, 0x02, 0x5E, 0x30, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x0A, 0x00, 0x0A, 0x00, 0x2A, 0x00, 0x2A,
- 0xA0, 0x02, 0x00, 0x01
+ 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E,
+ 0x00, 0xFA, 0x00, 0x64, 0x01, 0x34, 0x04, 0x02,
+ 0x4F, 0x4B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D,
+ 0x00, 0x4E, 0x00, 0x41, 0x01, 0x36, 0x08, 0x02,
+ 0x5E, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D,
+ 0x00, 0x17, 0x00, 0x2D, 0x00, 0x37, 0xA0, 0x02,
+ 0x00, 0x01
};
static Handle ask_ditl_h = NULL;
@@ -75,7 +77,8 @@ static TEHandle track_control_te = NULL;
enum {
STOP_ALERT,
CAUTION_ALERT,
- NOTE_ALERT
+ NOTE_ALERT,
+ APPICON_ALERT
};
/*
@@ -321,6 +324,7 @@ xstrndup(const char *str, size_t maxlen, char *note)
return copy;
}
+
/*
* String functions
*/
@@ -439,21 +443,21 @@ void
vwarn(short alert_func, const char *format, va_list ap)
{
Rect bounds;
- short hit;
+ short hit, itype;
WindowPtr win, dialog;
-
+ Handle ihandle;
+ Rect irect;
+
GetPort(&win);
vsnprintf(err_str, ERROR_STRING_SIZE, format, ap);
ParamText(CtoPstr(err_str), "\p", "\p", "\p");
- center_in_screen(300, 100, false, &bounds);
+ center_in_screen(320, 110, false, &bounds);
dialog = NewDialog(nil, &bounds, "\p", false, dBoxProc,
(WindowPtr)-1L, false, 0, alert_ditl_h);
-#if 0
- /* XXX: why doesn't changing this work? */
GetDItem(dialog, ALERT_DITL_ICON, &itype, &ihandle, &irect);
switch (alert_func) {
case CAUTION_ALERT:
@@ -462,19 +466,23 @@ vwarn(short alert_func, const char *format, va_list ap
case NOTE_ALERT:
ihandle = GetIcon(noteIcon);
break;
+ case APPICON_ALERT:
+ ihandle = GetResource('ICN#', APPICON_ICN_ID);
+ if (ihandle)
+ break;
default:
ihandle = GetIcon(stopIcon);
}
- ihandle = GetIcon(cautionIcon);
SetDItem(dialog, ALERT_DITL_ICON, itype, ihandle, &irect);
-#endif
-
+
ShowWindow(dialog);
+
for (;;) {
ModalDialog(0, &hit);
if (hit == ok)
break;
}
+ ReleaseResource(ihandle);
DisposDialog(dialog);
/*
@@ -547,6 +555,16 @@ note(const char *format, ...)
va_end(ap);
}
+void
+appicon_note(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vwarn(APPICON_ALERT, format, ap);
+ va_end(ap);
+}
+
short
ask(const char *format, ...)
{
@@ -649,9 +667,24 @@ center_in_screen(short width, short height, bool title
b->bottom = b->top + height;
}
+Point
+centered_sf_dialog(void)
+{
+ Point p;
+ Rect r;
+
+ center_in_screen(364, 216, false, &r);
+ p.h = r.left;
+ p.v = r.top;
+
+ return p;
+}
+
+
/*
* General Mac-specific non-GUI functions
*/
+
static unsigned long _xorshift_state = 0;
unsigned long
xorshift32(void)
@@ -1200,12 +1233,14 @@ TEGetWidth(short off, TEHandle te)
#define ceildiv(a,b) ((a / b) + (!!(a % b)))
void
-UpdateScrollbarForTE(ControlHandle control, TEHandle te, bool reset)
+UpdateScrollbarForTE(GrafPtr win, ControlHandle control, TEHandle te, bool reset)
{
size_t vlines, telines;
TERec *ter;
+ RgnHandle rgn;
short fheight, fwidth, max, val, per_line, horiz, max_chars, n;
+ HLock(control);
HLock(te);
ter = *te;
@@ -1275,7 +1310,13 @@ UpdateScrollbarForTE(ControlHandle control, TEHandle t
(*control)->contrlMax = max;
SetCtlValue(control, val);
+ rgn = NewRgn();
+ RectRgn(rgn, &(*control)->contrlRect);
+ UpdtControl(win, rgn);
+ CloseRgn(rgn);
+
HUnlock(te);
+ HUnlock(control);
}
void
@@ -1665,7 +1706,7 @@ strsep(char **stringp, const char *delim)
}
return (NULL);
}
-
+
static struct format {
unsigned leftJustify : 1;
unsigned forceSign : 1;
--- util.h Tue Sep 6 12:34:32 2022
+++ util.h Tue Sep 6 18:50:37 2022
@@ -112,12 +112,14 @@ void err(short ret, const char *format, ...);
void warnx(const char *format, ...);
void warn(const char *format, ...);
void note(const char *format, ...);
+void appicon_note(const char *format, ...);
short ask(const char *format, ...);
#define ASK_YES 1
#define ASK_NO 2
void progress(char *format, ...);
void window_rect(WindowPtr win, Rect *ret);
void center_in_screen(short width, short height, bool titlebar, Rect *b);
+Point centered_sf_dialog(void);
Handle xNewHandle(size_t size);
Handle xGetResource(ResType type, short id);
@@ -140,7 +142,8 @@ char * get_version(bool long_version);
short FontHeight(short font_id, short size);
void DrawGrowIconOnly(WindowPtr win);
short TEGetWidth(short off, TEHandle te);
-void UpdateScrollbarForTE(ControlHandle scroller, TEHandle te, bool reset);
+void UpdateScrollbarForTE(GrafPtr win, ControlHandle scroller, TEHandle te,
+ bool reset);
void SetTrackControlTE(TEHandle te);
pascal void TrackMouseDownInControl(ControlHandle control, short part);
pascal bool ModalDialogFilter(DialogPtr dlg, EventRecord *event,
@@ -160,6 +163,5 @@ char * strndup(const char *str, size_t maxlen);
char * strsep(char **stringp, const char *delim);
int snprintf(char *s, size_t size, const char *fmt, ...);
int vsnprintf(char *s, size_t size, const char *fmt, void *p);
-
#endif