home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / StrDemo / strdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  6.9 KB  |  289 lines

  1. /** Extended String Gadget Test Program :ts=8 **/
  2.  
  3. /*
  4. Copyright (c) 1989 Commodore-Amiga, Inc.
  5.  
  6. Executables based on this information may be used in software
  7. for Commodore Amiga computers. All other rights reserved.
  8. This information is provided "as is"; no warranties are made.
  9. All use is at your own risk, and no liability or responsibility
  10. is assumed.
  11. */
  12.  
  13. #include <exec/types.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/sghooks.h>
  16. #include <utility/hooks.h>
  17.  
  18. #include "strgad.h"
  19.  
  20. struct  Window      *getNewWind();
  21. struct  Screen      *getNewScreen();
  22. struct    TextFont    *OpenDiskFont();
  23.  
  24. struct  IntuitionBase   *IntuitionBase = NULL;
  25. struct  GfxBase         *GfxBase = NULL;
  26. APTR    DiskfontBase = NULL;
  27.  
  28. ULONG   flg = WINDOWCLOSE | NOCAREREFRESH | WINDOWDRAG
  29.     | WINDOWDEPTH | SIMPLE_REFRESH;
  30.  
  31. ULONG   iflg = GADGETUP | CLOSEWINDOW;
  32.  
  33. struct SGSpec sgadspec[] = {
  34.     {NULL, 0, 200, 20, 250, "long cent. SG", "FooBar", 215, STRINGCENTER, 0},
  35.     {NULL, 0, 200, 20, 250, "short cent. (cycle!)","FooBar",15,STRINGCENTER,1},
  36.     {NULL, 0, 200, 20, 250, "long left SG", "FooBar", 215, 0, 2},
  37.     {NULL, 0, 200, 20, 250, "short left SG", "FooBar", 15, 0, 3},
  38.     {NULL, 0, 200, 20, 250, "short right SG", "FooBar", 15, STRINGRIGHT, 4},
  39.     {NULL, 0, 200, 20, 250, "long right SG", "FooBar", 215, STRINGRIGHT, 5},
  40. };
  41.  
  42. #define NUMSGS    ( sizeof ( sgadspec ) / sizeof (struct SGSpec) )
  43. #define SGSEX( gadget )    \
  44.         (( (struct StringInfo *) (gadget)->SpecialInfo)->Extension)
  45.  
  46. struct Gadget    *sgarray[ NUMSGS ];    /* used for cycling active gadget    */
  47.  
  48. struct TextAttr propta = { (UBYTE *) "ruby.font", 12, 0, 0 };
  49. struct TextAttr testa = { (UBYTE *) "ruby.font", 8, 0, 0 };
  50.  
  51. UBYTE    workbuff[ 216 ];
  52.  
  53. extern struct Hook    tabhook;
  54. extern struct Hook    cyclehook;
  55.  
  56. struct StringExtend tabsex = { NULL, {3, 1}, {1, 3}, 0, &tabhook, workbuff, };
  57. struct StringExtend cyclesex = { NULL, {3, 1}, {1, 3}, 0, &cyclehook, workbuff};
  58.  
  59. main()
  60. {
  61.     struct  IntuiMessage    *msg;
  62.     struct Screen       *screen = NULL;
  63.     struct Window       *window = NULL;
  64.     WORD            exitval = 0;
  65.     int            gadgid;
  66.  
  67.     struct Gadget    *sgad;
  68.     struct Gadget    *g;
  69.     int            topskip;
  70.     int            sgx;
  71.     int            sgtop;
  72.     struct RastPort    *screenrport;
  73.     struct TextFont    *propfont = NULL;
  74.     struct TextFont    *testfont = NULL;
  75.  
  76.     /* hold data from *msg  */
  77.     ULONG           class;
  78.     USHORT          code;
  79.     APTR            iaddr;
  80.  
  81.     if ((IntuitionBase = 
  82.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)
  83.     ) == NULL)
  84.     {
  85.     printf("NO INTUITION LIBRARY\n");
  86.     exitval = 1;
  87.     goto EXITING;
  88.     }
  89.  
  90.     if ((GfxBase = 
  91.     (struct GfxBase *) OpenLibrary("graphics.library", 36L)
  92.     ) == NULL)
  93.     {
  94.     printf("NO GRAPHICS LIBRARY\n");
  95.     exitval = 2;
  96.     goto EXITING;
  97.     }
  98.  
  99.     if ((DiskfontBase = 
  100.     (APTR) OpenLibrary("diskfont.library", 0L)
  101.     ) == NULL)
  102.     {
  103.     printf("NO DISKFONT LIBRARY\n");
  104.     exitval = 3;
  105.     goto EXITING;
  106.     }
  107.  
  108.     propfont = OpenDiskFont( &propta );
  109.     testfont = OpenDiskFont( &testa );
  110.  
  111.     printf("propfont: %lx\n", propfont );
  112.     printf("testfont: %lx\n", testfont );
  113.  
  114.     screen = getNewScreen( &propta );
  115.     if (!screen) 
  116.     {
  117.     printf("No screen.\n");
  118.     goto EXITING;
  119.     }
  120.  
  121.     /* init libraries and window    */
  122.     window = getNewWind(21, 20, 500, 150, screen, flg, iflg);
  123.     if (window == NULL)
  124.     {
  125.     exitval = 1;
  126.     goto EXITING;
  127.     }
  128.  
  129.     screenrport = &window->WScreen->RastPort;
  130.  
  131.     sgtop = 20;
  132.     topskip = SGHEIGHT( screenrport ) +  BORDERGAP;
  133.     /* ZZZ: check they fit    */
  134.  
  135.     for ( sgx = 0; sgx < NUMSGS; sgx++ )
  136.     {
  137.     sgadspec[ sgx ].sgs_RPort = screenrport;
  138.     sgadspec[ sgx ].sgs_TxHeight = screenrport->TxHeight;
  139.     sgadspec[ sgx ].sgs_Top = sgtop;
  140.     sgtop += topskip;
  141.     }
  142.  
  143.     sgad = getStringGadgets( sgadspec, NUMSGS );
  144.  
  145.     tabsex.Font = testfont;
  146.     cyclesex.Font = testfont;
  147.  
  148.     initMyHooks();    /* set up my hook interface    */
  149.  
  150.     if ( sgad )
  151.     {
  152.     for ( g = sgad, sgx = 0; g && sgx < NUMSGS; ++sgx, g = g->NextGadget)
  153.     {
  154.         g->Activation |= STRINGEXTEND;
  155.  
  156.             sgarray[ sgx ] = g;    /* stash pointer to gadget for cycling */
  157.  
  158.         /* string gadget number 1 (second in list) is special */
  159.         SGSEX( g ) = ( sgx == 1 )? &cyclesex: &tabsex;
  160.     }
  161.     
  162.     AddGList( window, sgad, (LONG) -1, (LONG) NUMSGS, NULL );
  163.     RefreshGList( sgad, window, NULL, (LONG) NUMSGS );
  164.     }
  165.  
  166.     printf("test program ok\n");
  167.  
  168.     FOREVER
  169.     {
  170.     if ((msg = (struct IntuiMessage *)GetMsg(window->UserPort)) == NULL)
  171.     {
  172.         Wait(1L<<window->UserPort->mp_SigBit);
  173.         continue;
  174.     }
  175.  
  176.     /* Stash message contents and reply,
  177.      * important when message triggers some
  178.      * lengthy processing
  179.      */
  180.  
  181.     class   = msg->Class;
  182.     code    = msg->Code;
  183.     iaddr   = msg->IAddress;
  184.     ReplyMsg(msg);
  185.  
  186.     switch (class)
  187.     {
  188.     case GADGETUP:
  189.         gadgid = ((struct Gadget *) iaddr)->GadgetID;
  190.  
  191.  
  192.         if  ( code == MYCODETAB )
  193.         {
  194.         ActivateGadget( sgarray[ (gadgid + 1)% NUMSGS], window, NULL);
  195.         }
  196.         else if ( code == MYCODEBACKTAB )
  197.         {
  198.         ActivateGadget( sgarray[ (gadgid + (NUMSGS-1) ) % NUMSGS ],
  199.             window, NULL);
  200.         }
  201.         break;
  202.  
  203.  
  204.     case CLOSEWINDOW:
  205.         goto EXITING;
  206.  
  207.     }
  208.     }
  209.  
  210. EXITING:
  211.     if (window)
  212.     {
  213.     RemoveGList( window, sgad, NUMSGS );
  214.     freeStringGadgets( sgad, NUMSGS );
  215.     CloseWindow(window);
  216.     }
  217.     if (screen)
  218.     {
  219.     CloseScreen( screen );
  220.     }
  221.     if (propfont) CloseFont( propfont );
  222.     if (DiskfontBase) CloseLibrary(DiskfontBase);
  223.     if (GfxBase) CloseLibrary(GfxBase);
  224.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  225.     /** FIX: FreeRemember() */
  226.     exit (exitval);
  227. }
  228.  
  229.  
  230. struct  Window * getNewWind(left, top, width, height, screen, flg, iflg)
  231. SHORT   left, top, width, height;
  232. struct Screen    *screen;
  233. ULONG   flg, iflg;
  234. {
  235.     struct  Window  *OpenWindow();
  236.     struct  NewWindow   nw;
  237.  
  238.     nw.LeftEdge =   (SHORT) left;
  239.     nw.TopEdge  =   (SHORT) top;
  240.     nw.Width    =   (SHORT) width;
  241.     nw.Height   =   (SHORT) height;
  242.     nw.DetailPen    =   (UBYTE) -1;
  243.     nw.BlockPen =   (UBYTE) -1;
  244.     nw.IDCMPFlags   =   (ULONG) iflg;
  245.  
  246.     nw.Flags    =   (ULONG) flg;
  247.  
  248.     nw.FirstGadget  =   (struct Gadget *)   NULL;
  249.     nw.CheckMark    =   (struct Image *)    NULL;
  250.     nw.Title    =   (UBYTE *)   " New Window ";
  251.     nw.Screen   =   screen;
  252.     nw.BitMap   =   (struct BitMap *)   NULL;
  253.     nw.MinWidth =   (SHORT) 50;
  254.     nw.MinHeight=   (SHORT) 30;
  255.     /* work around bug  */
  256.     nw.MaxWidth =   (SHORT) nw.Width;
  257.     nw.MaxHeight    =   (SHORT) nw.Height;
  258.     nw.Type     =   (USHORT) CUSTOMSCREEN;
  259.  
  260.     return ((struct Window *) OpenWindow(&nw));
  261. }
  262.  
  263. /** get a new screen    **/
  264.  
  265. struct TextAttr SafeFont = { (UBYTE *) "topaz.font", 8, 0, 0, };
  266.  
  267. struct    Screen    *getNewScreen(textattr)
  268. struct TextAttr    *textattr;
  269. {
  270.     struct    NewScreen    ns;
  271.  
  272.     ns.LeftEdge    =    0;
  273.     ns.TopEdge    =    0;
  274.     ns.Width    =    STDSCREENWIDTH;
  275.     ns.Height    =    STDSCREENHEIGHT;
  276.     ns.Depth    =    2;    /* 4 colors    */
  277.     ns.DetailPen =    0;
  278.     ns.BlockPen    =    1;
  279.     ns.ViewModes =    HIRES;    /* determined above    */
  280.     ns.Type        =    CUSTOMSCREEN;
  281.     ns.Font        =    textattr? textattr: &SafeFont;
  282.     ns.DefaultTitle    =    (UBYTE *) " Test Screen ";
  283.     ns.Gadgets    =    NULL;
  284.     ns.CustomBitMap    =    NULL;
  285.  
  286.     return ((struct Screen *) OpenScreen(&ns));
  287. }
  288.  
  289.