⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpsgraph.c

📁 that is a useful book for people who like to learn more about C++.
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <Xlib.h>#include <Xatom.h>#include <stdio.h>#include <string.h>#include <Xutil.h>#define TRUE 1#define FALSE 0#define MAXPATH 500#define PSOnly 0#define XOnly 1#define XAndPS 2extern char **environ;static FILE *PSFile;static GC GraphContext, StopContext, StartContext, ContContext,          StepContext, BackContext; static int WinULX, WinULY, LastWinX, LastWinY, CurrentDashLength;static int PSULX, PSULY, PathLength, CurrentLineWidth, CurrentLineStyle;static unsigned int WinWidth, WinHeight, BorderWidth, Dep;static unsigned int PSWidth, PSHeight;static Window root, StopButton, StartButton, StepButton, ContButton;static Window GraphWin;static Display *display;static XEvent report;static int screen;static double UserLLX, UserLLY, UserWidth, UserHeight;static Colormap Kleurtjes;static Pixmap Backup;static Font CurrentFont;static XFontStruct *CurrentFontStruct;static XStandardColormap *mapinfo;static char *CurrentFontName, *PSFileName;static int CurrentFontSize;static double Scale;unsigned long BLUE, GREEN, RED, YELLOW, BLACK, WHITE;static short int InitOK, FramOK, StartOK, StopOK, Output;static char *BackColName;void error (Message)char *Message;{   printf ("%s\n", Message);   exit(1);}void Framing (Xll, Yll, Xur, Yur)double Xll, Yll, Xur, Yur;{   FramOK = TRUE;   if ((Xll!=Xur) && (Yll!=Yur))       {         UserLLX = Xll;         UserLLY = Yll;         UserWidth  = Xur-Xll;         UserHeight = Yur-Yll;      }   else      { printf("ERROR in function FRAMING: invalid arguments .\n ");        exit(1);      }}void MapStartButton(){   int Xul, Yul;   if (StartOK == TRUE)       {         XGetGeometry (display, GraphWin, &root, &WinULX, &WinULY, &WinWidth,                                         &WinHeight, &BorderWidth, &Dep);         Xul =  WinULX+0.03*WinWidth;         Yul =  WinULY+0.06*WinHeight;         XMoveWindow (display, StartButton, Xul, Yul);         XMapRaised(display, StartButton);         XDrawString (display, StartButton, StartContext, 5,12,"start",5);       }}void MapStopButton(){   int Xul, Yul;   if (StopOK == TRUE)      {       XGetGeometry (display, GraphWin, &root, &WinULX, &WinULY, &WinWidth,                                         &WinHeight, &BorderWidth, &Dep);       Xul =  WinULX+0.03*WinWidth;       Yul =  WinULY+0.01*WinHeight;       XMoveWindow (display, StopButton, Xul, Yul);       XMapRaised(display, StopButton);       XDrawString (display, StopButton, StopContext, 5,12,"stop",4); /*       Xul =  WinULX+0.03*WinWidth;       Yul =  WinULY+0.06*WinHeight;       XMoveWindow (display, StartButton, Xul, Yul);       XMapRaised(display, StartButton);       XDrawString (display, StartButton, StartContext, 5,12,"start",5); */    }}voidAssociateColor (ColorName, ColorPixel)char *ColorName;unsigned long *ColorPixel;{   XColor Kleur, KleurExact;   XAllocNamedColor (display,XDefaultColormap(display,screen),                     ColorName, &Kleur, &KleurExact);    *ColorPixel = Kleur.pixel;}voidInstallFastColors (){   XColor Kleur, KleurExact;   if (Output == XOnly || Output == XAndPS)     {        AssociateColor ("blue", &BLUE);       AssociateColor ("green", &GREEN);       AssociateColor ("red", &RED);       AssociateColor ("yellow", &YELLOW);       AssociateColor ("white", &WHITE);       AssociateColor ("black", &BLACK);     }}      voidSetQuickColor(ClPx)XColor *ClPx;{  XAllocColor (display, XDefaultColormap(display,screen), ClPx); }unsigned longColorCode(R, G, B)int R, G, B;{  XColor *ColorData;  unsigned long r;  ColorData = (XColor *) malloc (sizeof(ColorData));  ColorData->red   = (unsigned short) (R);  ColorData->green = (unsigned short) (G);  ColorData->blue  = (unsigned short) (B);  ColorData->pixel = 0;  SetQuickColor(ColorData);  r = (ColorData->pixel);  free (ColorData);  return (r);}voidSetNumColor(ClPx)unsigned long ClPx;{  XSetForeground(display, GraphContext, ClPx);  XSetForeground(display, BackContext, ClPx); }voidSetFastColor (Color)int Color;{   if (Output == XOnly || Output == XAndPS)     {        switch(Color)       {         case 1 : XSetForeground (display, GraphContext, BLUE);                   XSetForeground (display, BackContext, BLUE);                   break;         case 2 : XSetForeground (display, GraphContext, GREEN);                   XSetForeground (display, BackContext, GREEN);                   break;         case 3 : XSetForeground (display, GraphContext, RED);                   XSetForeground (display, BackContext, RED);                   break;         case 4 : XSetForeground (display, GraphContext, YELLOW);                   XSetForeground (display, BackContext, YELLOW);                   break;         case 5 : XSetForeground (display, GraphContext, WHITE);                   XSetForeground (display, BackContext, WHITE);                   break;         case 6 : XSetForeground (display, GraphContext, BLACK);                   XSetForeground (display, BackContext, BLACK);        }    }}void SetNamedColor (ColorName)char *ColorName;{   XColor Kleur, KleurExact;   unsigned long ColorPix;   if (Output == XOnly || Output == XAndPS)     {        XAllocNamedColor (display,XDefaultColormap(display,screen),ColorName, &Kleur, &KleurExact);        ColorPix = Kleur.pixel;       XSetForeground (display, GraphContext, ColorPix);        XSetForeground (display, BackContext, ColorPix);     }}voidSetNamedBackground (ColorName)char *ColorName;{   XColor Kleur, KleurExact;   unsigned long ColorPix;   if (Output == XOnly || Output == XAndPS)     {        XAllocNamedColor (display,XDefaultColormap(display,screen),ColorName, &Kleur, &KleurExact);        ColorPix = Kleur.pixel;       XSetForeground (display, GraphContext, ColorPix);        XFillRectangle (display, GraphWin, GraphContext, 0, 0, WinWidth, WinHeight);       XSetForeground (display, BackContext, ColorPix);        XFillRectangle (display, Backup, BackContext, 0, 0, WinWidth, WinHeight);       SetNamedColor("black");    }}voidSetFillStyle(style)char *style;{   int FillStyle;   if (Output == XOnly || Output == XAndPS)     {        if (style[0] == 's' || style[0] == 'S')          FillStyle = FillSolid;       else if (style[0] == 't' || style[0] == 'T')          FillStyle = FillTiled;       else if (style[0] == 'p' || style[0] == 'P')          FillStyle = FillStippled;       else if (style[0] == 'o' || style[0] == 'O')          FillStyle = FillOpaqueStippled;       XSetFillStyle (display, GraphContext, FillStyle);       XSetFillStyle (display, BackContext, FillStyle);    }}char*lcase(c)char c[];{  int i;     i = 0;  while (c[i] != '\0') {     if (c[i]>='A' && c[i] <= 'Z')        c[i] = c[i] + 'a' -'A';      i++;       }  return(c);}voidSetFont(FontName, Weight, Orientation, Size)char *FontName, *Weight, *Orientation;int Size;{   char *pattern, *instxt, SizeName[3], *LFontName, *LWeight, *LOrientation,         **names, *FirstName, Single[2];   int maxnames;   int ActCntNum;   if (InitOK == FALSE || FramOK == FALSE)      error ("Sorry, you must call Initplot and Framing before anything else");   if (FontName[0] == 't' || FontName[0] == 'T')     {       LFontName = (char *) calloc (1, sizeof("times"));       LFontName = strcpy(LFontName,"times");     }   else if (FontName[0] == 'n' || FontName[0] == 'N')     {       LFontName = (char *) calloc (1, sizeof("newcenturyschlbk"));       LFontName = strcpy(LFontName,"newcenturyschlbk");     }   else if (FontName[0] == 'h' || FontName[0] == 'H')     {       LFontName = (char *) calloc (1, sizeof("helvetica"));       LFontName = strcpy(LFontName,"helvetica");     }   else if (FontName[0] == 'c' || FontName[0] == 'C')     {       LFontName = (char *) calloc (1, sizeof("courier"));       LFontName = strcpy(LFontName,"courier");     }   else if (FontName[0] == 's' || FontName[0] == 'S')     {       LFontName = (char *) calloc (1, sizeof("symbol"));       LFontName = strcpy(LFontName,"symbol");     }   LOrientation = (char *) calloc (1, sizeof(Orientation));   LOrientation = strcpy (LOrientation, Orientation);   LOrientation = lcase(LOrientation);   LWeight = (char *) calloc (1, sizeof(Weight));   LWeight = strcpy (LWeight, Weight);   LWeight = lcase(LWeight);   CurrentFontName = (char *) calloc (1, 200);   pattern = (char *) calloc (1, 200);   CurrentFontName = strcpy (CurrentFontName, LFontName);   CurrentFontName[0] = CurrentFontName[0]+'A'-'a';   if (CurrentFontName[0] == 'N')      {       CurrentFontName = strcpy( CurrentFontName ,"NewCenturySchlbk");     }   if (LOrientation[0] == 'o' && LWeight[0] == 'm')      CurrentFontName = strcat (CurrentFontName, "-Oblique");   else if (LOrientation[0] == 'i' && LWeight[0] == 'm')      CurrentFontName = strcat (CurrentFontName, "-Italic");   else if (LOrientation[0] == 'r' && LWeight[0] == 'm')     {       if (CurrentFontName[0] == 'T' || CurrentFontName[0] == 'N')          CurrentFontName = strcat (CurrentFontName, "-Roman");     }   else if (LOrientation[0] == 'o' && LWeight[0] == 'b')      CurrentFontName = strcat (CurrentFontName, "-BoldOblique");   else if (LOrientation[0] == 'i' && LWeight[0] == 'b')      CurrentFontName = strcat (CurrentFontName, "-BoldItalic");   else if (LOrientation[0] == 'r' && LWeight[0] == 'b')      CurrentFontName = strcat (CurrentFontName, "-Bold");      if (Output == PSOnly || Output == XAndPS)     {        CurrentFontSize = (int) Size*Scale;       fprintf (PSFile, "/%s findfont %d scalefont setfont\n", CurrentFontName,                CurrentFontSize);     }   if (Output == XOnly || Output == XAndPS)     {        pattern = strcpy(pattern,"-adobe-");       strcat (pattern, LFontName);        strcat (pattern, "-");       if (LWeight[0] == 'm' || LWeight[0] == 'M')          strcat (pattern, "medium");       else          strcat (pattern, "bold");       strcat (pattern, "-");       Single[0] = Orientation[0];       Single[1] = '\0';       strcat (pattern, Single);       strcat (pattern,  "-normal--");       sprintf (SizeName, "%d", Size);       strcat (pattern, SizeName);       strcat (pattern, "-*");       maxnames = 1;       names = XListFonts(display, pattern, maxnames, &ActCntNum);        if (names != NULL)         {           FirstName = names[0];           if (CurrentFontStruct != NULL)               XFreeFont (display, CurrentFontStruct);           CurrentFontStruct=XLoadQueryFont(display, FirstName);           CurrentFont = CurrentFontStruct->fid;           XSetFont (display, GraphContext, CurrentFont);           XSetFont (display, BackContext, CurrentFont);         }       else         {            printf ("Font requested not found by x-server\n");            printf ("Requested font was: %s \n", pattern);            FirstName = "-adobe-times-medium-r-normal--24-240-75-75-p-124-iso8859-1";           CurrentFontStruct=XLoadQueryFont(display, FirstName);         }     }   cfree (pattern);   cfree (LFontName);   cfree (LWeight);   cfree (LOrientation);   cfree(CurrentFontName);}voidRedraw(){   XSetWindowBackgroundPixmap (display, GraphWin, Backup);   MapStartButton();   MapStopButton();}  void StartRedraw(){   XClearWindow (display, GraphWin);   MapStartButton();   MapStopButton();   Redraw ();}voidWaitRelease(){   XNextEvent (display, &report);   if (report.type == ButtonRelease)     {       XCloseDisplay(display);       exit(1);     }}voidCheckEvent(){   if (XPending(display) != 0)     {        XNextEvent (display, &report);         switch (report.type)            {                case (Expose)    : StartRedraw();                                  XGetGeometry (display, GraphWin, &root,                                         &WinULX, &WinULY, &WinWidth,                                        &WinHeight, &BorderWidth, &Dep);                                   XClearWindow (display, GraphWin);                                  Redraw ();                                  break;               case ButtonPress : if ((report.xbutton.window) == StopButton)                           {                            if (Output == PSOnly || Output == XAndPS)                              {                                 fprintf (PSFile, "stroke\ngrestore\nend\nshowpage\n");                                 fclose (PSFile);                              }                            WaitRelease();                          }            }     }}void User2Win (XUser,YUser, XWin, YWin)double XUser, YUser; int   *XWin, *YWin;{   *XWin = (XUser-UserLLX)/UserWidth*WinWidth;   *YWin = (1.0-(YUser-UserLLY)/UserHeight)*WinHeight;}void UserPSWin (XUser,YUser, XPS, YPS)double XUser, YUser; int   *XPS, *YPS;{   *XPS = (XUser-UserLLX)/UserWidth*PSWidth;   *YPS = (YUser-UserLLY)/UserHeight*PSHeight;}void Win2User (XWin, YWin, XUser, YUser)double *XUser, *YUser; int   XWin, YWin;{   *XUser = UserLLX+XWin*UserWidth/WinWidth;   *YUser = UserLLY+(WinHeight-YWin)*UserHeight/WinHeight;}voidCheckPathLength(incr, X, Y)int incr, X, Y;{   PathLength= PathLength+incr;   if (PathLength>MAXPATH) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -