home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / intuition / gadgets / gadgets.c next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  9.2 KB  |  335 lines

  1. /* Gadgets.c */
  2. /* How to use EACH kind of 1.2/1.3 Gadget Types                      */
  3. /* Try typing small integers into the string gadget, and hit return  */
  4. /* Compiled with Lattice C v5.02                                     */
  5. /* Compiler invoked with: lc -b1 -cfist -L -v -ms                    */
  6.  
  7. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  8.  *
  9.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  10.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  11.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  12.  * information on the correct usage of the techniques and operating system
  13.  * functions presented in this example.  The source and executable code of
  14.  * this example may only be distributed in free electronic form, via bulletin
  15.  * board or as part of a fully non-commercial and freely redistributable
  16.  * diskette.  Both the source and executable code (including comments) must
  17.  * be included, without modification, in any copy.  This example may not be
  18.  * published in printed form or distributed with any commercial product.
  19.  * However, the programming techniques and support routines set forth in
  20.  * this example may be used in the development of original executable
  21.  * software products for Commodore Amiga computers.
  22.  * All other rights reserved.
  23.  * This example is provided "as-is" and is subject to change; no warranties
  24.  * are made.  All use is at your own risk.  No liability or responsibility
  25.  * is assumed.
  26.  */
  27.  
  28. #include <exec/types.h>
  29. #include <intuition/intuition.h>
  30. #include <graphics/gfxbase.h>
  31. #include <libraries/dos.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #ifdef LATTICE
  37. #include <proto/all.h>
  38. #endif
  39.  
  40. #define RP window->RPort
  41. #define TOT_DISPLAY     10
  42. #define LINE_HEIGHT     9
  43. #define MAX_VALUE       9999
  44.  
  45. #include "gadgets.h"
  46.  
  47. /* function declarations */
  48. VOID    OpenAll(VOID);
  49. VOID    cleanExit(int);
  50. USHORT  HandleUGad(struct IntuiMessage *);
  51. USHORT  HandleDGad(struct IntuiMessage *);
  52. VOID    InitSlider(struct Gadget *g);
  53. VOID    GetSlider(struct Gadget *, SHORT);
  54. VOID    DisplayEntries(VOID);
  55. VOID    PrintText(struct RastPort *rp, UBYTE *msg,
  56.           USHORT x, USHORT y, USHORT f, USHORT b);
  57.  
  58. /* global declarations */
  59. struct  GfxBase *GfxBase = NULL;
  60. struct  IntuitionBase *IntuitionBase = NULL;
  61. struct  Window  *window = NULL;
  62.  
  63. /* for showing the list information */
  64. LONG    numentries;
  65. LONG    topentry;
  66.  
  67. VOID  main( int argc, char *argv[] )
  68. {
  69. struct  IntuiMessage *msg = NULL;
  70. ULONG   class;
  71. USHORT  flagi;
  72.  
  73.     OpenAll();
  74.     flagi=TRUE;
  75.     while(flagi) {
  76.        Wait(1L << window->UserPort->mp_SigBit);
  77.        while(msg=(struct IntuiMessage *)GetMsg(window->UserPort)) {
  78.           class=msg->Class;
  79.           switch(class) {
  80.             case INTUITICKS:
  81.               if(ArrowGads[0].Flags&SELECTED) GetSlider(&VertSlider,-1);
  82.               if(VertSlider.Flags&SELECTED)   GetSlider(&VertSlider, 0);
  83.               if(ArrowGads[1].Flags&SELECTED) GetSlider(&VertSlider, 1);
  84.               break;
  85.             case GADGETDOWN:
  86.               flagi=HandleDGad(msg); break;
  87.             case GADGETUP:
  88.               flagi=HandleUGad(msg); break;
  89.             case CLOSEWINDOW:
  90.               flagi=FALSE; break;
  91.             default: break;
  92.           }
  93.           ReplyMsg((struct Message *)msg);
  94.     }
  95.     }
  96.     cleanExit(0);
  97. }
  98.  
  99.  
  100. /* For this example, this is just used to clear the
  101.  * message areas.  You could use it to start a function
  102.  * on the down-press of a gadget 
  103.  */
  104.  
  105. USHORT  HandleDGad(struct IntuiMessage *m)
  106. {
  107. struct  Gadget  *g;
  108. USHORT  id;
  109. UBYTE   msg[12];
  110. USHORT  retval;
  111.  
  112.     retval=TRUE;
  113.     strcpy(msg,"           \000");
  114.     g = (struct Gadget *)m->IAddress;
  115.     id = g->GadgetID;
  116.  
  117. /* clear the entry number area */
  118.     PrintText(RP, msg, 10, 139, 1, 0);
  119.  
  120. /* clear the button number area */
  121.     sprintf(msg, "ID %-4dd", id);
  122.     PrintText(RP, msg, 104, 139, 1, 0);
  123.  
  124. /* return */
  125.     return(retval);
  126. }
  127.  
  128.  
  129. USHORT  HandleUGad(struct IntuiMessage *m)
  130. {
  131. struct  Gadget  *g;
  132. USHORT  id;
  133. UBYTE   msg[12];
  134. USHORT  retval;
  135. USHORT  entry;
  136.  
  137.     retval=TRUE;
  138.     g = (struct Gadget *)m->IAddress;
  139.     id = g->GadgetID;
  140.     switch(id) {
  141.        case 1:  /* The ENTRY area */
  142.           entry = topentry + ((m->MouseY - g->TopEdge + 1) / LINE_HEIGHT) + 1;
  143.           if(entry > numentries)   entry = 0;
  144.           sprintf(msg, "Entry %-4d", entry);
  145.           PrintText(RP, msg, 10, 139, 1, 0);
  146.           break;
  147.        case 2:  /* Clicked in the body of the Vertical PROP */
  148.           GetSlider(&VertSlider, 0);
  149.           break;
  150.        case 5:  /* The STRING gadget */
  151.           /* Get the number of entries.  Filter it out, put it back
  152.            * into the String gadget and display it.
  153.            */
  154.           numentries = NameGadSInfo.LongInt;
  155.           if(numentries>MAX_VALUE)     numentries=MAX_VALUE;
  156.           sprintf(NameGadSIBuff, "%d\000", numentries);
  157.           RefreshGList(&NameGad, window, NULL, 1);
  158.           InitSlider(&VertSlider);
  159.           break;
  160.        case 6:  /* OKAY button */
  161.        case 7:  /* CANCEL button */
  162.           retval = FALSE;
  163.           break;
  164.        default:
  165.           break;
  166.     }
  167. /* show the button number that we pushed */
  168.     sprintf(msg, "ID %-4du", id);
  169.     PrintText(RP, msg, 104, 139, 1, 0);
  170.  
  171. /* return */
  172.     return(retval);
  173. }
  174.  
  175.  
  176. /* Initialize the proportional gadget
  177.  */
  178.  
  179. VOID    InitSlider(struct Gadget *g)
  180. {
  181.     topentry=0L;
  182.     if(numentries>TOT_DISPLAY)
  183.     {
  184.        NewModifyProp(g,window,NULL,AUTOKNOB|FREEVERT,
  185.          NULL,((MAXBODY*topentry)/(numentries)),
  186.          MAXBODY,((MAXBODY*TOT_DISPLAY)/numentries),1L);
  187.     }
  188.     else
  189.     {
  190.        NewModifyProp(g,window,NULL,AUTOKNOB|FREEVERT,
  191.          NULL,NULL,MAXBODY,MAXBODY,1L);
  192.     }
  193. /* clear the entry area */
  194.     RefreshGList(&BackDrop,window,NULL,1);
  195. /* display the current entries */
  196.     DisplayEntries();
  197. }
  198.  
  199.  
  200. /* Get the current entry, based on either the movement of the
  201.  * proportional gadget, or the pressing of the arrow keys.
  202.  */
  203.  
  204. VOID    GetSlider(struct Gadget *g, SHORT dir)
  205. {
  206. USHORT  potv;
  207. struct  PropInfo *p;
  208. static  USHORT  update=0;
  209.  
  210.     p=(struct PropInfo *)g->SpecialInfo;
  211.     if(dir!=0)
  212.     {
  213.        topentry += (LONG)dir;
  214.        if(topentry>0L && topentry<(numentries-TOT_DISPLAY))
  215.        {
  216.           potv=((MAXBODY*topentry)/(numentries-TOT_DISPLAY));
  217.           update=0;
  218.        }
  219.        else
  220.        {
  221.           /* It is necessary that the check for topentry>=numentries
  222.            * is before topentry<=0 to catch the instance when
  223.            * numentries < TOT_DISPLAY
  224.            */
  225.           if(topentry>=(numentries-TOT_DISPLAY))
  226.           {
  227.              potv=MAXBODY;
  228.              topentry=(numentries-TOT_DISPLAY);
  229.              update++;
  230.           }
  231.           if(topentry<=0L)
  232.           {
  233.              potv=0;
  234.              topentry=0L;
  235.              update++;
  236.           }
  237.        }
  238.        if(numentries>TOT_DISPLAY && update<3)
  239.        {
  240.           NewModifyProp(g,window,NULL,AUTOKNOB|FREEVERT,
  241.             NULL,(LONG)potv,MAXBODY,
  242.             ((MAXBODY*TOT_DISPLAY)/numentries-TOT_DISPLAY),1L);
  243.        }
  244.     }
  245.     else
  246.     {
  247.        if(numentries>TOT_DISPLAY)
  248.           topentry=(p->VertPot*(numentries-TOT_DISPLAY))/MAXBODY;
  249.        else
  250.           topentry=0L;
  251.     }
  252.     DisplayEntries();
  253. }
  254.  
  255.  
  256. /* Update the display to show the current view of entries. */
  257.  
  258. VOID    DisplayEntries(VOID)
  259. {
  260. UBYTE   msg[5];
  261. register LONG i;
  262.  
  263. /* update the display */
  264.     for(i=0; (i<TOT_DISPLAY && i<numentries); i++)
  265.     {
  266.        sprintf(msg,"%4ld", i + topentry + 1L);
  267.        PrintText(RP, msg,
  268.           7, (USHORT)(EntryBox.TopEdge + (i * LINE_HEIGHT)), 1, 2);
  269.     }
  270.  
  271. /* put the pen back the way it was */
  272.     SetBPen(RP, 0);
  273. }
  274.  
  275. VOID    PrintText(struct RastPort *rp, UBYTE *msg,
  276.           USHORT x, USHORT y, USHORT f, USHORT b)
  277. {
  278.     strcpy(Buffer, msg);
  279.     Messages.FrontPen = f;
  280.     Messages.BackPen = b;
  281.     PrintIText(rp, &Messages, (LONG)x, (LONG)y);
  282. }
  283.  
  284. VOID    cleanExit(int retval)
  285. {
  286.     if(window)          CloseWindow(window);
  287.     if(GfxBase)         CloseLibrary((struct Library *)GfxBase);
  288.     if(IntuitionBase)   CloseLibrary((struct Library *)IntuitionBase);
  289.     exit(retval);
  290. }
  291.  
  292. VOID    OpenAll(VOID)
  293. {
  294. struct Gadget *g;
  295.  
  296.     if(!(IntuitionBase=(struct IntuitionBase *)
  297.      OpenLibrary("intuition.library",33)))
  298.        cleanExit(ERROR_INVALID_RESIDENT_LIBRARY);
  299.     if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",33)))
  300.        cleanExit(ERROR_INVALID_RESIDENT_LIBRARY);
  301.  
  302. /* center the window */
  303.     NewWindow.TopEdge = (GfxBase->NormalDisplayRows - NewWindow.Height) / 2;
  304.     NewWindow.LeftEdge= (GfxBase->NormalDisplayColumns-NewWindow.Width) / 2;
  305.  
  306. /* open the window */
  307.     if(!(window=OpenWindow(&NewWindow)))
  308.        cleanExit(ERROR_NO_FREE_STORE);
  309.  
  310. /* Adjust the top of the gadgets relative to the upper border.
  311.  * Usually is 11 if using Topaz80, 12 if using Topaz60
  312.  */
  313.     g=&VertSlider;
  314.     while(g)
  315.     {
  316.        g->TopEdge += window->BorderTop;
  317.        g = g->NextGadget;
  318.     }
  319.  
  320. /* pre-initialize the list variables */
  321.     numentries=100;
  322.  
  323. /* just to show how many entries we're working with */
  324.     sprintf(NameGadSIBuff,"%d\000", numentries);
  325.  
  326. /* hook the gadgets to the window */
  327.     AddGList(window, &VertSlider, 0, -1, NULL);
  328.  
  329. /* update the display to show the new gadgets */
  330.     RefreshGList(&VertSlider, window, NULL, -1);
  331.  
  332. /* update the vertical proportional gadget information */
  333.     InitSlider(&VertSlider);
  334. }
  335.