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

📄 _xview.c

📁 数据类型和算法库LEDA 数据类型和算法库LEDA
💻 C
📖 第 1 页 / 共 3 页
字号:
/*******************************************************************************
+
+  LEDA  3.0
+
+
+  _xview.c
+
+
+  Copyright (c) 1992  by  Max-Planck-Institut fuer Informatik
+  Im Stadtwald, 6600 Saarbruecken, FRG     
+  All rights reserved.
+ 
*******************************************************************************/


#include <math.h>
#include <string.h>
#include <stdio.h>
#include <values.h>

#include <X11/X.h>
#include <X11/Xlib.h>

#include <xview/xview.h>
#include <xview/canvas.h>
#include <xview/cms.h>
#include <xview/panel.h>
#include <xview/font.h>
#include <xview/xv_xrect.h>


/*
#include <xview/icon.h>
#include <xview/openmenu.h>
#include <xview/scrollbar.h>
#include <xview/notice.h>
*/


#undef TRACE 

#ifdef TRACE
#define DPRINT(x) printf x
#else
#define DPRINT(x)
#endif


#define XPIX(coord) (int)(xorigin + (coord)*x_draw_scale)
#define YPIX(coord) (int)(yorigin - (coord)*x_draw_scale)

#define XREAL(pix)  ((double)(pix-xorigin)/x_draw_scale)
#define YREAL(pix)  ((double)(yorigin-pix)/x_draw_scale)


extern Notify_error notify_dispatch();



static short icon_image[] = {

#include "leda.icon"

};

static mpr_static(leda_icon_pixrect,64,64,1,icon_image);

static char default_frame_label[128];
static int  repaint_count;

/* pr_tex_dashed */
static unsigned char dotted[2] = { 2,6 };
static unsigned char dashed[2] = { 4,4 };

static char frame_label[80];

static Frame	frame;
static Canvas	canvas;
static Pixwin  *pw = 0;

static Pixmap   buffer_pixmap[8];
static int      buffer_width[8];
static int      buffer_height[8];

static int              pixel_table[8];
static Xv_cmsdata	cms_data;	
static unsigned char	red[8], green[8], blue[8];


static char     text_panel_answer[256];

static Frame   panel_frame;
static Panel   panel;
static Icon    icon;

static void panel_input_notify();
static void init_panel_frame();
static void panel_input_text_notify();
static void init_panel_text_frame();
static void text_panel_menu_proc();


typedef void (*PRD)();
typedef void (*PMA)();

static void x_draw_mouse_default_action(x,y) 
double x,y;
{ /* do nothing */}

static int x_draw_show_coord = 1;

static PRD x_draw_redraw = 0;
static PMA x_draw_mouse_action = x_draw_mouse_default_action;

static int x_draw_done;

static Xv_Font text_font,bold_font,message_font;
static Display *display;
static Drawable drawable;
static int screen;

static GC  gc;

static int xpix_mode_op;

static int draw_init_done = FALSE;

static int xdots,ydots, xorigin,yorigin;  /* pixels          */

static int mouse_key,mouse_xpix, mouse_ypix; 
static int mouse_last_xpix, mouse_last_ypix;
static int mouse_start_xpix, mouse_start_ypix;  /* start for segments  ... */
static int mouse_read_kind;


static double mouse_xreal,mouse_yreal;
static double mouse_last_xreal,mouse_last_yreal;
static double mouse_start_xreal,mouse_start_yreal;

static char* mesg_list[32];
static int   mesg_count;




static int  screen_width, screen_height;
static int Line_style = 0;


static Xv_singlecolor x_draw_color_map[] = {

 {  255,  255,  255 },  /* white  */
 {    0,    0,    0 },  /* black  */
 {  255,    0,    0 },  /* red    */
 {    0,  255,    0 },  /* green  */
 {    0,  100,  255 },  /* blue   */
 {  128,  128,    0 },  /* yellow */
 {  128,    0,  178 },  /* violet */
 {  192,   64,    0 }   /* orange */

};


/* external variables */

int x_draw_window_xpos = 0;
int x_draw_window_ypos = 0;
int x_draw_window_width = 0;
int x_draw_window_height = 0;
   
double x_draw_xmax,x_draw_xmin,x_draw_ymax,x_draw_ymin,x_draw_scale;   

int    x_draw_grid_mode;
int    x_draw_depth;

int    x_draw_line_width, x_draw_node_width, x_draw_line_style, x_draw_color,
       x_draw_text_mode, x_draw_drawing_mode, x_draw_screen_flush;



x_draw_flush() { XFlush(display); }

x_draw_set_show_coord(x)
int x;
{ x_draw_show_coord = x; }

x_draw_set_redraw(f)
PRD f;
{ x_draw_redraw = f; }

x_draw_set_frame_label(message)
char* message;
{ /* strcpy(frame_label,message);  */
  window_set(frame,FRAME_LABEL,message,0);
}


x_draw_reset_frame_label() 
{ /* strcpy(frame_label,default_frame_label); } */
  window_set(frame,FRAME_LABEL,default_frame_label,0);
 }


x_draw_set_gc_read_mouse()
{ XGCValues gc_val;
  gc_val.function = GXxor; 
  gc_val.foreground = pixel_table[1];
  gc_val.line_style = LineSolid;
  gc_val.line_width = 1;
  XChangeGC(display,gc,GCForeground|GCFunction|GCLineStyle|GCLineWidth,&gc_val);
  XFlush(display);
}

x_draw_reset_gc()
{ XGCValues gc_val;
  gc_val.function   = xpix_mode_op; 
  gc_val.foreground = pixel_table[x_draw_color];
  gc_val.line_style = Line_style;
  gc_val.line_width = x_draw_line_width;
  XChangeGC(display,gc,GCForeground|GCFunction|GCLineStyle|GCLineWidth,&gc_val);
  XFlush(display);
}

void x_draw_mouse_segment_action(x,y)
double x,y;
{
  XDrawLine(display,drawable,gc,mouse_start_xpix,mouse_start_ypix,XPIX(x),YPIX(y));

}

void x_draw_mouse_rect_action(x,y)
double x,y;
{ x_draw_rectangle(mouse_start_xreal,mouse_start_yreal,x,y,1,1); } 

void x_draw_mouse_circle_action(x,y)
double x,y;
{ double r = hypot(x - mouse_start_xreal, y - mouse_start_yreal);
  x_draw_circle(mouse_start_xreal, mouse_start_yreal, r, 1, 1);    
 }


x_draw_init_fonts()
{ /* initialize fonts: text_font, bold_font, message_font */

  XGCValues gc_val;

 text_font = (Xv_Font)xv_find(frame, FONT,
                         FONT_FAMILY, FONT_FAMILY_DEFAULT_FIXEDWIDTH,
                         FONT_STYLE,  FONT_STYLE_NORMAL,
                         FONT_SIZE,   14,
                         0);

 bold_font = (Xv_Font)xv_find(frame, FONT,
                         FONT_FAMILY, FONT_FAMILY_DEFAULT_FIXEDWIDTH,
                         FONT_STYLE,  FONT_STYLE_BOLD,
                         FONT_SIZE,   14,
                         0);

 message_font = (Xv_Font)xv_find(frame, FONT,
                         FONT_FAMILY, FONT_FAMILY_LUCIDA_FIXEDWIDTH,
                         FONT_STYLE,  FONT_STYLE_NORMAL,
                         FONT_SIZE,   24,
                         0);

    if (!text_font)    
     { fprintf(stderr,"Cannot open text font\n");
       text_font = (Xv_Font)xv_get(frame,XV_FONT);
      }

    if (!bold_font)    
     { fprintf(stderr,"Cannot open bold font\n");
       bold_font = (Xv_Font)xv_get(frame,XV_FONT);
      }

    if (!message_font) 
     { fprintf(stderr,"Cannot open message font\n");
       message_font = (Xv_Font)xv_get(frame,XV_FONT);
      }


      gc_val.font = (Font)xv_get(text_font,XV_XID);
      XChangeGC(display,gc,GCFont,&gc_val);
}

x_draw_set_font(fname)
char* fname;
{
  XGCValues gc_val;
  Xv_Font f;

  xv_set(frame,FRAME_BUSY,TRUE,NULL);
  f = (Xv_Font)xv_find(frame,FONT,FONT_NAME,fname,NULL);
  xv_set(frame,FRAME_BUSY,FALSE,NULL);

  if (f) 
  { text_font = f;
    gc_val.font = (Font)xv_get(text_font,XV_XID);
    XChangeGC(display,gc,GCFont,&gc_val);
   }

}


int x_draw_set_color(col)
int col;
{ int save = x_draw_color;
  x_draw_color = (x_draw_depth==1 && col > 1) ? 1 : col;
  XSetForeground(display,gc,pixel_table[x_draw_color]);
  return save;
 }


int x_draw_set_line_width(w)
int w;
{ XGCValues gc_val;
  int save = x_draw_line_width;
  x_draw_line_width = w;
  gc_val.line_width = x_draw_line_width;
  XChangeGC(display,gc,GCLineWidth,&gc_val);
  return save;
}


int x_draw_set_line_style(s)
int s;
{ XGCValues gc_val;

  int save = x_draw_line_style;

  x_draw_line_style = s;
 
   switch (x_draw_line_style)  {

   case 0 : Line_style = LineSolid;
            break;

   case 1 : Line_style = LineOnOffDash;
            XSetDashes(display,gc,0,dashed,2);
            break;

   case 2 : Line_style = LineOnOffDash;
            XSetDashes(display,gc,0,dotted,2);
            break;

   default: Line_style = LineSolid;
            break;

   }

  gc_val.line_style = Line_style;
  XChangeGC(display,gc,GCLineStyle,&gc_val);
  return save;
}

int x_draw_set_node_width(w)
int w;
{ int save = x_draw_node_width;
  x_draw_node_width = w;
  return save;
 }

int x_draw_set_text_mode(m)
int m;
{ int save = x_draw_text_mode;
  x_draw_text_mode = m;
  return save;
 }

int x_draw_set_mode(m)
int m;
{ 
  XGCValues gc_val;

  int save = x_draw_drawing_mode;

  x_draw_drawing_mode = m;


  DPRINT(("x_draw_set_mode %d\n",x_draw_drawing_mode));

   switch (x_draw_drawing_mode)  {

   case 0 : xpix_mode_op = GXcopy;
            break;

   case 1 : xpix_mode_op = GXxor;
            break;

   case 2 : xpix_mode_op = GXor;
            break;

   default: break;

   }

  gc_val.function = xpix_mode_op; 
  XChangeGC(display,gc,GCFunction,&gc_val);

  return save;
}


static void x_draw_event_proc(canvas,event)
Canvas canvas;
Event* event;
{ 
  char s[32];


  int k = event_id(event); 

/*
printf("event = %d\n",k);
fflush(stdout);
*/

  switch (k) {

  case ACTION_SELECT:
  case MS_LEFT:   if (event_is_up(event)) break;
                  mouse_key = 1;

                  if (event_ctrl_is_down(event))  mouse_key +=3;
                  if (event_shift_is_down(event)) mouse_key = -mouse_key;
                  break;

  case ACTION_ADJUST:
  case MS_MIDDLE: if (event_is_up(event)) break;
                  mouse_key = 2;
                  if (event_ctrl_is_down(event))  mouse_key +=3;
                  if (event_shift_is_down(event)) mouse_key = -mouse_key;
                  break;

  case ACTION_MENU:
  case MS_RIGHT:  if (event_is_up(event)) break;
                  mouse_key = 3;
                  if (event_ctrl_is_down(event))  mouse_key +=3;
                  if (event_shift_is_down(event)) mouse_key = -mouse_key;
                  break;


  case LOC_MOVE: 
  case LOC_WINENTER : 
  case LOC_WINEXIT : 
           mouse_xpix = event_x(event);
           mouse_ypix = event_y(event);

           if (x_draw_grid_mode)  x_draw_cursor();
         
           mouse_xreal =  x_draw_xmin+((double)mouse_xpix)/x_draw_scale;
           mouse_yreal =  x_draw_ymax-((double)mouse_ypix)/x_draw_scale;
         
         if (x_draw_grid_mode) 
         { 
           mouse_xreal = x_draw_grid_mode * (int)(mouse_xreal/x_draw_grid_mode + ((mouse_xreal > 0) ? 0.5 : -0.5));

           mouse_yreal = x_draw_grid_mode * (int)(mouse_yreal/x_draw_grid_mode + ((mouse_yreal > 0) ? 0.5 : -0.5));

           mouse_xpix  = XPIX(mouse_xreal);
           mouse_ypix  = YPIX(mouse_yreal);
         
           x_draw_cursor();
         }
       
         sprintf(s,"%8.2f %8.2f", mouse_xreal,mouse_yreal);

         if (x_draw_show_coord)
         XDrawImageString(display,drawable,gc,xdots-160,13,s,strlen(s));
       
       
         if (x_draw_mouse_action) /* user defined action */
         { x_draw_mouse_action(mouse_last_xreal,mouse_last_yreal);
           x_draw_mouse_action(mouse_xreal,mouse_yreal);
          }
       
         mouse_last_xpix = mouse_xpix;
         mouse_last_ypix = mouse_ypix;
       
         mouse_last_xreal = mouse_xreal;
         mouse_last_yreal = mouse_yreal;
       
         break;

     } /* switch */

/*
printf("event_if = %d  key = %d  xreal = %f yreal = %f\n",k,mouse_key, mouse_xreal,mouse_yreal);
fflush(stdout);
*/


}

int x_read_mouse(kind, xstart,ystart,x,y)
int    kind;   /* 0: point, 1: segment, 2:rectangle, 3: circle */
double xstart;
double ystart;
double *x;
double *y;
{ 
  PMA f;
  int key;
  
  switch(kind) {

  case 0 : f = x_draw_mouse_default_action;
          break;

  case 1:  f = x_draw_mouse_segment_action;
           break;

  case 2:  f = x_draw_mouse_rect_action;
           break;

  case 3:  f = x_draw_mouse_circle_action;
           break;

  default: f = x_draw_mouse_default_action;
           break;

  }

  key = x_read_mouse_action(f,xstart,ystart,x,y);

  return key;

}


int x_draw_get_button()
{ x_draw_mouse_action = x_draw_mouse_default_action;
  mouse_key = 0;
  notify_dispatch();
  return mouse_key;
 }


int x_read_mouse_action(action, xstart,ystart,x,y)
PMA   action;
double xstart;
double ystart;
double *x;
double *y;
{ 
  DPRINT(("x_read_mouse_action\n"));
  x_draw_mouse_action = action;

  x_draw_set_gc_read_mouse();


  mouse_key = 0;

  mouse_start_xreal = xstart;
  mouse_start_yreal = ystart;

  mouse_start_xpix = XPIX(xstart);
  mouse_start_ypix = YPIX(ystart);

  if (x_draw_grid_mode) x_draw_cursor();

  if (x_draw_mouse_action)  /* user defined action */
  { x_draw_mouse_action(mouse_last_xreal,mouse_last_yreal);
   }


  /* notify-loop */

  while (!mouse_key && !x_draw_done)  notify_dispatch();

  

  if (x_draw_mouse_action)  /* user defined action */
  { x_draw_mouse_action(mouse_xreal,mouse_yreal);
   }

  if (x_draw_grid_mode) x_draw_cursor();

  *x = mouse_xreal;
  *y = mouse_yreal;

  x_draw_reset_gc();

  if (x_draw_done) return 0;

  return mouse_key;
}
  

static void x_draw_canvas_repaint_proc(canvas,paint_window,dpy,xwin,xrects)
Canvas canvas;
Xv_Window paint_window;
Display *dpy;
Window  xwin;
Xv_xrectlist *xrects;
{/* 
  printf("REPAINT repaint_count = %d\n",repaint_count);
  fflush(stdout);
  */
  repaint_count++;
 }

static void x_draw_canvas_resize_proc(canvas,width,height)
Canvas canvas;
int    width;
int    height;
{/*
  printf("RESIZE canvas = %d  width = %d  height =%d\n",canvas,width,height);
  fflush(stdout);
  */
  if (draw_init_done)
      x_draw_init(x_draw_xmin,x_draw_xmax,x_draw_ymin,x_draw_grid_mode); 
}




Notify_value x_draw_notice_destroy(frame, status)
Frame frame;
Destroy_status status;
{
/*
  printf("x_draw_notice_destroy called\n");
*/

  if (status != DESTROY_CHECKING)
  { x_draw_done = 1;
    exit(0);
   }

  return (notify_next_destroy_func(frame,status));
}


/*
static error_proc(object,avlist)
Xv_object    object;
Attr_avlist  avlist;
{ char  buf[32];
  printf("%s\n", xv_error_format(object,avlist));
  printf("Dump core ? ");
  fflush(stdout);
  if (gets(buf) && (buf[0] == 'y')) abort();
  return XV_OK;
 }
*/
  


x_draw_init_window(w_width,w_height,w_xpos,w_ypos,frame_label)
int w_width,
    w_height,
    w_xpos,
    w_ypos;
char* frame_label;
{ 
  int       i, *xpixels;
  Xv_Screen xv_screen;
  XGCValues gc_val;
  Rect      *r;


  DPRINT(("x_draw_init_window\n"));
  if (pw!=0) 
  { fprintf(stderr,"warning: second initialization of window ignored.\n");
    return;
   } 

  strcpy(default_frame_label,frame_label);

/*
  xv_init(XV_ERROR_PROC, error_proc,0);
*/


⌨️ 快捷键说明

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