home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / eagui11.lha / EAGUI / example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-22  |  10.5 KB  |  444 lines

  1. /*
  2.  * $RCSfile: example.c,v $
  3.  *
  4.  * $Author: Marcel_Offermans $
  5.  *
  6.  * $Revision: 1.7 $
  7.  *
  8.  * $Date: 1993/12/13 22:44:03 $
  9.  *
  10.  * $Locker: Marcel_Offermans $
  11.  *
  12.  * $State: Exp $
  13.  *
  14.  * Description: This is an example of how to use EAGUI. In fact it is the complete version of
  15.  *     the example used in the tutorial[2]. It can be compiled under SAS/C 6.3. It should be
  16.  *     fairly trivial to modify this example to create any window you want[1]. Please note that
  17.  *     the contents of the gadgets aren't saved, so after a resize, everything is lost. Under
  18.  *     V39 it is very easy to get and set these attributes (with GT_GetGadgetAttrs() and
  19.  *     GT_SetGadgetAttrs()), and although it's a bit more difficult under V37, it can be done
  20.  *     there too (it's something you'll have to do anyway).
  21.  *
  22.  * [1] If you want to create a new window, it is enough to specify a new tree of objects. The
  23.  *     only other thing you might want to change is the fact that the window in this example
  24.  *     can only be resized in horizontal direction. If you change the last argument of the
  25.  *     WindowLimits() call to "~0" that's fixed too.
  26.  *
  27.  * [2] In fact, it is a slightly enhanced example, which shows a little bit more.
  28.  *
  29.  * Use a tab size of 5 to read this source! Comments were formatted for a right margin of 95,
  30.  * which matches my overscan and font settings. I hope it is readable for others.
  31.  */
  32.  
  33. /* standard headers */
  34. #include <stdlib.h>
  35.  
  36. #include <exec/types.h>
  37. #include <graphics/text.h>
  38. #include <intuition/intuition.h>
  39. #include <libraries/gadtools.h>
  40.  
  41. #include <clib/macros.h>
  42.  
  43. #include <proto/diskfont.h>
  44. #include <proto/exec.h>
  45. #include <proto/intuition.h>
  46. #include <proto/gadtools.h>
  47. #include <proto/graphics.h>
  48. #include <proto/dos.h>
  49.  
  50. /* EAGUI headers */
  51. #include "EAGUI.h"
  52. #include "EAGUI_pragmas.h"
  53.  
  54. /* globals */
  55. struct ea_Object *            winobj_ptr        = NULL;
  56. struct ea_Object *            okobj_ptr            = NULL;
  57. struct ea_Object *            cancelobj_ptr        = NULL;
  58. struct ea_Object *            hgroupobj_ptr        = NULL;
  59. struct Window *            win_ptr            = NULL;
  60. struct Screen *            scr_ptr            = NULL;
  61. struct Gadget *            gadlist_ptr        = NULL;
  62. struct Gadget *            stringgadget_ptr    = NULL;
  63. APTR                     visualinfo_ptr        = NULL;
  64. struct DrawInfo *            drawinfo_ptr        = NULL;
  65. struct TextFont *            tf_ptr            = NULL;
  66. struct Library *            EAGUIBase            = NULL;
  67.  
  68. struct TextAttr             textattr             = {"helvetica.font", 15, FS_NORMAL, FPB_DISKFONT};
  69. struct Hook                relhook;
  70. struct IntuiMessage            imsg;
  71.  
  72. STATIC UBYTE rcs_id_string[] = "$Id: example.c,v 1.7 1993/12/13 22:44:03 Marcel_Offermans Exp Marcel_Offermans $";
  73.  
  74. /* prototypes */
  75. ULONG __saveds __asm HookEntry(register __a0 struct Hook *, register __a2 VOID *, register __a1 VOID *);
  76. VOID InitHook(struct Hook *, ULONG (*)(), VOID *);
  77. ULONG rel_samesize(struct Hook *, struct List *, APTR);
  78. LONG init(VOID);
  79. VOID cleanup(STRPTR);
  80. VOID resizewindow(VOID);
  81. ULONG handlemsgs(VOID);
  82.  
  83. /* functions for hook handling */
  84. ULONG __saveds __asm HookEntry
  85. (
  86.     register __a0 struct Hook *    hook_ptr,
  87.     register __a2 VOID *        object_ptr,
  88.     register __a1 VOID *        message_ptr
  89. )
  90. {
  91.     return((*hook_ptr->h_SubEntry)(hook_ptr, object_ptr, message_ptr));
  92. }
  93.  
  94. VOID InitHook(struct Hook *h_ptr, ULONG (*func_ptr)(), VOID *data_ptr)
  95. {
  96.     if (h_ptr)
  97.     {
  98.         h_ptr->h_Entry = (ULONG (*)())HookEntry;
  99.         h_ptr->h_SubEntry = func_ptr;
  100.         h_ptr->h_Data = data_ptr;
  101.     }
  102. }
  103.  
  104. /* same size relation */
  105. ULONG rel_samesize(struct Hook *hook_ptr, struct List *list_ptr, APTR msg_ptr)
  106. {
  107.      struct ea_RelationObject *ro_ptr;
  108.      ULONG minx, miny;
  109.      ULONG x, y;
  110.      minx = 0;
  111.      miny = 0;
  112.  
  113.      /* examine the list of objects that are affected by the relation */
  114.      ro_ptr = (struct ea_RelationObject *)list_ptr->lh_Head;
  115.      while (ro_ptr->node.ln_Succ)
  116.      {
  117.           ea_GetAttrs(ro_ptr->object_ptr,
  118.                EA_MinWidth,        &x,
  119.                EA_MinHeight,       &y,
  120.                TAG_DONE);
  121.  
  122.           /* find the maximum values of the minimum sizes */
  123.           minx = MAX(x, minx);
  124.           miny = MAX(y, miny);
  125.  
  126.           ro_ptr = (struct ea_RelationObject *)ro_ptr->node.ln_Succ;
  127.      }
  128.  
  129.      /* set all objects to the newly found minimum sizes */
  130.      ro_ptr = (struct ea_RelationObject *)list_ptr->lh_Head;
  131.      while (ro_ptr->node.ln_Succ)
  132.      {
  133.           ea_SetAttrs(ro_ptr->object_ptr,
  134.                EA_MinWidth,        minx,
  135.                EA_MinHeight,       miny,
  136.                TAG_DONE);
  137.  
  138.           ro_ptr = (struct ea_RelationObject *)ro_ptr->node.ln_Succ;
  139.      }
  140.      return(0);
  141. }
  142.  
  143. /* initialize everything */
  144. LONG init(VOID)
  145. {
  146.     LONG w, h, bl, br, bt, bb;
  147.  
  148.     /* open the EAGUI library */
  149.     if (!(EAGUIBase = OpenLibrary(EAGUILIBRARYNAME, EAGUILIBRARYVERSION)))
  150.     {
  151.         cleanup("Couldn't open EAGUI.library.\n");
  152.     }
  153.  
  154.     /* open the font */
  155.     if (!(tf_ptr = OpenDiskFont(&textattr)))
  156.     {
  157.         cleanup("Couldn't open font.\n");
  158.     }
  159.  
  160.     /* now we can build the object tree */
  161.     if (!( winobj_ptr = VGroup
  162.         EA_BorderLeft,        4,
  163.         EA_BorderRight,    4,
  164.         EA_BorderTop,        4,
  165.         EA_BorderBottom,    4,
  166.         EA_Child,            GTString("Enter a string, just for fun:")
  167.             EA_GTTextAttr,        &textattr,
  168.             EA_InstanceAddress,    &stringgadget_ptr,
  169.             End,
  170.         EA_Child,            hgroupobj_ptr = HGroup
  171.             EA_BorderTop,        4,
  172.             EA_Child,            okobj_ptr = GTButton("Ok")
  173.                 EA_GTTextAttr,        &textattr,
  174.                 End,
  175.             EA_Child,            EmptyBox(1)
  176.                 End,
  177.             EA_Child,            cancelobj_ptr = GTButton("Cancel")
  178.                 EA_GTTextAttr,        &textattr,
  179.                 End,
  180.             End,
  181.         End ) )
  182.     {
  183.         cleanup("Couldn't init the objects.\n");
  184.     }
  185.  
  186.     /* initialize the relation */
  187.     InitHook(&relhook, rel_samesize, NULL);
  188.  
  189.      ea_NewRelation(hgroupobj_ptr, &relhook,
  190.           EA_Object,          okobj_ptr,
  191.           EA_Object,          cancelobj_ptr,
  192.           TAG_DONE);
  193.  
  194.     /* lock the screen */
  195.     if (!(scr_ptr = LockPubScreen(NULL)))
  196.     {
  197.         cleanup("Couldn't lock default public screen.\n");
  198.     }
  199.  
  200.     /* get VisualInfo and DrawInfo */
  201.     if (!(visualinfo_ptr = GetVisualInfo(scr_ptr, TAG_DONE)))
  202.     {
  203.         cleanup("Couldn't get the visual info.\n");
  204.     }
  205.     if (!(drawinfo_ptr = GetScreenDrawInfo(scr_ptr)))
  206.     {
  207.         cleanup("Couldn't get the draw info.\n");
  208.     }
  209.  
  210.      /* obtain the minimum dimensions of every object in the tree */
  211.      ea_GetMinSizes(winobj_ptr);
  212.  
  213.     /* get some attributes */
  214.     ea_GetAttrs(winobj_ptr,
  215.         EA_MinWidth,        &w,
  216.         EA_MinHeight,        &h,
  217.         EA_BorderLeft,        &bl,
  218.         EA_BorderRight,    &br,
  219.         EA_BorderTop,        &bt,
  220.         EA_BorderBottom,    &bb,
  221.         TAG_DONE);
  222.  
  223.      /* open the window */
  224.      if (!(win_ptr = OpenWindowTags(NULL,
  225.          WA_Title,            "EAGUI example",
  226.         WA_Flags,            (WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SIZEGADGET | WFLG_SIZEBBOTTOM | WFLG_ACTIVATE),
  227.         WA_IDCMP,            (IDCMP_CLOSEWINDOW | BUTTONIDCMP | STRINGIDCMP | IDCMP_REFRESHWINDOW | IDCMP_NEWSIZE),
  228.         WA_InnerHeight,    h + bt + bb,
  229.         TAG_DONE)))
  230.     {
  231.         cleanup("Couldn't open the window.\n");
  232.     }
  233.  
  234.     /* set the window limits */
  235.     WindowLimits(
  236.         win_ptr,
  237.         w + win_ptr->BorderLeft + win_ptr->BorderRight + bl + br,
  238.         h + win_ptr->BorderTop + win_ptr->BorderBottom + bt + bb,
  239.         ~0,
  240.         h + win_ptr->BorderTop + win_ptr->BorderBottom + bt + bb);
  241.  
  242.     /* create the gadgets and add them to the window */
  243.     resizewindow();
  244.  
  245.     return(0);
  246. }
  247.  
  248. /* clean up everything that was created */
  249. VOID cleanup(STRPTR str_ptr)
  250. {
  251.     int rc = 0;
  252.  
  253.     /* if a string is passed, we assume there was some kind of error */
  254.     if (str_ptr)
  255.     {
  256.         Printf("Error: %s", str_ptr);
  257.         rc = 20;
  258.     }
  259.  
  260.     if (gadlist_ptr)
  261.     {
  262.         RemoveGList(win_ptr, gadlist_ptr, -1);
  263.         ea_FreeGadgetList(winobj_ptr, gadlist_ptr);
  264.         gadlist_ptr = NULL;
  265.     }
  266.  
  267.     if (win_ptr)
  268.     {
  269.         CloseWindow(win_ptr);
  270.         win_ptr = NULL;
  271.     }
  272.  
  273.     if (drawinfo_ptr)
  274.     {
  275.         FreeScreenDrawInfo(scr_ptr, drawinfo_ptr);
  276.         drawinfo_ptr = NULL;
  277.     }
  278.  
  279.     if (visualinfo_ptr)
  280.     {
  281.         FreeVisualInfo(visualinfo_ptr);
  282.         visualinfo_ptr = NULL;
  283.     }
  284.  
  285.     if (scr_ptr)
  286.     {
  287.         UnlockPubScreen(NULL, scr_ptr);
  288.         scr_ptr = NULL;
  289.     }
  290.  
  291.     if (winobj_ptr)
  292.     {
  293.         ea_DisposeObject(winobj_ptr);
  294.         winobj_ptr = NULL;
  295.     }
  296.  
  297.     if (tf_ptr)
  298.     {
  299.         CloseFont(tf_ptr);
  300.         tf_ptr = NULL;
  301.     }
  302.  
  303.     if (EAGUIBase)
  304.     {
  305.         CloseLibrary(EAGUIBase);
  306.         EAGUIBase = NULL;
  307.     }
  308.  
  309.     exit(rc);
  310. }
  311.  
  312. /* (re)create the gadget list */
  313. VOID resizewindow(VOID)
  314. {
  315.     LONG bl, br, bt, bb;
  316.  
  317.     /* if necessary, remove the gadget list from the window, and clean it up */
  318.     if (gadlist_ptr)
  319.     {
  320.         RemoveGList(win_ptr, gadlist_ptr, -1);
  321.         ea_FreeGadgetList(winobj_ptr, gadlist_ptr);
  322.         gadlist_ptr = NULL;
  323.     }
  324.  
  325.     ea_GetAttrs(winobj_ptr,
  326.         EA_BorderLeft,        &bl,
  327.         EA_BorderRight,    &br,
  328.         EA_BorderTop,        &bt,
  329.         EA_BorderBottom,    &bb,
  330.         TAG_DONE);
  331.  
  332.      ea_SetAttrs(winobj_ptr,
  333.         EA_Width,        win_ptr->Width -
  334.                     win_ptr->BorderLeft -
  335.                     win_ptr->BorderRight -
  336.                     bl -
  337.                     br,
  338.         EA_Height,    win_ptr->Height -
  339.                     win_ptr->BorderTop -
  340.                     win_ptr->BorderBottom -
  341.                     bt -
  342.                     bb,
  343.         EA_Left,        win_ptr->BorderLeft,
  344.         EA_Top,        win_ptr->BorderTop,
  345.         TAG_DONE);
  346.  
  347.     ea_LayoutObjects(winobj_ptr);
  348.  
  349.     if (ea_CreateGadgetList(winobj_ptr, &gadlist_ptr, visualinfo_ptr, drawinfo_ptr) != EA_ERROR_OK)
  350.     {     
  351.         cleanup("Couldn't create the gadget list.\n");
  352.     }     
  353.  
  354.     EraseRect(win_ptr->RPort,     
  355.         win_ptr->BorderLeft,
  356.         win_ptr->BorderTop,
  357.         win_ptr->Width - win_ptr->BorderRight - 1,
  358.         win_ptr->Height - win_ptr->BorderBottom - 1);
  359.  
  360.     RefreshWindowFrame(win_ptr);
  361.  
  362.     AddGList(win_ptr, gadlist_ptr, -1, -1, NULL);
  363.     RefreshGList(gadlist_ptr, win_ptr, NULL, -1);
  364.     GT_RefreshWindow(win_ptr, NULL);
  365. }
  366.  
  367. /* a normal message handling loop */
  368. ULONG handlemsgs(VOID)
  369. {
  370.     struct IntuiMessage    *    imsg_ptr;
  371.     ULONG                rc = 0;
  372.  
  373.     while (imsg_ptr = GT_GetIMsg(win_ptr->UserPort))
  374.     {
  375.         CopyMem((char *)imsg_ptr, (char *)&imsg, (long)sizeof(struct IntuiMessage));
  376.  
  377.         GT_ReplyIMsg(imsg_ptr);
  378.  
  379.         switch (imsg.Class)
  380.         {
  381.             case    IDCMP_REFRESHWINDOW:
  382.                 GT_BeginRefresh(win_ptr);
  383.                 GT_EndRefresh(win_ptr, TRUE);
  384.                 break;
  385.  
  386.             case    IDCMP_CLOSEWINDOW:
  387.                 rc = 10;
  388.                 break;
  389.  
  390.             case    IDCMP_NEWSIZE:
  391.                 resizewindow();
  392.                 /* Just for fun, we put a string in the string gadget after each
  393.                  * resize. This demonstrates how to use the EA_InstanceAddress
  394.                  * tag to obtain pointers to gadgets, which you can use to modify
  395.                  * the gadgets directly.
  396.                  */
  397.                 GT_SetGadgetAttrs(stringgadget_ptr, win_ptr, NULL,
  398.                     GTST_String,    "Ah, a size change! How nice.",
  399.                     TAG_DONE);
  400.                 break;
  401.         }
  402.     }
  403.     return(rc);
  404. }
  405.  
  406. /* main */
  407. int main(int argc, char *argv[])
  408. {
  409.     ULONG idcmpmask, signals;
  410.     BOOL done = FALSE;
  411.  
  412.     /* process any startup options the user has supplied */
  413.     if (argc > 1)
  414.     {
  415.         /* first argument is the font name */
  416.         textattr.ta_Name = argv[1];
  417.         if (argc > 2)
  418.         {
  419.             /* second argument is the font y-size */
  420.             textattr.ta_YSize = atoi(argv[2]);
  421.         }
  422.     }
  423.  
  424.     /* initialize everything */
  425.     init();
  426.  
  427.     /* event handling loop */
  428.     idcmpmask = 1L << win_ptr->UserPort->mp_SigBit;
  429.     while (done == FALSE)
  430.     {
  431.         signals = Wait(idcmpmask);
  432.         if (signals & idcmpmask)
  433.         {
  434.             if (handlemsgs() != 0)
  435.             {
  436.                 done = TRUE;
  437.             }
  438.         }
  439.     }
  440.  
  441.     /* cleanup everything */
  442.     cleanup(NULL);
  443. }
  444.