Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

mcd.c

Go to the documentation of this file.
00001 /* mcd.c                       Interface for McDraw
00002  * Biao Lu                     biao@eagle.drexel.edu
00003  *
00004  * Modifications by Steve McMillan, 8/96.
00005  *
00006  * Note old K&R-style arguments in functions!
00007  */
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 
00012 #define MAXPOINTS  16000
00013 #define MAXCOLOR   16
00014 
00015 #define DEFAULT_CM      0
00016 #define FILE_CM         1
00017 
00018 typedef struct _mcdwin {
00019     unsigned long  win;
00020     int            id;
00021     char           name[50];
00022     int            linewidth;
00023     unsigned long  fg_color;
00024     unsigned long  bg_color;
00025     int            colormap;
00026     struct _mcdwin *next;
00027 } mcdwin;
00028 
00029 static unsigned long   win;
00030 static mcdwin  *window = NULL;
00031 static int buffer_flag = 1;
00032 static float penx = 1.0,peny = 1.0;
00033 static float x[MAXPOINTS], y[MAXPOINTS];
00034 static unsigned long npoints = 0;
00035 static int   style = -1;  /* 0 for points, 1 for lines */
00036 static float aspect_ratio =  1.0;
00037 static unsigned int width, height;
00038 
00039 /* Only 16 colors available now */
00040 
00041 static unsigned long color[MAXCOLOR];
00042 static char         *colorname[] = { 
00043                          "white","black","blue","purple","violet","magenta",
00044                          "lightblue","gray","cyan","green","limegreen",
00045                          "yellow","orange","brown","red","pink"};
00046 
00047 extern unsigned long  lux_get_bgcolor();
00048 extern unsigned long  lux_get_fgcolor();
00049 extern unsigned long  lux_rgb_pixel();
00050 extern unsigned long  lux_lookup_color();
00051 
00052 #define SCREEN_SIZE_FRACTION 0.4
00053 #define SCREEN_XPOS_FRACTION 0.3
00054 #define SCREEN_YPOS_FRACTION 0.05
00055 
00056 mcdxinit(aspect, nxpix, nypix, ncolor, ierr)
00057 float *aspect;
00058 int *nxpix, *nypix, *ncolor;
00059 int *ierr;
00060 {
00061     static int status = 1;
00062     static int n_color;
00063 
00064     lux_use_mcdraw();
00065 
00066     aspect_ratio = *aspect;
00067 
00068     if (lux_display(&width,&height)) {*ierr = 2;return 2;}
00069 
00070     mcdxopenwin();
00071     
00072     if (status) {status = 0; n_color = mcdxcolors();}
00073 
00074     *ncolor = n_color;
00075 
00076     *nxpix = width*SCREEN_SIZE_FRACTION;
00077     *nypix = (int)(*nxpix * aspect_ratio);
00078 
00079     if (win == 0) *ierr = 2;
00080     else *ierr = 0;
00081 }
00082 
00083 mcdxcolors()
00084 {
00085     int size,im_colormap,i;
00086 
00087     lux_get_colorinfo(&size,&im_colormap);
00088 /*    fprintf(stderr,"Colormap size %d ,  %d\n", size, im_colormap);*/
00089     if (im_colormap) {
00090         color[0] = lux_rgb_pixel(win,1.0,1.0,1.0); 
00091         color[1] = lux_rgb_pixel(win,0.0,0.0,0.0); 
00092         if (size == 2) return(2);
00093         if (size == 256) {
00094             float v = 256.0;
00095             for(i=2;i<MAXCOLOR;i++)
00096               color[i]=(long)(v=v/1.4);
00097             return(MAXCOLOR);
00098         }
00099         else {
00100             for(i=2;i<size;i++) 
00101                 color[i]=(long)(i-1);
00102             return(size);
00103         } 
00104     }
00105     else {
00106         if (size >= MAXCOLOR) {
00107             for(i=0;i<MAXCOLOR;i++) 
00108               color[i] = lux_lookup_color(win,colorname[i]);
00109             return(MAXCOLOR);
00110         }
00111         else {
00112             for(i=0;i<size;i++) 
00113               color[i] = lux_lookup_color(win,colorname[i]);
00114             return(size);
00115         }
00116     }
00117 }
00118 
00119 mcdxopenwin()
00120 {
00121     mcdwin *w;
00122     static int id_number = 0;
00123     static int xwin = -1, ywin = -1;
00124     static int inc = 25;
00125 
00126     int wwin, hwin;
00127 
00128     w = (mcdwin *)malloc(sizeof(mcdwin));
00129     if (window == NULL) {
00130         window = w;
00131         window->next = window;
00132     }
00133     else {
00134         w->next = window->next;
00135         window->next = w;
00136         window = w;
00137     }
00138 
00139     if (xwin == -1) {
00140         xwin = width*SCREEN_XPOS_FRACTION;
00141         ywin = height*SCREEN_YPOS_FRACTION;
00142     }
00143 
00144     wwin = width*SCREEN_SIZE_FRACTION;
00145     hwin = (int)(wwin*aspect_ratio);
00146 
00147     xwin += inc;
00148     ywin += inc;
00149     if (xwin + wwin > width || ywin + hwin > height) {
00150         xwin = width*SCREEN_XPOS_FRACTION;
00151         ywin = height*SCREEN_YPOS_FRACTION;
00152     }
00153     
00154     window->id = id_number;
00155     id_number++;
00156     sprintf(window->name,"McDraw  #%d",window->id);
00157     window->win = lux_openwin(xwin,ywin,wwin,hwin);
00158     lux_set_window_name(window->win,window->name);
00159     lux_setup_region(window->win, 0.0,0.0,10.0,10.0);
00160     lux_setup_axis(window->win, 0.0,10.0,0.0,10.0*aspect_ratio); 
00161     window->fg_color = lux_rgb_pixel(window->win,0.0,0.0,0.0);
00162     window->bg_color = lux_rgb_pixel(window->win,1.0,1.0,1.0);
00163     window->linewidth = 0;
00164     window->colormap = DEFAULT_CM;
00165 
00166     win = window->win;
00167 }
00168 
00169 mcdxsetwin(id)
00170 int *id;
00171 {
00172     mcdwin *w;
00173     
00174     w = window;
00175     do {
00176         if (window->id == *id) {
00177             win = window->win; 
00178             lux_uniconify_window(win);
00179             lux_raise_window(win);
00180             return *id;
00181         }
00182         window = window->next;
00183     } while(window != w);
00184     fprintf(stderr,"Window %d not found\n", *id);
00185     return -1;
00186 }
00187 
00188 mcdxsetwin_no_raise(id)
00189 int *id;
00190 {
00191     mcdwin *w;
00192     
00193     w = window;
00194     do {
00195         if (window->id == *id) {
00196             win = window->win; 
00197             return *id;
00198         }
00199         window = window->next;
00200     } while(window != w);
00201     fprintf(stderr,"Window %d not found\n", *id);
00202     return -1;
00203 }
00204 
00205 mcdxcurrwin()
00206 {
00207     if (window == NULL) return -1;
00208     return window->id;
00209 }
00210 
00211 mcdxnopen()
00212 {
00213     mcdwin *w;
00214     int n_win;
00215 
00216     if (window == NULL) return 0;
00217 
00218     w = window;
00219     n_win = 0;
00220 
00221     do {
00222         n_win++;
00223         window = window->next;
00224     } while(window != w);
00225 
00226     return n_win;
00227 }
00228 
00229 mcdxkillwin(id)
00230 int *id;
00231 {
00232     mcdwin *w;
00233     int    old_id;
00234 
00235     old_id = window->id;
00236     if (mcdxsetwin_no_raise(id) < 0) return;
00237     lux_freewin(win);
00238 
00239     if (window->next == window) {
00240         free(window);
00241         window = NULL;
00242     }
00243     else {
00244         w = window;
00245         while(window->next != w) window = window->next;
00246         window->next = w->next;
00247         win = window->win;
00248         free(w);
00249         if (old_id != *id) mcdxsetwin(&old_id);
00250         else {
00251           mcdxsetwin(id);
00252           fprintf(stderr,"Current window id = %d\n",window->id);
00253         }
00254     }
00255 
00256 }
00257 
00258 mcdxflush()
00259 {
00260   if (style == -1) return;
00261   if (style == 0) {
00262     lux_draw_pointsf(win,x,y,npoints,0);
00263   }
00264   else if (style == 1) {
00265     lux_draw_segmentsf(win,x,y,x,y,npoints/2,0);
00266   }
00267   npoints = 0;
00268 }
00269 
00270 mcdxbuffer()
00271 {
00272   buffer_flag = 1;
00273 }
00274 
00275 mcdxnobuffer()
00276 {
00277   buffer_flag = 0;
00278   mcdxflush();
00279 }
00280 
00281 mcdxlinew(iw)
00282 unsigned int *iw;
00283 {
00284   if (npoints) mcdxflush();
00285   lux_set_linewidth(win,*iw);
00286   window->linewidth = *iw;
00287 }
00288 
00289 mcdxcolor(ic)
00290 int  *ic;
00291 {
00292   if (npoints) mcdxflush();
00293 
00294   if (window->colormap == DEFAULT_CM) {
00295       lux_set_color(win,color[*ic%MAXCOLOR]);
00296       window->fg_color = color[*ic%MAXCOLOR];
00297   } else {
00298       lux_set_color(win,*ic);
00299       window->fg_color = *ic;
00300   }
00301 }
00302 
00303 mcdxbackg(ic)
00304 unsigned long *ic;
00305 {
00306   if (npoints) mcdxflush();
00307   /* Do nothing right now */
00308 }
00309 
00310 mcdxmove(r,s)
00311 float *r,*s;
00312 {
00313   penx = *r;
00314   peny = *s;
00315 }
00316 
00317 mcdxdraw(r,s)
00318 float *r,*s;
00319 {
00320   if (style != 1) mcdxflush();
00321   style = 1;
00322   if (buffer_flag) {
00323     if (npoints < MAXPOINTS) {
00324       x[npoints] = penx;
00325       y[npoints] = peny;
00326       npoints++;
00327       x[npoints] = *r;
00328       y[npoints] = *s;
00329       npoints++;
00330     }
00331     else {
00332       mcdxflush();
00333       x[npoints] = penx;
00334       y[npoints] = peny;
00335       npoints++;
00336       x[npoints] = *r;
00337       y[npoints] = *s;
00338       npoints++;
00339     }
00340   }
00341   else
00342     lux_draw_linef(win,penx,peny,*r,*s);
00343   penx = *r;
00344   peny = *s;
00345 }
00346 
00347 mcdxpoint(r,s)
00348 float *r,*s;
00349 {
00350   if (style != 0) mcdxflush();
00351   style = 0;
00352   if (buffer_flag) {
00353     if (npoints < MAXPOINTS) {
00354       x[npoints] = *r;
00355       y[npoints] = *s;
00356       npoints++;
00357     }
00358     else {
00359       mcdxflush();
00360       x[npoints] = *r;
00361       y[npoints] = *s;    
00362       npoints++;
00363     }
00364   }
00365   else
00366     lux_draw_pointf(win,*r,*s);
00367   penx = *r;
00368   peny = *s;  
00369 }
00370 
00371 #define REASONABLE_WAIT_TIME 50000      /* Unit = microseconds. */
00372 
00373 mcdxgin(r,s)
00374 float *r, *s;
00375 {
00376     int i;
00377     char strng[1024];
00378 
00379     if (npoints) mcdxflush();
00380 
00381     set_timeout();
00382     i = get_wait_time();
00383     set_wait_time(REASONABLE_WAIT_TIME);
00384 
00385     get_mouse_position(win,r,s);
00386 
00387     set_wait_time(i);
00388     reset_term(strng);
00389 }
00390 
00391 mcdxpolyf(r,s,n,ic)
00392 float *r,*s;
00393 int *n;
00394 unsigned long *ic;
00395 {
00396   unsigned long fg;
00397 
00398   if (npoints) mcdxflush();
00399   fg = lux_get_fgcolor(win);
00400   mcdxcolor(ic);
00401   lux_fill_polygonf(win,r,s,*n,0);
00402   lux_set_color(win, fg);
00403   penx = r[*n-1];
00404   peny = s[*n-1];
00405 }
00406 
00407 mcdxpolyc(r,s,n)
00408 float *r,*s;
00409 int *n;
00410 {
00411   unsigned long fg;
00412 
00413   if (npoints) mcdxflush();
00414   fg = lux_get_fgcolor(win);
00415   lux_set_color(lux_get_bgcolor(win));
00416   lux_draw_linesf(win,r,s,r,s,*n,0);
00417   lux_set_color(win, fg);
00418   penx = r[*n-1];
00419   peny = s[*n-1];
00420 }
00421 
00422 mcdxtext(h,a,string)
00423 float *h,*a;
00424 char *string;
00425 {
00426     if (npoints) mcdxflush();
00427     lux_draw_string(win,penx,peny,*h,string,0);
00428 }
00429 
00430 mcdxclear()
00431 {
00432     if (npoints) mcdxflush();
00433     lux_reset_window(win);
00434     lux_setup_region(win, 0.0,0.0,10.0,10.0);
00435     lux_setup_axis(win, 0.0,10.0,0.0,10.0*aspect_ratio); 
00436     lux_set_color(win, window->fg_color);
00437     lux_set_linewidth(win, window->linewidth);
00438 }
00439 
00440 mcdxreset()
00441 {
00442     if (npoints) mcdxflush();
00443     lux_reset_window(win);
00444     lux_setup_region(win, 0.0,0.0,10.0,10.0);
00445     lux_setup_axis(win, 0.0,10.0,0.0,10.0*aspect_ratio); 
00446 }
00447 
00448 mcdxquit()
00449 {
00450   lux_quick_exit();
00451 }
00452 
00453 mcdxidle()
00454 {
00455     float r,s;
00456     if (npoints) mcdxflush();
00457     mcdxgin(&r,&s);
00458 }
00459 
00460 mcdxrdline(strng)
00461 char *strng;
00462 {
00463 
00464     if (npoints) mcdxflush();
00465 
00466     set_timeout();
00467 
00468     lux_getevent();
00469 
00470     strng[0] = '\0';
00471     reset_term(strng);
00472 }
00473 
00474 mcdxiconifywin(id)
00475 int *id;
00476 {
00477   int old_id;
00478 
00479   if (window == NULL) return(0);
00480   old_id = window->id;
00481   if (mcdxsetwin_no_raise(id)<0) return 0;
00482   
00483   lux_iconify_window(win);
00484   mcdxsetwin_no_raise(&old_id);
00485 }
00486 
00487 mcdxuniconifywin(id)
00488 int *id;
00489 {
00490   int old_id;
00491 
00492   if (window == NULL) return(0);
00493   old_id = window->id;
00494   if (mcdxsetwin_no_raise(id)<0) return 0;
00495   
00496   lux_uniconify_window(win);
00497   mcdxsetwin(&old_id);
00498 }
00499 
00500 mcdxiconifyall()
00501 {
00502   mcdwin *w;
00503 
00504   if (window == NULL) return(0);
00505 
00506   w = window;
00507   do {
00508     mcdxiconifywin(&(w->id));
00509     w = w->next;
00510   }while(w != window);
00511 }
00512 
00513 mcdxuniconifyall()
00514 {
00515   mcdwin *w;
00516 
00517   if (window == NULL) return(0);
00518 
00519   w = window;
00520   do {
00521     mcdxuniconifywin(&(w->id));
00522     w = w->next;
00523   }while(w != window);
00524 }
00525 
00526 mcdxflushio()
00527 {
00528     clear_buffer();
00529     lux_clear_keyboard_buffer();
00530 }
00531 
00532 mcdxsetwincolormap(filename, nc, ier)
00533 char* filename;
00534 int* nc;
00535 int* ier;
00536 {
00537     int n;
00538     if ( (n = lux_set_window_colormap(win, filename)) > 0) {
00539         *nc = n;
00540         window->colormap = FILE_CM;
00541         *ier = 0;
00542     } else
00543         *ier = 1;
00544 }
00545 
00546 #include <time.h>
00547 
00548 lux_pause(time)
00549 int time;
00550 {
00551     /* Wait for the specified number of microseconds, if possible. */
00552 
00553 #ifdef HAS_USLEEP
00554     usleep(time);
00555 #endif
00556 
00557 #ifdef HAS_NANOSLEEP
00558     struct timespec t;
00559     time_t i;
00560     i = time/1000000;
00561     t.tv_sec = i;
00562     t.tv_nsec = 1000*(time - 1000000*i);
00563     nanosleep(&t, &t);
00564 #endif
00565 }
00566 
00567 /* Alternate names for use by name-mangling compilers: */
00568 
00569 mcdxinit_(aspect, nxpix, nypix, ncolor, ierr)
00570 float *aspect;
00571 int *nxpix, *nypix, *ncolor;
00572 int *ierr;
00573 {
00574   mcdxinit(aspect, nxpix, nypix, ncolor, ierr);
00575 }
00576 
00577 mcdxlinew_(iw)
00578 unsigned int *iw;
00579 {
00580   mcdxlinew(iw);
00581 }
00582 
00583 mcdxcolor_(ic)
00584 int *ic;
00585 {
00586   mcdxcolor(ic);
00587 }
00588 
00589 mcdxbackg_(ic)
00590 unsigned long *ic;
00591 {
00592   mcdxbackg(ic);
00593 }
00594 
00595 mcdxmove_(r,s)
00596 float *r,*s;
00597 {
00598   mcdxmove(r,s);
00599 }
00600 
00601 mcdxdraw_(r,s)
00602 float *r,*s;
00603 {
00604   mcdxdraw(r,s);
00605 }
00606 
00607 mcdxpoint_(r,s)
00608 float *r,*s;
00609 {
00610   mcdxpoint(r,s);
00611 }
00612 
00613 mcdxgin_(r,s)
00614 float *r, *s;
00615 {
00616   mcdxgin(r,s);
00617 }
00618 
00619 mcdxpolyf_(r,s,n,ic)
00620 float *r,*s;
00621 int *n;
00622 unsigned long *ic;
00623 {
00624   mcdxpolyf(r,s,n,ic);
00625 }
00626 
00627 mcdxpolyc_(r,s,n)
00628 float *r,*s;
00629 int *n;
00630 {
00631   mcdxpolyc(r,s,n);
00632 }
00633 
00634 mcdxtext_(h,a,string)
00635 float *h,*a;
00636 char *string;
00637 {
00638     mcdxtext(h,a,string);
00639 }
00640 
00641 mcdxclear_()
00642 {
00643   mcdxclear();
00644 }
00645 
00646 mcdxreset_()
00647 {
00648   mcdxreset();
00649 }
00650 
00651 mcdxquit_()
00652 {
00653   mcdxquit();
00654 }
00655 
00656 mcdxidle_()
00657 {
00658   mcdxidle();
00659 }
00660 
00661 mcdxrdline_(strng)
00662 char *strng;
00663 {
00664   mcdxrdline(strng);
00665 }
00666 
00667 mcdxiconifywin_(id)
00668 int *id;
00669 {
00670   mcdxiconifywin(id);
00671 }
00672 
00673 mcdxuniconifywin_(id)
00674 int *id;
00675 {
00676   mcdxuniconifywin(id);
00677 }
00678 
00679 mcdxiconifyall_()
00680 {
00681   mcdxiconifyall();
00682 }
00683 
00684 mcdxuniconifyall_()
00685 {
00686   mcdxuniconifyall();
00687 }
00688 
00689 mcdxflushio_()
00690 {
00691   mcdxflushio();
00692 }
00693 
00694 mcdxsetwincolormap_(filename, nc, ier)
00695 char* filename;
00696 int* nc;
00697 int* ier;
00698 {
00699   mcdxsetwincolormap(filename, nc, ier);
00700 }
00701 
00702 lux_pause_(time)
00703 int time;
00704 {
00705   lux_pause(time);
00706 }
00707 
00708 mcdxsetwin_(id)
00709 int *id;
00710 {
00711   mcdxsetwin(id);
00712 }
00713 
00714 mcdxsetwin_no_raise_(id)
00715 int *id;
00716 {
00717   mcdxsetwin_no_raise(id);
00718 }
00719 
00720 mcdxcurrwin_()
00721 {
00722   mcdxcurrwin();
00723 }
00724 
00725 mcdxnopen_()
00726 {
00727   mcdxnopen();
00728 }
00729 
00730 mcdxkillwin_(id)
00731 int *id;
00732 {
00733   mcdxkillwin(id);
00734 }
00735 
00736 mcdxflush_()
00737 {
00738   mcdxflush();
00739 }
00740 
00741 mcdxbuffer_()
00742 {
00743   mcdxbuffer();
00744 }
00745 
00746 mcdxnobuffer_()
00747 {
00748   mcdxnobuffer();
00749 }

Generated at Sun Feb 24 09:57:09 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001