home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
17 Bit Software 3: The Continuation
/
17-Bit_The_Continuation_Disc.iso
/
files
/
nz18.dms
/
nz18.adf
/
Prop.Gadgets
/
PropDemo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-03
|
19KB
|
459 lines
/*
| PropDemo.c : Experimenting with proportional gadgets
| Written 11/88 by Mike Christie
|
| *** This is a Public Domain program, use it as you wish! ***
|
| This program is a fairly straight-forward example of using a vertical
| proportional gadget as used in applications such as word-processors.
| As well as using the normal Intuition-supplied proportional gadget,
| I also created two image gadgets, (the scroll-up and scroll-down arrows
| above and below the proportional slider gagdet.) which scrolls one line
| at a time as opposed to the gadget 'slider' which scrolls by pages.
| When you start experimenting with the gadgets you will get the following
| diagnostics displayed as the window title:
| o VertPot - The current position of the slider in the gadget box.
| o VertBody - The size of the slider gadget in relation to the entire
| slider box.
| o First - First line of text on screen.
| o Last - Last text line on screen.
|
| I have tried to made this demo program as simple as possible so it
| is understandable to someone trying to nut out proporiotnal gadgets
| for the first time. WARNING: Trying to use Intuition in your programs
| without the aid of an Intuition manual is a serious health risk!!
|
| USAGE: PropDemo <Max Lines> <Page Size>
| There are two optional command line arguments with this program.
| If we liken it to a word-processor then the first argument determines the
| number of lines in the document. This can be between 2 and 1000 but will
| default to 100 if either an invalid number or no argument is supplied
| The second argument specifies how many lines of text to display on the
| screen each time the 'slider' is clicked or dragged. (eg: 'Page size' in
| our theoretical word-processor). This argument should be between 1 and
| 30 and will default to 20.
| To get a better idea of these arguments, try running the program the
| first time with no arguments.
|
| PropDemo.c was compiled under Lattice V4.0 using
| 'lc -Lcdn -ccw -v -oRAM: ram:PropDemo.c'
| If you have any FAST ram (expansion memory) then you will need to run
| FixHunk over the executable, so the image data will load into CHIP ram
| at execution time. (Remember the graphics chip can only access 512K.)
|
| If you have any questions (or criticisms?) you can drop me a line or
| give me a ring. My address and phone number (no modem sorry!) is:
| Mike Christie
| 25 Oak Avenue
| Paremata (Wellington)
| Phone 331-651 (After 6PM)
*/
#include "stdio.h"
#include "string.h"
#include "exec/types.h"
#include "graphics/gfxbase.h"
#include "graphics/text.h"
#include "intuition/intuition.h"
struct Window *OpenWindow();
struct IntuiMessage *GetMsg();
/* Gadget I.D's. When you use a gadget, Intuition returns the gadget
number in the Intuition message. */
#define PROP_GADG 1
#define SCROLL_UP 2
#define SCROLL_DOWN 3
/* This is the data which makes up the up- and down-arrow imagery */
USHORT UpArrowData[] = {
0xFFFF,
0xFE7F,
0xF81F,
0xE007,
0xC003,
0xFC3F,
0xFC3F,
0xFC3F,
0xFC3F,
0xFC3F,
0xFC3F,
0xFFFF };
USHORT DownArrowData[] = {
0xFFFF,
0xFC3F,
0xFC3F,
0xFC3F,
0xFC3F,
0xFC3F,
0xFC3F,
0xC003,
0xE007,
0xF81F,
0xFE7F,
0xFFFF
};
/*
| This structure holds the initial values for the proportional gadget.
| AUTOKNOB specifies that Intuition is to supply the entire gadget.
| FREEVERT tells Intuition to allow free vertical sliding of the gadget.
| HorizPot and VertPot are given values once MaxLines & PageSize are set.
| HorizBody is always 0 for vertical proportional gadgets, and VertBody is
| set to 0xffff (decimal 65535) to represent the gadget's total size.
*/
struct PropInfo PropGadgSInfo = {
AUTOKNOB + FREEVERT, /* Flags */
0, 0, /* HorizPot, VertPot */
0, 0xffff /* HorizBody, VertBody */
};
/*
| This is the image of the 'slider' gagdet.
| Note that the 'pointer to ImageData' is null, this is because we are
| asking Intuition to supply it's standard proportional gadget. If we
| use our own image for the slider, then this field would point to the
| 'Image' structure and the AUTOKNOB flag in the PropInfo structure
| (above) would be omitted.
*/
struct Image PropImage = {
0, 0, /* XY origin */
7, 45, /* Image width & height */
0, /* No. of bitplanes */
NULL, /* pointer to ImageData */
0x0000, 0x0000, /* PlanePick, PlaneOnOff */
NULL /* Next image structure */
};
/*
| These two structures below tell Intuition to create gadgets using my
| own custom images, and the 'pointer to ImageData' is therefore
| appropriately set to point to the data which makes up the image.
*/
struct Image UpArrow = {
0, 0, /* XY origin */
16, 12, /* Image width & height */
1, /* No. of bitplanes */
&UpArrowData[0], /* pointer to ImageData */
0x0001, 0x0000, /* PlanePick, PlaneOnOff */
NULL /* Next image structure */
};
struct Image DownArrow = {
0, 0, /* XY origin */
16, 12, /* Image width & height */
1, /* No. of bitplanes */
&DownArrowData[0], /* pointer to ImageData */
0x0001, 0x0000, /* PlanePick, PlaneOnOff */
NULL /* Next image structure */
};
/*
| This is the main proportional gadget structure.
| The GRELRIGHT & GRELHEIGHT flags specify that the proportional gadget
| is sized in relation to the right side of the window, and to the height
| of the window. So, if the window is resized in either width or height
| the gadget will resize itself accordingly and even adjust the slider
| size so it still represents the correct amount of data!
| Because the gadget is sized this way, the LeftEdge field has a value of
| -15 which means the gadget slider box always starts 15 pixels from the
| right edge of the screen. The same goes for the Height. ie: The gadget
| is always 45 pixels shorter than the window height. (Try running the
| program and resizing the window to see what I mean.)
*/
struct Gadget PropGadg = {
NULL, /* No next gadget */
-15, 23, /* LeftEdge, TopEdge */
16, -45, /* hit box width and height */
GADGHCOMP + GRELRIGHT + GRELHEIGHT, /* gadget flags */
RELVERIFY + GADGIMMEDIATE, /* other flags */
PROPGADGET, /* gadget type (Proporiotnal) */
(APTR)&PropImage, /* gadget border/image */
NULL, /* alternate imagery */
NULL, /* first IntuiText structure */
NULL, /* gadget mutual-exclude */
(APTR)&PropGadgSInfo, /* SpecialInfo structure */
PROP_GADG, /* gadget number as #define'd */
NULL /* pointer to user data */
};
/* Gadget to scroll down by one line */
struct Gadget ScrollDown = {
&PropGadg, /* next gadget */
-15, -21, /* LeftEdge, TopEdge */
16, 12, /* hit box width and height */
GADGIMAGE + GRELRIGHT + GRELBOTTOM, /* gadget flags */
RELVERIFY + GADGIMMEDIATE, /* other flags */
BOOLGADGET, /* gadget type */
(APTR)&DownArrow, /* gadget border/image */
NULL, /* alternate imagery */
NULL, /* first IntuiText structure */
NULL, /* gadget mutual-exclude */
NULL, /* SpecialInfo structure */
SCROLL_DOWN, /* user-defined gadget number */
NULL /* pointer to user data */
};
/* Gadget to scroll up by one line */
struct Gadget ScrollUp = {
&ScrollDown, /* next gadget */
-15, 11, /* LeftEdge, TopEdge */
16, 12, /* hit box width and height */
GADGIMAGE + GRELRIGHT, /* gadget flags */
RELVERIFY + GADGIMMEDIATE, /* other flags */
BOOLGADGET, /* gadget type */
(APTR)&UpArrow, /* gadget border/image */
NULL, /* alternate imagery */
NULL, /* first IntuiText structure */
NULL, /* gadget mutual-exclude */
NULL, /* SpecialInfo structure */
SCROLL_UP, /* user-defined gadget number */
NULL /* pointer to user data */
};
struct NewWindow newwindow = {
0, 0, /* LeftEdge, TopEdge */
640, 255, /* window width and height */
0, 1, /* detail and block pens */
REFRESHWINDOW + MOUSEBUTTONS + GADGETUP + GADGETDOWN + CLOSEWINDOW,
WINDOWSIZING + WINDOWDRAG + WINDOWDEPTH + WINDOWCLOSE + SMART_REFRESH +
SIZEBBOTTOM + ACTIVATE, /* other window flags */
&ScrollUp, /* first gadget */
NULL, /* custom CHECKMARK imagery */
" Proportional Gadget Demo by Mike Christie ", /* window name */
NULL, /* custom screen */
NULL, /* custom bitmap */
125, 50, /* minimum width and height */
640, 255, /* maximum width and height */
WBENCHSCREEN /* destination screen type */
};
struct Window *window;
struct GfxBase *GfxBase;
struct IntuitionBase *IntuitionBase;
#define rp window->RPort /* Define RastPort */
char data[1000][15]; /* Array to hold text */
char title[60]; /* Title for SetWindowTitles() */
SHORT first, last, i;
int MaxLines = 100, PageSize = 20; /* Command line argument defaults */
/* Display a line of text in the window */
Display(x, y, text)
USHORT x, y;
char *text;
{
Move(rp, x, y); /* Move to x,y pixel position */
Text(rp, text, strlen(text)); /* Put the text here... */
}
/* Display a page of data on the window */
show_text(start, finish)
SHORT start, finish;
{
int y = 20; /* y position in window for first line of text */
for (i = first; i < (last+1); i++, y+=8)
Display(10, y, data[i]);
}
/* ------------------------ MAINLINE -----------------------*/
main(argc, argv)
int argc;
char **argv;
{
struct IntuiMessage *message; /* Message which Intuition returns */
ULONG Mclass; /* Class of message */
USHORT Mcode; /* Message identifier */
SHORT Mmx, Mmy; /* mouse x & y position */
int len = 0; /* return from stcd_i */
char buf[10];
/* argv[1]: Number of text lines (1 - 1000) or default to 100 */
if (argv[1])
{
/* stcd_i() converts an ascii numeric string to an integer */
len = stcd_i(argv[1], &MaxLines);
if (MaxLines < 1 || MaxLines > 1000)
MaxLines = 100;
}
/* argv[2]: Number of text lines per screen (1 - 30), default to 20 */
if (argv[2])
{
len = stcd_i(argv[2], &PageSize);
if (PageSize < 2 || PageSize > 30)
PageSize = 20;
}
if (MaxLines < PageSize)
Cleanup("PageSize must be less than MaxLines\n");
/* Open graphics and Intuition libraries */
GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",1);
if (GfxBase == NULL)
Cleanup("Can't open graphics.library\n");
IntuitionBase = (struct IntutionBase *) OpenLibrary("intuition.library",1);
if (IntuitionBase == NULL)
Cleanup("Can't open intuition.library\n");
/* Attempt to open window */
window = (OpenWindow(&newwindow));
if (window == NULL)
Cleanup("Can't open window\n");
SetAPen(rp, 1); /* Foreground pen */
sprintf(title, " MaxLines: %d, PageSize: %d ",MaxLines,PageSize);
SetWindowTitles(window, title, -1);
/* fill text array to use as data */
for (i = 0; i < MaxLines; i++)
sprintf(data[i], "record # %3d",i);
/* Set the size of the slider so it represents one page
of text (PageSize lines) out of the total text (MaxLines).
Note that the total size of the gadget is always 65535 (Hex FFFF). */
PropGadgSInfo.VertBody = 0xffff/(MaxLines/PageSize);
ModifyProp(&PropGadg, window, NULL, PropGadgSInfo.Flags,
PropGadgSInfo.HorizPot, PropGadgSInfo.VertPot,
PropGadgSInfo.HorizBody, PropGadgSInfo.VertBody);
first = 0;
last = PageSize - 1;
show_text(first, last); /* Display first page to begin */
/* Start a loop which polls for Intuition 'messages' (eg: when gadget
is selected.) Loop is terminated when user hits close-window gadget. */
for(;;)
{
Wait ( 1 << window->UserPort->mp_SigBit);
while (message = GetMsg(window->UserPort))
{
Mclass = message->Class;
Mcode = message->Code;
Mmx = message->MouseX;
Mmy = message->MouseY;
ReplyMsg(message); /* Tell Intuition we have received the message
so it can wait for the next one. */
/* Intuition only sends messages to us for events declared in the
'Intuition flags' in the window structure. */
switch(Mclass)
{
case CLOSEWINDOW: /* User has hit close-window gadget */
Cleanup(NULL);
break;
case GADGETUP: /* Proportional gadget selected */
do_gadgets(message, window);
break;
}
}
}
}
/*
| This routine handles the gadgets. (updating, resizing etc)
| Note that I am sending the address of the window where the gadget
| resides to the routine. This is not really necessary unless you have
| opened multiple windows in your application.
*/
do_gadgets(message, window)
struct IntuiMessage *message;
struct Window *window;
{
struct Gadget *igad;
USHORT gadget_ID;
LONG vp, vb; /* VertPot & VertBody */
/* Get the ID of the selected gadget from the Intuition message */
igad = (struct Gadget *)message->IAddress;
gadget_ID = igad->GadgetID;
switch (gadget_ID)
{
case PROP_GADG:
/* Get the current 'slider' position */
vp = PropGadgSInfo.VertPot;
vb = PropGadgSInfo.VertBody;
/* Calculate the first and last lines of text to display
on screen in relation to the 'slider' position. */
first = ((MaxLines - PageSize) * vp) / 0xffff;
last = first + PageSize - 1;
/* Display current gadget variables */
sprintf(title, " VertPot: %d, VertBody: %d, First: %d, Last: %d ",
vp, vb, first, last);
SetWindowTitles(window, title, -1);
/* Display the relevant page of text on screen */
show_text(first, last);
break;
case SCROLL_UP: /* Scroll text up one line */
if (first > 0)
first--;
else
first = 0;
last = first + (PageSize - 1);
/* Calculate and set the current slider position */
vp = (first * 0xffff) / (MaxLines - PageSize);
PropGadgSInfo.VertPot = vp;
vb = PropGadgSInfo.VertBody;
sprintf(title, " VertPot: %d, VertBody: %d, First: %d, Last: %d ",
vp, vb, first, last);
SetWindowTitles(window, title, -1);
show_text(first, last);
/* Modify the gadget so the slider is at the right place */
ModifyProp(&PropGadg, window, NULL, PropGadgSInfo.Flags,
PropGadgSInfo.HorizPot, PropGadgSInfo.VertPot,
PropGadgSInfo.HorizBody, PropGadgSInfo.VertBody);
break;
case SCROLL_DOWN: /* Scroll text down one line */
if (last >= MaxLines-1) /* Ensure we don't scroll too far */
last = MaxLines - 1;
else
last++;
first = last - (PageSize - 1);
/* Calculate the slider position in relation to 'first' */
vp = (first * 0xffff) / (MaxLines - PageSize);
PropGadgSInfo.VertPot = vp;
vb = PropGadgSInfo.VertBody;
sprintf(title, " VertPot: %d, VertBody: %d, First: %d, Last: %d ",
vp, vb, first, last);
SetWindowTitles(window, title, -1);
show_text(first, last);
ModifyProp(&PropGadg, window, NULL, PropGadgSInfo.Flags,
PropGadgSInfo.HorizPot, PropGadgSInfo.VertPot,
PropGadgSInfo.HorizBody, PropGadgSInfo.VertBody);
break;
}
}
Cleanup(exit_text)
char *exit_text;
{
if (window) CloseWindow(window);
if (GfxBase) CloseLibrary(GfxBase);
if (IntuitionBase) CloseLibrary(IntuitionBase);
if (exit_text)
puts(exit_text);
exit(0);
}
void MemCleanup() {}