home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 256.lha / fonz / fonz.c < prev    next >
C/C++ Source or Header  |  1989-06-08  |  6KB  |  262 lines

  1. /*
  2.  * fonz - setfont for program that opens its own screen
  3.  *
  4.  * April 18, 1989
  5.  * A hack to subtitute my own font for Handshake.
  6.  * With a little more work, could be turn into a general purpose
  7.  * setfont for program openning its own screen. I am too lazy to do it.
  8.  *
  9.  * April 19, 1989
  10.  * Let's rename it "fonz".
  11.  * Make necessary changes so that "fonz" can be used in general.
  12.  * Usage: %s [-s font_size] [-w wait_max] font_name screen [window]
  13.  *            -s font_size: default to 8
  14.  *            -w wait_max: number of times to go around the wait loop
  15.  *            font_name: name of font
  16.  *            screen: screen name
  17.  *            window: name
  18.  * With no argument:
  19.  *    "fonz" printed out all the screens each followed by its windows
  20.  *
  21.  * If no window is specified, "fonz" will pick out the first NULL window
  22.  *
  23.  * April 20, 1989
  24.  * Let's call thist Version 1.0
  25.  * Bug found:
  26.  *    Use the [-w] flag, "run" the application. For example,
  27.  * runback c:fonz -w 12 clean Handshake; run df2:c/handshake
  28.  * Now, do something in your CLI while the application is loaded.
  29.  * Do a "status", for example, "fonz" will pretend that it does its job
  30.  * and exist but no font is changed to the application's window.
  31.  *
  32.  * Fix:
  33.  *     I don't know the reason for the bug. Attempt to fix by "delaying"
  34.  * 10 secs before do SetFont(). Seems to work OK now.
  35.  * If you know why please let me know.
  36.  *
  37.  * Hung Le (mott@ucscb.ucsc.edu)
  38.  *  
  39.  * compiled under Manx v3.4a (someday I will upgrade ... )
  40.  * 1> cc fonz.c 
  41.  * 1> ln fonz.o -lc 
  42.  */
  43.  
  44. #include <intuition/intuitionbase.h>
  45. #include <libraries/diskfont.h>
  46. #include <graphics/rastport.h>
  47. #include <graphics/text.h>
  48. #include <functions.h>
  49. #include <stdio.h>
  50.  
  51. #define INTUI_REV 0L
  52. #define usage(n) fprintf(stderr,"Usage: %s [-s font_size] [-w wait_max] font_name screen [window]\n", n)
  53. /* 500 ticks */
  54. #define TEN_SECS 500L
  55.  
  56. struct IntuitionBase *IntuitionBase = 0L;
  57. struct Window *Window = 0L;
  58. struct TextFont *Font = 0L;
  59. long DiskfontBase = 0L;
  60. long GfxBase = 0L;
  61.  
  62. int Font_Size = 0;
  63. int Wait_Max = 0;
  64.  
  65. /* not parsing workbench */
  66. _wb_parse() {}
  67.  
  68. main(ac,av)
  69. int ac;
  70. char *av[];
  71. {
  72.     char my_name[20];
  73.  
  74.     strcpy(my_name, av[0]);
  75.  
  76.     /* Open the necessary libraries */
  77.     Open_Libs();
  78.  
  79.     if (ac == 1)
  80.         print_windows();
  81.     else    
  82.     {
  83.         /* two global variables: Font_Size and Wait_Max are set */
  84.         while (av[1][0] == '-')
  85.         {
  86.             if (strcmp(av[1], "-s") == 0)
  87.             {
  88.                 Font_Size = atoi(av[2]);
  89.                 ac -= 2; av += 2;
  90.             }
  91.             else if (strcmp(av[1], "-w") == 0)
  92.             {
  93.                 Wait_Max = atoi(av[2]);
  94.                 ac -= 2; av += 2;
  95.             }
  96.             else
  97.             {
  98.                 /* ignore bad flag */
  99.                 ac--; av++;
  100.             }
  101.         }
  102.  
  103.         /*
  104.          * Make sure that at least the following two
  105.          * variables are set to default values
  106.          */
  107.         if (Font_Size <= 0)
  108.             Font_Size = 8;
  109.         if (Wait_Max < 0)
  110.             Wait_Max = 0;
  111.  
  112.         if ((ac == 3) || (ac == 4))
  113.             do_it(ac, av);
  114.         else
  115.         {
  116.             usage(my_name);
  117.             clean_up(1);
  118.         }
  119.     }
  120.  
  121.     /* Returns resouces to the Amiga */
  122.     clean_up(0);
  123. }
  124.  
  125. do_it(ac, av)
  126. int ac;
  127. char *av[];
  128. {
  129.     char font_name[20], screen_name[81], window_name[81];
  130.     struct TextAttr ta;
  131.     struct TextFont *old_font;
  132.     struct RastPort *rast_port;
  133.  
  134.     /* set up the parameters */
  135.     sprintf(font_name,"%s.font", av[1]);
  136.     strcpy(screen_name, av[2]);
  137.     if (ac == 4)
  138.         strcpy(window_name, av[3]);
  139.     else
  140.         window_name[0] = '\0';
  141.  
  142.     /* my text attributes */
  143.     ta.ta_Name = (UBYTE *) font_name;
  144.     ta.ta_YSize = (long) Font_Size;
  145.     ta.ta_Style = 0L;
  146.     ta.ta_Flags = FPF_DISKFONT;
  147.  
  148.     Font = (struct TextFont *) OpenDiskFont(&ta);
  149.     if (Font == (struct TextFont *) NULL)
  150.     {
  151.         fprintf(stderr,"Cannot find font \"%s %d\"\n", font_name, Font_Size);
  152.         clean_up(2);
  153.     }
  154.  
  155.     while (Wait_Max-- >= 0)
  156.     {
  157.         if (get_window(screen_name, window_name))
  158.         /* Window is a global variable and it is set in get_window() */
  159.         {
  160.             rast_port = Window->RPort;
  161.             old_font = rast_port->Font;
  162.             /* see heading comment box 04/20/89 */
  163.             if (Wait_Max >= 0) Delay(TEN_SECS);
  164.             if ( SetFont(rast_port, Font))
  165.                 Font = old_font;     
  166.             break;
  167.         }
  168.         else  if (Wait_Max >= 0) Delay(TEN_SECS);
  169.     }
  170.  
  171.     if (Window == (struct Window *) NULL)
  172.     {
  173.         fprintf(stderr,"Cannot find window \"%s\" in screen \"%s\"\n", window_name, screen_name);
  174.         clean_up(2);
  175.     }
  176. }
  177.  
  178. get_window(sn, wn)
  179. char *sn;
  180. char *wn;
  181. {
  182.  
  183.     struct Screen *screen;
  184.  
  185.     LockIBase(NULL);
  186.  
  187.     for (screen = IntuitionBase->FirstScreen; screen != (struct Screen *) NULL; screen = screen->NextScreen)
  188.     {
  189.         if (strncmp(screen->Title, sn, strlen(sn)) == 0)
  190.         {
  191.             for (Window = screen->FirstWindow; Window != (struct Window *) NULL; Window = Window->NextWindow)
  192.             {
  193.                 if (wn[0] == '\0')
  194.                 {
  195.                     if (Window->Title == (UBYTE *) NULL)
  196.                     {
  197.                         UnlockIBase(NULL);
  198.                         return(TRUE);
  199.                     }
  200.                 }
  201.                 else if (strncmp(Window->Title, wn, strlen(wn)) == 0)
  202.                 {
  203.                     UnlockIBase(NULL);
  204.                     return(TRUE);
  205.                 }
  206.             }
  207.         }
  208.     }
  209.     Window = (struct Window *) NULL;
  210.     UnlockIBase(NULL);
  211.     return(FALSE);
  212. }
  213.  
  214. clean_up(code)
  215. int code;
  216. {
  217.     if (Font) CloseFont(Font);
  218.     if (DiskfontBase) CloseLibrary(DiskfontBase);
  219.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  220.     if (GfxBase) CloseLibrary(GfxBase);
  221.     exit(code);
  222. }
  223.  
  224. Open_Libs()
  225. {
  226.     GfxBase = (long ) OpenLibrary("graphics.library", INTUI_REV);
  227.     if (GfxBase == NULL)
  228.     {
  229.         fprintf(stderr,"Cannot open graphics.library\n");
  230.         clean_up(1);
  231.     }
  232.  
  233.     IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", INTUI_REV);
  234.     if (IntuitionBase == (struct IntuitionBase *) NULL)
  235.     {
  236.         fprintf(stderr,"Cannot open intuition.library\n");
  237.         clean_up(1);
  238.     }
  239.  
  240.     DiskfontBase = (long ) OpenLibrary("diskfont.library", INTUI_REV);
  241.     if (DiskfontBase ==  NULL)
  242.     {
  243.         fprintf(stderr,"Cannot open diskfont.library\n");
  244.         clean_up(1);
  245.     }
  246. }
  247.  
  248. print_windows()
  249. {
  250.     struct Window *window;
  251.     struct Screen *screen;
  252.  
  253.     /* Should I lock the IntuitionBase? ... Nah ... */
  254.     for (screen = IntuitionBase->FirstScreen; screen != (struct Screen *) NULL; screen = screen->NextScreen)
  255.     {
  256.         printf("Screen: \"%s\"\n", screen->Title);
  257.         for (window = screen->FirstWindow; window != (struct Window *) NULL; window = window->NextWindow)
  258.             printf("Window: \"%s\"\n", window->Title);
  259.     }
  260. }
  261.  
  262.