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 >
C/C++ Source or Header  |  1992-07-16  |  8KB  |  251 lines

  1. /*                         ===  NM-Start.c  ===
  2.  *
  3.  * void DoExposeEvent(report)
  4.  * void DoConfigureEvent(report,width,height)
  5.  * void DoEvents(width,height)
  6.  *
  7.  * void main(argc,argv)
  8.  * void get_GC(win,gc,font_info)
  9.  * void load_font(font_info,fontname)
  10.  * void PrintMsgText(win,gc,font_info,text_string)
  11.  * void FatalError (message)
  12.  * void Quit()
  13.  *
  14.  */
  15.  
  16. #include "NM.h" 
  17. #include "icon_bitmap"
  18.  
  19. /* since display and screen are used throughout Xlib calls we define up front */
  20.  
  21. Display *display;
  22. int      screen;
  23. Window win;
  24. GC gc;
  25. XFontStruct  *font_info;
  26. XWindowAttributes attribs;
  27. char tmpstr[128],*cmd;
  28. XColor mycolor,exactcolor;
  29. Colormap theCmap;
  30.  
  31.  
  32. /* This is an example of MANUAL Main Event Loop in X windows, and this
  33.  * version uses ONLY the lowest level Xlib calls. */
  34.  
  35.  
  36. /*  This may not do what you want because it processes
  37.  * Expose events whenever you do ClearArea with the lastargument set!!
  38.  */
  39.  
  40. void DoExposeEvent(report)
  41.   XEvent report;
  42. {
  43. /* 
  44.   int count;
  45.   XEvent ExposeReport;
  46.   if (report.xexpose.window==win)
  47.    sprintf(tmpstr,"Graphics Window:(x,y)(%d,%d);(w,h) (%d,%d)",report.xexpose.x,
  48.            report.xexpose.y,report.xexpose.width, report.xexpose.height);
  49.   PrintMsgText(win,gc,font_info,tmpstr);
  50.   XGetWindowAttributes(display,win,&attribs);
  51.   printf("\n report.type %d  ",report.type);
  52.   while (XCheckTypedEvent(display,Expose,&ExposeReport))  
  53.     {
  54.       printf("\nLOOP  report.type %d, count = %d.  ",ExposeReport.type,
  55.           ExposeReport.xexpose.count);
  56.     }
  57. */
  58. }
  59.  
  60. void DoConfigureEvent(report,width,height)
  61.   XEvent report;
  62.   unsigned int *width,*height;
  63. {
  64.   if ((report.xconfigure.window==win) && ((report.xconfigure.width != *width)
  65.        || (report.xconfigure.height != *height)))
  66.      /* window has been resized, change width and height */
  67.     {
  68.       *width=report.xconfigure.width;*height=report.xconfigure.height;
  69.     }
  70.   XGetWindowAttributes(display,win,&attribs);
  71. }
  72.  
  73. void DoEvents(width,height)
  74.   unsigned int width,height;
  75. {
  76.   XEvent report;
  77.   int button,mouse_x,mouse_y,old_mouse_x,old_mouse_y,dx,dy;
  78.  
  79.   old_mouse_x=width/2;old_mouse_y=height/2;
  80.   mouse_x=old_mouse_x;mouse_y=old_mouse_y;dx=0;dy=0;
  81.   while(1)
  82.     {  
  83.       XNextEvent(display, &report);
  84.       switch(report.type) 
  85.     {
  86.           case Expose:DoExposeEvent(report);
  87.         break;
  88.       case ConfigureNotify:DoConfigureEvent(report,&width,&height);
  89.                            old_mouse_x=width/2;old_mouse_y=height/2;
  90.                            mouse_x=old_mouse_x;mouse_y=old_mouse_y;
  91.         break;
  92.           case MotionNotify:mouse_x=report.xmotion.x;mouse_y=report.xmotion.y;
  93.                         dx=mouse_x-old_mouse_x;dy=mouse_y-old_mouse_y;
  94.                         DoMotionEvent(report,mouse_x,mouse_y,dx,dy);
  95.         break;
  96.           case ButtonPress:button=report.xbutton.button;
  97.                            mouse_x=report.xbutton.x;mouse_y=report.xbutton.y;
  98.                        dx=mouse_x-old_mouse_x;dy=mouse_y-old_mouse_y;
  99.                        DoButtonEvent(report,button,mouse_x,mouse_y,1,1,1);
  100.         break;
  101.      case ButtonRelease:button=report.xbutton.button;
  102.                         mouse_x=report.xbutton.x;mouse_y=report.xbutton.y;
  103.                         dx=mouse_x-old_mouse_x;dy=mouse_y-old_mouse_y;
  104.                         DoButtonReleaseEvent(report,button,mouse_x,mouse_y,
  105.                          dx,dy);
  106.         break;
  107.       case KeyPress:DoKeyPressEvent(report);
  108.         break;
  109.       case KeyRelease:DoKeyReleaseEvent(report);
  110.         break;
  111.           default:/* all events selected by StructureNotifyMask except
  112.                  ConfigureNotify are thrown away here, since nothing
  113.                  is done with them  */
  114.         break;
  115.     }  /* end switch */ 
  116.  
  117.     /* Reset the state  */
  118.       old_mouse_x=mouse_x;old_mouse_y=mouse_y;
  119.     
  120.     }  /* end while */
  121. }
  122.  
  123. void main(argc,argv)
  124.   int argc;
  125.   char **argv;
  126. {  
  127.    unsigned int width, height;  /* window size */
  128.    int  x=0, y=0;               /* window position */
  129.    unsigned int border_width=4; 
  130.    char *window_name=" NM 2.0  Interaction Window",*icon_name=" NM 2.0 ";
  131.    Pixmap icon_pixmap;
  132.    XSizeHints size_hints;
  133.    char *display_name = NULL;
  134.    int first_time;
  135.    unsigned long fg,bg;
  136.  
  137.    cmd=argv[0];first_time=1;
  138.  
  139.    printf("\n\n       *** Welcome to NM 2.0 ***\n\n");
  140.  
  141. /* connect to X server */
  142.    if((display=XOpenDisplay(display_name))==NULL)
  143.      { 
  144.        (void)fprintf(stderr,"%s: cannot connect to X server %s\n",cmd, 
  145.              XDisplayName(display_name));
  146.        exit( -1 ); 
  147.      }
  148. /* get screen size from display structure macro */
  149.    screen=DefaultScreen(display);
  150.    width=DisplayWidth(display,screen);height=DisplayHeight(display,screen);
  151.    bg = BlackPixel(display,screen);fg = WhitePixel(display,screen);
  152. /* initialize size hint property for the window manager */
  153.    size_hints.flags=PPosition | PSize | PMinSize;
  154.    size_hints.x=x;size_hints.y=y;size_hints.width=width;
  155.    size_hints.height=height;size_hints.min_width=250;size_hints.min_height=250;
  156.  
  157. /* create an opaque window */   
  158.  
  159.    win=XCreateSimpleWindow(display,RootWindow(display,screen),0,0,width,height,
  160.                border_width,WhitePixel(display,screen),
  161.                            BlackPixel(display,screen));
  162.    icon_pixmap=XCreateBitmapFromData(display,win,icon_bitmap_bits,
  163.                            icon_bitmap_width, icon_bitmap_height);
  164. /* set properties for window manager (before mapping) */
  165.    XSetStandardProperties(display,win,window_name,icon_name,icon_pixmap,argv,
  166.               argc,&size_hints);
  167.    load_font(&font_info,"12x24");
  168. /* create GC for text and drawing and display window */
  169.    get_GC(win,&gc,font_info);
  170.    XSetForeground(display,gc,fg);XSetBackground(display,gc,bg);
  171.  
  172. /* select event types wanted */
  173.    XSelectInput(display,win,ExposureMask|KeyPressMask|KeyReleaseMask|
  174.         ButtonPressMask|ButtonMotionMask|ButtonReleaseMask|
  175.         PointerMotionMask|StructureNotifyMask );
  176.    XMapWindow(display,win);    /* Display the window */
  177.    theCmap=DefaultColormap(display, screen);
  178.    /* Wait for events, etc. */
  179.    XGetWindowAttributes(display,win,&attribs);
  180.    /* ***Initialize Coordinate Frame and do other initializations*** */
  181.    SetUpCoordSystem(width,height);DoEvents(width,height);
  182. }
  183.  
  184. get_GC(win,gc,font_info)
  185.   Window win; GC *gc;
  186.   XFontStruct *font_info;
  187. {  
  188.   unsigned long valuemask=0; /*ignore XGCvalues and use defaults */
  189.   XGCValues values;
  190.   unsigned int line_width=2;
  191.   int line_style=LineSolid,dash_offset=0,list_length=2;
  192.   static char dash_list[]={12,24};
  193. /* Create default graphics context:
  194.  * ****  If "plane_mask = bg | fg", color assignments
  195.  * won't work properly.
  196.  */
  197.    values.plane_mask = 0xffffff;
  198.    *gc = XCreateGC(display,win,valuemask,&values);
  199. /* specify font */
  200.    XSetFont(display,*gc,font_info->fid);
  201. /* specify black foreground since default may be white on white */
  202.    XSetForeground(display,*gc,BlackPixel(display,screen));
  203. }
  204.  
  205. load_font(font_info,fontname)
  206.   XFontStruct **font_info;
  207.   char *fontname;
  208.   if ((*font_info = XLoadQueryFont(display,fontname))==NULL)
  209.     { 
  210.       (void) fprintf(stderr,"Basic: Cannot open font \n");exit(-1); 
  211.     }
  212. }
  213.  
  214. PrintMsgText(win,gc,font_info,text_string)
  215.   Window win;
  216.   GC gc;
  217.   XFontStruct *font_info;
  218.   char *text_string;
  219. {
  220.   unsigned int wid,y_offset,x_offset,strL;
  221.   unsigned long fcol;
  222.  
  223.   y_offset=font_info->max_bounds.ascent+7;x_offset=2;strL=strlen(text_string);
  224.  
  225. /* output test, centered on each line */
  226.     /* fcol=BlackPixel(theDisp,theScreen); 
  227.      * XSetForeground(theDisp,theGC,fcol);  */
  228.  /* *** True below generates Expose! (which can be undesirable) */
  229.  /* wid= 500;  wid=XTextWidth(font_info,text_string,strL); */
  230.  
  231.   wid=attribs.width;XClearArea(display,win,x_offset,0,wid,18,False); 
  232.   XDrawString(display,win,gc,x_offset,y_offset,text_string,strL);
  233. }
  234.  
  235. /* FATAL ERROR */
  236. FatalError (message)
  237.   char    *message;
  238. {
  239.   fprintf (stderr,"%s: %s\n",cmd,message);Quit();
  240. }
  241.  
  242. /* QUIT */
  243.  
  244. Quit()
  245. {
  246.   XUnloadFont(display,font_info->fid);XFreeGC(display,gc);
  247.   XCloseDisplay(display);if (In_Menu==1) exit(0);
  248. }
  249.  
  250.