?? hgraf.c
字號:
/* ----------------------------------------------------------- *//* *//* ___ *//* |_| | |_/ SPEECH *//* | | | | \ RECOGNITION *//* ========= SOFTWARE */ /* *//* *//* ----------------------------------------------------------- *//* Copyright: Microsoft Corporation *//* 1995-2000 Redmond, Washington USA *//* http://www.microsoft.com *//* *//* Use of this software is governed by a License Agreement *//* ** See the file License for the Conditions of Use ** *//* ** This banner notice must not be removed ** *//* *//* ----------------------------------------------------------- *//* File: HGraf.X.c: HGraf for X-Windows *//* ----------------------------------------------------------- */char *hgraf_version = "!HVER!HGraf(X): 3.1 [CUED 16/01/02]";char *hgraf_vc_id = "$Id: HGraf.c,v 1.8 2002/12/19 16:37:11 ge204 Exp $";/* This is the X Windows implementation of HGraf. It is server independent except for two areas. a) a set of fonts in sizes 8,9,10,12,14,15,16,19,20,24 should be provided by your server. This distribution attempts to load these using the pattern *-Medium-R-Normal-*. If your X server does not provide fonts matching this pattern you will need to modify the FontNm initialiser below. b) the set of X colours used for the 16 HGraf colours may need to be modified to give the best results on your machine. See the XColArray initialiser below. It may be necessary to modify this source and/or change compiler options in order to compile this version of HGraf. For example, under HP-UX 8.07 you would need to add the following immediately after this comment #define _HPUX_SOURCE The include search path also needs to be altered by using -I/usr/include/X11R4 compiler option */#include "HShell.h"#include "HMem.h"#include "HMath.h"#include "HGraf.h"/* Enable use of timer to add hysteresis to button response. This should work for most UNIX systems but the location of <time.h> may vary from system to system. #define USE_TIMER*/#ifdef USE_TIMER#include <sys/time.h>#define _BSD_SIGNALS#include <signal.h>#endif #include <X11/Xlib.h> /* the X11 stuff makes string.h also available */#include <X11/Xutil.h>#include <X11/Xos.h>#define XK_MISCELLANY /* use miscellaneous keysym defs */#include <X11/keysymdef.h>#define MAX_GC 4 /* we need 1 GC for each transfer mode since changing the transfer mode in the current GC is unreliable on some X-servers that we have tested */static MemHeap btnHeap; /* heap for HButton structures *//* Global X Stuff */static Display *theDisp; static Window rootW, theWindow; static int theScreen; static unsigned int ncells, dispWIDE, dispHIGH, dispDEEP;static Colormap theCmap;static GC theGC;static GC gcs[MAX_GC];static unsigned long black, white;static Visual *theVisual;static XEvent report;static XSizeHints hints;static Boolean winCreated = FALSE;static int colours[MAX_COLOURS];static int greys[MAX_GREYS]; static char *XColArray[] = { "white","yellow","orange","red", "plum", "purple","blue","lightblue", "darkgreen","palegreen", "brown","tan", "lightgray", "gray","darkslategray", "black" };static int TFuncX11[4] = { GXcopy, GXor, GXxor, GXinvert};/* --------------------------- Initialisation ---------------------- */static ConfParam *cParm[MAXGLOBS]; /* config parameters */static int nParm = 0;static int trace = 0; /* Just for consistency *//* EXPORT->InitGraf: initialise memory and configuration parameters */void InitGraf(void){ int i; Register(hgraf_version,hgraf_vc_id); nParm = GetConfig("HGRAF", TRUE, cParm, MAXGLOBS); if (nParm>0){ if (GetConfInt(cParm,nParm,"TRACE",&i)) trace = i; }}/*------------------- Font Handling Routines -----------------------*/#define NO_OF_FONTS 10#define FONTS_AVAILABLE 10static char *FontNm[NO_OF_FONTS] = { "*-Medium-R-Normal-*-8-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-9-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-10-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-12-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-14-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-15-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-16-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-19-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-20-*-*-*-*-*-*-*", "*-Medium-R-Normal-*-24-*-*-*-*-*-*-*" };static int FontSize[NO_OF_FONTS] = {8, 9, 10, 12, 14, 15, 16, 19, 20, 24};static XFontStruct *DefaultFont, *CurrentFont, *FontInfo[NO_OF_FONTS];#define FONT1 "-*-lucida-medium-r-*-*-12-*"#define FONT2 "-*-helvetica-medium-r-*-*-12-*"#define FONT3 "6x13"/* InstallFonts: install the HGraf font set */static void InstallFonts(void){ int i; /* load user fonts */ for (i=0; i < FONTS_AVAILABLE; i++) if ((FontInfo[i] = XLoadQueryFont(theDisp, FontNm[i])) == NULL) HError(-6870, "InstallFonts: Cannot load font %s", FontNm[i]); /* load the default font */ if ((DefaultFont = XLoadQueryFont(theDisp, FONT1))==NULL && (DefaultFont = XLoadQueryFont(theDisp, FONT2))==NULL && (DefaultFont = XLoadQueryFont(theDisp, FONT3))==NULL) HError(6870, "InstallFonts: Cannot load default font");}/* ----------------------------- Event Handling --------------------------------- *//* DecodeKeyPress: decode the given keypress into char+modifier */static void DecodeKeyPress(XKeyEvent *xkev, HEventRec *hev){ char buf[20]; int n; KeySym key; XComposeStatus compose; n = XLookupString(xkev,buf,20,&key,&compose); hev->c = buf[0]; switch (key) { case XK_Shift_L: case XK_Shift_R: hev->ktype = SHIFTKEY; break; case XK_Control_L: case XK_Control_R: hev->ktype = CONTROLKEY; break; case XK_Meta_L: case XK_Meta_R: case XK_Alt_L: case XK_Alt_R: hev->ktype = COMMANDKEY; break; case XK_Return: case XK_KP_Enter: hev->ktype = ENTERKEY; break; case XK_Escape: hev->ktype = ESCKEY; break; case XK_BackSpace: case XK_Delete: hev->ktype = DELKEY; break; default: hev->ktype = NORMALKEY; }}/* EXPORT->HGetEvent: return next relevant event in event queue */HEventRec HGetEvent(Boolean anyEvent, void (*action)(void)){ XEvent xev; HEventRec hev; Boolean found,dummy; XFlush(theDisp); found = FALSE; do { if(XEventsQueued(theDisp, QueuedAfterFlush) > 0 || action==NULL){ XNextEvent(theDisp, &xev); found = TRUE; if (xev.xany.window==theWindow || anyEvent){ switch (xev.type) { case ButtonPress: hev.event = HMOUSEDOWN; hev.x = xev.xbutton.x; hev.y = xev.xbutton.y; break; case ButtonRelease: hev.event = HMOUSEUP; hev.x = xev.xbutton.x; hev.y = xev.xbutton.y; break; case MotionNotify: hev.event = HMOUSEMOVE; hev.x = xev.xmotion.x; hev.y = xev.xmotion.y; break; case KeyPress: hev.event = HKEYPRESS; hev.x = xev.xkey.x; hev.y = xev.xkey.y; DecodeKeyPress(&(xev.xkey), &hev); break; case KeyRelease: hev.event = HKEYRELEASE; hev.x = xev.xkey.x; hev.y = xev.xkey.y; DecodeKeyPress(&(xev.xkey), &hev); break; case Expose: if (xev.xexpose.count==0) hev.event = HREDRAW; else found = FALSE; break; default: found = FALSE; } } } else if (action!=NULL){ (*action)(); XFlush(theDisp); /* execute a round-robin command to make sure that */ /* client doesnt get too far ahead of the server */ dummy = HMousePos(&hev.x,&hev.y); } } while (!found); return hev; }/* EXPORT->HEventsPending: Return number of events pending */int HEventsPending(void){ return XEventsQueued(theDisp, QueuedAfterFlush);}/* EXPORT->HMousePos: return mouse pos in x, y, returns TRUE if the pointer is on the window */Boolean HMousePos(int *x, int *y){ Window root,child; int rx,ry; unsigned int keys; return (Boolean) XQueryPointer(theDisp, theWindow, &root, &child, &rx, &ry, x, y, &keys);}/* EXPORT: IsInRect: return TRUE iff (x,y) is in the rectangle (x0,y0,x1,y1) */ Boolean IsInRect(int x, int y, int x0, int y0, int x1, int y1){ return (x >= x0 && x<=x1 && y >= y0 && y <= y1);}/* ------------------------- Colour Handling ------------------------------ */#define GSTP 4static void InstallColours(void){ int pixVal=0; int c, f, ggap, steps[GSTP] = {8, 4, 2, 1}; XColor greyDef, whiteDef, blackDef, colourDef; short RGBval, step; /* initialise the grey levels/colours depending on the number of planes */ for (c = 0; c < MAX_COLOURS; c++){ /* get colour from the X11 database */ if (!XParseColor(theDisp, theCmap, XColArray[c], &colourDef)) HError(6870,"InstallColours: Colour name %s not in X11 database", XColArray[c]); if (!XAllocColor(theDisp, theCmap, &colourDef)) HError(-6870,"InstallColours: Cannot allocate colour %s", XColArray[c]); else pixVal = colourDef.pixel; colours[c] = pixVal; } if (dispDEEP == 1){ /* map all grey levels onto b/w */ for (c = 0; c < MAX_GREYS/2; c++) greys[c] = white; for (c = MAX_GREYS/2; c < MAX_GREYS; c++) greys[c] = black; } else { /* then the grey levels */ whiteDef.pixel = white; XQueryColor(theDisp, theCmap, &whiteDef); blackDef.pixel = black; XQueryColor(theDisp, theCmap, &blackDef); ggap = ((int)(whiteDef.red - blackDef.red))/MAX_GREYS; for (f = 0; f < GSTP; f++){ step = steps[f]*ggap; for (c = 0; c < (MAX_GREYS/steps[f]); c++){ RGBval = whiteDef.red - c*step; greyDef.red = RGBval; greyDef.green = RGBval; greyDef.blue = RGBval; if (!XAllocColor(theDisp, theCmap, &greyDef)) HError(-6870, "InstallColours: Cannot allocate grey level %d", c*steps[f]); else pixVal = greyDef.pixel; greys[c] = pixVal; } } }}/* EXPORT-> HSetColour: Set current colour to c */void HSetColour(HColour c){ XferMode xf; for (xf = GCOPY; xf < GINVERT; xf=(XferMode) (xf+1)) /* change all GCs except GINVERT*/ XSetForeground(theDisp, gcs[(int) xf], colours[(int) c]);}/* EXPORT-> HSetGrey: Set current colour to grey level g */void HSetGrey(int g){ XferMode xf; for (xf = GCOPY; xf < GINVERT; xf=(XferMode) (xf+1)) /* change all GCs except GINVERT*/ XSetForeground(theDisp, gcs[(int) xf], greys[g]);}/* ------------------------ Drawing Primitives ---------------------------- *//* CheckCorners: make sure (x0,y0) is north-west of (x1,y1) */static void CheckCorners(int *x0, int *y0, int *x1, int *y1){ int a,b,c,d; if (*x0<*x1) {a=*x0; c=*x1;} else {a=*x1; c=*x0;} if (*y0<*y1) {b=*y0; d=*y1;} else {b=*y1; d=*y0;} *x0=a; *y0=b; *x1=c; *y1=d;}/* EXPORT-> HDrawLines: Draw multiple lines */void HDrawLines(HPoint *points, int n){ XDrawLines(theDisp, theWindow, theGC, (XPoint *) points, n, CoordModeOrigin);}/* EXPORT-> HDrawRectangle: draw a rectangle */void HDrawRectangle(int x0, int y0, int x1, int y1){ CheckCorners(&x0,&y0,&x1,&y1); XDrawRectangle(theDisp, theWindow, theGC, x0, y0, x1 - x0, y1 - y0);}/* EXPORT-> HFillRectangle: fill a rectangle */void HFillRectangle(int x0, int y0, int x1, int y1){ CheckCorners(&x0,&y0,&x1,&y1); XFillRectangle(theDisp, theWindow, theGC, x0, y0, x1 - x0, y1 - y0);}/* EXPORT-> HDrawLines: Draw multiple lines */void HDrawLine(int x0, int y0, int x1, int y1){ XDrawLine(theDisp, theWindow, theGC, x0, y0, x1, y1);}/* EXPORT-> HFillPolygon: fill a convex polygon */void HFillPolygon(HPoint *points, int n){ XFillPolygon(theDisp, theWindow, theGC, (XPoint *) points, n, Convex, CoordModeOrigin);}/* EXPORT-> HDrawArc: Draw arc from stAngle thru arcAngle degrees */void HDrawArc(int x0, int y0, int x1, int y1, int stAngle, int arcAngle){ unsigned int rw, rh; CheckCorners(&x0,&y0,&x1,&y1); /* calculate width and height */ rw = abs(x1 - x0); rh = abs(y1 - y0); /* the angles are signed integers in 64ths of a degree */ stAngle *=64; arcAngle*=64; XDrawArc(theDisp, theWindow, theGC, x0, y0, rw, rh, stAngle, arcAngle);}/* EXPORT-> HFillArc: Draw filled arc from stAngle thru arcAngle degrees */void HFillArc(int x0,int y0,int x1,int y1,int stAngle,int arcAngle){ unsigned int rw, rh; CheckCorners(&x0,&y0,&x1,&y1);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -