home *** CD-ROM | disk | FTP | other *** search
- /*
- * slide.c - An example program to demonstrate the use of a
- * numerical proportional gadget knob.
- * Compiled with Manx 3.6 and 32-bit integers.
- *
- * Written Jan 6/89 by Hobie Orris.
- */
-
- #include <stdio.h>
- #include <intuition/intuition.h>
- #include <graphics/gfx.h>
- #include <exec/memory.h>
-
- /* Offsets of the text drawing inside the slider knob */
- #define TEXT_X 2 /* leave 2 pixels to the left as a border */
- #define TEXT_Y 7 /* topaz 8 has baseline of 6. add 1 for a border */
-
- /* these define the position of the gadget in the window */
- #define SLIDE_X 20
- #define SLIDE_Y 29
-
- struct PropInfo sliderSInfo = {
- FREEHORIZ, /* PROPINFO flags */
- 0,-1, /* horizontal and vertical pot values */
- 655,-1, /* horizontal and vertical body values */
- };
-
- struct Image slider_image = {
- 0,0, /* XY origin relative to container TopLeft */
- 20,9, /* Image width and height in pixels */
- 2, /* number of bitplanes in Image */
- NULL, /* pointer to ImageData */
- 0x0003,0x0000, /* PlanePick and PlaneOnOff */
- NULL /* next Image structure */
- };
-
- struct Gadget slider =
- {
- NULL, /* next gadget */
- SLIDE_X,SLIDE_Y,/* origin XY of hit box relative to window TopLeft */
- 320,13, /* hit box width and height */
- GADGHNONE|GADGHIMAGE, /* gadget flags */
- RELVERIFY|FOLLOWMOUSE, /* activation flags */
- PROPGADGET, /* gadget type flags */
- (APTR)&slider_image, /* gadget border or image to be rendered */
- NULL, /* alternate imagery for selection */
- NULL, /* first IntuiText structure */
- NULL, /* gadget mutual-exclude long word */
- (APTR)&sliderSInfo, /* SpecialInfo structure */
- 1, /* user-definable data */
- NULL /* pointer to user-definable data */
- };
-
- struct NewWindow slider_window =
- {
- 140,60, /* window XY origin relative to TopLeft of screen */
- 360,60, /* window width and height */
- 0,1, /* detail and block pens */
- GADGETUP|CLOSEWINDOW|MOUSEMOVE, /* IDCMP flags */
- WINDOWDRAG|SIMPLE_REFRESH|WINDOWCLOSE, /* other window flags */
- &slider, /* first gadget in gadget list */
- NULL, /* custom CHECKMARK imagery */
- (UBYTE *)"Slider example", /* window title */
- NULL, /* custom screen pointer */
- NULL, /* custom bitmap */
- NULL,NULL, /* minimum width and height */
- NULL,NULL, /* maximum width and height */
- WBENCHSCREEN /* destination screen type */
- };
-
- struct TextFont *tf;
- struct TextAttr default_font =
- {
- (UBYTE *)"topaz.font", 8, 0, 0
- };
-
- struct RastPort srp;
- struct BitMap sbm;
- struct Window *win;
- struct IntuitionBase *IntuitionBase;
- struct GfxBase *GfxBase;
-
- struct Window *OpenWindow();
- struct Message *GetMsg();
- struct TextFont *OpenFont();
- void *OpenLibrary(), *AllocMem();
-
- main()
- {
- struct IntuiMessage *imsg;
- ULONG class;
- int slide_value;
-
- /* Open libraries */
- openstuff();
-
- /* Set up the slider image before displaying it with OpenWindow */
- init_slider(&slider);
- if (!(win = OpenWindow(&slider_window)))
- {
- fprintf(stderr,"Can't open window\n");
- die();
- }
- set_slider(&slider);
-
- FOREVER
- {
- Wait(1L << win->UserPort->mp_SigBit);
- /* get only latest event to avoid queuing up mousemoves */
- while ((imsg = (struct IntuiMessage *)GetMsg(win->UserPort)) != NULL)
- {
- class = imsg->Class;
- ReplyMsg(imsg);
- }
- switch (class)
- {
- case CLOSEWINDOW:
- closestuff();
- exit(0);
- break;
-
- case MOUSEMOVE:
- set_slider(&slider);
- break;
-
- case GADGETUP:
- set_slider(&slider);
- slide_value = get_slider(&slider);
- printf("slider = %d\n",slide_value);
- break;
- }
- }
- }
-
-
- /* Read the value of the slider's horizontal pot and convert it to
- * a value in the intended range.
- */
- get_slider(gad)
- struct Gadget *gad;
- {
- register ULONG longval;
- register struct PropInfo *sinfo = (struct PropInfo *)gad->SpecialInfo;
- register USHORT steps, val;
-
- steps = 0xffff / sinfo->HorizBody; /* Figure out the range of values */
- longval = (ULONG)sinfo->HorizPot; /* Get the current pot value */
- val = (USHORT)((longval * steps) >> 16); /* Convert to range */
- return(val);
- }
-
- /* Write the converted pot value to the bitmap which is the knob image.
- * The imagery MUST be refreshed by RefreshGList, or else the number
- * will be out of sync with reality. This also has the unpleasant
- * side-effect of causing the the gadget border to strobe. To avoid this,
- * make the gadget PROPBORDERLESS and draw your own.
- */
- set_slider(gad)
- struct Gadget *gad;
- {
- int val;
- char str[4];
-
- val = get_slider(gad);
-
- /* Convert the value of the prop gadget to a string and write it
- * to the rastport connected to the slider knob's rastport.
- */
- sprintf(str, "%2.2d", val);
- Move(&srp, TEXT_X, TEXT_Y);
-
- Text(&srp, str, 2);
-
- RefreshGList(gad, win, NULL, 1);
- }
-
-
- /* Initialize the structures that control drawing to the knob's image
- * data. Also, open topaz 8 font, just in case the default font is
- * something larger (like topaz 9) which won't fit in the knob.
- */
- init_slider(gad)
- struct Gadget *gad;
- {
- register struct Image *im = (struct Image *)gad->GadgetRender;
- register int i, plane_size;
- register long imdata;
-
- InitBitMap(&sbm, im->Depth, im->Width, im->Height);
- plane_size = RASSIZE(im->Width, im->Height);
-
- /* Allocate some image data memory. Image data planes must occupy
- * contiguous memory, but BitMap planes aren't so particular.
- */
- if (!(im->ImageData = AllocMem(plane_size * im->Depth, MEMF_CHIP)))
- {
- fprintf(stderr,"Yikes! Can't allocate image data.\n");
- die();
- }
- /* This prevents the following addition from being calculated in
- * SHORTS rather than bytes.
- */
- imdata = (long)im->ImageData;
-
- /* Set the BitMap plane pointers to the knob image data. */
- for (i = 0; i < im->Depth; ++i)
- {
- sbm.Planes[i] = (PLANEPTR)(imdata + i * plane_size);
- }
-
- /* Initialize the RastPort structure */
- InitRastPort(&srp);
- srp.BitMap = &sbm;
-
- tf = OpenFont(&default_font);
- if (!tf)
- {
- fprintf(stderr,"Can't open font.\n");
- die();
- }
- SetFont(&srp, tf);
- SetAPen(&srp, 2); /* Draw in black */
- SetBPen(&srp, 3);
- SetDrMd(&srp, JAM2);
- SetRast(&srp, 3); /* Fill the knob in the background pen */
- }
-
- /* Just preliminary dull stuff */
-
- openstuff()
- {
- /* Note that we need at least 1.2 since RefreshGList is called */
- if (!(IntuitionBase = OpenLibrary("intuition.library",33)))
- {
- fprintf(stderr,"Can't open intuition library.\n");
- die();
- }
- if (!(GfxBase = OpenLibrary("graphics.library", 0)))
- {
- fprintf(stderr,"Can't open graphics library\n");
- die();
- }
- }
-
- closestuff()
- {
- if (slider_image.ImageData)
- FreeMem(slider_image.ImageData, RASSIZE(slider_image.Width,
- slider_image.Height) * slider_image.Depth);
-
- if (win)
- CloseWindow(win);
-
- if (IntuitionBase)
- CloseLibrary(IntuitionBase);
-
- if (GfxBase)
- CloseLibrary(GfxBase);
-
- if (tf)
- CloseFont(tf);
- }
-
- die()
- {
- closestuff();
- exit(-1);
- }
-