Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83 (MC840302); site ttds.UUCP
Path: utzoo!linus!philabs!cmcl2!seismo!mcvax!enea!ttds!roland
From: roland@ttds.UUCP (Roland Karlsson)
Newsgroups: net.sources.mac
Subject: new and better ascii source.
Message-ID: <1041@ttds.UUCP>
Date: Thu, 31-Oct-85 17:35:57 EST
Article-I.D.: ttds.1041
Posted: Thu Oct 31 17:35:57 1985
Date-Received: Sun, 3-Nov-85 14:38:53 EST
Reply-To: roland@ttds.UUCP (Roland Karlsson)
Organization: The Royal Inst. of Techn., Stockholm
Lines: 779

This is a new version of the Ascii program for displaying fonts. I am very
greatfull for the help i got from poeple on the net. Most of all for the
help i got from Guido van Rossum (guido@mcvax). Copying from and pasting
to the desc scrap is now possible. Scaling of font and the file filter
that chose which files to show with SFGetFile works. I have implemented
command key equivalents for menu items.

This is: SUMacC Source code.
This is not: .hqx code (but i post it at the same time)

==============================================================================
		Have a nice day wishes:
		Roland Karlsson  (roland@ttds)
		Dpt. of Telecomm. & Computer Systems
		Royal Institute of Technology
		S-100 44 Stockholm
		SWEDEN
==============================================================================



------ Cut here ------------------------------ and here ---------------------
: This is a shar archive.  Extract with sh, not csh.
: This archive ends with exit, so do not worry about trailing junk.
echo 'Extracting ascii.rc'
sed 's/^X//' > ascii.rc << '+ END-OF-FILE ascii.rc'
Xascii.rsrc
X*
XType AscI = STR
X  ,0(0)
Xascii is a font displaying program by Roland Karlsson made 1985.
X
X* pointing index finger
XType CURS = HEXA
X  ,256(0)
X00002000 51402aa0 15500a88 1d041602 08010400 03e00010 00080004 00000000
X00002000 71403be0 1ff00ff8 1ffc1ffe 0fff07fe 03fe001c 000c0004 00000000
X00010001 
X
XType STR
X  ,1(0)
XThis is a sample text.
X
XType STR
X  ,2(0)
XRoland Karlsson, Dpt. of Telecomm. & Computer Systems, Royal Institute of Technology, S-100 44 Stockholm, SWEDEN
X
XType WIND
X  ,128(0)
X  Cursed
X  20 0 342 512
X  Visible noGoAway
X  2
X  0
X
XType MENU
X  ,1(0)
X* @ is a dummy for the apple mark (Silly thing this !! ).
X  @
X    About ascii
X   (-
X
XType MENU
X  ,256(0)
X  File
X    Open
X    Open FONT file
X    Open file with FONT resource This is slow
X   (-
X    Quit ascii.h << '+ END-OF-FILE ascii.h'
X/* This is because of bug in compiler */
X#ifdef lint
X#       define VOID (void)
X#else
X#       define VOID
X#endif
X
X/* This is for compatibility with other C compilers */
X#define long int
X
X#define WINDOW 128
X
X#define SAMPLESTR 1
X#define AUTHORSTR 2
X
X#define MENUS 5
X#define APPLE 1
X#define ABOUT 1
X#define FILE 256
X#define OPEN1 1
X#define OPEN2 2
X#define OPEN3 3
X#define QUIT 5
X#define EDIT 257
X#define TEUNDO 1
X#define TECUT 3
X#define TECOPY 4
X#define TEPASTE 5
X#define TECLEAR 6
X#define FONTS 258
X#define FONTSIZE 259
X
X#define BAR 1
X#define WATCH 4
X#define FINGER 256
X
X#define NONE -1
XMenuHandle my_menus[MENUS];
Xlong the_menu, the_item;
XEventRecord my_event;
XCursHandle finger_c, watch_c, bar_c;
XWindowPtr which_w, the_w;
XWindowRecord w_record;
XRect ascii_r, text_r, view_r, char_r[16][16];
XPoint mouse_p;
XHandle sample_str, author_str;
XTEHandle h_text, h_authortext;
Xint which_font;
Xchar font_name[64];
Xint font_sizes[64];
Xint num_of_sizes = 0;
Xshort scrap_count;
Xint file_num, volume_num;
XSFReply file_record;
+ END-OF-FILE ascii.h
chmod 'u=r,g=r,o=r' 'ascii.h'
echo '	-r--r--r--  1 roland       1013 Oct 30 08:36 ascii.h        (as sent)'
echo -n '	'
/bin/ls -l ascii.h
echo 'Extracting ascii.c'
sed 's/^X//' > ascii.c << '+ END-OF-FILE ascii.c'
X#include 
X#include 
X#include 
X#include 
X#include "ascii.h"
X
Xmain () {
X    /* Standard initialisation */
X    struct QDVar QDVar;
X
X    QD = &QDVar;
X    InitGraf (&thePort);
X    InitFonts ();
X    FlushEvents (everyEvent, 0);
X    InitWindows ();
X    InitMenus ();
X    TEInit ();
X    InitDialogs ( (ProcPtr) 0);
X/*  InitCursor ();                       Done in the LOOP  */
X
X    /* Application dependent initialisation */
X    init_the_w ();
X    init_the_text ();
X    init_the_menu ();
X    init_the_scrap ();
X    init_the_cursor ();
X    file_num = NONE;
X
X/* The Main Event LOOP */
X    while (1){
X	TEIdle (h_text);
X	SystemTask ();
X
X	VOID GetNextEvent (everyEvent, &my_event);
X
X	if (the_w == FrontWindow ()){
X	    adjust_cursor ();
X	    mouse_p.h = my_event.where.h;
X	    mouse_p.v = my_event.where.v;
X	    GlobalToLocal (&mouse_p);
X	}
X
X	switch (my_event.what){
X	    case mouseDown:
X		switch (FindWindow (&my_event.where, &which_w)){
X		    case inMenuBar:
X			do_command (MenuSelect (&my_event.where));
X			break;
X		    case inContent:
X			if (the_w == FrontWindow ())
X			    the_w_click ();
X			else
X			    SelectWindow (which_w);
X			break;
X		    case inDrag:
X			break;    /* No drag done */
X		    case inGoAway:
X			break;    /* Ne exit availible */
X		    case inSysWindow:
X			SystemClick (&my_event, which_w);
X			break;
X		}
X		break;
X	    case autoKey:
X	    case keyDown:
X		if (the_w == FrontWindow ())
X		    do_key ();
X		break;
X	    case activateEvt:
X		if (the_w == (WindowPtr) my_event.message){
X		    TEActivate (h_text);
X		    DisableItem (my_menus[2], TEUNDO);
X		    EnableItem (my_menus[3], 0);
X		    EnableItem (my_menus[4], 0);
X		}
X		else{
X		    TEDeactivate (h_text);
X		    EnableItem (my_menus[2], TEUNDO);
X		    DisableItem (my_menus[3], 0);
X		    DisableItem (my_menus[4], 0);
X		}
X		DrawMenuBar ();
X		break;
X	    case updateEvt:
X		if (the_w == (WindowPtr) my_event.message){
X		    BeginUpdate (the_w);
X		    SetCursor (*watch_c);
X		    draw_the_w ();
X		    TEUpdate (&text_r, h_text);
X		    ShowCursor ();
X		    EndUpdate (the_w);
X		}
X		break;
X	 }
X    }
X}
X
Xinit_the_w (){
X    int x, y;
X
X    the_w = GetNewWindow (WINDOW, &w_record,  (WindowPtr) - 1);
X    SetRect (&ascii_r, 18, 18, 418, 320);
X    SetRect (&text_r, 425, 18, 500, 317);
X    SetRect (&view_r, 420, 0, 512, 322);
X    for (x = 0; x < 16; x++)
X	for (y = 0; y < 16; y++)
X	    SetRect (&char_r[x][y], 18+25*x, 18+19*y, 43+25*x, 37+19*y);
X    SetPort (the_w);
X}
X
Xinit_the_text (){
X    char temp_str[256];
X
X    h_text = TENew (&text_r, &view_r);               /* view_r > text_r */
X    h_authortext = TENew (&text_r, &view_r);
X    sample_str = GetString(SAMPLESTR);
X    author_str = GetString(AUTHORSTR);
X    p2c_string(*sample_str, temp_str);
X    TESetText (temp_str, (int) (*sample_str)[0], h_text);
X    p2c_string(*author_str, temp_str);
X    TESetText (temp_str, (int) (*author_str)[0], h_authortext);
X}
X
Xchange_font (size) int size;{   /* 0 means new font => chose smallest */
X    int font, i;
X    TEHandle TEnew_h;
X    Handle save_h;
X    short int save_length,save_start, save_end;
X
X    for (i = 1; i <= num_of_sizes; i++)
X	CheckItem (my_menus[4], i, 0);
X    GetItem (my_menus[3], which_font, font_name);
X    GetFNum (font_name, &font);
X    if (! size){
X	for (i = 1; i <= num_of_sizes; i++)
X	    if (RealFont (font, font_sizes[i])){
X		size = font_sizes[i];
X		break;
X	    }
X	if (size > 18)
X	    size = 12;
X    }
X    if (! size)
X	size = 12;
X
X    TextFont(font);
X    TextSize(size);
X
X    save_h = (*h_text)->hText;              /* Save handle to text */
X    save_length = (*h_text)->teLength;
X    save_start = (*h_text)->selStart;
X    save_end = (*h_text)->selEnd;
X
X    TEnew_h = TENew(&text_r, &view_r);      /* Create new record */
X
X    (*h_text)->hText = (*TEnew_h)->hText;   /* Change handle in old record */
X    TEDispose(h_text);                      /* Dispose of old record */
X
X    h_text = TEnew_h;                       /* Rename new record */
X
X    (*h_text)->hText = save_h;              /* Restore handle to text */
X    (*h_text)->teLength = save_length;
X    (*h_text)->selStart = save_start;
X    (*h_text)->selEnd = save_end;
X
X    TEActivate(h_text);
X    TECalText (h_text);
X
X    for (i = 1; i <= num_of_sizes; i++)
X	if (the_w->txSize == font_sizes[i])
X	    CheckItem (my_menus[4], i, 1);
X}
X
Xinit_the_menu (){
X    my_menus[0] = GetMenu(APPLE);
X    (**my_menus[0]).menuData.s[1] = '\024'; /* Silly thing this !! */
X    AddResMenu (my_menus[0], "DRVR");
X    InsertMenu (my_menus[0], 0);
X
X    my_menus[1] = GetMenu (FILE);
X    InsertMenu (my_menus[1], 0);
X
X    my_menus[2] = GetMenu (EDIT);
X    InsertMenu (my_menus[2], 0);
X
X    add_font_menu ();
X
X    DrawMenuBar ();
X}
X
Xadd_font_menu (){
X    my_menus[3] = NewMenu (FONTS, "Font");
X    AddResMenu (my_menus[3], "FONT");
X    InsertMenu (my_menus[3], 0);
X    which_font = 1;
X    CheckItem (my_menus[3], which_font, 1);
X    change_font (12);
X    add_size_menu ();
X    change_font (0);
X}
X
Xadd_size_menu (){
X    char name[64];
X    int i;
X
X    my_menus[4] = NewMenu (FONTSIZE, font_name);
X
X    num_of_sizes = 0;
X    for (i = 1; i < 128; i++)
X	if (RealFont (the_w->txFont, i)){
X	    NumToString (i, name);
X	    append_string ("txSize == font_sizes[i])
X	    CheckItem (my_menus[4], i, 1);
X}
X
Xappend_string (app, str) char *app, *str;{
X    int i, j;
X
X    i = 0;
X    while (str[i++] != 0 && i < 60);
X    i--;
X
X    j = 0;
X    while (app[j] != 0 && i + j < 60)
X	str[i++] = app[j++];
X    str[i] = 0;
X}
X
Xinit_the_cursor (){
X    finger_c = GetCursor (FINGER); HNoPurge ((Handle) finger_c);
X    watch_c = GetCursor (WATCH); HNoPurge ((Handle) watch_c);
X    bar_c = GetCursor (BAR); HNoPurge ((Handle) bar_c);
X}
X
Xinit_the_scrap (){
X    ScrapStuff *scrap_stuff;
X
X    scrap_stuff = InfoScrap ();
X    scrap_count = ! ( scrap_stuff->scrapCount );
X/*  get_scrap ();                Dosn't work yet   */
X}
X
Xdo_key (){
X    char the_char;
X
X    the_char = my_event.message & 0xff;
X    if(my_event.modifiers & cmdKey)
X	do_command(MenuKey(the_char));
X    else{
X	InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
X	TEKey (the_char, h_text);
X	InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
X    }
X}
X
Xdraw_the_w (){
X    int x, y, save_font, save_size;
X
X    EraseRect (&the_w->portRect);
X    FrameRect (&ascii_r);
X
X    for (x = 0; x < 16; x++){
X	save_font = the_w->txFont;
X	save_size = the_w->txSize;
X	the_w->txFont = systemFont;
X	the_w->txSize = 12;
X	MoveTo (5, char_r[0][x].top + 14);
X	if (x < 10)
X	    DrawChar ( (char) ( x + '0') );
X	else
X	    DrawChar ( (char) ( x + 'a' - 10) );
X	MoveTo (char_r[x][0].topLeft.h + 5, 10);
X	if (x < 10)
X	    DrawChar ( (char) ( x + '0') );
X	else
X	    DrawChar ( (char) ( x + 'a' - 10) );
X	the_w->txFont = save_font;
X	the_w->txSize = save_size;
X	for (y = 0; y < 16; y++){
X	    MoveTo (char_r[x][y].topLeft.h + 5, char_r[x][y].top + 14);
X	    DrawChar ( (char) ( x*16 + y ) );
X	}
X    }
X}
X
Xdo_command (help) long help;{
X    char name[64];
X
X    the_menu =  HiWord (help);
X    the_item =  LoWord (help);
X    switch (the_menu){
X	case APPLE:
X	    if(the_item == ABOUT){
X		about();
X		break;
X	    }
X	    if(the_item == ABOUT + 1)
X		break;
X	    GetItem (my_menus[0], the_item, name);
X		{
X		GrafPtr save_port;
X		char *da_name ();
X
X		GetPort (&save_port);
X		if (GetNamedResource ("DRVR", name))/* Without leading zero */
X		    VOID OpenDeskAcc (name);
X		else                                /* With leading zero    */
X		    VOID OpenDeskAcc (da_name (name));
X		SetPort (save_port);
X		}
X	    break;
X	case FILE:
X	    switch (the_item){
X		case OPEN1:
X		case OPEN2:
X		case OPEN3:
X		    open_file (the_item);
X		    break;
X		case QUIT:
X		    VOID LoadScrap ();
X		    ExitToShell ();
X		    break;
X	    }
X	    break;
X	case EDIT:
X	    if (SystemEdit (the_item - 1))
X		break;
X	    switch (the_item){
X		case TECUT:
X		    TECut (h_text);
X		    put_scrap ();
X		    break;
X		case TECOPY:
X		    TECopy (h_text);
X		    put_scrap ();
X		    break;
X		case TEPASTE:
X		    get_scrap ();
X		    TEPaste (h_text);
X		    break;
X		case TECLEAR:
X		    TEDelete (h_text);
X		    break;
X	    }
X	    break;
X	case FONTS:
X	    CheckItem (my_menus[3], which_font, 0);
X	    which_font = the_item;
X	    change_font (0);
X	    CheckItem (my_menus[3], which_font, 1);
X
X	    num_of_sizes = 0;
X	    DeleteMenu (FONTSIZE);
X	    add_size_menu ();
X	    DrawMenuBar ();
X
X	    InvalRect (&the_w->portRect);
X	    break;
X	case FONTSIZE:
X	    change_font (font_sizes[the_item]);
X	    InvalRect (&the_w->portRect);
X	break;
X    }
X    HiliteMenu (0);
X}
X
Xadjust_cursor (){
X    if (PtInRect (&mouse_p, &ascii_r)){
X	SetCursor (*finger_c);
X	return;
X    }
X    if (PtInRect (&mouse_p, &text_r)){
X	SetCursor (*bar_c);
X	return;
X    }
X    InitCursor ();
X}
X
Xabout(){
X    EraseRect (&text_r);
X    TEUpdate (&text_r, h_authortext);
X    while (! Button ());
X    while (Button ());
X    EraseRect (&text_r);
X    TEUpdate (&text_r, h_text);
X    FlushEvents (everyEvent, 0);
X}
X
Xthe_w_click (){
X    int x, y;
X
X    if (PtInRect (&mouse_p, &ascii_r)){
X	x =  (mouse_p.h - 18) / 25;
X	y =  (mouse_p.v - 18) / 19;
X	InvertRect (&char_r[x][y]);
X	TEKey ( (char) ( x*16 + y ) , h_text);
X	InvertRect (&char_r[x][y]);
X    }
X    if (PtInRect (&mouse_p, &text_r))
X	if(my_event.modifiers & shiftKey)
X	    TEClick (&mouse_p, 1, h_text);
X	else
X	    TEClick (&mouse_p, 0, h_text);
X}
X
Xput_scrap (){
X    ScrapStuff *scrap_stuff;
X
X    VOID ZeroScrap ();
X    HLock (TEScrapHandle);
X    VOID PutScrap (GetHandleSize (TEScrapHandle), "TEXT", *TEScrapHandle);
X    HUnlock (TEScrapHandle);
X    scrap_stuff = InfoScrap ();
X    scrap_count = scrap_stuff->scrapCount;
X}
X
Xget_scrap (){
X    int off, len;
X    ScrapStuff *scrap_stuff;
X
X    scrap_stuff = InfoScrap ();
X    if (scrap_count == scrap_stuff->scrapCount)
X	return;
X    scrap_count = scrap_stuff->scrapCount;
X    len = GetScrap (TEScrapHandle, "TEXT", &off);
X    if (len >= 0)
X        TEScrapLen = len;
X    /* Else, it was probably a PICT or other type of scrap;
X       in this case TEScrapHandle's contents has not been touched. */
X}
X
Xopen_file (item) int item;{
X    Point top_left;
X    int file_filter1(), file_filter2();
X    OsType file_type;
X
X    SetPt (&top_left, 50, 50);
X    switch(item){
X	case OPEN1:
X	    SFGetFile (&top_left, "", file_filter1, -1,
X		       (char *) 0, (ProcPtr) 0, &file_record);
X	    break;
X	case OPEN2:
X	    file_type.s[0] = 'F';
X	    file_type.s[1] = 'F';
X	    file_type.s[2] = 'I';
X	    file_type.s[3] = 'L';
X	    SFGetFile (&top_left, "", file_filter1, 1,
X		       (char *) &file_type, (ProcPtr) 0, &file_record);
X	    break;
X	case OPEN3:
X	    SFGetFile (&top_left, "", file_filter2, -1,
X		       (char *) 0, (ProcPtr) 0, &file_record);
X	    break;
X    }
X    if (file_record.good){
X	if (file_num != NONE){
X	    VOID SetVol ((char *) 0, volume_num);
X	    CloseResFile (file_num);
X	}
X	volume_num = file_record.vRefNum;
X	VOID SetVol ((char *) 0, volume_num);
X	p2cstr ((char *) file_record.fName);
X	file_num = OpenResFile (file_record.fName);
X	DeleteMenu (FONTS);
X	DeleteMenu (FONTSIZE);
X	add_font_menu ();
X	DrawMenuBar ();
X	InvalRect (&the_w->portRect);
X    }
X}
X
X/* This filter dosn't open System file, Ascii Application file and
X   files without resource fork. */
X
X
Xfile_filter1() {
X  struct pargs {
X    FileParam *paramBlock;
X  } pargs;
X  char *retval= (char *) getpargs(&pargs, sizeof pargs);
X  if (pargs.paramBlock->ioFlRLgLen == 0)
X    *retval= 1; /* TRUE => skip this file */
X  else{
X    char fdType[5], fdCreator[5];
X
X    Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdType, fdType);
X    Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdCreator, fdCreator);
X    if(EqualString("ZSYS", fdType, 0, 0) &&
X       EqualString("MACS", fdCreator, 0, 0) ||
X       EqualString("APPL", fdType, 0, 0) &&
X       EqualString("AscI", fdCreator, 0, 0))
X      *retval= 1; /* TRUE => skip this file */
X    else
X      *retval= 0; /* FALSE => show this file*/
X  }
X}
X
X/* This filter dosn't open System file, Ascii Application file,
X   files without resource fork and files whithout FONT resource. */
X
X
Xfile_filter2() {
X  struct pargs {
X    FileParam *paramBlock;
X  } pargs;
X  char *retval= (char *) getpargs(&pargs, sizeof pargs);
X  if (pargs.paramBlock->ioFlRLgLen == 0)
X    *retval= 1; /* TRUE => skip this file */
X  else{
X    char fdType[5], fdCreator[5];
X
X    Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdType, fdType);
X    Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdCreator, fdCreator);
X    if(EqualString("ZSYS", fdType, 0, 0) &&
X       EqualString("MACS", fdCreator, 0, 0) ||
X       EqualString("APPL", fdType, 0, 0) &&
X       EqualString("AscI", fdCreator, 0, 0))
X      *retval= 1; /* TRUE => skip this file */
X    else{
X      int num_resources, f_num;
X      char f_name[256];
X
X      VOID SetVol ((char *) 0, pargs.paramBlock->ioVRefNum);
X      num_resources = CountResources("FONT");
X      p2c_string((char *) pargs.paramBlock->ioNamePtr, f_name);
X      f_num = OpenResFile (f_name);
X      if(CountResources("FONT") > num_resources)
X	*retval= 0; /* FALSE => show this file*/
X      else
X	*retval= 1; /* TRUE => skip this file */
X      CloseResFile(f_num);
X    }
X  }
X}
X
X/*
X * Some desk accessories have weird names that begin with a leading zero.
X * Since this fools the automatic C/Pascal string conversion stuff, we
X * have this little gem;  given a C string, it returns a Pascal string
X * with leading zero.
X */
Xchar *da_name (s) char *s; {
X	static char ps[32];
X	int i = 1;
X	register char *cp,*dp;
X
X	cp = s;
X	dp = &ps[2];
X	while ((*dp++ = *cp++))
X		i++;
X	ps[0] = i;
X	return (isapstr (ps));
X}
X
X/*
X * Copy a Pascal string into a buffer, converting it into a C string.
X */
Xp2c_string(pSource, cDestination)
Xchar *cDestination;
Xchar *pSource;
X{
X       int i;
X
X       for (i = 0; i < pSource[0]; ++i)
X	       cDestination[i] = pSource[i + 1];
X       cDestination[i] = '\0';
X}
X
X/*
X * Copy a OsType string into a buffer, converting it into a C string.
X */
XOs2c_string(OsSource, cDestination)
Xchar *cDestination;
XOsType OsSource;
X{
X       int i;
X
X       for (i = 0; i < 4; ++i)
X	       cDestination[i] = OsSource.s[i];
X       cDestination[4] = '\0';
X}
+ END-OF-FILE ascii.c
chmod 'u=r,g=r,o=r' 'ascii.c'
echo '	-r--r--r--  1 roland      14358 Oct 30 11:03 ascii.c        (as sent)'
echo -n '	'
/bin/ls -l ascii.c
exit 0