Version bumped to 1.0.0 for the initial release.
[wllib.git] / wllib-Amiga.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <unistd.h>
6 #include <ctype.h>
7 #include <limits.h>
8 #include <assert.h>
9 #include <intuition/intuition.h>
10 #include <intuition/intuitionbase.h>
11 #include <exec/ports.h>
12 #include <proto/exec.h>
13 #include <proto/intuition.h>
14 #include <proto/graphics.h>
15 #include "wllib.h"
16
17 #define SAFE_BORDER (4)
18 #define SIZE_MIN (16)
19
20 extern void drawgfx(void);
21
22 long xcur,ycur,xmin,xmax,ymin,ymax;
23 int winx,winy,iniline,minimax=1;
24 float coefx,coefy;
25 int xofs,yofs,xbor,ybor;
26
27 struct RastPort *rport;
28 struct Window *win;
29 char *pname;
30
31 struct NewWindow nwin={0,0,0,0,UCHAR_MAX,UCHAR_MAX,
32 IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_VANILLAKEY,
33 WFLG_SIZEGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SIZEBRIGHT
34 |WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_RMBTRAP|WFLG_NOCAREREFRESH,
35 NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN};
36 #define pubscr (nwin.Screen)
37
38 #define gfxfail() { fprintf(stderr,"%s: Gfx subsys failed!\n",pname); \
39 exit(EXIT_FAILURE); } while (0)
40
41 void wl_line(long x,long y)
42 {
43         if (iniline) {
44                 iniline=0;
45                 if (minimax) { xmin=xmax=x; ymin=ymax=y; }
46                 else Move(rport,(long)(coefx*(x-xmin)+xofs),
47                                 (long)(yofs-coefy*(y-ymin)));
48                 }
49         else if (minimax) {
50                 if (x<xmin) xmin=x; if (x>xmax) xmax=x;
51                 if (y<ymin) ymin=y; if (y>ymax) ymax=y;
52                 }
53         else Draw(rport,(long)(coefx*(x-xmin)+xofs),
54                         (long)(yofs-coefy*(y-ymin)));
55 }
56
57 static void wl_safeclose(void)
58 {
59 struct IntuiMessage *msg;
60         Forbid();
61         while (msg=(struct IntuiMessage *)GetMsg(win->UserPort))
62                 ReplyMsg((struct Message *)msg);
63         ModifyIDCMP(win,0);
64         Permit();
65         CloseWindow(win);
66         win=NULL;
67 }
68
69 void wl_done(void)
70 {
71 struct IntuiMessage *msg;
72 ULONG cls;
73         iniline=1; drawgfx();
74         minimax=0;
75         if (xmax==xmin) xmax++;
76         if (ymax==ymin) ymax++;
77         if (IntuitionBase->LibNode.lib_Version>=39) SetWindowPointer(win,TAG_DONE);
78         for (;;) {
79                 if (!iniline) {
80                         coefx=(float)(winx-xbor-1)/(xmax-xmin);
81                         coefy=(float)(winy-ybor-1)/(ymax-ymin);
82                         yofs=winy-SAFE_BORDER-win->BorderBottom;
83                         SetAPen(rport,0);
84                         RectFill(rport,(long)win->BorderLeft,(long)win->BorderTop,
85                                         (long)(winx-win->BorderRight-1),(long)(winy-win->BorderBottom-1));
86                         SetAPen(rport,1);
87                         iniline=1; drawgfx();
88                         }
89                 while (!(msg=(struct IntuiMessage *)GetMsg(win->UserPort)))
90                         WaitPort(win->UserPort);
91                 cls=msg->Class;
92                 ReplyMsg((struct Message *)msg);
93                 if (cls!=IDCMP_NEWSIZE) { wl_safeclose(); return; }
94                 winx=win->Width; winy=win->Height;
95                 }
96 }
97
98 static void wl_cleanup(void)
99 {
100         while (win) wl_safeclose();
101         if (pubscr) {
102                 UnlockPubScreen(NULL,pubscr);
103                 pubscr=NULL;
104                 }
105 }
106
107 void wl_init(int *argcp,char **argv,const char *class)
108 {
109         if ((pname=rindex(*argv,'/'))) pname++;
110         else pname=*argv;
111         atexit(wl_cleanup);
112         if (!(pubscr=LockPubScreen(NULL))) gfxfail();
113         nwin.Title=(char *)class;
114         nwin.LeftEdge=(winx=nwin.Width=pubscr->Width/2)/2;
115         nwin.TopEdge=(winy=nwin.Height=pubscr->Height/2)/2;
116         if (!(win=OpenWindow(&nwin))) gfxfail();
117         if (IntuitionBase->LibNode.lib_Version>=39)
118                 SetWindowPointer(win,WA_BusyPointer,TRUE,WA_PointerDelay,TRUE,TAG_DONE);
119         WindowLimits(win,
120                         (long)(xbor=(xofs=win->BorderLeft+SAFE_BORDER)
121                                         +SAFE_BORDER+win->BorderRight )+SIZE_MIN,
122                         (long)(ybor=win->BorderTop +SAFE_BORDER
123                                         +SAFE_BORDER+win->BorderBottom)+SIZE_MIN,~0,~0);
124         SetDrMd(rport=win->RPort,JAM1);
125 }