Newsgroups: comp.sys.amiga.programmer
Subject: Menus and Gadgets!!!
From: cste3@cs.aukuni.ac.nz (Christopher Paul    Stevens        )
Date: Thu, 3 Sep 1992 22:53:33 GMT
Organization: Computer Science Dept. University of Auckland

I've been programming C now for 6 days and am having difficulty in getting
gadgets and menus to work...

The code which follows works fine without all the gadget code in at the
beginning of Open_Windows() but as soon as I put the gadget code in
my program hangs at the LayoutMenus() call in the DefineMenus().

Can anybody tell me what I have done wrong ?

The code dosn't actually hang a ssuch it just sits there for about 20 secs
and then the menus appear, somtimes when I select a item that item will
work but other times when I select an item the whole system hangs!!

All that I can think of is that I am some how changing the VisualInfo structure
which is stuffing up LayoutMenus ????

Also if any one could give me some example code of how to set up Gadgets using
2.0 calls I would greatly appreciate it.

AdvaTHANKSnce.

-----------Source Code Follows-----------

struct TextAttr fnt_Gadget = {
   "topaz.font",
   TOPAZ_EIGHTY,
   FS_NORMAL,
   FPF_ROMFONT
};

APTR vi = NULL;         /* Visual Info *Structure */

struct NewGadget firstGadget = {
    100,100,50,50,    /* Size */
    "Test",           /* Name */
    &fnt_Gadget,      /* font */
    0,                /* ID   */
    PLACETEXT_IN,     /* Flags */
    NULL,             /* Visual Info */
    0                 /* User data */
};

/*=======================================================================*/
void main(void)
{
   OpenLibraries();
   Open_Screen();
   vi = GetVisualInfo(MyScreen,TAG_DONE);
   Open_Windows();
   Define_Menus();
   [....]
}  /* Main */
/*=======================================================================*/
void Define_Menus(void)
/* Set up all menus for use throughout */
{

   /*  ULONG errorcode = NULL; */
   BOOL suc;

   if((menu = CreateMenus(nm,
      GTMN_FrontPen, 0,
   /* GTMN_SecondaryError, &errorcode,*/      /* Does not seem to be */
      TAG_DONE))==NULL) {                     /* implemented ????    */
	 Abort("Error opening menus");             /* in SASC 5.10        */
      }

   suc = LayoutMenus(menu,vi,TAG_DONE); /* <-- this is the line */

   if (suc == FALSE) {
     Abort("Menu Layout failed");
   }

   ClearMenuStrip(Wind0);     /* Set the menus to the windows */
   SetMenuStrip(Wind0,menu);
   ClearMenuStrip(Wind1);
   SetMenuStrip(Wind1,menu);
}  /* Define_Menus */
/*=======================================================================*/
void Open_Windows(void)
/* Open all windows at specified locations */
{
   SHORT udh = 50; /* Upper display window height */
   SHORT mbh = (MyScreen->BarHeight)+1; /* menuBarHeight */
   SHORT cdt = udh + mbh; /* Centre display top */
   SHORT cdh = MyScreen->Height-cdt; /* Centre display height */

   /* Open Central display window with a gadget list */              /**/
								     /**/
   struct Gadget *gad, *glist = NULL;                                /**/
								     /**/
   firstGadget.ng_VisualInfo = vi;                                   /**/
								     /**/
   gad = CreateContext(&glist);                                      /**/
								     /**/
   if (gad == NULL) {                                                /**/
      Abort("Create context failed!!!!");                            /**/
   }                                                                 /**/
								     /**/
   gad = CreateGadget(BUTTON_KIND,glist,&firstGadget,TAG_DONE);      /**/
								     /**/
   if (gad == NULL) {                                                /**/
      Abort("Create Gadget failed");                                 /**/
   }                                                                 /**/

   if((Wind0=OpenWindowTags(NULL,            /* When I remove all lines */
      WA_Left,          0,                      /* marked /**/
      WA_Top,           cdt,                    /* I get fine menus that   */
      WA_Width,         640,                    /* work with no delay      */
      WA_Height,        cdh,                    /* with the marked code it */
      WA_DetailPen,     0,                      /* stuffs it every time !! */
      WA_BlockPen,      1,
      WA_IDCMP,         MENUPICK,
      WA_CustomScreen,  MyScreen,
      WA_Activate,      TRUE,
      WA_Backdrop,      TRUE,
      WA_Gadgets,       glist,                                          /**/
      TAG_END))==NULL) {
	 Abort("Error opening display window!!!");
      }

 /* Open Upper display window */
    if((Wind1=OpenWindowTags(NULL,
       WA_Left,          0,
       WA_Top,           mbh,
       WA_Width,         640,
       WA_Height,        udh,
       WA_DetailPen,     0,
       WA_BlockPen,      1,
       WA_IDCMP,         NULL,
       WA_CustomScreen,  MyScreen,
       WA_Activate,      FALSE,
       WA_Backdrop,      TRUE,
       WA_Borderless,    TRUE,
       TAG_END))==NULL) {
	  Abort("Error opening upper display window!!!");
       }

}  /* Open_Windows */
/*=======================================================================*/

--------End of Code--------