home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
d
/
dir_nm20.zip
/
Dir_NM_2.0
/
NM-Start.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-16
|
8KB
|
251 lines
/* === NM-Start.c ===
*
* void DoExposeEvent(report)
* void DoConfigureEvent(report,width,height)
* void DoEvents(width,height)
*
* void main(argc,argv)
* void get_GC(win,gc,font_info)
* void load_font(font_info,fontname)
* void PrintMsgText(win,gc,font_info,text_string)
* void FatalError (message)
* void Quit()
*
*/
#include "NM.h"
#include "icon_bitmap"
/* since display and screen are used throughout Xlib calls we define up front */
Display *display;
int screen;
Window win;
GC gc;
XFontStruct *font_info;
XWindowAttributes attribs;
char tmpstr[128],*cmd;
XColor mycolor,exactcolor;
Colormap theCmap;
/* This is an example of MANUAL Main Event Loop in X windows, and this
* version uses ONLY the lowest level Xlib calls. */
/* This may not do what you want because it processes
* Expose events whenever you do ClearArea with the lastargument set!!
*/
void DoExposeEvent(report)
XEvent report;
{
/*
int count;
XEvent ExposeReport;
if (report.xexpose.window==win)
sprintf(tmpstr,"Graphics Window:(x,y)(%d,%d);(w,h) (%d,%d)",report.xexpose.x,
report.xexpose.y,report.xexpose.width, report.xexpose.height);
PrintMsgText(win,gc,font_info,tmpstr);
XGetWindowAttributes(display,win,&attribs);
printf("\n report.type %d ",report.type);
while (XCheckTypedEvent(display,Expose,&ExposeReport))
{
printf("\nLOOP report.type %d, count = %d. ",ExposeReport.type,
ExposeReport.xexpose.count);
}
*/
}
void DoConfigureEvent(report,width,height)
XEvent report;
unsigned int *width,*height;
{
if ((report.xconfigure.window==win) && ((report.xconfigure.width != *width)
|| (report.xconfigure.height != *height)))
/* window has been resized, change width and height */
{
*width=report.xconfigure.width;*height=report.xconfigure.height;
}
XGetWindowAttributes(display,win,&attribs);
}
void DoEvents(width,height)
unsigned int width,height;
{
XEvent report;
int button,mouse_x,mouse_y,old_mouse_x,old_mouse_y,dx,dy;
old_mouse_x=width/2;old_mouse_y=height/2;
mouse_x=old_mouse_x;mouse_y=old_mouse_y;dx=0;dy=0;
while(1)
{
XNextEvent(display, &report);
switch(report.type)
{
case Expose:DoExposeEvent(report);
break;
case ConfigureNotify:DoConfigureEvent(report,&width,&height);
old_mouse_x=width/2;old_mouse_y=height/2;
mouse_x=old_mouse_x;mouse_y=old_mouse_y;
break;
case MotionNotify:mouse_x=report.xmotion.x;mouse_y=report.xmotion.y;
dx=mouse_x-old_mouse_x;dy=mouse_y-old_mouse_y;
DoMotionEvent(report,mouse_x,mouse_y,dx,dy);
break;
case ButtonPress:button=report.xbutton.button;
mouse_x=report.xbutton.x;mouse_y=report.xbutton.y;
dx=mouse_x-old_mouse_x;dy=mouse_y-old_mouse_y;
DoButtonEvent(report,button,mouse_x,mouse_y,1,1,1);
break;
case ButtonRelease:button=report.xbutton.button;
mouse_x=report.xbutton.x;mouse_y=report.xbutton.y;
dx=mouse_x-old_mouse_x;dy=mouse_y-old_mouse_y;
DoButtonReleaseEvent(report,button,mouse_x,mouse_y,
dx,dy);
break;
case KeyPress:DoKeyPressEvent(report);
break;
case KeyRelease:DoKeyReleaseEvent(report);
break;
default:/* all events selected by StructureNotifyMask except
ConfigureNotify are thrown away here, since nothing
is done with them */
break;
} /* end switch */
/* Reset the state */
old_mouse_x=mouse_x;old_mouse_y=mouse_y;
} /* end while */
}
void main(argc,argv)
int argc;
char **argv;
{
unsigned int width, height; /* window size */
int x=0, y=0; /* window position */
unsigned int border_width=4;
char *window_name=" NM 2.0 Interaction Window",*icon_name=" NM 2.0 ";
Pixmap icon_pixmap;
XSizeHints size_hints;
char *display_name = NULL;
int first_time;
unsigned long fg,bg;
cmd=argv[0];first_time=1;
printf("\n\n *** Welcome to NM 2.0 ***\n\n");
/* connect to X server */
if((display=XOpenDisplay(display_name))==NULL)
{
(void)fprintf(stderr,"%s: cannot connect to X server %s\n",cmd,
XDisplayName(display_name));
exit( -1 );
}
/* get screen size from display structure macro */
screen=DefaultScreen(display);
width=DisplayWidth(display,screen);height=DisplayHeight(display,screen);
bg = BlackPixel(display,screen);fg = WhitePixel(display,screen);
/* initialize size hint property for the window manager */
size_hints.flags=PPosition | PSize | PMinSize;
size_hints.x=x;size_hints.y=y;size_hints.width=width;
size_hints.height=height;size_hints.min_width=250;size_hints.min_height=250;
/* create an opaque window */
win=XCreateSimpleWindow(display,RootWindow(display,screen),0,0,width,height,
border_width,WhitePixel(display,screen),
BlackPixel(display,screen));
icon_pixmap=XCreateBitmapFromData(display,win,icon_bitmap_bits,
icon_bitmap_width, icon_bitmap_height);
/* set properties for window manager (before mapping) */
XSetStandardProperties(display,win,window_name,icon_name,icon_pixmap,argv,
argc,&size_hints);
load_font(&font_info,"12x24");
/* create GC for text and drawing and display window */
get_GC(win,&gc,font_info);
XSetForeground(display,gc,fg);XSetBackground(display,gc,bg);
/* select event types wanted */
XSelectInput(display,win,ExposureMask|KeyPressMask|KeyReleaseMask|
ButtonPressMask|ButtonMotionMask|ButtonReleaseMask|
PointerMotionMask|StructureNotifyMask );
XMapWindow(display,win); /* Display the window */
theCmap=DefaultColormap(display, screen);
/* Wait for events, etc. */
XGetWindowAttributes(display,win,&attribs);
/* ***Initialize Coordinate Frame and do other initializations*** */
SetUpCoordSystem(width,height);DoEvents(width,height);
}
get_GC(win,gc,font_info)
Window win; GC *gc;
XFontStruct *font_info;
{
unsigned long valuemask=0; /*ignore XGCvalues and use defaults */
XGCValues values;
unsigned int line_width=2;
int line_style=LineSolid,dash_offset=0,list_length=2;
static char dash_list[]={12,24};
/* Create default graphics context:
* **** If "plane_mask = bg | fg", color assignments
* won't work properly.
*/
values.plane_mask = 0xffffff;
*gc = XCreateGC(display,win,valuemask,&values);
/* specify font */
XSetFont(display,*gc,font_info->fid);
/* specify black foreground since default may be white on white */
XSetForeground(display,*gc,BlackPixel(display,screen));
}
load_font(font_info,fontname)
XFontStruct **font_info;
char *fontname;
{
if ((*font_info = XLoadQueryFont(display,fontname))==NULL)
{
(void) fprintf(stderr,"Basic: Cannot open font \n");exit(-1);
}
}
PrintMsgText(win,gc,font_info,text_string)
Window win;
GC gc;
XFontStruct *font_info;
char *text_string;
{
unsigned int wid,y_offset,x_offset,strL;
unsigned long fcol;
y_offset=font_info->max_bounds.ascent+7;x_offset=2;strL=strlen(text_string);
/* output test, centered on each line */
/* fcol=BlackPixel(theDisp,theScreen);
* XSetForeground(theDisp,theGC,fcol); */
/* *** True below generates Expose! (which can be undesirable) */
/* wid= 500; wid=XTextWidth(font_info,text_string,strL); */
wid=attribs.width;XClearArea(display,win,x_offset,0,wid,18,False);
XDrawString(display,win,gc,x_offset,y_offset,text_string,strL);
}
/* FATAL ERROR */
FatalError (message)
char *message;
{
fprintf (stderr,"%s: %s\n",cmd,message);Quit();
}
/* QUIT */
Quit()
{
XUnloadFont(display,font_info->fid);XFreeGC(display,gc);
XCloseDisplay(display);if (In_Menu==1) exit(0);
}