[net.sources.mac] ascii.source

roland@ttds.UUCP (Roland Karlsson) (10/15/85)

This is a program that displays fonts.

Features:
	1. Always look at current system fonts.
	2. Open files that contains fonts.
	3. Write sample text with key board.
	4. Write sample text with mouse.
	5. Copy sample text to desc scrap (for later pasting in MacWrite).

This posting is not only a distribution of my program. It is also a request
for HELP. There are some things that i don't manages to do right. Therefore
i will send three postings.
	1. Source code in SUMacC.
	2. hqx code.
	3. HELP.

This is: Source code.

==============================================================================
		Thanks in advance:
		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 New
X   (-
X    Quit<B
X
XType MENU
X  ,257(0)
X  Edit
X   (Undo
X   (-
X    Cut
X    Copy
X    Paste
X    Clear
X
XType CODE
X  b.out,0
X
XType BNDL = HEXA
X  ,128(0)
X* AscI 0
X*   ICN#
X*     0 128
X*   FREF
X*     0 128
X41736349 00000001 49434e23 00000000 00804652 45460000 00000080 
X
XType FREF = HEXA
X  ,128(0)
X* APPL 0
X4150504c 000000ff 
X
XType ICN# = HEXA
X  ,128(0)
X00010000 00028000 00044000 00082000 00101000 00200800 00400400 00bdc200
X01252100 02252080 043dc040 08252020 10252010 2025c008 40003f04 80604082
X40908041 20813022 1081c814 088e7f8f 04923007 02610007 01008007 00806007
X00401fe7 0020021f 00100407 00080800 00041000 00022000 00014000 00008000
X*
X00010000 00038000 0007c000 000fe000 001ff000 003ff800 007ffc00 00fffe00
X01ffff00 03ffff80 07ffffc0 0fffffe0 1ffffff0 3ffffff8 7ffffffc fffffffe
X7fffffff 3ffffffe 1ffffffc 0fffffff 07ffffff 03ffffff 01ffffff 00ffffff
X007fffff 003ffe1f 001ffc07 000ff800 0007f000 0003e000 0001c000 00008000
X
X
+ END-OF-FILE ascii.rc
chmod 'u=r,g=r,o=r' 'ascii.rc'
echo '	-r--r--r--  1 roland       1621 Aug 30 14:53 ascii.rc        (as sent)'
echo -n '	'
/bin/ls -l ascii.rc
echo 'Extracting ascii.h'
sed 's/^X//' > ascii.h << '+ END-OF-FILE ascii.h'
X#define TEScrpLength 0xab0
X#define TEScrpHandle 0xab4
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 OPEN 1
X#define QUIT 3
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
XMenuHandle my_menus[MENUS];
Xint 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, root_system, my_own_file, 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        857 Mar 29  1985 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 <mac/quickdraw.h>
X#include <mac/toolintf.h>
X#include <mac/osintf.h>
X#include <mac/packintf.h>
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    init_the_resource ();        /* This must be the last one ... */
X
X/* The Main Event LOOP */
X    while (1){
X	TEIdle (h_text);
X	SystemTask ();
X	GetNextEvent (everyEvent, &my_event);
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	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 keyDown:
X		if (the_w != FrontWindow ())
X		    break;
X		do_key ();
X		break;
X	    case activateEvt:
X		if (the_w == FrontWindow ()){
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		BeginUpdate (the_w);
X		SetCursor (*watch_c);
X		draw_the_w ();
X		TEUpdate (&text_r, h_text);
X		ShowCursor ();
X		EndUpdate (the_w);
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    c2p_string(*sample_str, temp_str);
X    TESetText (temp_str, (int) (*sample_str)[0], h_text);
X    c2p_string(*author_str, temp_str);
X    TESetText (temp_str, (int) (*author_str)[0], h_authortext);
X}
X
Xinit_the_resource (){
X    file_num = root_system = HomeResFile ( GetIndResource ("FONT", 1) );
X    my_own_file = CurResFile ();
X}
X
Xchange_font (size) int size;{   /* 0 means new font => chose smallest */
X    int font, i;
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    (*h_text)->fontAscent =  (size * 3) / 4;          /* How do this right ? */
X    (*h_text)->lineHeight =  (*h_text)->txSize = size;
X    (*h_text)->txFont = font;
X    TECalText (h_text);
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    int i, dummy;
X
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 ("<O", name);
X	    AppendMenu (my_menus[4], name);
X	    font_sizes[++num_of_sizes] = i;
X	}
X	else
X	    if (i == 9 || i == 10 || i == 12 || i == 14 || i == 18){
X		NumToString (i, name);
X		AppendMenu (my_menus[4], name);
X		font_sizes[++num_of_sizes] = i;
X	    }
X    InsertMenu (my_menus[4], 0);
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
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 (finger_c);
X    watch_c = GetCursor (WATCH); HNoPurge (watch_c);
X    bar_c = GetCursor (BAR); HNoPurge (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    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
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) int help;{
X    char name[64];
X    int i;
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		    OpenDeskAcc (name);
X		else                                /* With leading zero    */
X		    OpenDeskAcc (da_name (name));
X		SetPort (save_port);
X		}
X	    break;
X	case FILE:
X	    switch (the_item){
X		case OPEN:
X		    open_file ();
X		    break;
X		case QUIT:
X		    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 ();        Dosn't work yet   */
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	TEClick (&mouse_p, 0, h_text);
X}
X
Xput_scrap (){
X    Handle text_scrap;
X    ScrapStuff *scrap_stuff;
X
X    ZeroScrap ();
X    text_scrap = (Ptr) TEScrpHandle;
X    text_scrap = (Ptr) *text_scrap;
X    HLock (text_scrap);
X    PutScrap (GetHandleSize (text_scrap), "TEXT", *text_scrap);
X    HUnlock (text_scrap);
X    scrap_stuff = InfoScrap ();
X    scrap_count = scrap_stuff->scrapCount;
X}
X
X/* Dosn't work yet --------------------------------------------------------
Xget_scrap (){
X    Handle hdl, text_scrap;
X    int text_length, off, *text_length_ptr;
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
X    hdl = NewHandle (0);
X    text_length = GetScrap (hdl, "TEXT", &off);
X    if (text_length > 0){
X	text_scrap =  (Ptr) TEScrpHandle;
X	text_scrap =  (Ptr) *text_scrap;
X	SetHandleSize (text_scrap, text_length);
X	text_length_ptr =  (Ptr) TEScrpLength;
X	*text_length_ptr = text_length;
X	HLock (hdl);
X	HLock (text_scrap);
X	BlockMove (*hdl, *text_scrap, text_length);
X	HUnlock (hdl);
X	HUnlock (text_scrap);
X    }
X    DisposHandle (hdl);
X}
X---------------------------------------------------------------------------*/
X
Xopen_file (){
X    Point top_left;
X
X    SetPt (&top_left, 50, 50);
X    SFGetFile (&top_left, "", (ProcPtr) 0, -1, "", (ProcPtr) 0, &file_record);
X    if (file_record.good){
X	if (file_num != root_system && file_num != my_own_file){
X	    SetVol (0, volume_num);
X	    CloseResFile (file_num);
X	}
X	volume_num = file_record.vRefNum;
X	SetVol (0, volume_num);
X	p2cstr (file_record.fName);
X	if (! EqualString ("System", file_record.fName, 0, 0) ){
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	else{
X	    EraseRect (&ascii_r);
X	    InvalRect (&ascii_r);
X	    MoveTo (30, 30);
X	    DrawString ("Can't open System file");
X	    Delay (60);
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)
X	char *s;
X{
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 */
Xc2p_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}
+ END-OF-FILE ascii.c
chmod 'u=r,g=r,o=r' 'ascii.c'
echo '	-r--r--r--  1 roland      11565 Apr  1  1985 ascii.c        (as sent)'
echo -n '	'
/bin/ls -l ascii.c
exit 0