Coder's Clinic 3



   CCCC    OOOO   DDDD   EEEEEE    CCCC  L      IIIII N    N IIIII  CCCC
  C    C  O    O  D   D  E        C    C L        I   NN   N   I   C    C
 C       O      O D    D EEE     C       L        I   N NN N   I  C
  C    C  O    O  D   D  E        C    C L        I   N   NN   I   C    C
   CCCC    OOOO   DDDD   EEEEEE    CCCC  LLLLLL IIIII N    N IIIII  CCCC
--------------------------------------------------------------------------
CODE CLINIC #3                                        I LUV GADGETS!!!  


        As promised, We will add a gadget to our window. First we have to
        get some info on how to do this.

        GADGETS:Two types of gadgets.

        *FIRST TYPE OF GADGET:System                 
        System gadgets are those that you see in the windows and screens
        that you open. And we are  not going to go much further than that.
        
        *SECOND TYPE OF GADGET:Application
        Application gadgets are the ones in which you create and 
                use in your own programs. They can be placed at any 
                location inside the window and can use any image. 
                There are four basic types of application gadgets.

        [1] Boolean    : OK buttons ect...                 
        [2] Proportion : Sound level ect...
        [3] String     : Enter your name ect...                 
        [4] Custom     : New gadgets.
        
       
      *****--------------------------------------------------       
      * N *   I STRONGLY SUGGEST INVESTING IN THE COMPLETE  |
      * O *   SET OF AMIGA ROM KERNEL MANUALS... AT THE     |
      * T *   VERY LEAST YOU SHOULD TRY TO GET THE LIBRARIES|       
      * E *   MANUAL ...THERE ARE MANY EXAMPLES/EXPLANATIONS|       
      *****--------------------------------------------------
                 To create a gadget you simply fill in a Gadget structure. 
        (see fig. 1). You may also use the Gadtools Library to create 
        gadgets (which we will), As you can see the Gadget structure has 
        many items 
        and to cover all these would     struct Gadget                   
        consume a great deal of text    {                                
        as suggested before get the        struct Gadget *NextGadget     
        ROM Kernel manual:Libraries        WORD LeftEdge, TopEdge;       
        (at the very least) as there       WORD Width, Height;           
        is detailed information on all     UWORD Flags;                  
        structures the amiga programmer    UWORD Activation;             
        needs to understand.               UWORD GadgetType;             
                                           APTR GadgetRender;            
        OK lets do something different     APTR SelectRender;            
        like use the Gadtools Library      struct IntuiText *GadgetText; 
        to put some gadgets up on the      LONG MutualExclude;           
        window and let us know which one   APTR SpecialInfo;             
        we selected.                       UWORD GadgetID;               
                                           APTR UserData;                
        Now that we know about the       };                              
        Intuistion gadgets lets take a               FIG. 1              
        look at the Gadtools gadgets.
        
        There are 12 types of gadgets that the Gadtools library supports
        and they are:
        
       Button                 
       String             
       Integer            
       Checkboxes              
       Mutually exclusive 
       Cycle              
       Sliders            
       Scrollers          
       Listviews          
       Palette            
       Text-display       
       Numeric-display    
       
       With all of these types of gads what couldnt you do? And as a real
       bonus there are several GUI buiders on aminet, just look in dev/gui
       and you will see several I personally like GadToolBox by Jan van den
       Baard. These programs take a great deal of coding off of your back 
       and allow you to work on the inner workings of your own program.
       
       Lets take a look a the structure used to create a gadtools gadget:
       
                struct NewGadget                       
                {                                      
                        WORD ng_LeftEdge,ng_TopEdge;  
                        WORD ng_Width,ng_Height;      
                        UBYTE *ng_GadgetText;         
                        struct TextAttr *ng_TextAttr; 
                        UWORD ng_GadgetID;             
                        ULONG ng_Flags;                
                        APTR  ng_VisualInfo;           
                        APTR  ng_UserData;             
                };                                     
                               FIG 2                 
                
                If you compare the Gadget (fig 1) and the NewGadget (fig 2)
        structures you will see some common items.
       
       The way you create a gadget using gadtools is the CreateGadget()
       function, its prototype is:
        
        struct Gadget *CreateGadget(ULONG kind,
                                    struct Gadget *prevgad,
                                    struct NewGadget *newgad,
                                    struct TagItem *taglist);
       so to create a single button gadget we would do it like this:
       
       ng.ng_TextAttr   = &Topaz80;
       ng,ng_VisualInfo = vi;
       ng.ng_LeftEdge   = 10;
       ng.ng_TopEdge    = 10;
       ng.ng_Width      = 60;
       ng.ng_Height     = 12;
       ng.ng_GadgetText = "CLICK";
       ng.ng_GadgetID   = 1;
       ng.ng_Flags      = 0;
       gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);
                                            _____________________________
       If you look at fig 2 you will        []hello__________________[][]
       see that we have filled the          |                           |
       structure and then made a call       |  CLICK  QUIT            |
       to CreateGadget(). The nice          |                           |
       thing is that if you were to         |                           | 
       add another gadget you now only      |                           |
       fill in the items that will          |                           |
       change, an example of this           |                           |
       is given below. Lets add a           |___________________________|
       gadget that has "QUIT" inside                     
       it.                                             FIG 4             
       
       ng.LeftEdge      =80;
       ng.ng_GadgetText ="QUIT";
       ng.ng_GadgetID   =2;
       gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

       and now we would have two gadgets that would look like fig 4
       
       
       The example program was created using the example from the ROM 
       Kernel Manual:Libraries in the chapter Gadtools Library pg.383-385.
       
       I would also like to mention that this layout was written by me and 
       not by a GUI builder... and this is the GUI that we will use to 
       create our AddressBook program.
       
       There are several new items to this code and alot of it you will not
       understand, I will try to clear up any questions in the next issue
       as we will go line by line of this code and explain what is going
       on... 
       
      *****-------------------------  *****-------------------------       
      * N *   ALL CODE IS INTENDED |  * N *   I COMPILED THIS CODE |
      * O *   FOR VERSION 2.0 OR   |  * O *   WITH SAS/C V6.0 AND  |
      * T *   ABOVE...             |  * T *   HAD NO ERRORS OR     |       
      * E *                        |  * E *   WARNINGS             |       
      *****-------------------------  *****-------------------------

//.C..C.O.D.E..............................................................

#include <exec/types.h>  #include <intuition/intuition.h>  #include
<intuition/gadgetclass.h>  #include <libraries/gadtools.h> #include
<clib/exec_protos.h>  #include <clib/intuition_protos.h>  #include
<clib/gadtools_protos.h> #include <stdio.h>



VOID process_window_events(struct Window *);  VOID gadtoolsWindow(VOID);
VOID Do_Gad(struct Window *mywin,struct Gadget *gad,UWORD code);  BOOL
terminated = FALSE;  struct TextAttr Topaz80 = { "topaz.font",8,0,0,};

struct Library *IntuitionBase; struct Library *GadToolsBase;

void main(void) {
        if ((IntuitionBase = OpenLibrary("intuition.library",37))!=NULL)
           {
           if ((GadToolsBase = OpenLibrary("gadtools.library",37))!=NULL)
              {
              gadtoolsWindow();
              
              CloseLibrary(GadToolsBase);
              }
           CloseLibrary(IntuitionBase);
           } }

VOID gadtoolsWindow(VOID) { struct Screen *mysc; struct Window *mywin;
struct Gadget *glist, *gad; struct NewGadget ng; void   *vi;

glist = NULL;

if ((mysc = LockPubScreen(NULL)) != NULL)
   {
   if ((vi = GetVisualInfo(mysc, TAG_END)) != NULL)
      {
      gad = CreateContext(&glist);
      
      ng.ng_TextAttr    =&Topaz80;
      ng.ng_VisualInfo  =vi;
      
      ng.ng_LeftEdge    =300;
      ng.ng_TopEdge     =2+mysc->WBorTop+(mysc->Font->ta_YSize+1);
      ng.ng_Width       =16;
      ng.ng_Height      =12;
      ng.ng_GadgetText  ="A";
      ng.ng_GadgetID    =1;
      ng.ng_Flags       =0;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="B";
      ng.ng_GadgetID    =2;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="C";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="D";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);      

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="E";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    =300;
      ng.ng_TopEdge     +=14;
      ng.ng_GadgetText  ="F";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="G";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="H";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="I";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="J";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    =300;
      ng.ng_TopEdge     +=14;
      ng.ng_GadgetText  ="K";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="L";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="M";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="N";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="O";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    =300;
      ng.ng_TopEdge     +=14;
      ng.ng_GadgetText  ="P";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="Q";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="R";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="S";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="T";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    =300;
      ng.ng_TopEdge     +=14;
      ng.ng_GadgetText  ="U";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="V";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="W";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="X";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    +=18 ;
      ng.ng_GadgetText  ="Y";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge    =300;
      ng.ng_TopEdge     +=14;
      ng.ng_GadgetText  ="Z";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);

      ng.ng_LeftEdge     +=18;
      ng.ng_GadgetText  ="?";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);


      ng.ng_LeftEdge    +=18 ;
      ng.ng_Width       =54;
      ng.ng_GadgetText  ="ADD";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);      

      ng.ng_TopEdge    +=14;
      ng.ng_Width       =54;
      ng.ng_GadgetText  ="QUIT";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);    

      ng.ng_LeftEdge    =10;
      ng.ng_TopEdge     =2+mysc->WBorTop+(mysc->Font->ta_YSize+1);
      ng.ng_Width       =180;
      ng.ng_Height       =12;
      ng.ng_GadgetText  ="BOOL 1";
      ng.ng_GadgetID    ++;
      gad= CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);




      if (gad != NULL)
         {
         if ((mywin = OpenWindowTags(NULL,
                WA_Title,  "PhoneBook V1.0",
                WA_Gadgets, glist,      WA_AutoAdjust, TRUE,
                WA_Width, 400,          WA_InnerHeight, 100,
                WA_DragBar, TRUE,       WA_DepthGadget, TRUE,
                WA_Activate, TRUE,      WA_CloseGadget, TRUE,
                WA_IDCMP, IDCMP_CLOSEWINDOW | 
                          IDCMP_REFRESHWINDOW | 
                          BUTTONIDCMP,
                WA_PubScreen, mysc,
                TAG_END)) != NULL)
                  {
                  GT_RefreshWindow(mywin,NULL);
                  
                  process_window_events(mywin);
                  
                  CloseWindow(mywin);
                  
                  }
         }
        FreeGadgets(glist);
        FreeVisualInfo(vi);
        }
      UnlockPubScreen(NULL,mysc);
      } }

VOID process_window_events(struct Window *mywin) { struct IntuiMessage
*imsg; ULONG  class; UWORD  code; struct Gadget *gad;


while (!terminated)
        {
        Wait(1<< mywin->UserPort->mp_SigBit);
        while ((!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)))
           {
           gad = (struct Gadget *)imsg->IAddress;
           class=imsg->Class;
           code =imsg->Code;
           
           switch (class)
              {
              case IDCMP_GADGETUP:
                                  Do_Gad(mywin,gad,code);
                                  break;
      
              case IDCMP_CLOSEWINDOW:
                                  terminated = TRUE;
                                  break;
              case IDCMP_REFRESHWINDOW:
                                  GT_BeginRefresh(mywin);
                                  GT_EndRefresh(mywin,TRUE);
                                  break;
              }
              GT_ReplyIMsg(imsg);
       }
   } } VOID Do_Gad(struct Window *mywin,struct Gadget *gad,UWORD code) {
switch(gad->GadgetID)
        {
        case 1:printf("A\n");
               break;
        case 2:printf("B\n");
               break;
        case 3:printf("C\n");
               break;
        case 4:printf("D\n");
               break;
        case 5:printf("E\n");
               break;
        case 6:printf("F\n");
               break;
        case 7:printf("G\n");
               break;
        case 8:printf("H\n");
               break;
        case 9:printf("I\n");
               break;
        case 10:printf("J\n");
               break;
        case 11:printf("K\n");
               break;
        case 12:printf("L\n");
               break;
        case 13:printf("M\n");
               break;
        case 14:printf("N\n");
               break;
        case 15:printf("O\n");
               break;
        case 16:printf("P\n");
               break;
        case 17:printf("Q\n");
               break;
        case 18:printf("R\n");
               break;
        case 19:printf("S\n");
               break;
        case 20:printf("T\n");
               break;
        case 21:printf("U\n");
               break;
        case 22:printf("V\n");
               break;
        case 23:printf("W\n");
               break;
        case 24:printf("X\n");
               break;
        case 25:printf("Y\n");
               break;
        case 26:printf("Z\n");
               break;
        case 29:printf("TERMINATE\n");
                terminated = TRUE;
                break;
        }  }


// End C Code 

NEXT MONTH: HEY DONT MISS IT !!!!

        I will explain the code you just read line by line.
        See you on IRC #amiga!!!

[Contents] [Browse ->] [Browse <-]
HTML Conversion by AG2HTML.pl V2.950424, perl 5.001 & witbrock@cs.cmu.edu