/****************************************************************************/ /* Program: Graph It II (The C Program) */ /* SubModule: window.c 1-20-87 */ /* CopyRight By Flynn D. Fishman January 1987 */ /* Feel Free to copy and alter this source */ /****************************************************************************/ /* This module contains some of the more "primitive" graphic routines such as Line, DrawImages, DrawWindow */ #include "graph.h" #define MAXMENU 2 struct Menu menu[MAXMENU]; /* Image data structures (Icons) */ USHORT Move_Icon[] = { 0xffff, 0x8001, 0x8001, 0x8181, 0x8d81, 0x8db1, 0x8db1, 0xedb5, 0xeffd, 0xbffd, 0x9ffd, 0x8ff1, 0x8001, 0x8001, 0xffff, 0xffff, /* 1111 1111 1111 1111 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- 22-2 2--- ---1 */ 0x8001, /* 1--- 22-2 2-22 ---1 */ 0x8001, /* 1--- 22-2 2-22 ---1 */ 0x8001, /* 122- 22-2 2-22 -2-1 */ 0x8001, /* 122- 2222 2222 22-1 */ 0x8001, /* 1-22 2222 2222 22-1 */ 0x8001, /* 1--2 2222 2222 22-1 */ 0x8001, /* 1--- 2222 2222 2--1 */ 0x9ffd, /* 1--3 3333 3333 33-1 */ 0x9ffd, /* 1--3 3333 3333 33-1 */ 0xffff /* 1111 1111 1111 1111 */ }; USHORT Zoom_Icon[] = { 0xffff, 0x8001, 0x83c1, 0x8661, 0x8c31, 0x8c31, 0x8661, 0x83c1, 0x8181, 0x8181, 0x8181, 0x8181, 0x8181, 0x8001, 0xffff, 0xffff, /* 1111 1111 1111 1111 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 1--- --33 33-- ---1 */ 0x8001, /* 1--- -332 233- ---1 */ 0x8001, /* 1--- 33-- 2233 ---1 */ 0x8001, /* 1--- 33-- -233 ---1 */ 0x8001, /* 1--- -33- -33- ---1 */ 0x8001, /* 1--- --33 33-- ---1 */ 0x8001, /* 1--- ---3 3--- ---1 */ 0x8001, /* 1--- ---3 3--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---3 3--- ---1 */ 0x8001, /* 1--- ---3 3--- ---1 */ 0x8001, /* 1--- ---- ---- ---1 */ 0xffff, /* 1111 1111 1111 1111 */ }; USHORT Command_Icon[] = { 0xffff, 0x8001, 0x8001, 0xe0c1, 0xe0c1, 0xb19f, 0x9b1f, 0x8e01, 0x8c1f, 0x8c1f, 0x8c01, 0x8c01, 0x8001, 0x8001, 0xffff, 0xffff, /* 1111 1111 1111 1111 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 122- ---- 22-- ---1 */ 0x8001, /* 122- ---- 22-- ---1 */ 0x8001, /* 1-22 ---2 2--2 2221 */ 0x8001, /* 1--2 2-22 ---2 2221 */ 0x8001, /* 1--- 222- ---- ---1 */ 0x8001, /* 1--- 22-- ---2 2221 */ 0x8001, /* 1--- 22-- ---2 2221 */ 0x8001, /* 1--- 22-- ---- ---1 */ 0x8001, /* 1--- 22-- ---- ---1 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 1--- ---- ---- ---1 */ 0xffff /* 1111 1111 1111 1111 */ }; USHORT Help_Icon[] = { 0xffff, 0x8001, 0x83e1, 0x87e1, 0x8c71, 0x80e1, 0x80e1, 0x81c1, 0x81c1, 0x8001, 0x8081, 0x81c1, 0x8081, 0x8001, 0xffff, 0xffff, /* 1111 1111 1111 1111 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 1--- --22 222- ---1 */ 0x8001, /* 1--- -222 2222 ---1 */ 0x8001, /* 1--- 22-- -222 ---1 */ 0x8001, /* 1--- ---- 222- ---1 */ 0x8001, /* 1--- ---- 222- ---1 */ 0x8001, /* 1--- ---2 22-- ---1 */ 0x8001, /* 1--- ---2 22-- ---1 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 1--- ---- 2--- ---1 */ 0x8001, /* 1--- ---2 22-- ---1 */ 0x8001, /* 1--- ---- 2--- ---1 */ 0x8001, /* 1--- ---- ---- ---1 */ 0xffff, /* 1111 1111 1111 1111 */ }; USHORT Hidden_Icon[] = { 0xffff, 0x8001, 0x8001, 0x8001, 0x83cf, 0x8301, 0x8301, 0x8301, 0x8001, 0x8301, 0x8301, 0x8301, 0x83ff, 0x8001, 0xffff, 0xffff, /* 1111 1111 1111 1111 */ 0x8001, /* 1--- ---- ---- ---1 */ 0xbff1, /* 1-22 2222 2222 ---1 */ 0xb031, /* 1-22 ---- --22 ---1 */ 0xb03f, /* 1-22 --11 1122 3331 */ 0xb031, /* 1-22 --11 --22 ---1 */ 0xb031, /* 1-22 --11 --22 ---1 */ 0xb031, /* 1-22 --11 --22 ---1 */ 0xbff1, /* 1-22 2222 2222 ---1 */ 0x8301, /* 1--- --33 ---- ---1 */ 0x8301, /* 1--- --33 ---- ---1 */ 0x8301, /* 1--- --33 ---- ---1 */ 0x83ff, /* 1--- --33 3333 3331 */ 0x8001, /* 1--- ---- ---- ---1 */ 0xffff, /* 1111 1111 1111 1111 */ }; USHORT Home_Icon[] = { 0xffff, 0x8181, 0x8181, 0x8181, 0x8181, 0x8181, 0x8181, 0xffff, 0x8181, 0x8181, 0x8181, 0x8181, 0x8181, 0x8181, 0xffff, 0xffff, /* 1111 1111 1111 1111 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1222 2222 2222 2221 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0x8001, /* 1--- ---2 2--- ---1 */ 0xffff /* 1111 1111 1111 1111 */ }; USHORT Quit_Icon[] = { 0xffff, 0x8001, 0x83c1, 0x8661, 0x8c31, 0x8c31, 0x9819, 0x9819, 0x9999, 0x9db9, 0x8cf1, 0x8679, 0x83cd, 0x8001, 0xffff, 0xffff, /* 1111 1111 1111 1111 */ 0x8001, /* 1--- ---- ---- ---1 */ 0x8001, /* 1--- --22 22-- ---1 */ 0x8001, /* 1--- -22- -22- ---1 */ 0x8001, /* 1--- 22-- --22 ---1 */ 0x8001, /* 1--- 22-- --22 ---1 */ 0x8001, /* 1--2 2--- ---2 2--1 */ 0x8001, /* 1--2 2--- ---2 2--1 */ 0x8001, /* 1--2 2--2 2--2 2--1 */ 0x8001, /* 1--- 22-2 2-22 ---1 */ 0x8001, /* 1--- 22-- 2222 ---1 */ 0x8001, /* 1--- -22- -222 2--1 */ 0x8001, /* 1--- --22 22-- 22-1 */ 0x8001, /* 1--- ---- ---- ---1 */ 0xffff /* 1111 1111 1111 1111 */ }; #define BALLHEIGHT 5 #define BALLWIDTH 6 struct Image Icons = { 0,0, /* left Top */ 16, ICONY, 2, /* width hieght, depth */ &Move_Icon[0], /* image date */ 0x3, 0x0, /* PlanePick, PlaneOnOff */ NULL, /* Next Image */ }; struct IntuitionBase *IntuitionBase; struct GfxBase *GfxBase; struct TextAttr MyFont = { "topaz.font", /* Font Name */ TOPAZ_SIXTY, /* Font Height */ FS_NORMAL, /* Style */ FPF_ROMFONT, /* Preferences */ }; struct NewWindow NewWindow = { 0l, 0l, XSIZE, YSIZE, 0l, 1l, CLOSEWINDOW | MOUSEMOVE | MOUSEBUTTONS | MENUPICK | NEWSIZE | INACTIVEWINDOW | SIZEVERIFY, WINDOWCLOSE | SMART_REFRESH | ACTIVATE | WINDOWDRAG | WINDOWDEPTH | WINDOWSIZING | REPORTMOUSE, NULL, NULL, "Graph It II Beta 1.0 By Flynn D. Fishman", NULL, NULL, 100l, 35l, NULL, NULL, WBENCHSCREEN, }; openstuff() { struct Window *Window; IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", INTUITION_REV); if( IntuitionBase == NULL ) { printf("Error: opening Intuition\n"); exit(FALSE); } GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",GRAPHICS_REV); if( GfxBase == NULL ) { printf("Error: opening graphics\n"); exit(FALSE); } if(( Window = (struct Window *)OpenWindow(&NewWindow) ) == NULL) { printf("Error: opening window\n"); exit(FALSE); } InitMenu(); SetMenuStrip( Window, &menu[0] ); return(Window); } closestuff(Window) struct Window *Window; { if (Window != NULL) { CloseWindow( Window ); ClearMenuStrip( Window ); } } DrawWindow(Window, parameters) struct Window *Window; struct Graph_Parameters *parameters; { long int minx, miny, maxx, maxy; long int y; char line[80]; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight - XBORDER; maxy = Window->Height - Window->BorderBottom - YBORDER; ClearWindow(Window); /* Place the data */ SetAPen(Window->RPort, 3); Move(Window->RPort, minx, maxy + 9); sprintf(line,"%2.2f", parameters->xstart); Text(Window->RPort, line, strlen(line) ); Move(Window->RPort, maxx - XEXPANDX + 3, maxy + 9); sprintf(line,"%2.2f", parameters->xend); Text(Window->RPort, line, strlen(line) ); Move(Window->RPort, maxx, maxy - YEXPANDY + 9); sprintf(line,"%3.0f", parameters->ystart); Text(Window->RPort, line, strlen(line) ); Move(Window->RPort, maxx, miny + 9); sprintf(line,"%3.0f", parameters->yend); Text(Window->RPort, line, strlen(line) ); /* Draw the Graph rectangle area */ DrawRectangle(Window, 1, minx, miny, maxx, maxy); /* Draw the X-Expand area */ DrawRectangle(Window, 3, minx + XEXPANDX, maxy + 2, maxx - XEXPANDX, maxy + XEXPANDY); /* Draw the Y-Expand area */ DrawRectangle(Window, 3, maxx + 2, miny + YEXPANDY, maxx + YEXPANDX, maxy - YEXPANDY); /* Draw the Icon Area */ for (y = miny + YEXPANDY; y < maxy - YEXPANDX - ICONY; y += ICONY + 2) DrawRectangle(Window, 3, maxx + YEXPANDX + 2, y, maxx + XBORDER - 1, y + ICONY); DrawImages(Window, maxx + YEXPANDX + 2, miny + YEXPANDY); DrawButton(Window, minx + XEXPANDX + 1, maxy + 3); DrawButton(Window, maxx + 3, maxy - YEXPANDY - BALLHEIGHT - 1 ); } ClearWindow(Window) struct Window *Window; { long int minx, miny, maxx, maxy; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight; maxy = Window->Height - Window->BorderBottom; SetAPen(Window->RPort, 0l); SetOPen(Window->RPort, 0l); SetDrMd(Window->RPort, JAM2); RectFill(Window->RPort, minx, miny, maxx, maxy); } DrawRectangle(Window, colour, x1, y1, x2, y2) struct Window *Window; long int colour, x1, y1, x2, y2; { long int minx, miny, maxx, maxy; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight; maxy = Window->Height - Window->BorderBottom; x1 = (x1 < minx) ? minx : x1; y1 = (y1 < miny) ? miny : y1; x2 = (x2 > maxx) ? maxx : x2; y2 = (y2 > maxy) ? maxy : y2; if (x1 > x2) return(FALSE); if (y1 > y2) return(FALSE); SetDrMd(Window->RPort, JAM2); SetOPen(Window->RPort, colour); SetAPen(Window->RPort, 0l); RectFill(Window->RPort, x1, y1, x2, y2); } DrawImages(Window, x, y) struct Window *Window; long int x, y; { USHORT *icon; int loop; Icons.Height = ICONY; Icons.Width = ICONX; Icons.PlanePick = 0x3; Icons.PlaneOnOff = 0x0; icon = AllocMem( (long) sizeof(USHORT) * ICONY * Icons.Depth + 10, MEMF_CHIP); Icons.ImageData = icon; for (loop=0; loop < ICONY * Icons.Depth; loop++) icon[loop] = Zoom_Icon[loop]; DrawImage(Window->RPort, &Icons, x, y); y += ICONY + 2; for (loop=0; loop < ICONY * Icons.Depth; loop++) icon[loop] = Move_Icon[loop]; DrawImage(Window->RPort, &Icons, x, y); y += ICONY + 2; for (loop=0; loopRPort, &Icons, x, y); y += ICONY + 2; for (loop=0; loop< ICONY * Icons.Depth; loop++) icon[loop] = Hidden_Icon[loop]; DrawImage(Window->RPort, &Icons, x, y); y += ICONY + 2; /* future location of home icon */ for (loop=0; loop < ICONY * Icons.Depth; loop++) icon[loop] = Home_Icon[loop]; DrawImage(Window->RPort, &Icons, x, y); y += ICONY + 2; /* future location of quit icon */ for (loop=0; loop < ICONY * Icons.Depth; loop++) icon[loop] = Quit_Icon[loop]; DrawImage(Window->RPort, &Icons, x, y); y += ICONY + 2; for (loop=0; loop < ICONY * Icons.Depth; loop++) icon[loop] = Help_Icon[loop]; DrawImage(Window->RPort, &Icons, x, y); FreeMem( icon, (long) sizeof(USHORT) * ICONY * Icons.Depth); } InvertIcon(Window, icon) struct Window *Window; long int icon; { long int x, y; long int minx, miny, maxx, maxy; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight - XBORDER; maxy = Window->Height - Window->BorderBottom - YBORDER; SetDrMd(Window->RPort, COMPLEMENT); SetAPen(Window->RPort, 3l); SetOPen(Window->RPort, 0l); y = miny + YEXPANDY + (icon - 1) * (ICONY + 2); RectFill(Window->RPort, maxx + YEXPANDX +2, y, maxx +XBORDER -1, y +ICONY); } DrawButton(Window, x, y) struct Window *Window; long int x,y; { long int minx, miny, maxx, maxy; SetDrMd(Window->RPort, JAM2); SetAPen(Window->RPort, 2l); SetOPen(Window->RPort, 2l); RectFill(Window->RPort, x, y, x + BALLWIDTH - 1, y + BALLHEIGHT - 1); } EraseButton(Window, x, y) struct Window *Window; long int x,y; { long int minx, miny, maxx, maxy; SetDrMd(Window->RPort, JAM2); SetAPen(Window->RPort, 0l); SetOPen(Window->RPort, 0l); RectFill(Window->RPort, x, y, x + BALLWIDTH - 1, y + BALLHEIGHT - 1); } swap (x1, x2) long int *x1, *x2; { long int tmp; tmp = *x1; *x1 = *x2; *x2 = tmp; } Line(Window, colour, x1, y1, x2, y2) struct Window *Window; long int colour, x1, y1, x2, y2; { long int minx, miny, maxx, maxy; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight - XBORDER; maxy = Window->Height - Window->BorderBottom - YBORDER; /* Clip the edges of the graph */ /* start with the horizontal borders */ if (y1 >= miny && y1 <= maxy && y2 >= miny && y2 <= maxy);/* Do nothing */ else if ( (y1 - miny) * (y2 - miny) < 0 ) { /* Clip against top edge */ if (y2 > miny) { swap(&x1, &x2); swap(&y1, &y2); } x2 = (miny - y1)*(x1-x2)/(y1-y2)+x1; y2 = miny; } else if ( (y1 - maxy) * (y2 - maxy) < 0 ) { /* Clip against bottom edge */ if (y2 < maxy) { swap(&x1, &x2); swap(&y1, &y2); } x2 = (maxy - y1)*(x1-x2)/(y1-y2)+x1; y2 = maxy; } else return(); /* and know the vertical edges */ if (x1 >= minx && x1 <= maxx && x2 >= minx && x2 <= maxx);/* Do nothing */ else if ( (x1 - minx) * (x2 - minx) < 0 ) { /* Clip against top edge */ if (x2 > minx) { swap(&x1, &x2); swap(&y1, &y2); } y2 = (minx - x1)*(y1-y2)/(x1-x2)+y1; x2 = minx; } else if ( (x1 - maxx) * (x2 - maxx) < 0 ) { /* Clip against bottom edge */ if (x2 < maxx) { swap(&x1, &x2); swap(&y1, &y2); } y2 = (maxx - x1)*(y1-y2)/(x1-x2)+y1; x2 = maxx; } else return(); SetAPen(Window->RPort, colour); Move(Window->RPort, x1, y1); Draw(Window->RPort, x2, y2); } CrossHair(Window, x, y) struct Window *Window; long int x, y; { long int minx, miny, maxx, maxy; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight - XBORDER; maxy = Window->Height - Window->BorderBottom - YBORDER; SetDrMd(Window->RPort, COMPLEMENT); Line(Window, 3l, x, miny, x, maxy); Line(Window, 3l, minx, y, maxx, y); SetDrMd(Window->RPort, JAM2); } Box(Window, deltax, deltay) struct Window *Window; long int deltax, deltay; { long int minx, miny, maxx, maxy; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight - XBORDER; maxy = Window->Height - Window->BorderBottom - YBORDER; SetDrMd(Window->RPort, COMPLEMENT); if (deltax > 0) Line(Window, 3l, minx + deltax, miny, minx + deltax, maxy); if (deltax < 0) Line(Window, 3l, maxx + deltax, miny, maxx + deltax, maxy); if (deltay > 0) Line(Window, 3l, minx, miny + deltay, maxx, miny + deltay); if (deltay < 0) Line(Window, 3l, minx, maxy + deltay, maxx, maxy + deltay); SetDrMd(Window->RPort, JAM2); } DrawGrid(Window) struct Window *Window; { long int minx, miny, maxx, maxy; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight - XBORDER; maxy = Window->Height - Window->BorderBottom - YBORDER; SetDrMd(Window->RPort, COMPLEMENT); Line(Window, 1l, maxx / 3, miny, maxx / 3, maxy); Line(Window, 1l, maxx * 2/ 3, miny, maxx * 2 / 3, maxy); Line(Window, 1l, minx, maxy / 3, maxx, maxy / 3); Line(Window, 1l, minx, maxy * 2 / 3, maxx, maxy * 2 / 3); SetDrMd(Window->RPort, JAM2); } Real(Window, parameters, x1, y1, x2, y2) struct Window *Window; struct Graph_Parameters *parameters; long int x1, y1, x2, y2; { long int minx, miny, maxx, maxy; double xsize, ysize, xstart, ystart, xend, yend, xconversion, yconversion; if (x1 == x2 || y1 == y2) return(); minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight - XBORDER; maxy = Window->Height - Window->BorderBottom - YBORDER; x1 = x1 - minx; x2 = x2 - minx; y1 = y1 - miny; y2 = y2 - miny; xsize = parameters->xend - parameters->xstart; ysize = parameters->yend - parameters->ystart; xstart = parameters->xstart; ystart = parameters->ystart; xend = parameters->xend; yend = parameters->yend; xconversion = xsize /(maxx - minx); yconversion = ysize /(maxy - miny); if (x1 > x2) swap (&x1, &x2); if (y1 > y2) swap (&y1, &y2); parameters->xstart = xstart + xconversion * x1; parameters->ystart = ystart + yconversion * y1; parameters->xend = xstart + xconversion * x2; parameters->yend = ystart + yconversion * y2; DrawWindow(Window, parameters); } Shift(Window, parameters, dx, dy) struct Window *Window; struct Graph_Parameters *parameters; long int dx, dy; { long int minx, miny, maxx, maxy; double xsize, ysize, xstart, ystart, xend, yend, xconversion, yconversion; double deltax, deltay; deltax = dx; deltay = dy; minx = Window->BorderLeft; miny = Window->BorderTop; maxx = Window->Width - Window->BorderRight - XBORDER; maxy = Window->Height - Window->BorderBottom - YBORDER; xsize = parameters->xend - parameters->xstart; ysize = parameters->yend - parameters->ystart; xstart = parameters->xstart; ystart = parameters->ystart; xend = parameters->xend; yend = parameters->yend; xconversion = xsize /(maxx - minx); yconversion = ysize /(maxy - miny); parameters->xstart = xstart + xconversion * deltax; parameters->ystart = ystart + yconversion * deltay; parameters->xend = xend + xconversion * deltax; parameters->yend = yend + yconversion * deltay; } /* define maximum number of menu items */ #define MENU0MAX 4 #define MENU1MAX 7 struct MenuItem ItemText0[MENU0MAX]; struct MenuItem ItemText1[MENU1MAX]; struct IntuiText MenuItem0[MENU0MAX]; struct IntuiText MenuItem1[MENU1MAX]; InitMenu() { InitMenu0(); InitMenu1(); menu[0].NextMenu = &menu[1]; menu[0].LeftEdge = 10; menu[0].TopEdge = 0; menu[0].Width = 80; menu[0].Height = 10; menu[0].Flags = MENUENABLED; menu[0].MenuName = "Project"; /* text for menu-bar display */ menu[0].FirstItem = &ItemText0[0]; /* pointer to first item in list */ menu[1].NextMenu = NULL; menu[1].LeftEdge = 100; menu[1].TopEdge = 0; menu[1].Width = 100; menu[1].Height = 10; menu[1].Flags = MENUENABLED; menu[1].MenuName = "Functions"; /* text for menu-bar display */ menu[1].FirstItem = &ItemText1[0]; /* pointer to first item in list */ } InitMenu0() { short n; /* initialize each meu item and IntuiText with loop */ for( n=0; n