open-axiom repository from github
/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2010, Gabriel Dos Reis.4All rights reserved.56Redistribution and use in source and binary forms, with or without7modification, are permitted provided that the following conditions are8met:910- Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.1213- Redistributions in binary form must reproduce the above copyright14notice, this list of conditions and the following disclaimer in15the documentation and/or other materials provided with the16distribution.1718- Neither the name of The Numerical Algorithms Group Ltd. nor the19names of its contributors may be used to endorse or promote products20derived from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS23IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED24TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A25PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER26OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435#define _VIEWPORT2D_C3637#include <stdio.h>38#include <stdlib.h>39#include <string.h>40#include <unistd.h>41#include <math.h>42#include <X11/X.h>43#include <X11/Xlib.h>44#include <X11/Xutil.h>45#include <limits.h>4647#include "open-axiom.h"4849#define NotPoint (SHRT_MAX)50#define eqNANQ(x) (x == NotPoint)5152#include "header2.h"5354#include "all_2d.H1"55#include "Gfun.H1"56#include "util.H1"57#include "XSpadFill.h"5859#include "spadBitmap.bitmap"60#include "spadMask.mask"6162using namespace OpenAxiom;6364#define rint(z) ((int)(z))6566Atom wm_delete_window;676869/***************************70*** void writeTitle() ***71***************************/7273void74writeTitle(void)75{7677int strlength;78XWindowAttributes attribInfo;7980XGetWindowAttributes(dsply,viewport->titleWindow,&attribInfo);81if (mono) GSetForeground(anotherGC,(float)foregroundColor,Xoption);82else GSetForeground(anotherGC,(float)titleColor,Xoption);83XClearWindow(dsply,viewport->titleWindow); /* it's behind the viewWindow */84strlength = strlen(viewport->title);85GDrawImageString(anotherGC,viewport->titleWindow,86centerX(anotherGC,viewport->title,strlength,attribInfo.width),8715,viewport->title,strlength,Xoption);8889}909192/********************************/93/*** void drawTheViewport() ***/94/********************************/9596void97drawTheViewport(int dFlag)98{99100Window vw;101XWindowAttributes vwInfo;102pointListStruct *aList;103pointStruct *aPoint;104XPoint *anXPoint,*tempXpt;105XArc *anXarc;106Vertex *anX10Point;107float jj,diffX, diffY, tickStart,oneTickUnit;108int i,j,k,ii,halfSize;109int charlength,strlength,halflength;110int ptX,ptY,ptX1,ptY1,clipped, clipped1;111int xAxis,yAxis,dummyInt, ascent, descent;112int unitWidth,boxX,boxY,boxW,boxH;113char aunit[20];114XCharStruct overall;115116drawMore = yes;117vw = viewport->viewWindow;118XGetWindowAttributes(dsply,vw,&vwInfo);119aspectR = (float)vwInfo.width/(float)vwInfo.height;120121XTextExtents(unitFont,"o",1,&dummyInt,&ascent,&descent,&overall);122123/* Calculate various factors for use in projection. */124/* Scale the plot, so that the scaling between the axes remains125constant and fits within the smaller of the two dimensions. */126127charlength = overall.width;128129if (dFlag==Xoption) XClearWindow(dsply,vw);130131for (i=0; i<maxGraphs; i++) {132133if ((graphArray[i].key) && (graphStateArray[i].showing)) {134135/* Scale y coordinate dimensions relative to viewport aspect ratio. */136137graphArray[i].yNorm = 1.0/((graphArray[i].ymax-graphArray[i].ymin) *138aspectR);139graphArray[i].originY = -graphArray[i].ymin*graphArray[i].yNorm140- 0.5/aspectR;141graphArray[i].unitY = graphArray[i].spadUnitY*graphArray[i].yNorm;142143xAxis = rint(vwInfo.width *144((graphArray[0].originX - graphStateArray[0].centerX) *145graphStateArray[0].scaleX + 0.5));146yAxis= rint(vwInfo.height * aspectR *147(1 - ((graphArray[0].originY*aspectR -148graphStateArray[0].centerY) *149graphStateArray[0].scaleY + 0.5*aspectR )));150151if (graphStateArray[i].axesOn) {152if (dFlag==Xoption) /* do only for X, ps uses default of black */153GSetForeground(globalGC1,154(float)monoColor(graphStateArray[i].axesColor),155dFlag);156157if ((yAxis >=0) && (yAxis <= vwInfo.height))158GDrawLine(globalGC1,vw,1590,yAxis,160vwInfo.width,yAxis,161dFlag);162if ((xAxis >=0) && (xAxis <= vwInfo.width))163GDrawLine(globalGC1,vw,164xAxis,0,165xAxis,vwInfo.height,166dFlag);167}168169170tempXpt = anXPoint = xPointsArray[i].xPoint;171anX10Point = xPointsArray[i].x10Point;172anXarc = xPointsArray[i].arc;173174for (j=0,aList=graphArray[i].listOfListsOfPoints;175(j<graphArray[i].numberOfLists);176j++, aList++) {177178for (k=0,aPoint=aList->listOfPoints;179(k<aList->numberOfPoints);180k++,aPoint++) {181182if (graphStateArray[i].scaleX > 99.0)183graphStateArray[i].scaleX = 99.0;184if (graphStateArray[i].scaleY > 99.0)185graphStateArray[0].scaleY = 99.0;186if (i > 0) {187if (isNaN(aPoint->x)) {188anXPoint->x = anX10Point->x = NotPoint;189}190else {191diffX = graphArray[i].xmax-graphArray[i].xmin;192anXPoint->x = anX10Point->x = vwInfo.width *193((aPoint->x * diffX/(graphArray[0].xmax-graphArray[0].xmin)194+ (graphArray[0].originX - graphArray[i].originX*diffX /195(graphArray[0].xmax-graphArray[0].xmin))196- graphStateArray[0].centerX)*graphStateArray[i].scaleX+0.5);197}198if (isNaN(aPoint->y)) {199anXPoint->y = anX10Point->y = NotPoint;200}201else {202diffY = graphArray[i].ymax-graphArray[i].ymin;203anXPoint->y = anX10Point->y = vwInfo.height * aspectR *204(1 - ((aPoint->y * diffY/(graphArray[0].ymax-graphArray[0].ymin)205+ (graphArray[0].originY - graphArray[i].originY* diffY/206(graphArray[0].ymax-graphArray[0].ymin))*aspectR207- graphStateArray[0].centerY) *208graphStateArray[i].scaleY + 0.5*aspectR));209}210} else {211if (isNaN(aPoint->x)) {212anXPoint->x = anX10Point->x = NotPoint;213}214else {215anXPoint->x = anX10Point->x = vwInfo.width *216((aPoint->x - graphStateArray[i].centerX) *217graphStateArray[i].scaleX + 0.5);218}219if (isNaN(aPoint->y)) {220anXPoint->y = anX10Point->y = NotPoint;221}222else {223anXPoint->y = anX10Point->y = vwInfo.height * aspectR *224(1 - ((aPoint->y - graphStateArray[i].centerY) *225graphStateArray[i].scaleY + 0.5*aspectR));226}227}228229/* first or last point */230if (k == 0 || k == (aList->numberOfPoints - 1)) {231anX10Point->flags = 0;232} else {233anX10Point->flags = VertexCurved;234}235236anXPoint++;237anX10Point++;238anXarc++;239} /* for aPoint in pointList */240241aPoint--; /* make it legal, the last one*/242if (graphStateArray[i].connectOn || graphStateArray[i].pointsOn) {243halfSize = aList->pointSize/2;244ptX = tempXpt->x;245ptY = tempXpt->y;246clipped = ptX > vwInfo.x && ptX < vwInfo.width &&247ptY > 0 && ptY < vwInfo.height;248if (graphStateArray[i].pointsOn) {249if (dFlag==Xoption) {250if (mono) {251GSetForeground(globalGC1,252(float)monoColor((int)(aPoint->hue)),253dFlag);254} else {255GSetForeground(globalGC1,256(float)XSolidColor((int)(aPoint->hue),257(int)(aPoint->shade)),258dFlag);259}260}261if (clipped && !eqNANQ(ptX) && !eqNANQ(ptY))262GFillArc(globalGC1,vw,ptX-halfSize,263ptY-halfSize,aList->pointSize,aList->pointSize,2640,360*64, dFlag);265266} /* if points on */267for (ii=0, aPoint=aList->listOfPoints;268ii<aList->numberOfPoints;269++ii, ++tempXpt, ++aPoint) {270ptX1 = tempXpt->x;271ptY1 = tempXpt->y;272clipped1 = ptX1 > vwInfo.x && ptX1 < vwInfo.width &&273ptY1 > 0 && ptY1 < vwInfo.height;274if (graphStateArray[i].connectOn) {275if (dFlag==Xoption) {276if (mono) {277GSetForeground(globalGC1,278(float)monoColor((int)(aList->lineColor-1)/5),279dFlag);280} else {281GSetForeground(globalGC1,282(float)XSolidColor((int)(aList->lineColor-1)/5,283(int)((aList->lineColor-1)%5)/2),284dFlag);285}286} /* if X */287if ((clipped || clipped1) && !eqNANQ(ptX) && !eqNANQ(ptY) &&288!eqNANQ(ptX1) && !eqNANQ(ptY1))289GDrawLine(globalGC1,vw,290ptX,ptY,ptX1,ptY1,291dFlag);292} /* if lines on */293if (graphStateArray[i].pointsOn) {294if (dFlag==Xoption) {295if (mono) {296GSetForeground(globalGC1,297(float)monoColor((int)(aPoint->hue)),298dFlag);299} else {300GSetForeground(globalGC1,301(float)XSolidColor((int)(aPoint->hue),302(int)(aPoint->shade)),303dFlag);304}305}306if (clipped1 && !eqNANQ(ptX1) && !eqNANQ(ptY1))307GFillArc(globalGC1,vw,ptX1-halfSize,308ptY1-halfSize,aList->pointSize,aList->pointSize,3090,360*64, dFlag);310} /* if points on */311ptX = ptX1; ptY = ptY1; clipped = clipped1;312} /* for all points */313} /* if points or lines on */314315if (graphStateArray[i].splineOn) { /* need spline color as well */316if (dFlag==Xoption) /* do only for X, ps uses default of black */317GSetForeground(globalGC1,318(float)monoColor(148),319dFlag);320boxX = vwInfo.width *321((-0.5 - graphStateArray[i].centerX)*322graphStateArray[i].scaleX + 0.5);323boxY = vwInfo.height * aspectR *324(1 - ((0.5 - graphStateArray[i].centerY)*325graphStateArray[i].scaleY + 0.5*aspectR));326327boxW = graphStateArray[i].scaleX * vwInfo.width + 1;328boxH = graphStateArray[i].scaleY * vwInfo.height * aspectR + 1;329330GDrawRectangle(globalGC1,vw,331boxX,boxY,boxW,boxH,332dFlag);333}334335tempXpt = anXPoint;336} /* for a aList in listofListsOfPoints */337if (graphStateArray[i].unitsOn) {338/* do only for X, ps uses default of black */339if (dFlag==Xoption)340GSetForeground(unitGC,341(float)monoColor(graphStateArray[i].unitsColor),342dFlag);343344345tickStart = calcUnitX(0);346oneTickUnit = calcUnitX(1) - tickStart;347348/* ticks along the positive X axis */349350unitWidth = 5*overall.width; /* limit on acceptable separation : 5 chars */351k = floor(unitWidth/oneTickUnit) +1; /* get skipping integer */352for (ii=0, jj = tickStart;353jj < vwInfo.width;354ii=ii+k,jj =jj+k* oneTickUnit) {355if (jj >= 0) {356357/* ticks stuck to viewport*/358GDrawLine(unitGC,vw,359(int)rint(jj),vwInfo.height-8,(int)rint(jj),vwInfo.height-4,360dFlag);361362sprintf(aunit,"%0.3g",ii*graphArray[0].spadUnitX);363strlength=strlen(aunit);364halflength=XTextWidth(unitFont,aunit,strlength)/2;365366if (dFlag == Xoption) GDrawImageString(unitGC,367vw,368(int)rint(jj) - halflength,369vwInfo.height -8 -descent,370aunit,371strlength,372dFlag);373if (dFlag == PSoption) GDrawImageString(unitGC,374vw,375(int)rint(jj) -(strlength*3) ,376vwInfo.height -14,377aunit,378strlength,379dFlag);380/* these are "eyeball" parameters for the given PS font */381382}383384}385/* ticks along the negative X axis */386for (ii=-k,jj=tickStart - k*oneTickUnit;387jj > 0;388ii=ii-k,jj = jj-k*oneTickUnit) {389if (jj <= vwInfo.width) {390391/* ticks stuck to viewport*/392GDrawLine(unitGC,vw,393(int)rint(jj),vwInfo.height-8,(int)rint(jj),vwInfo.height-4,394dFlag);395396sprintf(aunit,"%0.3g",ii*graphArray[0].spadUnitX);397strlength=strlen(aunit);398halflength=XTextWidth(unitFont,aunit,strlength)/2;399400if (dFlag == Xoption) GDrawImageString(unitGC,401vw,402(int)rint(jj) - halflength,403vwInfo.height -8 -descent,404aunit,405strlength,406dFlag);407if (dFlag == PSoption) GDrawImageString(unitGC,408vw,409(int)rint(jj) -(strlength*3) ,410vwInfo.height -14,411aunit,412strlength,413dFlag);414/* these are "eyeball" parameters for the given PS font */415}416}417418tickStart = calcUnitY(0);419oneTickUnit = calcUnitY(1) - tickStart;420421/* ticks along the positive Y axis */422unitWidth = 2*(ascent+descent); /* limit of acceptable separation */423k = floor(unitWidth/fabs(oneTickUnit)) +1; /* get skipping integer */424for (ii=0,jj = tickStart;425jj > 0;426ii=ii+k,jj =jj+k*oneTickUnit ) {427if (jj < vwInfo.height) {428429/* ticks stuck to viewport*/430/* on the right */431/*432GDrawLine(unitGC,vw,433vwInfo.width-6,(int)rint(jj),434vwInfo.width-2,(int)rint(jj),dFlag);435*/436/* on the left */437GDrawLine(unitGC,vw,4382,(int)rint(jj),4396,(int)rint(jj),440dFlag);441sprintf(aunit,"%0.3g",ii*graphArray[0].spadUnitY);442strlength=strlen(aunit);443XTextExtents(unitFont,aunit,strlength,&dummyInt,444&ascent,&descent,&overall);445halflength=overall.width; /* let's reuse that variable */446447if(dFlag == Xoption){448/* on the right */449/*450GDrawImageString(unitGC, vw,451vwInfo.width-halflength -6-descent,452(int)rint(jj)+ascent/2 ,453aunit, strlength, dFlag);454*/455/* on the left */456GDrawImageString(unitGC, vw,4578 + charlength/2,458(int)rint(jj)+ascent/2 ,459aunit, strlength, dFlag);460}461if(dFlag == PSoption){462/* on the right */463/*464GDrawImageString(unitGC, vw,465vwInfo.width - 6 - (strlength*6),466(int)rint(jj)+4,467aunit, strlength, dFlag);468*/469/* on the left */470GDrawImageString(unitGC, vw,4718,(int)rint(jj)+4,472aunit, strlength, dFlag);473/* these are "eyeball" parameters for the given PS font */474}475}476}477478/* ticks along the negative Y axis */479480for (ii=(-k),jj = tickStart - k*oneTickUnit;481jj < vwInfo.height;482ii=ii-k,jj =jj-k*oneTickUnit) {483if (jj > 0) {484485/* ticks stuck to viewport*/486/* on the right */487/*488GDrawLine(unitGC,vw,489vwInfo.width-6,(int)rint(jj),490vwInfo.width-2,(int)rint(jj),491dFlag);492*/493/* on the left */494GDrawLine(unitGC,vw,4952,(int)rint(jj),4966,(int)rint(jj),497dFlag);498499sprintf(aunit,"%0.3g",ii*graphArray[0].spadUnitY);500strlength=strlen(aunit);501XTextExtents(unitFont,aunit,strlength,&dummyInt,502&ascent,&descent,&overall);503halflength=overall.width; /* let's reuse that variable */504505if(dFlag == Xoption){506/* on the right */507/*508GDrawImageString(unitGC, vw,509vwInfo.width-halflength -6-descent,510(int)rint(jj)+ascent/2 ,511aunit, strlength, dFlag);512*/513/* on the left */514GDrawImageString(unitGC, vw,5158 + charlength/2,516(int)rint(jj)+ascent/2 ,517aunit, strlength, dFlag);518}519if(dFlag == PSoption){520/* on the right */521/*522GDrawImageString(unitGC, vw,523vwInfo.width -6 -(strlength*6),524(int)rint(jj)+4 ,525aunit, strlength, dFlag);526*/527/* on the left */528GDrawImageString(unitGC, vw,5298,530(int)rint(jj)+4 ,531aunit, strlength, dFlag);532/* these are "eyeball" parameters for the given PS font */533}534}535}536537} /* if unitsOn */538} /* if graph i exists and is showing */539} /* for i in graphs */540541542if (dFlag==Xoption) {543if (!followMouse) {544/* no need to do this while autorepeating */545makeMessageFromData(queriedGraph);546writeControlMessage();547}548XFlush(dsply);549}550551} /* drawViewport() */552553554555/************************************556*** viewPoints *makeViewport() ***557************************************/558559viewPoints *560makeViewport(const char* title,int vX,int vY,int vW,int vH,int showCP)561{562Pixmap spadbits,spadmask;563XSetWindowAttributes viewAttrib;564XSizeHints titleSizeHints,viewSizeHints;565Window viewTitleWindow,viewGraphWindow;566XColor foreColor, backColor;567568#ifdef DEBUG569fprintf(stderr,"view2D: About to make a viewport\n");570#endif571572/* Create a viewport */573if (!(viewport = (viewPoints *)malloc(sizeof(viewPoints)))) {574fprintf(stderr,"Ran out of memory (malloc) trying to create a viewport.\n");575openaxiom_sleep(5);576exitWithAck(RootWindow(dsply,scrn),Window,-1);577}578579#ifdef DEBUG580fprintf(stderr,"view2D: Made a viewport\n");581#endif582583strcpy(viewport->title,title);584585viewport->closing = no;586viewport->allowDraw = yes; /* just draw axes the first time around */587viewport->axesOn = axesON;588viewport->unitsOn = unitsON;589viewport->pointsOn = pointsON;590viewport->linesOn = connectON;591viewport->splineOn = splineON;592593/**** Make the windows for the viewport ****/594spadbits = XCreateBitmapFromData(dsply,rtWindow,595(const char*) spadBitmap_bits,596spadBitmap_width,spadBitmap_height);597spadmask = XCreateBitmapFromData(dsply,rtWindow,598(const char*) spadMask_bits,599spadMask_width,spadMask_height);600viewAttrib.background_pixel = backgroundColor;601viewAttrib.border_pixel = foregroundColor;602viewAttrib.override_redirect = overrideManager;603viewAttrib.colormap = colorMap;604605foreColor.pixel = foregroundColor;606backColor.pixel = backgroundColor;607XQueryColor(dsply,colorMap,&foreColor);608XQueryColor(dsply,colorMap,&backColor);609viewAttrib.cursor = XCreatePixmapCursor(dsply,spadbits,spadmask,610&foreColor,&backColor,spadBitmap_x_hot,spadBitmap_y_hot);611612viewAttrib.event_mask = titleMASK;613if (vW) {614titleSizeHints.flags = PPosition | PSize;615titleSizeHints.x = vX;616titleSizeHints.y = vY;617titleSizeHints.width = vW;618titleSizeHints.height = vH;619} else {620titleSizeHints.flags = PSize;621titleSizeHints.width = viewWidth;622titleSizeHints.height = viewHeight;623}624625viewTitleWindow = XCreateWindow(dsply,rtWindow,vX,vY,vW,vH,626viewBorderWidth,627CopyFromParent,InputOutput,CopyFromParent,628viewportTitleCreateMASK,&viewAttrib);629630wm_delete_window = XInternAtom(dsply, "WM_DELETE_WINDOW", False);631(void) XSetWMProtocols(dsply, viewTitleWindow, &wm_delete_window, 1);632633XSetNormalHints(dsply,viewTitleWindow,&titleSizeHints);634XSetStandardProperties(dsply,viewTitleWindow,"OpenAxiom 2D",viewport->title,635None,NULL,0,&titleSizeHints);636637viewport->titleWindow = viewTitleWindow;638viewAttrib.event_mask = viewportMASK;639viewSizeHints.flags = PPosition | PSize;640viewSizeHints.x = -viewBorderWidth;641viewSizeHints.y = titleHeight;642viewSizeHints.width = titleSizeHints.width;643viewSizeHints.height = titleSizeHints.height -644(titleHeight + appendixHeight);645viewGraphWindow = XCreateWindow(dsply,viewTitleWindow,646viewSizeHints.x,viewSizeHints.y,647viewSizeHints.width,viewSizeHints.height,648viewBorderWidth,649CopyFromParent,InputOutput,CopyFromParent,650viewportCreateMASK,&viewAttrib);651XSetNormalHints(dsply,viewGraphWindow,&viewSizeHints);652XSetStandardProperties(dsply,viewGraphWindow,"2D Viewport","2D Viewport",653None,NULL,0,&viewSizeHints);654655viewport->viewWindow = viewGraphWindow;656657/*Make the control panel for the viewport. */658viewport->controlPanel = makeControlPanel();659if ((viewport->haveControl = showCP)) putControlPanelSomewhere(anywhere);660661XSync(dsply,False);662return(viewport);663664}665666667/*********************************************668***** viewPoints *makeView2D(viewdata) ****669*********************************************/670671672viewPoints *673makeView2D(view2DStruct *viewdata)674{675viewPoints *vPoints;676677vPoints = makeViewport(viewdata->title, viewdata->vX,viewdata->vY,678viewdata->vW,viewdata->vH,viewdata->showCP);679680vPoints->allowDraw = yes; /* draw everything from now on */681682if (viewdata->showCP) clearControlMessage();683684writeTitle();685686XMapWindow(dsply,vPoints->viewWindow);687XMapWindow(dsply,vPoints->titleWindow);688XSync(dsply,0);689690drawViewport(Xoption); /* draw viewport with X routines (as opposed to PS) */691return(vPoints);692693} /* makeView2D */694695696697