home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / Libraries / Intuition / StrDemo / strdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  7.0 KB  |  295 lines

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