home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 357.lha / intuisup_v1.15 / examples / text / test.c < prev    next >
C/C++ Source or Header  |  1990-03-10  |  7KB  |  185 lines

  1. #include <intuition/intuisup.h>
  2.  
  3. #define TEXT_AREA    100
  4. #define FONTS_NUM    2
  5.  
  6. struct IntuitionBase *IntuitionBase;
  7. struct GfxBase       *GfxBase;
  8. struct DiskFontBase  *DiskfontBase;
  9.  
  10. #ifdef ISUP_RUNTIME
  11. struct   Library  *iSupBase;
  12. #else
  13. struct   Device   *ConsoleDevice;
  14. struct   IOStdReq  ConsoleMsg;
  15. #endif
  16.  
  17.  
  18. struct   TextAttr Attribute[2];
  19. struct   TextFont *Font[2];
  20. struct   Window   *window[2];
  21. struct   TextArea *textarea[2];
  22. UBYTE    Title[2][40] =
  23.    {
  24.    "Sample Topaz 8 Text Area",
  25.    "Sample Helvetica 13 Text Area"
  26.    };
  27.  
  28.  
  29. BOOL
  30. LoadFonts()
  31. {
  32.    extern struct   TextFont *OpenFont(), *OpenDiskFont();
  33.  
  34.    USHORT   i;
  35.  
  36.    /* Just change these attributes to get new fonts in the text areas,  */
  37.    /* they will adapt to any font (as long as it's left->right one)     */
  38.    Attribute[0].ta_Name  = "topaz.font";
  39.    Attribute[0].ta_YSize = 8;
  40.    Attribute[1].ta_Name  = "helvetica.font";
  41.    Attribute[1].ta_YSize = 13;
  42.  
  43.    for(i=0; i<FONTS_NUM; i++)
  44.       {
  45.       if (  !(Font[i]=OpenFont(&Attribute[i]))
  46.          || (Font[i]->tf_YSize!=Attribute[i].ta_YSize) )
  47.          {
  48.          if (!(Font[i]=OpenDiskFont(&Attribute[i])))
  49.             {
  50.             DisplayBeep(NULL);
  51.             return(FALSE);
  52.             }
  53.          }
  54.       }
  55.    return(TRUE);
  56. }
  57.  
  58. VOID main(argc, argv)
  59. LONG argc;
  60. char **argv;
  61. {
  62.    extern struct IntuiMessage *GetMsg();
  63.    extern struct Window       *OpenStdWindow();
  64.  
  65.    UBYTE    rc = 0;
  66.    LONG     class;
  67.    USHORT   mouse_x, mouse_y, area;
  68.    APTR     address;
  69.    struct   IntuiMessage *message;
  70.    struct   Window       *w;
  71.  
  72.    if (  !(GfxBase       = (struct Library *)OpenLibrary("graphics.library", 0))
  73.       || !(IntuitionBase = (struct Library *)OpenLibrary("intuition.library", 0))
  74.       || !(DiskfontBase  = (struct Library *)OpenLibrary("diskfont.library", 0))
  75. #ifdef ISUP_RUNTIME
  76.       || !(iSupBase      = (struct Library *)OpenLibrary("isup.library", 0))
  77. #else
  78.       || !(!OpenDevice("console.device", -1, &ConsoleMsg, 0) && (ConsoleDevice=ConsoleMsg.io_Device))
  79. #endif
  80.       ) goto MAIN_DONE;
  81.  
  82.    window[0] = OpenStdWindow(Title[0], 0x1F, 15,20,350,150, NULL);
  83.    window[1] = OpenStdWindow(Title[1], 0x17, 35,43,350,150, NULL);
  84.    ModifyIDCMP(window[0],  CLOSEWINDOW | GADGETDOWN | NEWSIZE);
  85.    ModifyIDCMP(window[1],  GADGETDOWN | NEWSIZE);
  86.  
  87.    if (!LoadFonts()) { rc = 20; goto MAIN_DONE; }
  88.    if (!window[0])   { rc = 30; goto MAIN_DONE; }
  89.    if (!window[1])   { rc = 40; goto MAIN_DONE; }
  90.  
  91.    /* Prepare the windows for text areas. Set the colors (already done) */
  92.    /* and the fonts. Note that we leave a little room around the text   */
  93.    /* areas: this is safer in case some character have fancy kerning -  */
  94.    /* for instance 'u' as -1 in Times 18 and will come out of the area  */
  95.    /* by one pixel towards the left if it appears first on a line.      */
  96.    SetFont(window[0]->RPort, Font[0]);
  97.    SetFont(window[1]->RPort, Font[1]);
  98.    textarea[0]=OpenTextArea(window[0], 6,12,(USHORT)(window[0]->Width-11),(USHORT)(window[0]->Height-15), TEXT_AREA, -1, 0);
  99.    textarea[1]=OpenTextArea(window[1], 6,12,(USHORT)(window[1]->Width-11),(USHORT)(window[1]->Height-15), TEXT_AREA, -1, 0);
  100.  
  101.    if (!textarea[0] || !textarea[1]) { rc = 50; goto MAIN_DONE; }
  102.  
  103.    /* Place some text in the text areas. You should ALWAYS place some   */
  104.    /* text in a text area. They are returned with 0 acitves lines, and  */
  105.    /* must have at least one line to be usable. If no text is wanted    */
  106.    /* upon startup, you MUST use the following:                         */
  107.    /*    AddTextAreaLine(myarea, -1, "");                               */
  108.    AddTextAreaLine(textarea[0], -1, "This font is non proportional.", LN_PARAGRAPH_BEGIN);
  109.    RefreshTextArea(textarea[0]);
  110.    AddTextAreaLine(textarea[1], -1, "This font is proportional.", LN_PARAGRAPH_BEGIN);
  111.    AddTextAreaLine(textarea[1], -1, "Try typing some text and scrolling around!", LN_PARAGRAPH_BEGIN);
  112.    RefreshTextArea(textarea[1]);
  113.  
  114.    /* Force editing into the second area with some fake mouse coords    */
  115.    /* that will posiion the cursor at the end of the text.              */
  116.    /* We come back from here whenever the user does anything else then  */
  117.    /* editing (clicking somewhere outside the area, pulling a menu or   */
  118.    /* anything else), and the message that caused the exit will be left */
  119.    /* on our port transparently. How's that?                            */
  120.    EditTextArea(textarea[1], 1000, 1000);
  121.  
  122.    FOREVER
  123.          {
  124.          /* DO NOT take this as an example for a good control loop!!    */
  125.          /* I just dont have time make the windows share their port.    */
  126.          Wait( (window[0]? (1<<window[0]->UserPort->mp_SigBit) : 0)
  127.              | (window[1]? (1<<window[1]->UserPort->mp_SigBit) : 0) );
  128.  
  129.          while (  (message=(window[0]? GetMsg(window[0]->UserPort) : NULL))
  130.                || (message=(window[1]? GetMsg(window[1]->UserPort) : NULL)) )
  131.                {
  132.                class   = message->Class;
  133.                address = message->IAddress;
  134.                mouse_x = message->MouseX;
  135.                mouse_y = message->MouseY;
  136.                w       = message->IDCMPWindow;
  137.                ReplyMsg(message);
  138.  
  139.                /* Get the right window pointer back.       */
  140.                /* This is screwy, but... it works (sight!) */
  141.                area = (w==window[0]? 0 : 1);
  142.  
  143.                switch(class)
  144.                      {
  145.                      case CLOSEWINDOW: goto MAIN_DONE;
  146.  
  147.                      case GADGETDOWN:
  148.                      /* here we go to the actual editing. As you can   */
  149.                      /* see, it's not too complicated.                 */
  150.                         if (((struct Gadget *)address)->GadgetID==TEXT_AREA)
  151.                            EditTextArea(textarea[area], mouse_x, mouse_y);
  152.                      break;
  153.  
  154.                      case NEWSIZE:
  155.                         SetPointerNum(w, WAIT_POINTER);
  156.                         RemakeStdWindow(w, Title[area]);
  157.                         textarea[area]->Width  = w->Width- 11;
  158.                         textarea[area]->Height = w->Height-15;
  159.                         RethinkTextArea(textarea[area], 0);
  160.                         SetPointerNum(w, WBENCH_POINTER);
  161.                         EditTextArea(textarea[area], -1, -1);
  162.                      break;
  163.                      }
  164.                }
  165.          }
  166.  
  167.    MAIN_DONE:
  168.         for (area=0; area<2; area++)
  169.             {
  170.             if (textarea[area])   CloseTextArea(textarea[area]);
  171.             if (window[area])     CloseStdWindow(window[area]);
  172.             }
  173.  
  174. #ifdef ISUP_RUNTIME
  175.         if (iSupBase)      CloseLibrary(iSupBase);
  176. #else
  177.         if (ConsoleDevice) CloseDevice(&ConsoleMsg);
  178. #endif
  179.         if (DiskfontBase)  CloseLibrary(DiskfontBase);
  180.         if (IntuitionBase) CloseLibrary(IntuitionBase);
  181.         if (GfxBase)       CloseLibrary(GfxBase);
  182.         exit(rc);
  183. }
  184.  
  185.