/* This example demonstrates how to open a disk font (BTopaz, 8)   */
/* and prints some characters with the new font in a window. Note! */
/* The font "BTopaz" must exist in the systems Fonts: directory or */
/* you will receive an error message!                              */

#include <intuition/intuitionbase.h>

struct Intuition *IntuitionBase; /* Running under Intuition. */
struct GfxBase *GfxBase;         /* Move() and Text().       */
struct Library *DiskfontBase;    /* OpenDiskFont() etc.      */
/* NOTE! If you want to use the functions OpenDiskFont() or  */
/* AvailFonts() you have to open the Diskfont Library!       */

UBYTE ju=0xFE, ja=0xFF; /* The source can't be compiled with the last 2
                           letters of the Bulgarian charset, so this is
                           Simple workaround. Use these codes intead.
                           Òake care of this when writting BG messages */

/* The new font's attributes: */
struct TextAttr my_font_attr=
{
  "BTopaz.font",/* Name of the font.  */
  8,            /* Height (in pixels) */
  FS_NORMAL,    /* Style              */
  FPF_DISKFONT  /* Exist on Disk.     */
};

/* Pointer to our new font: */
struct TextFont *my_font;


/* Declare a pointer to a Window structure: */ 
struct Window *my_window;

/* Declare and initialize your NewWindow structure: */
struct NewWindow my_new_window=
{
  0,             /* LeftEdge    x position of the window. */
  12,            /* TopEdge     y positio of the window. */
  640,           /* Width       640 pixels wide. */
  100,           /* Height      100 lines high. */
  0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  CLOSEWINDOW,   /* IDCMPFlags  The window will give us a message if the */
                 /*             user has selected the Close window gad. */
  SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  WINDOWCLOSE|   /*             Close Gadget. */
  WINDOWDRAG|    /*             Drag gadget. */
  WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  ACTIVATE,      /*             The window should be Active when opened. */
  NULL,          /* FirstGadget No Custom gadgets. */
  NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  "Wow! This BTopaz font works!",/* Title       Title of the window. */
  NULL,          /* Screen      Connected to the Workbench Screen. */
  NULL,          /* BitMap      No Custom BitMap. */
  0,             /* MinWidth    No sizing gadget. */
  0,             /* MinHeight          -"-        */
  0,             /* MaxWidth           -"-        */
  0,             /* MaxHeight          -"-        */
  WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
};

void clean_up();

main()
{
  BOOL close_me;                   /* Used in the loop.       */
  ULONG class;                     /* IDCMP flag.             */
  struct IntuiMessage *my_message; /* IntuiMessage structure. */


  /* Open the necessary libraries: */
  IntuitionBase = (struct IntuitionBase *)
    OpenLibrary( "intuition.library", 0 );
  if( !IntuitionBase )
    clean_up( "Could not open Intuition library!" );

  GfxBase = (struct GfxBase *)
    OpenLibrary( "graphics.library", 0 );
  if( !GfxBase )
    clean_up( "Could not open Graphics library!" );

  DiskfontBase = (struct DiskfontBase *)
    OpenLibrary( "diskfont.library", 0 );
  if( !DiskfontBase )
    clean_up( "Could not open Diskfont library!" );


  /* Open a window in which we will display the new font: */
  my_window = (struct Window *) OpenWindow( &my_new_window );
  
  /* Have we opened the window succesfully? */
  if(my_window == NULL)
    clean_up( "Could not open the window!" );


  /* Set colour and draw mode: */
  SetAPen( my_window->RPort, 1 );
  SetDrMd( my_window->RPort, JAM1 );


  /* Try to open a disk font: */
  my_font = (struct TextFont *)
    OpenDiskFont( &my_font_attr );

  /* Have we opened the font successfully? */
  if( !my_font )
    clean_up( "Could not open the font!" );


  /* Change the window's default font: */
  SetFont( my_window->RPort, my_font );


  /* Position the cursor, and print some characters: */

  Move( my_window->RPort, 85, 27 );
  Text( my_window->RPort, "Òîçè êðàòúê ïðèìåð ïîêàçâà èçïîëçâàíåòî íà BTopaz øðèôòà", 56 );

  Move( my_window->RPort, 85, 42 );
  Text( my_window->RPort, "â åëåìåíòàðíà ïðîãðàìà. Èçõîäíèÿ êîä íà C å â Primer01.c", 56 );

  Move( my_window->RPort, 85, 69 );
  Text( my_window->RPort, "ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞß", 56 );

  Move( my_window->RPort, 85, 84 );
  Text( my_window->RPort, "abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõö÷øùüú", 54 );
  Text( my_window->RPort, &ju,1); Text( my_window->RPort, &ja,1);


  /* Wait until the user closes the window: */
  close_me = FALSE;
  while( close_me == FALSE )
  {
    /* Wait until we have recieved a message: */
    Wait( 1 << my_window->UserPort->mp_SigBit );

    /* Collect the message: */
    while( (my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort )) )
    {
      /* After we have collected the message we can read it, and save any */
      /* important values which we maybe want to check later: */
      class = my_message->Class;

      /* After we have read it we reply as fast as possible: */
      /* REMEMBER! Do never try to read a message after you have replied! */
      /* Some other process has maybe changed it. */
      ReplyMsg( my_message );

      /* Check which IDCMP flag was sent: */
      if( class == CLOSEWINDOW )
        close_me=TRUE; /* The user selected the Close window gadget! */  
    }
  }


  clean_up( "The End" );
}

void clean_up( message )
STRPTR message;
{
  if( my_window )
    CloseWindow( my_window );

  if( my_font )
    CloseFont( my_font );

  if( DiskfontBase )
    CloseLibrary( DiskfontBase );

  if( GfxBase )
    CloseLibrary( GfxBase );

  if( IntuitionBase )
    CloseLibrary( IntuitionBase );
  
  printf( "%s\n", message );

  exit();
}

