open-axiom repository from github
/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2011, 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 _CONTROL2D_C36#include "openaxiom-c-macros.h"3738#include <stdlib.h>39#include <unistd.h>40#include <stdio.h>41#include <string.h>42#include <X11/X.h>43#include <X11/Xlib.h>44#include <X11/Xutil.h>4546#include "cfuns.h"47#include "header2.h"48#include "buttons2d.H1"49#include "all_2d.H1"50#include "Gfun.H1"51#include "XSpadFill.h"52#include "util.H1"535455#include "mouse11.bitmap"56#include "mouse11.mask"5758/* Defines the pixmap for the arrow displayed in the scale window */5960#define scaleArrowN 1161static XPoint scaleArrow[scaleArrowN] = {62{55,14},{64,23},{59,23},63{66,45},{79,45},64{55,69},65{31,45},{44,45},66{51,23},{46,23},{55,14} };6768/* Defines the pixmap for the arrows displayed in the translate window */6970#define translateArrowN 2571static XPoint translateArrow[translateArrowN] = {72{55,2},{60,10},{58,10},{58,37},73{85,37},{85,35},{93,40},{85,45},{85,43},{58,43},74{58,70},{60,70},{55,78},{50,70},{52,70},{52,43},75{25,43}, {25,45}, {17,40}, {25,35}, {25,37}, {52,37},76{52,10},{50,10},{55,2} };7778/****************************79* void writeControlTitle() *80****************************/8182void83writeControlTitle(void)84{8586int strlength;8788s = viewport->title;89strlength = strlen(s);90XClearArea(dsply,control->controlWindow,0,0,controlWidth,potA,False);91GSetForeground(anotherGC,(float)controlTitleColor,Xoption);92GDrawImageString(anotherGC,control->controlWindow,93centerX(anotherGC,s,strlength,controlWidth),9415,s,strlength,Xoption);9596} /* writeControlTitle() */9798void99makeMessageFromData(int whichGraph)100{101if (viewport->haveControl) {102if ((graphStateArray[whichGraph].scaleX) > 99.0) {103strcpy(scaleXReport,"big");104} else {105sprintf(scaleXReport,"%4.1f",graphStateArray[whichGraph].scaleX);106}107if ((graphStateArray[whichGraph].scaleY) > 99.0) {108strcpy(scaleYReport,"big");109} else {110sprintf(scaleYReport,"%4.1f",graphStateArray[whichGraph].scaleY);111}112if ((graphStateArray[whichGraph].centerX) > 999.0) {113strcpy(deltaXReport,"+big");114} else if ((graphStateArray[whichGraph].centerX) < -999.0) {115strcpy(deltaXReport,"-big");116} else {117sprintf(deltaXReport,"%4.0f",118-graphStateArray[whichGraph].centerX /119graphArray[whichGraph].unitX);120}121if ((graphStateArray[whichGraph].centerY) > 999.0) {122strcpy(deltaYReport,"+big");123} else if ((graphStateArray[whichGraph].centerY) < -999.0) {124strcpy(deltaYReport,"-big");125} else {126sprintf(deltaYReport,"%4.0f",127-graphStateArray[whichGraph].centerY /128graphArray[whichGraph].unitY);129}130sprintf(viewport->controlPanel->message,"[%s,%s] >%d< [%s,%s]",131scaleXReport,scaleYReport,whichGraph+1,deltaXReport,deltaYReport);132} /* if haveControl */133134} /* makeMessageFromData() */135136137void138writeControlMessage(void)139{140int strlength;141controlPanelStruct *cp;142XWindowAttributes cwInfo;143144cp = viewport->controlPanel;145XGetWindowAttributes(dsply,cp->controlWindow,&cwInfo);146strlength = strlen(cp->message);147148149150GDrawImageString(controlMessageGC,cp->controlWindow,151centerX(globalGC1,cp->message,strlength,controlWidth),152controlMessageY + globalFont->max_bounds.ascent - 2,153cp->message,strlength,Xoption);154}155156/*********************************/157/*** void drawControlPanel() ***/158/*********************************/159160void161drawControlPanel(void)162{163164controlPanelStruct *cp;165int i,strlength;166const char* s;167168cp = viewport->controlPanel;169/* Draw border lines to separate the potentiometer, message, graph select170and button regions of the control panel. */171GSetForeground(trashGC,(float)foregroundColor,Xoption);172GSetLineAttributes(trashGC,3,LineSolid,CapButt,JoinMiter,Xoption);173GDrawLine(trashGC, cp->controlWindow, 0, potA, controlWidth, potA, Xoption);174GSetLineAttributes(trashGC,2,LineSolid,CapButt,JoinMiter,Xoption);175GDrawLine(trashGC, cp->controlWindow, 0, potB, controlWidth, potB, Xoption);176GDrawLine(trashGC, cp->controlWindow, 0, messageBot,177controlWidth, messageBot, Xoption);178GDrawLine(trashGC, cp->controlWindow, 0, 286, controlWidth, 286, Xoption);179180/** put the line width as 1 last because used below as well **/181GSetLineAttributes(trashGC,1,LineSolid,CapButt,JoinMiter,Xoption);182GDrawRectangle(trashGC,cp->controlWindow,closeLeft,closeTop,183(controlWidth-closeLeft+8),(controlHeight-closeTop+8),Xoption);184185/* Write potentiometer titles on the control panel. */186writeControlTitle();187GSetForeground(globGC,(float)controlPotHeaderColor,Xoption);188s = "Scale";189strlength = strlen(s);190GDrawString(globGC,cp->controlWindow,191centerX(globGC,s,strlength,192cp->buttonQueue[scale2D].buttonWidth) +193cp->buttonQueue[scale2D].buttonX, 31+headerHeight,s,strlength,Xoption);194195s = "Translate";196strlength = strlen(s);197GDrawString(globGC,cp->controlWindow,198centerX(globGC,s,strlength,199cp->buttonQueue[translate2D].buttonWidth) +200cp->buttonQueue[translate2D].buttonX,20131+headerHeight,s,strlen(s),Xoption);202203GSetForeground(globGC,(float)controlColorColor,Xoption);204205/* Write title of the graph selection window. */206s = "Graphs";207strlength = strlen(s);208GDrawString(globGC,cp->controlWindow,209centerX(globGC,s,strlength,controlWidth),graphHeaderHeight,210s,strlength,Xoption);211212/* Write titles on regular buttons and draw pixmaps on potentiometers. */213214for (i=0; i<(maxButtons2D); i++) {215if ((cp->buttonQueue[i]).pot) {216217GSetForeground(globalGC1,(float)buttonColor,Xoption);218GDrawRectangle(globalGC1,cp->controlWindow,219(cp->buttonQueue[i]).buttonX,220(cp->buttonQueue[i]).buttonY,221(cp->buttonQueue[i]).buttonWidth,222(cp->buttonQueue[i]).buttonHeight,Xoption);223224GSetForeground(trashGC,225(float)monoColor((cp->buttonQueue[i]).textColor),Xoption);226227GDrawLine(globalGC1,cp->controlWindow, /* trashGC, */228(cp->buttonQueue[i]).buttonX + (cp->buttonQueue[i]).xHalf,229(cp->buttonQueue[i]).buttonY,230(cp->buttonQueue[i]).buttonX + (cp->buttonQueue[i]).xHalf,231(cp->buttonQueue[i]).buttonY + 2*(cp->buttonQueue[i]).yHalf,Xoption);232GDrawLine(globalGC1,cp->controlWindow, /* trashGC, */233(cp->buttonQueue[i]).buttonX,234(cp->buttonQueue[i]).buttonY + (cp->buttonQueue[i]).yHalf,235(cp->buttonQueue[i]).buttonX + 2*(cp->buttonQueue[i]).xHalf,236(cp->buttonQueue[i]).buttonY + (cp->buttonQueue[i]).yHalf,Xoption);237switch (i) {238case scale2D:239GDrawLines(trashGC,cp->controlWindow,scaleArrow,240scaleArrowN,CoordModeOrigin,Xoption);241break;242case translate2D:243GDrawLines(trashGC,cp->controlWindow,translateArrow,244translateArrowN,CoordModeOrigin,Xoption);245break;246} /* switch i */247} else if (cp->buttonQueue[i].graphNum) {248249if (mono) {250if (graphStateArray[i-graphStart].showing) {251GSetForeground(graphGC,(float)backgroundColor,Xoption);252GSetBackground(graphGC,(float)foregroundColor,Xoption);253} else {254GSetForeground(graphGC,(float)foregroundColor,Xoption);255GSetBackground(graphGC,(float)backgroundColor,Xoption);256}257strlength = strlen((cp->buttonQueue[i]).text);258GDrawImageString(graphGC,cp->controlWindow,259(cp->buttonQueue[i]).buttonX +260centerX(graphGC,cp->buttonQueue[i].text,261strlength,(cp->buttonQueue[i]).buttonWidth),262(cp->buttonQueue[i]).buttonY +263centerY(graphGC,(cp->buttonQueue[i]).buttonHeight),264cp->buttonQueue[i].text,strlength,Xoption);265} else {266if (graphStateArray[i-graphStart].showing)267GSetForeground(graphGC,(float)graphBarShowingColor,Xoption);268else269GSetForeground(graphGC,(float)graphBarHiddenColor,Xoption);270strlength = strlen((cp->buttonQueue[i]).text);271GDrawString(graphGC,cp->controlWindow,272(cp->buttonQueue[i]).buttonX +273centerX(graphGC,cp->buttonQueue[i].text,274strlength,(cp->buttonQueue[i]).buttonWidth),275(cp->buttonQueue[i]).buttonY +276centerY(graphGC,(cp->buttonQueue[i]).buttonHeight),277cp->buttonQueue[i].text,strlength,Xoption);278}279} else if (cp->buttonQueue[i].graphSelect) {280/* The select characters are defined as: "^" for on and "-" for off. */281if (graphStateArray[i-graphSelectStart].selected) {282GSetForeground(graphGC,(float)graphBarSelectColor,Xoption);283strcpy((cp->buttonQueue[i]).text,"^");284} else {285GSetForeground(graphGC,(float)graphBarNotSelectColor,Xoption);286*(cp->buttonQueue[i]).text = '-';287strcpy((cp->buttonQueue[i]).text,"-");288}289GDrawString(graphGC,cp->controlWindow,290(cp->buttonQueue[i]).buttonX +291centerX(graphGC,cp->buttonQueue[i].text,292strlength,(cp->buttonQueue[i]).buttonWidth),293(cp->buttonQueue[i]).buttonY +294centerY(graphGC,(cp->buttonQueue[i]).buttonHeight),295cp->buttonQueue[i].text,strlength,Xoption);296}297else { /* a regular button */298int isOn = 1;299300switch(i) {301case pointsOnOff:302isOn = pointsON = graphStateArray[0].pointsOn;303if (graphStateArray[0].pointsOn)304strcpy((cp->buttonQueue[i]).text,"Pts On ");305else306strcpy((cp->buttonQueue[i]).text,"Pts Off");307break;308309case spline2D:310isOn = splineON = graphStateArray[0].splineOn;311if (graphStateArray[0].splineOn)312strcpy((cp->buttonQueue[i]).text, "Box On ");313else314strcpy((cp->buttonQueue[i]).text, "Box Off");315break;316317case connectOnOff:318isOn = connectON = graphStateArray[0].connectOn;319if (graphStateArray[0].connectOn)320strcpy((cp->buttonQueue[i]).text, "Lines On ");321else322strcpy((cp->buttonQueue[i]).text, "Lines Off");323break;324325case axesOnOff2D:326isOn = axesON = graphStateArray[0].axesOn;327if (graphStateArray[0].axesOn)328strcpy((cp->buttonQueue[i]).text , "Axes On ");329else330strcpy((cp->buttonQueue[i]).text , "Axes Off");331break;332333case unitsOnOff2D:334isOn = unitsON = graphStateArray[0].unitsOn;335if (graphStateArray[0].unitsOn)336strcpy( (cp->buttonQueue[i]).text , "Units On ");337else338strcpy( (cp->buttonQueue[i]).text , "Units Off");339break;340case closeAll2D:341isOn = 0;342343default:344break;345} /* switch i */346347s = (cp->buttonQueue[i]).text;348strlength = strlen(s);349350GDrawPushButton(dsply, globalGC1, trashGC, processGC, cp->controlWindow,351(cp->buttonQueue[i]).buttonX, (cp->buttonQueue[i]).buttonY,352(cp->buttonQueue[i]).buttonWidth, (cp->buttonQueue[i]).buttonHeight,353isOn, s,buttonColor,354monoColor((cp->buttonQueue[i]).textColor), Xoption);355356} /* else a regular button */357} /* for each button */358359/* Refresh the latest message */360makeMessageFromData(0);361writeControlMessage();362XFlush(dsply);363364} /*** drawControlPanel ***/365366controlXY367getControlXY(int whereDoYouWantPanel)368{369XWindowAttributes wAttr, wAttrib;370controlXY cXY;371int tmp=1;372Window rootW, parentW, *childrenWs, tmpW;373unsigned int nChildren;374375tmpW = viewport->titleWindow;376while(tmp) {377XQueryTree(dsply,tmpW,&rootW,&parentW,&childrenWs,&nChildren);378XFree(childrenWs);379if (parentW == rtWindow) tmp = 0;380else tmpW = parentW;381}382XGetWindowAttributes(dsply,tmpW,&wAttrib);383384XGetWindowAttributes(dsply,viewport->titleWindow,&wAttr);385if (whereDoYouWantPanel) {386switch (whereDoYouWantPanel) {387case 1: /* right */388cXY.putX = wAttrib.x + wAttrib.width;389cXY.putY = wAttrib.y;390break;391case 2: /* bottom */392cXY.putX = wAttrib.x + (wAttr.width - controlWidth)/2; /* center it */393cXY.putY = wAttrib.y + wAttrib.height;394break;395case 3: /* left */396cXY.putX = wAttrib.x - controlWidth - borderWidth;397cXY.putY = wAttrib.y;398break;399case 4: /* top */400cXY.putX = wAttrib.x + (wAttr.width - controlWidth)/2;401cXY.putY = wAttrib.y - controlHeight - borderHeight;402}403} else {404if ((physicalWidth - (wAttrib.x + wAttr.width)) >= controlWidth) {405cXY.putX = wAttrib.x + wAttrib.width;406cXY.putY = wAttrib.y;407} else if ((physicalHeight - (wAttrib.y + wAttr.height)) >=408controlHeight) {409cXY.putX = wAttrib.x + (wAttr.width - controlWidth)/2;410cXY.putY = wAttrib.y + wAttrib.height;411} else if (wAttrib.x >= controlWidth) {412cXY.putX = wAttrib.x - controlWidth - borderWidth;413cXY.putY = wAttrib.y;414} else if (wAttrib.y >= controlHeight) {415cXY.putX = wAttrib.x + (wAttr.width - controlWidth)/2;416cXY.putY = wAttrib.y - controlHeight - borderHeight;417} else { /* put inside of viewport */418cXY.putX = wAttrib.x + wAttr.width - controlWidth;419cXY.putY = wAttrib.y + wAttr.height - controlHeight;420}421}422return(cXY);423424}425426/************************************************/427/*** controlPanelStruct *makeControlPanel() ***/428/************************************************/429430controlPanelStruct *431makeControlPanel(void)432{433434Window cw;435int i,num;436controlPanelStruct *control;437buttonStruct *buttons;438controlXY cXY;439XSetWindowAttributes cwAttrib, controlAttrib;440XSizeHints sizehints;441Pixmap mousebits,mousemask;442XColor foreColor, backColor;443444if (!(control = (controlPanelStruct *)malloc(sizeof(controlPanelStruct)))) {445fprintf(stderr,"Ran out of memory trying to create a control panel.\n");446exitWithAck(RootWindow(dsply,scrn),Window,-1);447}448449cXY = getControlXY(0);450451/* Define and assign a mouse cursor. */452mousebits = XCreateBitmapFromData(dsply,rtWindow,453(const char*) mouseBitmap_bits,454mouseBitmap_width,mouseBitmap_height);455mousemask = XCreateBitmapFromData(dsply,rtWindow,456(const char*) mouseMask_bits,457mouseMask_width,mouseMask_height);458cwAttrib.background_pixel = backgroundColor; /* controlBackground; */459cwAttrib.border_pixel = foregroundColor;460cwAttrib.backing_store = WhenMapped;461cwAttrib.event_mask = controlMASK;462cwAttrib.colormap = colorMap;463cwAttrib.override_redirect = overrideManager;464foreColor.pixel = controlCursorForeground;465XQueryColor(dsply,colorMap,&foreColor);466backColor.pixel = controlCursorBackground;467XQueryColor(dsply,colorMap,&backColor);468cwAttrib.cursor = XCreatePixmapCursor(dsply,mousebits,mousemask,469&foreColor,&backColor,470mouseBitmap_x_hot,mouseBitmap_y_hot);471472cw = XCreateWindow(dsply,rtWindow,473cXY.putX,cXY.putY,controlWidth,controlHeight,3,474CopyFromParent,InputOutput,CopyFromParent,475controlCreateMASK,&cwAttrib);476477sizehints.flags = PPosition | PSize;478sizehints.x = cXY.putX;479sizehints.y = cXY.putY;480sizehints.width = controlWidth;481sizehints.height = controlHeight;482483/*** the None stands for icon pixmap...change.... ***/484485XSetNormalHints(dsply,cw,&sizehints);486XSetStandardProperties(dsply,cw,"2D Control Panel","2D Control Panel",487None,NULL,0,&sizehints);488489control->controlWindow = cw;490num = initButtons(control->buttonQueue);491control->numOfButtons = num;492buttons = control->buttonQueue;493494for (i=0; i<num; i++) {495controlAttrib.event_mask = (control->buttonQueue[i]).mask;496(control->buttonQueue[i]).self = XCreateWindow(dsply,cw,497(control->buttonQueue[i]).buttonX,498(control->buttonQueue[i]).buttonY,499(control->buttonQueue[i]).buttonWidth,500(control->buttonQueue[i]).buttonHeight,5010,0,InputOnly,CopyFromParent,502buttonCreateMASK,&controlAttrib);503504XMakeAssoc(dsply,table,(control->buttonQueue[i]).self,505&((control->buttonQueue[i]).buttonKey));506/* Use buttonKey instead of i because buttonKey has a permanent address */507XMapWindow(dsply,(control->buttonQueue[i]).self);508}509510/* Create message window */511control->messageWindow = XCreateWindow(dsply,cw,0,controlMessageY,512controlWidth,controlMessageHeight,5130,0,InputOnly,CopyFromParent,514messageCreateMASK,&cwAttrib);515XMapWindow(dsply,control->messageWindow);516517for (i=0; i<scaleArrowN; i++) {518scaleArrow[i].x += buttons[scale2D].buttonX;519scaleArrow[i].y += buttons[scale2D].buttonY;520}521for (i=0; i<translateArrowN; i++) {522translateArrow[i].x += buttons[translate2D].buttonX;523translateArrow[i].y += buttons[translate2D].buttonY;524}525526viewport->justMadeControl = yes;527return(control);528529} /* makeControlPanel() */530531532533534535536/*****************************************/537/*** void putControlPanelSomewhere() ***/538/*****************************************/539540/* This routine puts up the control panel associated with the viewport541passed in. It first tries to put it to the right of the viewport. If542there isn't enough room there, it tries the bottom and so on going543clockwise. If the viewport is too big and there is no room to put the544control panel outside of it, it placed the control panel in the bottom545right hand corner of the viewport window. */546547void548putControlPanelSomewhere(int whereDoesPanelGo)549{550controlPanelStruct *control;551controlXY whereControl;552553control = viewport->controlPanel;554whereControl = getControlXY(whereDoesPanelGo);555556viewport->haveControl = yes;557558XRaiseWindow(dsply,control->controlWindow);559XMoveWindow(dsply,control->controlWindow,whereControl.putX,560whereControl.putY);561562drawControlPanel();563if (viewport->justMadeControl) {564XMapWindow(dsply,control->controlWindow);565viewport->justMadeControl = no;566}567XMapWindow(dsply,control->controlWindow);568}569570571572573574/************************************/575/*** void clearControlMessage() ***/576/************************************/577578void579clearControlMessage(void)580{581582strcpy(viewport->controlPanel->message,"");583584XClearArea(dsply,viewport->controlPanel->controlWindow,5850,controlMessageY-2,controlWidth,controlMessageHeight,False);586}587588589590591