home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / graphics / fonts / example6.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  12KB  |  341 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: Fonts                       Tulevagen 22       */
  8. /* File:    Example6.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how you can use a font loaded */
  21. /* from a disk in your own Intuition programs.             */
  22.  
  23.  
  24.  
  25. #include <intuition/intuition.h>
  26.  
  27.  
  28. /* Max 50 characters: */
  29. #define MAXCHAR 50
  30.  
  31.  
  32. struct IntuitionBase *IntuitionBase; /* Intuition library. */
  33. struct GfxBase *GfxBase;             /* Graphics library.  */
  34. struct Library *DiskfontBase;        /* DiskFont library.  */
  35.  
  36.  
  37.  
  38. /*****************/
  39. /* THE NEW FONT: */
  40. /*****************/
  41.  
  42. /* The new font's attributes: */
  43. struct TextAttr my_font_attr=
  44. {
  45.   "Helvetica.font", /* Name of the font.  */
  46.   24,               /* Height (in pixels) */
  47.   FS_NORMAL,        /* Style              */
  48.   FPF_DISKFONT      /* Exist on Disk.     */
  49. };
  50.  
  51. /* Pointer to our new font: */
  52. struct TextFont *my_font;
  53.  
  54.  
  55.  
  56. /***********************************/
  57. /* THE STRING GADGET's STRUCTURES: */
  58. /***********************************/
  59.  
  60. /* The coordinates for the box: */
  61. SHORT my_points[]=
  62. {
  63.    -7, -4, /* Start at position (-7, -4) */
  64.   200, -4, /* Draw a line to the right to position (200,-4) */
  65.   200, 11, /* Draw a line down to position (200,11) */
  66.    -7, 11, /* Draw a line to the right to position (-7,11) */
  67.    -7, -4  /* Finish of by drawing a line up to position (-7,-4) */ 
  68. };
  69.  
  70. /* The Border structure: */
  71. struct Border my_border=
  72. {
  73.   0, 0,        /* LeftEdge, TopEdge. */
  74.   1,           /* FrontPen, colour register 1. */
  75.   0,           /* BackPen, for the moment unused. */
  76.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  77.   5,           /* Count, 5 pair of coordinates in the array. */
  78.   my_points,   /* XY, pointer to the array with the coordinates. */
  79.   NULL,        /* NextBorder, no other Border structures are connected. */
  80. };
  81.  
  82. /* The IntuiText structure: */
  83. struct IntuiText my_text=
  84. {
  85.   1,             /* FrontPen, colour register 1. */
  86.   0,             /* BackPen, colour register 0. */
  87.   JAM1,          /* DrawMode, draw the characters with colour 1, do not */
  88.                  /* change the background. */ 
  89.   -65, -8,       /* LeftEdge, TopEdge. */
  90.   &my_font_attr, /* ITextFont, use default font. */
  91.   "Text:",       /* IText, the text that will be printed. */
  92.   NULL,          /* NextText, no other IntuiText structures. */
  93. };
  94. /* Once the font has been loaded you can start to use */
  95. /* the IntuiText structure with a pointer to the new  */
  96. /* font's attributes. Since the text is connected to  */
  97. /* a gadget which is connected to a window, we must   */
  98. /* open the new font BEFORE we open the window!       */
  99.  
  100. UBYTE my_buffer[MAXCHAR];      /* Max nr. characters including the NULL-sign. */
  101. UBYTE my_undo_buffer[MAXCHAR]; /* Must be at least as big as my_buffer.       */
  102.  
  103. struct StringInfo my_string_info=
  104. {
  105.   my_buffer,       /* Buffer, pointer to a null-terminated string. */
  106.   my_undo_buffer,  /* UndoBuffer, pointer to a null-terminated string. */
  107.                    /* (Remember my_buffer is equal to &my_buffer[0]) */
  108.   0,               /* BufferPos, initial position of the cursor. */
  109.   MAXCHAR,         /* MaxChars, (50) characters + null-sign ('\0'). */
  110.   0,               /* DispPos, first character in the string should be */
  111.                    /* first character in the display. */
  112.  
  113.   /* Intuition initializes and maintaines these variables: */
  114.  
  115.   0,               /* UndoPos */
  116.   0,               /* NumChars */
  117.   0,               /* DispCount */
  118.   0, 0,            /* CLeft, CTop */
  119.   NULL,            /* LayerPtr */
  120.   NULL,            /* LongInt */
  121.   NULL,            /* AltKeyMap */
  122. };
  123.  
  124. struct Gadget my_gadget=
  125. {
  126.   NULL,          /* NextGadget, no more gadgets in the list. */
  127.   80,            /* LeftEdge, 80 pixels out. */
  128.   30,            /* TopEdge, 30 lines down. */
  129.   198,           /* Width, 198 pixels wide. */
  130.   8,             /* Height, 8 pixels lines heigh. */
  131.   GADGHCOMP,     /* Flags, draw the select box in the complement */
  132.                  /* colours. Note: it actually only the cursor which */
  133.                  /* will be drawn in the complement colours (yellow). */
  134.                  /* If you set the flag GADGHNONE the cursor will not be */
  135.                  /* highlighted, and the user will therefore not be able */
  136.                  /* to see it. */
  137.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  138.   RELVERIFY,     /* the user has selected this gadget, and when the user */
  139.                  /* has released it. */ 
  140.   STRGADGET,     /* GadgetType, a String gadget. */
  141.   (APTR) &my_border, /* GadgetRender, a pointer to our Border structure. */
  142.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  143.                  /* with an alternative image. */
  144.   &my_text,      /* GadgetText, a pointer to our IntuiText structure. */
  145.   NULL,          /* MutualExclude, no mutual exclude. */
  146.   (APTR) &my_string_info, /* SpecialInfo, a pointer to a StringInfo str. */
  147.   0,             /* GadgetID, no id. */
  148.   NULL           /* UserData, no user data connected to the gadget. */
  149. };
  150.  
  151.  
  152.  
  153. /***************/
  154. /* THE WINDOW: */
  155. /***************/
  156.  
  157. /* Declare a pointer to a Window structure: */ 
  158. struct Window *my_window;
  159.  
  160. /* Declare and initialize your NewWindow structure: */
  161. struct NewWindow my_new_window=
  162. {
  163.   50,              /* LeftEdge    x position of the window. */
  164.   25,              /* TopEdge     y positio of the window. */
  165.   320,             /* Width       320 pixels wide. */
  166.   100,             /* Height      100 lines high. */
  167.   0,               /* DetailPen   Text should be drawn with colour reg. 0 */
  168.   1,               /* BlockPen    Blocks should be drawn with colour reg. 1 */
  169.   CLOSEWINDOW|     /* IDCMPFlags  The window will give us a message if the */
  170.                    /*             user has selected the Close window gad, */
  171.   GADGETDOWN|      /*             or a gadget has been pressed on, or */
  172.   GADGETUP,        /*             a gadge has been released. */
  173.   SMART_REFRESH|   /* Flags       Intuition should refresh the window. */
  174.   WINDOWCLOSE|     /*             Close Gadget. */
  175.   WINDOWDRAG|      /*             Drag gadget. */
  176.   WINDOWDEPTH|     /*             Depth arrange Gadgets. */
  177.   WINDOWSIZING|    /*             Sizing Gadget. */
  178.   ACTIVATE,        /*             The window should be Active when opened. */
  179.   &my_gadget,      /* FirstGadget A pointer to the String gadget. */
  180.   NULL,            /* CheckMark   Use Intuition's default CheckMark. */
  181.   "String Window", /* Title       Title of the window. */
  182.   NULL,            /* Screen      Connected to the Workbench Screen. */
  183.   NULL,            /* BitMap      No Custom BitMap. */
  184.   320,             /* MinWidth    We will not allow the window to become */
  185.   50,              /* MinHeight   smaller than 320 x 50, and not bigger */
  186.   640,             /* MaxWidth    than 640 x 200. */
  187.   200,             /* MaxHeight   */
  188.   WBENCHSCREEN     /* Type        Connected to the Workbench Screen. */
  189. };
  190.  
  191.  
  192. /* Declare our functions: */
  193. void main();
  194. void clean_up();
  195.  
  196.  
  197. /******************************************************************************/
  198.  
  199.  
  200. void main()
  201. {
  202.   /* Boolean variable used for the while loop: */
  203.   BOOL close_me;
  204.  
  205.   /* Declare a variable in which we will store the IDCMP flag: */
  206.   ULONG class;
  207.  
  208.   /* Declare a pointer to an IntuiMessage structure: */
  209.   struct IntuiMessage *my_message;
  210.  
  211.  
  212.  
  213.   /* Put some text into the my_buffer string: */
  214.   strcpy( my_buffer, "Some text" );
  215.  
  216.  
  217.  
  218.   /* Open the necessary libraries: (Note the DiskFont library!) */
  219.   IntuitionBase = (struct IntuitionBase *)
  220.     OpenLibrary( "intuition.library", 0 );
  221.   if( !IntuitionBase )
  222.     clean_up( "Could not open Intuition library!" );
  223.  
  224.   GfxBase = (struct GfxBase *)
  225.     OpenLibrary( "graphics.library", 0 );
  226.   if( !GfxBase )
  227.     clean_up( "Could not open Graphics library!" );
  228.  
  229.   DiskfontBase = (struct DiskfontBase *)
  230.     OpenLibrary( "diskfont.library", 0 );
  231.   if( !DiskfontBase )
  232.     clean_up( "Could not open Diskfont library!" );
  233.  
  234.  
  235.  
  236.   /* Try to open a disk font: */
  237.    my_font = (struct TextFont *)
  238.     OpenDiskFont( &my_font_attr );
  239.  
  240.   /* Have we opened the font successfully? */
  241.   if( !my_font )
  242.     clean_up( "Could not open the font!" );
  243.  
  244.  
  245.  
  246.   /* We will now try to open the window: */
  247.   my_window = (struct Window *) OpenWindow( &my_new_window );
  248.   
  249.   /* Have we opened the window succesfully? */
  250.   if(my_window == NULL)
  251.     clean_up( "Could NOT open the Window!" );
  252.  
  253.  
  254.  
  255.   /* Stay in the while loop until the user has selected the Close window */
  256.   /* gadget: */
  257.   close_me = FALSE;
  258.   while( close_me == FALSE )
  259.   {
  260.     /* Wait until we have recieved a message: */
  261.     WaitPort(  my_window->UserPort );
  262.  
  263.     /* Collect as many messages as possible: */
  264.     while( my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort ) )
  265.     {
  266.       /* After we have collected the message we can read it, and save any */
  267.       /* important values which we maybe want to check later: */
  268.       class = my_message->Class;      /* Save the IDCMP flag. */
  269.   
  270.       /* After we have read it we reply as fast as possible: */
  271.       /* REMEMBER! Do never try to read a message after you have replied! */
  272.       /* Some other process has maybe changed it. */
  273.       ReplyMsg( my_message );
  274.   
  275.       /* Check which IDCMP flag was sent: */
  276.       switch( class )
  277.       {
  278.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  279.                close_me=TRUE;
  280.                break;
  281.                
  282.         case GADGETDOWN:   /* The user has selected the String gadget: */
  283.                            /* (Clicked inside the select box) */
  284.                printf("String gadget selected.\n");
  285.                break;
  286.                
  287.         case GADGETUP:     /* The user has released the String gadget: */
  288.                            /* (Pressed ENTER or RETURN) */
  289.                printf("String gadget released.\n");
  290.                break;
  291.       }
  292.     }
  293.   }
  294.  
  295.  
  296.  
  297.   /* Print out the final string: */
  298.   printf("String: %s\n", my_string_info.Buffer);
  299.  
  300.  
  301.   /* Give everything back nice and neatly before */
  302.   /* this program terminates:                    */
  303.   clean_up( "The End" );
  304. }
  305.  
  306.  
  307.  
  308. void clean_up( message )
  309. STRPTR message;
  310. {
  311.   /* Note that this function will only close */
  312.   /* things which we have previously opened. */
  313.  
  314.   /* Close the window: */
  315.   if( my_window )
  316.     CloseWindow( my_window );
  317.  
  318.   /* Close the new font: */
  319.   if( my_font )
  320.     CloseFont( my_font );
  321.  
  322.   /* Close the Font Library: */
  323.   if( DiskfontBase )
  324.     CloseLibrary( DiskfontBase );
  325.  
  326.   /* Close the Graphics Library: */
  327.   if( GfxBase )
  328.     CloseLibrary( GfxBase );
  329.  
  330.   /* Close the Intuition Library: */
  331.   if( IntuitionBase )
  332.     CloseLibrary( IntuitionBase );
  333.  
  334.   /* Print the message: */
  335.   printf( "%s\n", message );
  336.  
  337.   /* Quit: */
  338.   exit( 0 );
  339. }
  340.  
  341.