home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 324.lha / slide.c < prev    next >
C/C++ Source or Header  |  1989-12-01  |  7KB  |  269 lines

  1. /*
  2.  * slide.c -   An example program to demonstrate the use of a
  3.  *          numerical proportional gadget knob.
  4.  *          Compiled with Manx 3.6 and 32-bit integers.
  5.  *
  6.  *          Written Jan 6/89 by Hobie Orris.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <intuition/intuition.h>
  11. #include <graphics/gfx.h>
  12. #include <exec/memory.h>
  13.  
  14. /* Offsets of the text drawing inside the slider knob */
  15. #define TEXT_X 2  /* leave 2 pixels to the left as a border */
  16. #define TEXT_Y 7  /* topaz 8 has baseline of 6. add 1 for a border */
  17.  
  18. /* these define the position of the gadget in the window */
  19. #define SLIDE_X 20
  20. #define SLIDE_Y 29
  21.  
  22. struct PropInfo sliderSInfo = {
  23.    FREEHORIZ,  /* PROPINFO flags */
  24.    0,-1, /* horizontal and vertical pot values */
  25.    655,-1,  /* horizontal and vertical body values */
  26. };
  27.  
  28. struct Image slider_image = {
  29.    0,0,  /* XY origin relative to container TopLeft */
  30.    20,9, /* Image width and height in pixels */
  31.    2, /* number of bitplanes in Image */
  32.    NULL, /* pointer to ImageData */
  33.    0x0003,0x0000, /* PlanePick and PlaneOnOff */
  34.    NULL  /* next Image structure */
  35. };
  36.  
  37. struct Gadget slider =
  38. {
  39.    NULL, /* next gadget */
  40.    SLIDE_X,SLIDE_Y,/* origin XY of hit box relative to window TopLeft */
  41.    320,13,  /* hit box width and height */
  42.    GADGHNONE|GADGHIMAGE,   /* gadget flags */
  43.    RELVERIFY|FOLLOWMOUSE,   /* activation flags */
  44.    PROPGADGET, /* gadget type flags */
  45.    (APTR)&slider_image, /* gadget border or image to be rendered */
  46.    NULL, /* alternate imagery for selection */
  47.    NULL, /* first IntuiText structure */
  48.    NULL, /* gadget mutual-exclude long word */
  49.    (APTR)&sliderSInfo,  /* SpecialInfo structure */
  50.    1, /* user-definable data */
  51.    NULL  /* pointer to user-definable data */
  52. };
  53.  
  54. struct NewWindow slider_window =
  55. {
  56.    140,60,  /* window XY origin relative to TopLeft of screen */
  57.    360,60,  /* window width and height */
  58.    0,1,  /* detail and block pens */
  59.    GADGETUP|CLOSEWINDOW|MOUSEMOVE, /* IDCMP flags */
  60.    WINDOWDRAG|SIMPLE_REFRESH|WINDOWCLOSE, /* other window flags */
  61.    &slider, /* first gadget in gadget list */
  62.    NULL, /* custom CHECKMARK imagery */
  63.    (UBYTE *)"Slider example", /* window title */
  64.    NULL, /* custom screen pointer */
  65.    NULL, /* custom bitmap */
  66.    NULL,NULL,  /* minimum width and height */
  67.    NULL,NULL,  /* maximum width and height */
  68.    WBENCHSCREEN   /* destination screen type */
  69. };
  70.  
  71. struct TextFont *tf;
  72. struct TextAttr default_font =
  73. {
  74.    (UBYTE *)"topaz.font", 8, 0, 0
  75. };
  76.  
  77. struct RastPort srp;
  78. struct BitMap sbm;
  79. struct Window *win;
  80. struct IntuitionBase *IntuitionBase;
  81. struct GfxBase *GfxBase;
  82.  
  83. struct Window *OpenWindow();
  84. struct Message *GetMsg();
  85. struct TextFont *OpenFont();
  86. void *OpenLibrary(), *AllocMem();
  87.  
  88. main()
  89. {
  90.    struct IntuiMessage *imsg;
  91.    ULONG class;
  92.    int slide_value;
  93.  
  94.    /* Open libraries */
  95.    openstuff();
  96.  
  97.    /* Set up the slider image before displaying it with OpenWindow */
  98.    init_slider(&slider);
  99.    if (!(win = OpenWindow(&slider_window)))
  100.    {
  101.       fprintf(stderr,"Can't open window\n");
  102.       die();
  103.    }
  104.    set_slider(&slider);
  105.  
  106.    FOREVER
  107.    {
  108.       Wait(1L << win->UserPort->mp_SigBit);
  109.       /* get only latest event to avoid queuing up mousemoves */
  110.       while ((imsg = (struct IntuiMessage *)GetMsg(win->UserPort)) != NULL)
  111.       {
  112.          class = imsg->Class;
  113.          ReplyMsg(imsg);
  114.       }
  115.       switch (class)
  116.       {
  117.          case CLOSEWINDOW:
  118.             closestuff();
  119.             exit(0);
  120.             break;
  121.  
  122.          case MOUSEMOVE:
  123.             set_slider(&slider);
  124.             break;
  125.  
  126.          case GADGETUP:
  127.             set_slider(&slider);
  128.             slide_value = get_slider(&slider);
  129.             printf("slider = %d\n",slide_value);
  130.             break;
  131.       }
  132.    }
  133. }
  134.  
  135.  
  136. /* Read the value of the slider's horizontal pot and convert it to
  137.  * a value in the intended range.
  138.  */
  139. get_slider(gad)
  140. struct Gadget *gad;
  141. {
  142.    register ULONG longval;
  143.    register struct PropInfo *sinfo = (struct PropInfo *)gad->SpecialInfo;
  144.    register USHORT steps, val;
  145.  
  146.    steps = 0xffff / sinfo->HorizBody;  /* Figure out the range of values */
  147.    longval = (ULONG)sinfo->HorizPot;   /* Get the current pot value */
  148.    val = (USHORT)((longval * steps) >> 16);  /* Convert to range */
  149.    return(val);
  150. }
  151.  
  152. /* Write the converted pot value to the bitmap which is the knob image.
  153.  * The imagery MUST be refreshed by RefreshGList, or else the number
  154.  * will be out of sync with reality.  This also has the unpleasant
  155.  * side-effect of causing the the gadget border to strobe.  To avoid this,
  156.  * make the gadget PROPBORDERLESS and draw your own.
  157.  */
  158. set_slider(gad)
  159. struct Gadget *gad;
  160. {
  161.    int val;
  162.    char str[4];
  163.  
  164.    val = get_slider(gad);
  165.  
  166.    /* Convert the value of the prop gadget to a string and write it
  167.     * to the rastport connected to the slider knob's rastport.
  168.     */
  169.    sprintf(str, "%2.2d", val);
  170.    Move(&srp, TEXT_X, TEXT_Y);
  171.  
  172.    Text(&srp, str, 2);
  173.  
  174.    RefreshGList(gad, win, NULL, 1);
  175. }
  176.  
  177.  
  178. /* Initialize the structures that control drawing to the knob's image
  179.  * data.  Also, open topaz 8 font, just in case the default font is
  180.  * something larger (like topaz 9) which won't fit in the knob.
  181.  */
  182. init_slider(gad)
  183. struct Gadget *gad;
  184. {
  185.    register struct Image *im = (struct Image *)gad->GadgetRender;
  186.    register int i, plane_size;
  187.    register long imdata;
  188.  
  189.    InitBitMap(&sbm, im->Depth, im->Width, im->Height);
  190.    plane_size = RASSIZE(im->Width, im->Height);
  191.  
  192.    /* Allocate some image data memory.  Image data planes must occupy
  193.     * contiguous memory, but BitMap planes aren't so particular.
  194.     */
  195.    if (!(im->ImageData = AllocMem(plane_size * im->Depth, MEMF_CHIP)))
  196.    {
  197.       fprintf(stderr,"Yikes! Can't allocate image data.\n");
  198.       die();
  199.    }
  200.    /* This prevents the following addition from being calculated in
  201.     * SHORTS rather than bytes.
  202.     */
  203.    imdata = (long)im->ImageData;
  204.  
  205.    /* Set the BitMap plane pointers to the knob image data. */
  206.    for (i = 0; i < im->Depth; ++i)
  207.    {
  208.       sbm.Planes[i] = (PLANEPTR)(imdata + i * plane_size);
  209.    }
  210.  
  211.    /* Initialize the RastPort structure */
  212.    InitRastPort(&srp);
  213.    srp.BitMap = &sbm;
  214.  
  215.    tf = OpenFont(&default_font);
  216.    if (!tf)
  217.    {
  218.       fprintf(stderr,"Can't open font.\n");
  219.       die();
  220.    }
  221.    SetFont(&srp, tf);
  222.    SetAPen(&srp, 2); /* Draw in black */
  223.    SetBPen(&srp, 3);
  224.    SetDrMd(&srp, JAM2);
  225.    SetRast(&srp, 3); /* Fill the knob in the background pen */
  226. }
  227.  
  228. /* Just preliminary dull stuff */
  229.  
  230. openstuff()
  231. {
  232.    /* Note that we need at least 1.2 since RefreshGList is called */
  233.    if (!(IntuitionBase = OpenLibrary("intuition.library",33)))
  234.    {
  235.       fprintf(stderr,"Can't open intuition library.\n");
  236.       die();
  237.    }
  238.    if (!(GfxBase = OpenLibrary("graphics.library", 0)))
  239.    {
  240.       fprintf(stderr,"Can't open graphics library\n");
  241.       die();
  242.    }
  243. }
  244.  
  245. closestuff()
  246. {
  247.    if (slider_image.ImageData)
  248.       FreeMem(slider_image.ImageData, RASSIZE(slider_image.Width,
  249.          slider_image.Height) * slider_image.Depth);
  250.  
  251.    if (win)
  252.       CloseWindow(win);
  253.  
  254.    if (IntuitionBase)
  255.       CloseLibrary(IntuitionBase);
  256.  
  257.    if (GfxBase)
  258.       CloseLibrary(GfxBase);
  259.  
  260.    if (tf)
  261.       CloseFont(tf);
  262. }
  263.  
  264. die()
  265. {
  266.    closestuff();
  267.    exit(-1);
  268. }
  269.