[BACK]Return to ox_plot_xevent.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / plot

Annotation of OpenXM_contrib2/asir2000/plot/ox_plot_xevent.c, Revision 1.19

1.2       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2       noro       27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.19    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/plot/ox_plot_xevent.c,v 1.18 2002/07/20 02:28:08 noro Exp $
1.2       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52: #include "ox.h"
                     53: #include "ifplot.h"
                     54: #include "cursor.h"
1.8       takayama   55: #include <X11/Xaw/MenuButton.h>
                     56: #include <X11/Xaw/Paned.h>
1.14      noro       57:
                     58: /* XXX : these lines are in plotg.c, but ld says they are not defined */
                     59: #if __DARWIN__
                     60: int stream;
                     61:
                     62: DISPLAY *display;
                     63: CURSOR normalcur,runningcur,errorcur;
                     64:
                     65: #if defined(VISUAL)
                     66: POINT start_point, end_point;
                     67: SIZE cansize;
                     68: #else
                     69: Window rootwin;
                     70: GC drawGC,dashGC,hlGC,scaleGC,clearGC,xorGC,colorGC,cdrawGC;
                     71: XFontStruct *sffs;
                     72: #endif
                     73:
                     74: struct canvas *canvas[MAXCANVAS];
1.19    ! noro       75: struct canvas *closed_canvas[MAXCANVAS];
1.14      noro       76: struct canvas *current_can;
                     77: #endif /* __DARWIN__ */
1.1       noro       78:
                     79: #ifdef ABS
                     80: #undef ABS
                     81: #define ABS(a) ((a)>0?(a):-(a))
                     82: #endif
                     83:
                     84: static char *dname;
                     85: static int remotes;
                     86: static int depth,scrn;
                     87:
1.13      noro       88: extern JMP_BUF ox_env;
1.1       noro       89: static Widget toplevel;
                     90: static XtAppContext app_con;
                     91:
                     92: static int busy;
                     93:
                     94: static struct PlotResources {
                     95:        Pixel ForePixel,BackPixel,DashPixel;
                     96:        char *ForeName,*BackName,*DashName;
                     97:        Boolean Reverse;
1.18      noro       98:        Boolean UpsideDown;
1.1       noro       99: } PlotResources;
                    100:
                    101: #define forePixel PlotResources.ForePixel
                    102: #define backPixel PlotResources.BackPixel
                    103: #define dashPixel PlotResources.DashPixel
                    104: #define foreName PlotResources.ForeName
                    105: #define backName PlotResources.BackName
                    106: #define dashName PlotResources.DashName
                    107: #define reverse PlotResources.Reverse
1.18      noro      108: #define upsidedown PlotResources.UpsideDown
1.1       noro      109:
                    110: Cursor create_cursor();
                    111:
                    112: #define blackPixel     BlackPixel(display,scrn)
                    113: #define whitePixel     WhitePixel(display,scrn)
                    114:
                    115: #define LABELWIDTH 150
                    116:
                    117: process_xevent() {
                    118:        XEvent ev;
                    119:
                    120:        while ( XPending(display) ) {
                    121:                XtAppNextEvent(app_con,&ev);
                    122:                XtDispatchEvent(&ev);
                    123:        }
                    124: }
                    125:
                    126: /* event handlers */
                    127:
                    128: static POINT spos,cpos;
                    129:
                    130: void press(w,can,ev)
                    131: Widget w;
                    132: struct canvas *can;
                    133: XButtonEvent *ev;
                    134: {
                    135:        POINT p;
                    136:
                    137:        switch ( ev->button ) {
                    138:                case Button1:
                    139:                        XC(spos) = ev->x; YC(spos) = ev->y; cpos = spos; break;
                    140:                case Button3:
                    141:                        XC(p) = ev->x; YC(p) = ev->y; draw_coord(can,&p); break;
                    142:                default:
                    143:                        break;
                    144:        }
                    145: }
                    146:
                    147: void motion(w,can,ev)
                    148: Widget w;
                    149: struct canvas *can;
                    150: XMotionEvent *ev;
                    151: {
                    152:
                    153:        POINT o,p;
                    154:
                    155:        if ( ev->state & Button1Mask ) {
                    156:                o = cpos; XC(cpos) = ev->x; YC(cpos) = ev->y;
                    157:                draw_frame(can->window,spos,o,cpos);
                    158:        } else if ( ev->state & Button3Mask ) {
                    159:                XC(p) = ev->x; YC(p) = ev->y;
                    160:                draw_coord(can,p);
                    161:        }
                    162: }
                    163:
                    164: void release(w,can,ev)
                    165: Widget w;
                    166: struct canvas *can;
                    167: XButtonEvent *ev;
                    168: {
                    169:        POINT e;
                    170:
                    171:        switch ( ev->button ) {
                    172:                case Button1:
                    173:                        e.x = ev->x; e.y = ev->y;
                    174:                        draw_frame0(can->window,spos,e);
1.10      noro      175:                        if ( !busy && can->mode != MODE_INTERACTIVE ) {
1.1       noro      176:                                if ( can->mode == MODE_PLOT )
                    177:                                        plot_resize(can,spos,e);
                    178:                                else
                    179:                                        ifplot_resize(can,spos,e);
                    180:                        }
                    181:                        break;
                    182:                default:
                    183:                        break;
                    184:        }
                    185: }
                    186:
                    187: void structure(w,can,ev)
                    188: Widget w;
                    189: struct canvas *can;
                    190: XEvent *ev;
                    191: {
                    192:        switch ( ev->xany.type ) {
                    193:                case Expose:
                    194:                                if ( !ev->xexpose.count )
                    195:                                        redraw_canvas(can);
                    196:                        break;
                    197:                case ConfigureNotify:
                    198:                        redraw_canvas(can); break;
                    199:                default:
                    200:                        break;
                    201:        }
                    202: }
                    203:
                    204: static int lindex;
                    205:
                    206: void lpress(w,can,ev)
                    207: Widget w;
                    208: struct canvas *can;
                    209: XButtonEvent *ev;
                    210: {
                    211:        lindex = (can->height-ev->y)/(can->height/can->nzstep);
                    212:        draw_level(can,lindex,hlGC);
                    213: }
                    214:
                    215: void jumpproc(w,can,percent)
                    216: Widget w;
                    217: struct canvas *can;
                    218: float *percent;
                    219: {
                    220:        int index;
                    221:
                    222:        index = can->nzstep * (1.0-*percent);
                    223:        if ( index == lindex )
                    224:                return;
                    225:        if ( lindex >= 0 )
                    226:                draw_level(can,lindex,drawGC);
                    227:        lindex = index;
                    228:        draw_level(can,lindex,hlGC);
                    229: }
                    230:
                    231: void jumpproc_m(w,can,percent)
                    232: Widget w;
                    233: struct canvas *can;
                    234: float *percent;
                    235: {
                    236:        int index;
                    237:
                    238:        index = can->nzstep * (1.0-*percent);
                    239:        if ( index != lindex ) {
                    240:                draw_level(can,lindex,drawGC);
                    241:                draw_level(can,lindex,hlGC);
                    242:        }
                    243: }
                    244:
                    245: void lrelease(w,can,ev)
                    246: Widget w;
                    247: struct canvas *can;
                    248: XButtonEvent *ev;
                    249: {
                    250:        draw_level(can,lindex,drawGC); lindex = -1;
                    251: }
                    252:
                    253: void lrelease_m(w,can,ev)
                    254: Widget w;
                    255: struct canvas *can;
                    256: XButtonEvent *ev;
                    257: {
                    258:        lindex = -1;
                    259: }
                    260:
                    261: draw_level(can,index,gc)
                    262: struct canvas *can;
                    263: int index;
                    264: GC gc;
                    265: {
                    266:        Pixmap pix;
                    267:        struct pa *pa;
                    268:        int i,len;
                    269:        POINT *p;
                    270:        Arg arg[2];
                    271:        char buf[BUFSIZ];
                    272:
                    273:        if ( busy || can->wide || index < 0 || index > can->nzstep )
                    274:                return;
                    275:        pix = can->pix; pa = &can->pa[index]; len = pa->length; p = pa->pos;
                    276:        for ( i = 0; i < len; i++ )
                    277:                XDrawPoint(display,pix,gc,p[i].x,p[i].y);
                    278:        sprintf(buf,"level:%g",can->zmin+(can->zmax-can->zmin)*index/can->nzstep);
                    279:        XtSetArg(arg[0],XtNlabel,buf); XtSetArg(arg[1],XtNwidth,LABELWIDTH);
                    280:        XtSetValues(can->level,arg,2);
                    281:        copy_to_canvas(can);
                    282: }
                    283:
                    284: draw_frame(window,spos,opos,epos)
                    285: Window window;
                    286: POINT spos,opos,epos;
                    287: {
                    288:        if ( XC(opos) != XC(epos) || YC(opos) != YC(epos) )
                    289:                draw_frame0(window,spos,opos);
                    290:        draw_frame0(window,spos,epos);
                    291: }
                    292:
                    293: draw_frame0(window,spos,epos)
                    294: Window window;
                    295: POINT spos,epos;
                    296: {
                    297:        int ulx,uly,w,h;
                    298:
                    299:        ulx = MIN(XC(spos),XC(epos)); uly = MIN(YC(spos),YC(epos));
                    300:        w = ABS(XC(spos)-XC(epos)); h = ABS(YC(spos)-YC(epos));
                    301:        if ( !w || !h )
                    302:                return;
                    303:        XDrawRectangle(display,window,xorGC,ulx,uly,w,h);
                    304:        XFlush(display);
                    305: }
                    306:
                    307: draw_coord(can,pos)
                    308: struct canvas *can;
                    309: POINT pos;
                    310: {
                    311:        char buf[BUFSIZ];
                    312:        Arg arg[2];
                    313:        double x,y,xmin,ymax,dx,dy;
                    314:
                    315:        if ( can->wide ) {
                    316:                dx = 10*(can->xmax-can->xmin); dy = 10*(can->ymax-can->ymin);
                    317:                xmin = (can->xmax+can->xmin-dx)/2;
                    318:                ymax = (can->ymax+can->ymin+dy)/2;
                    319:        } else {
                    320:                dx = can->xmax-can->xmin; dy = can->ymax-can->ymin;
                    321:                xmin = can->xmin; ymax = can->ymax;
                    322:        }
                    323:        x = xmin+XC(pos)*dx/can->width;
                    324:        y = ymax-YC(pos)*dy/can->height;
                    325:        sprintf(buf,"%s:%g",can->vx?can->vx->name:"horiz",x);
                    326:        XtSetArg(arg[0],XtNlabel,buf); XtSetArg(arg[1],XtNwidth,LABELWIDTH);
                    327:        XtSetValues(can->xcoord,arg,2);
                    328:        sprintf(buf,"%s:%g",can->vy?can->vy->name:"vert",y);
                    329:        XtSetArg(arg[0],XtNlabel,buf); XtSetArg(arg[1],XtNwidth,LABELWIDTH);
                    330:        XtSetValues(can->ycoord,arg,2);
                    331: }
                    332:
                    333: redraw_canvas(can)
                    334: struct canvas *can;
                    335: {
                    336:        if ( can->wide )
                    337:                draw_wideframe(can);
                    338:        else
                    339:                copy_to_canvas(can);
                    340: }
                    341:
                    342: search_canvas()
                    343: {
                    344:        int i;
                    345:
                    346:        for ( i = 0; i < MAXCANVAS; i++ )
                    347:                if ( !canvas[i] ) {
                    348:                        canvas[i] = (struct canvas *)MALLOC(sizeof(struct canvas));
                    349:                        canvas[i]->index = i; return i;
                    350:                }
                    351: }
                    352:
                    353: search_active_canvas()
                    354: {
                    355:        int i;
                    356:
                    357:        for ( i = 0; i < MAXCANVAS; i++ )
                    358:                if ( canvas[i] )
                    359:                        return i;
                    360:        return -1;
                    361: }
                    362:
1.19    ! noro      363: void popup_canvas(index)
        !           364: {
        !           365:        XtPopup(canvas[index]->shell,XtGrabNone);
        !           366: }
1.4       takayama  367:
1.1       noro      368: void destroy_canvas(w,can,calldata)
                    369: Widget w;
                    370: struct canvas *can;
                    371: XtPointer calldata;
                    372: {
                    373:        XtPopdown(can->shell);
1.19    ! noro      374: /*     XtDestroyWidget(can->shell); */
1.1       noro      375:        XFlush(display);
                    376:        if ( can == current_can ) {
                    377:                reset_busy(can); current_can = 0;
                    378:        }
1.19    ! noro      379:        if ( closed_canvas[can->index] )
        !           380:                XtDestroyWidget(closed_canvas[can->index]->shell);
        !           381:        closed_canvas[can->index] = can;
1.1       noro      382:        canvas[can->index] = 0;
                    383: }
                    384:
                    385: void precise_canvas(w,can,calldata)
                    386: Widget w;
                    387: struct canvas *can;
                    388: XtPointer calldata;
                    389: {
                    390:        if ( can->precise )
                    391:                can->precise = 0;
                    392:        else
                    393:                can->precise = 1;
                    394: }
                    395:
                    396: void wide_canvas(w,can,calldata)
                    397: Widget w;
                    398: struct canvas *can;
                    399: XtPointer calldata;
                    400: {
                    401:        if ( can->wide ) {
                    402:                can->wide = 0; copy_to_canvas(can);
                    403:        } else {
                    404:                can->wide = 1; draw_wideframe(can);
                    405:        }
                    406: }
                    407:
                    408: void noaxis_canvas(w,can,calldata)
                    409: Widget w;
                    410: struct canvas *can;
                    411: XtPointer calldata;
                    412: {
                    413:        if ( can->noaxis )
                    414:                can->noaxis = 0;
                    415:        else
                    416:                can->noaxis = 1;
                    417:        if ( can->wide )
                    418:                draw_wideframe(can);
                    419:        else
                    420:                copy_to_canvas(can);
                    421: }
                    422:
                    423: toggle_button(w,flag)
                    424: Widget w;
                    425: int flag;
                    426: {
                    427:        Arg arg[2];
                    428:
                    429:        if ( flag ) {
                    430:                XtSetArg(arg[0],XtNforeground,backPixel);
                    431:                XtSetArg(arg[1],XtNbackground,forePixel);
                    432:        } else {
                    433:                XtSetArg(arg[0],XtNforeground,forePixel);
                    434:                XtSetArg(arg[1],XtNbackground,backPixel);
                    435:        }
                    436:        XtSetValues(w,arg,2); XFlush(display);
                    437: }
                    438:
                    439: draw_wideframe(can)
                    440: struct canvas *can;
                    441: {
                    442:        struct canvas fakecan;
                    443:        double xmin,xmax,ymin,ymax,xmid,ymid,dx,dy;
                    444:        POINT s,e;
                    445:
                    446:        fakecan = *can;
                    447:        dx = 10*(can->xmax-can->xmin); dy = 10*(can->ymax-can->ymin);
                    448:        xmid = (can->xmax+can->xmin)/2; ymid = (can->ymax+can->ymin)/2;
                    449:
                    450:        fakecan.xmin = xmid-dx/2; fakecan.xmax = xmid+dx/2;
                    451:        fakecan.ymin = ymid-dy/2; fakecan.ymax = ymid+dy/2;
                    452:
                    453:        XFillRectangle(display,can->window,clearGC,
                    454:                0,0,can->width,can->height);
                    455:        pline(display,&fakecan,can->window);
                    456:        XC(s) = can->width*9/20; YC(s) = can->height*9/20;
                    457:        XC(e) = can->width*11/20; YC(e) = can->height*11/20;
                    458:        draw_frame0(can->window,s,e);
                    459: }
                    460:
                    461: create_popup(parent,name,str,shell,dialog)
                    462: Widget parent;
                    463: char *name,*str;
                    464: Widget *shell,*dialog;
                    465: {
                    466:        Arg arg[3];
                    467:        Position x,y;
                    468:
                    469:        XtTranslateCoords(parent,0,0,&x,&y);
                    470:        XtSetArg(arg[0],XtNx,x); XtSetArg(arg[1],XtNy,y);
                    471:        *shell = XtCreatePopupShell(name,transientShellWidgetClass,parent,arg,2);
                    472:        XtSetArg(arg[0],XtNlabel,str);
                    473:        *dialog = XtCreateManagedWidget("dialog",dialogWidgetClass,*shell,arg,1);
                    474: }
                    475:
                    476: warning(can,s)
                    477: struct canvas *can;
                    478: char *s;
                    479: {
                    480:        void popdown_warning();
                    481:        Widget warnshell,warndialog;
                    482:        Position x,y;
                    483:        Arg arg[3];
                    484:
                    485:        if ( !can->shell )
                    486:                return;
                    487:        create_popup(can->shell,"warning",s,&warnshell,&warndialog);
                    488:        XawDialogAddButton(warndialog,"dismiss",popdown_warning,warnshell);
                    489:        XtPopup(warnshell,XtGrabNone);
                    490: }
                    491:
                    492: void popdown_warning(w,client,call)
                    493: Widget w;
                    494: XtPointer client,call;
                    495: {
                    496:        XtPopdown(client); XtDestroyWidget(client);
                    497: }
                    498:
                    499: void show_formula(w,can,calldata)
                    500: Widget w;
                    501: struct canvas *can;
                    502: XtPointer calldata;
                    503: {
                    504:        void popdown_formula();
                    505:        Widget fshell,fdialog;
                    506:        char buf[BUFSIZ];
                    507:
                    508:        soutput_init(buf); sprintexpr(CO,(Obj)can->formula);
                    509:        create_popup(can->shell,"formula",buf,&fshell,&fdialog);
                    510:        XawDialogAddButton(fdialog,"dismiss",popdown_formula,w);
                    511:        XtSetSensitive(w,False); XtPopup(fshell,XtGrabNone);
                    512: }
                    513:
                    514: void popdown_formula(w,fbutton,call)
                    515: Widget w,fbutton;
                    516: XtPointer call;
                    517: {
                    518:        Widget shell = XtParent(XtParent(w));
                    519:        XtPopdown(shell); XtDestroyWidget(shell);
                    520:        XtSetSensitive(fbutton,True);
                    521: }
                    522:
                    523: #define NormalSelection ButtonPressMask|ButtonReleaseMask|Button1MotionMask|Button3MotionMask|StructureNotifyMask| ExposureMask
                    524:
                    525: create_canvas(can)
                    526: struct canvas *can;
                    527: {
                    528:        XEvent event;
                    529:        Widget box,frame,commands,
1.4       takayama  530:                coords,quit,print,wide,precise,canvas,formula;
1.1       noro      531:        Window window;
                    532:        Pixmap pix;
                    533:        int i,width,height;
                    534:        Arg arg[6];
                    535:        char buf[BUFSIZ];
1.8       takayama  536:        static void print_canvas();
1.1       noro      537:
                    538:        width = can->width; height = can->height;
                    539:
                    540:        sprintf(buf,"%s : %d/%d", can->wname?can->wname:"Plot",
                    541:                remotes,can->index);
                    542:        XtSetArg(arg[0],XtNiconName,buf);
                    543:        can->shell =
                    544:                XtCreatePopupShell("shell",topLevelShellWidgetClass,toplevel,arg,1);
                    545:
                    546:        XtSetArg(arg[0],XtNhSpace,0);
                    547:        XtSetArg(arg[1],XtNvSpace,0);
                    548:        XtSetArg(arg[2],XtNborderWidth,0);
                    549:        box = XtCreateManagedWidget("box",boxWidgetClass,can->shell,arg,3);
                    550:
                    551:        frame = XtCreateManagedWidget("form",formWidgetClass,box,NULL,0);
                    552:
                    553:        XtSetArg(arg[0],XtNorientation,XtorientHorizontal);
                    554:        XtSetArg(arg[1],XtNborderWidth,0);
                    555:        commands = XtCreateManagedWidget("commands",boxWidgetClass,frame,arg,2);
                    556:
                    557:        quit = XtCreateManagedWidget("quit",commandWidgetClass,commands,NULL,0);
                    558:        XtAddCallback(quit,XtNcallback,destroy_canvas,can);
1.4       takayama  559:        print = XtCreateManagedWidget("print",commandWidgetClass,commands,NULL,0);
                    560:        XtAddCallback(print,XtNcallback,print_canvas,can);
1.1       noro      561:        can->wideb = wide =
                    562:                XtCreateManagedWidget("wide",toggleWidgetClass,commands,NULL,0);
                    563:        XtAddCallback(wide,XtNcallback,wide_canvas,can);
                    564:        can->preciseb = precise =
                    565:                XtCreateManagedWidget("precise",toggleWidgetClass,commands,NULL,0);
                    566:        XtAddCallback(precise,XtNcallback,precise_canvas,can);
                    567:        formula =
                    568:                XtCreateManagedWidget("formula",commandWidgetClass,commands,NULL,0);
                    569:        XtAddCallback(formula,XtNcallback,show_formula,can);
                    570:        can->noaxisb =
                    571:                XtCreateManagedWidget("noaxis",toggleWidgetClass,commands,NULL,0);
                    572:        XtAddCallback(can->noaxisb,XtNcallback,noaxis_canvas,can);
                    573:
                    574:        XtSetArg(arg[0],XtNfromVert,commands);
                    575:        XtSetArg(arg[1],XtNwidth,width);
                    576:        XtSetArg(arg[2],XtNheight,height);
                    577:        canvas = XtCreateManagedWidget("canvas",simpleWidgetClass,frame,arg,3);
                    578:
                    579:        XtSetArg(arg[0],XtNfromVert,canvas);
                    580:        XtSetArg(arg[1],XtNheight,5);
                    581:        XtSetArg(arg[2],XtNwidth,width);
                    582:        XtSetArg(arg[3],XtNorientation,XtorientHorizontal);
                    583:        XtSetArg(arg[4],XtNsensitive,False);
                    584:        can->xdone =
                    585:                XtCreateManagedWidget("xdone",scrollbarWidgetClass,frame,arg,5);
                    586:
                    587:        XtSetArg(arg[0],XtNfromHoriz,canvas);
                    588:        XtSetArg(arg[1],XtNfromVert,commands);
                    589:        XtSetArg(arg[2],XtNwidth,5);
                    590:        XtSetArg(arg[3],XtNheight,height);
                    591:        XtSetArg(arg[4],XtNorientation,XtorientVertical);
                    592:        XtSetArg(arg[5],XtNsensitive,False);
                    593:        can->ydone =
                    594:                XtCreateManagedWidget("ydone",scrollbarWidgetClass,frame,arg,6);
                    595:
                    596:        XtSetArg(arg[0],XtNfromVert,can->xdone);
                    597:        XtSetArg(arg[1],XtNorientation,XtorientHorizontal);
                    598:        XtSetArg(arg[2],XtNborderWidth,0);
                    599:        coords = XtCreateManagedWidget("coords",boxWidgetClass,frame,arg,3);
                    600:
                    601:        XtSetArg(arg[0],XtNwidth,LABELWIDTH);
                    602:        can->xcoord = XtCreateManagedWidget("xcoord",labelWidgetClass,coords,arg,1);
                    603:        XtSetArg(arg[0],XtNwidth,LABELWIDTH);
                    604:        can->ycoord = XtCreateManagedWidget("ycoord",labelWidgetClass,coords,arg,1);
                    605:
                    606:        XtAddEventHandler(canvas,ButtonPressMask,False,press,can);
                    607:        XtAddEventHandler(canvas,ButtonReleaseMask,False,release,can);
                    608:        XtAddEventHandler(canvas,Button1MotionMask,False,motion,can);
                    609:        XtAddEventHandler(canvas,Button3MotionMask,False,motion,can);
                    610:        XtAddEventHandler(canvas,StructureNotifyMask,False,structure,can);
                    611:        XtAddEventHandler(canvas,ExposureMask,False,structure,can);
                    612:
                    613:        if ( can->mode == MODE_CONPLOT ) {
                    614:                Widget scale;
                    615:
                    616:                XtSetArg(arg[0],XtNwidth,LABELWIDTH);
                    617:                can->level = XtCreateManagedWidget("level",labelWidgetClass,
                    618:                        commands,arg,1);
                    619:
                    620:                XtSetArg(arg[0],XtNsensitive,True);
                    621:                XtSetValues(can->ydone,arg,1);
                    622:                if ( depth >= 2 ) {
                    623:                        XtAddCallback(can->ydone,XtNjumpProc,jumpproc,can);
                    624:                        XtAddEventHandler(can->ydone,ButtonReleaseMask,False,lrelease,can);
                    625:                } else {
                    626:                        XtAddCallback(can->ydone,XtNjumpProc,jumpproc_m,can);
                    627:                        XtAddEventHandler(can->ydone,ButtonReleaseMask,False,lrelease_m,can);
                    628:                }
                    629:        }
                    630:        if ( can->mode != MODE_IFPLOT || !qpcheck((Obj)can->formula) )
                    631:                XtSetSensitive(precise,False);
                    632:        XtPopup(can->shell,XtGrabNone);
                    633:
                    634:        window = can->window = XtWindow(canvas);
                    635:        pix = can->pix = XCreatePixmap(display,window,width,height,depth);
                    636:        XFillRectangle(display,pix,clearGC,0,0,width,height);
                    637:        XDefineCursor(display,window,normalcur);
                    638:        XFlush(display);
                    639:        current_can = can;
                    640: }
                    641:
                    642: alloc_pixmap(can)
                    643: struct canvas *can;
                    644: {
                    645:        can->pix = XCreatePixmap(display,can->window,
                    646:                can->width,can->height,depth);
                    647:        XFillRectangle(display,can->pix,clearGC,0,0,can->width,can->height);
                    648: }
                    649:
                    650: static XrmOptionDescRec options[] = {
1.18      noro      651: {"-upsidedown","*upsidedown",XrmoptionNoArg,"on"},
1.1       noro      652: {"-reverse","*reverse",XrmoptionNoArg,"on"},
                    653: {"-fg","*foreground",XrmoptionSepArg,NULL},
                    654: {"-bg","*background",XrmoptionSepArg,NULL},
                    655: };
                    656:
                    657: #define offset(name) XtOffset(struct PlotResources *,name)
                    658:
                    659: static XtResource resources[] = {
1.18      noro      660: {"upsidedown","UpsideDown",XtRBoolean,sizeof(Boolean),
                    661:        offset(UpsideDown),XtRBoolean,&upsidedown},
1.1       noro      662: {"reverse","Reverse",XtRBoolean,sizeof(Boolean),
                    663:        offset(Reverse),XtRBoolean,&reverse},
                    664: {"foreground","Foreground",XtRString,sizeof(char *),
                    665:        offset(ForeName),XtRString,NULL},
                    666: {"foreground","Foreground",XtRPixel,sizeof(Pixel),
                    667:        offset(ForePixel),XtRPixel,(XtPointer)&forePixel},
                    668: {"background","Background",XtRString,sizeof(char *),
                    669:        offset(BackName),XtRString,NULL},
                    670: {"background","Background",XtRPixel,sizeof(Pixel),
                    671:        offset(BackPixel),XtRPixel,(XtPointer)&backPixel},
                    672: {"dash","Dash",XtRString,sizeof(char *),
                    673:        offset(DashName),XtRString,NULL},
                    674: {"dash","Dash",XtRPixel,sizeof(Pixel),
                    675:        offset(DashPixel),XtRPixel,(XtPointer)&dashPixel},
                    676: };
                    677:
1.12      noro      678: int init_plot_display(argc,argv)
1.1       noro      679: int argc;
                    680: char **argv;
                    681: {
                    682:        int ac;
                    683:        char **av;
                    684:        unsigned int tmp;
                    685:
                    686:        for ( ac = argc, av = argv; ac; ac--, av++ )
1.12      noro      687:                if ( !strcmp(*av,"nox") )
                    688:                        return 0;
                    689:                else if ( index(*av,':') )
1.1       noro      690:                        dname = *av;
                    691:        XtToolkitInitialize();
                    692:        app_con = XtCreateApplicationContext();
                    693:        display = XtOpenDisplay(app_con,dname,"plot","Plot",
                    694:                options,XtNumber(options),&argc,argv);
                    695:        if ( !display ) {
                    696:                fprintf(stderr,"Can't open display\n");
1.12      noro      697:                return 0;
1.1       noro      698:        }
                    699:        toplevel = XtAppCreateShell(0,"Plot",applicationShellWidgetClass,
                    700:                display,0,0);
                    701:
                    702:        forePixel = blackPixel; backPixel = whitePixel;
                    703:        dashPixel = blackPixel;
                    704:        XtGetApplicationResources(toplevel,&PlotResources,
                    705:                resources,XtNumber(resources),NULL,0);
                    706:        display = XtDisplay(toplevel);
                    707:        scrn = DefaultScreen(display);
                    708:        depth = DefaultDepth(display,scrn);
                    709:        rootwin = RootWindow(display,scrn);
                    710:
                    711:        if ( reverse ) {
                    712:                tmp = forePixel; forePixel = backPixel; backPixel = tmp;
                    713:        }
                    714:
                    715:        create_gc();
                    716:        create_font();
                    717:        create_cursors();
1.12      noro      718:        return 1;
1.1       noro      719: }
                    720:
                    721: static char *scalefont = "*-8-80-*";
                    722:
                    723: create_font() {
                    724:        Font sfid;
                    725:
                    726:        sfid = XLoadFont(display,scalefont);
                    727:        sffs = XQueryFont(display,sfid);
                    728:        XSetFont(display,scaleGC,sfid);
                    729: }
                    730:
                    731: create_gc() {
                    732:        static XColor color = {0,0x0,0x0,0x0,DoRed|DoGreen|DoBlue,0};
                    733:        int i,b,step;
                    734:
                    735:        drawGC = XCreateGC(display,rootwin,0,NULL);
                    736:        dashGC = XCreateGC(display,rootwin,0,NULL);
                    737:        hlGC = XCreateGC(display,rootwin,0,NULL);
                    738:        clearGC = XCreateGC(display,rootwin,0,NULL);
                    739:        scaleGC = XCreateGC(display,rootwin,0,NULL);
                    740:        xorGC = XCreateGC(display,rootwin,0,NULL);
                    741:        colorGC = XCreateGC(display,rootwin,0,NULL);
1.11      noro      742:        cdrawGC = XCreateGC(display,rootwin,0,NULL);
1.1       noro      743:        XCopyGC(display,DefaultGC(display,scrn),(1L<<(GCLastBit+1))-1,drawGC);
                    744:        XCopyGC(display,DefaultGC(display,scrn),(1L<<(GCLastBit+1))-1,dashGC);
                    745:        XCopyGC(display,DefaultGC(display,scrn),(1L<<(GCLastBit+1))-1,clearGC);
                    746:        XCopyGC(display,DefaultGC(display,scrn),(1L<<(GCLastBit+1))-1,scaleGC);
                    747:        XCopyGC(display,DefaultGC(display,scrn),(1L<<(GCLastBit+1))-1,xorGC);
                    748:        XCopyGC(display,DefaultGC(display,scrn),(1L<<(GCLastBit+1))-1,colorGC);
1.11      noro      749:        XCopyGC(display,DefaultGC(display,scrn),(1L<<(GCLastBit+1))-1,cdrawGC);
1.1       noro      750:        XSetForeground(display,drawGC,forePixel);
                    751:        XSetForeground(display,scaleGC,forePixel);
                    752:        XSetForeground(display,clearGC,backPixel);
                    753:        XSetForeground(display,xorGC,forePixel^backPixel);
                    754:        XSetFunction(display,xorGC,GXxor);
                    755:        XSetForeground(display,dashGC,dashPixel);
                    756:        XSetLineAttributes(display,dashGC,1,LineOnOffDash,CapButt,JoinRound);
                    757:
                    758:        color.red = color.green = color.blue = 0xffff/2;
                    759:        XAllocColor(display,DefaultColormap(display,scrn),&color);
                    760:        XSetForeground(display,hlGC,color.pixel);
                    761:        color.red = 0xffff; color.green = color.blue = 0;
                    762:        XAllocColor(display,DefaultColormap(display,scrn),&color);
                    763:        XSetForeground(display,colorGC,color.pixel);
1.11      noro      764: }
                    765:
                    766: set_drawcolor(c)
                    767: unsigned int c;
                    768: {
                    769:        XColor color = {0,0x0,0x0,0x0,DoRed|DoGreen|DoBlue,0};
                    770:
                    771:        color.red = (c&0xff0000)>>8;
                    772:        color.green = (c&0xff00);
                    773:        color.blue = (c&0xff)<<8;
                    774:        XAllocColor(display,DefaultColormap(display,scrn),&color);
                    775:        XSetForeground(display,cdrawGC,color.pixel);
1.1       noro      776: }
                    777:
                    778: create_cursors() {
                    779:        static XColor fg = {0, 0x0, 0x0, 0x0,DoRed|DoGreen|DoBlue,0};
                    780:        static XColor bg = {0, 0xffff, 0xffff, 0xffff,DoRed|DoGreen|DoBlue,0};
                    781:
                    782:        XAllocColor(display,DefaultColormap(display,scrn),&fg);
                    783:        XAllocColor(display,DefaultColormap(display,scrn),&bg);
                    784:        normalcur = create_cursor(h_bits,h_m_bits,h_width,h_height,
                    785:                h_x_hot,h_y_hot,&fg,&bg);
                    786:        runningcur = create_cursor(ht_bits,ht_m_bits,ht_width,ht_height,
                    787:                ht_width/2,ht_height/2,&fg,&bg);
                    788:        errorcur = create_cursor(m_bits,m_m_bits,m_width,m_height,
                    789:                m_width/2,m_height/2,&fg,&bg);
                    790: }
                    791:
                    792: Cursor create_cursor(image,mask,width,height,xhot,yhot,fg,bg)
                    793: char *image,*mask;
                    794: int width,height,xhot,yhot;
                    795: XColor *fg,*bg;
                    796: {
                    797:        Pixmap ipix,mpix;
                    798:
                    799:        ipix = XCreateBitmapFromData(display,rootwin,image,width,height);
                    800:        mpix = XCreateBitmapFromData(display,rootwin,mask,width,height);
                    801:        return XCreatePixmapCursor(display,ipix,mpix,fg,bg,xhot,yhot);
                    802: }
                    803:
                    804: copy_to_canvas(can)
                    805: struct canvas *can;
                    806: {
                    807:        if ( display ) {
                    808:                XCopyArea(display,can->pix,can->window,
                    809:                        drawGC,0,0,can->width,can->height,0,0);
                    810:                pline(display,can,can->window);
                    811:                XFlush(display);
                    812:        }
                    813: }
                    814:
                    815: copy_subimage(subcan,can,pos)
                    816: struct canvas *subcan,*can;
                    817: XPoint pos;
                    818: {
                    819:        if ( display ) {
                    820:                XCopyArea(display,subcan->pix,can->pix,
                    821:                        drawGC,0,0,subcan->width,subcan->height,pos.x,pos.y);
                    822:                XFlush(display);
                    823:        }
                    824: }
                    825:
                    826: #include <signal.h>
                    827: #include <fcntl.h>
                    828:
                    829: set_selection() {
                    830:        if ( current_can ) {
                    831:                XSelectInput(display,current_can->window,0);
                    832:                XFlush(display);
                    833:        }
                    834: }
                    835:
                    836: reset_selection() {
                    837:        if ( current_can ) {
                    838:                XSelectInput(display,current_can->window,NormalSelection);
                    839:                XFlush(display);
                    840:        }
                    841: }
                    842:
                    843: set_busy(can)
                    844: struct canvas *can;
                    845: {
                    846:        busy = 1;
                    847:        XtSetSensitive(can->wideb,False);
                    848:        XtSetSensitive(can->preciseb,False);
                    849:        XtSetSensitive(can->noaxisb,False);
                    850:        XFlush(display);
                    851: }
                    852:
                    853: reset_busy(can)
                    854: struct canvas *can;
                    855: {
                    856:        busy = 0;
                    857:        if ( can->window ) {
                    858:                XtSetSensitive(can->wideb,True);
                    859:                XtSetSensitive(can->noaxisb,True);
                    860:                if ( can->mode == MODE_IFPLOT && qpcheck((Obj)can->formula) )
                    861:                        XtSetSensitive(can->preciseb,True);
                    862:                XFlush(display);
                    863:        }
                    864: }
                    865:
                    866: reset_current_computation()
                    867: {
                    868:        if ( current_can ) {
                    869:                reset_selection(); reset_busy(current_can);
                    870:                define_cursor(current_can->window,normalcur);
                    871:        }
1.8       takayama  872: }
                    873:
                    874: static struct canvas *Can;
                    875: /* void print_canvas(Widget w,struct canvas *can, XtPointer calldata); */
                    876: static void output_to_file(Widget , XtPointer, XtPointer );
                    877: static void output_to_ps_printer(Widget , XtPointer, XtPointer );
                    878: static void cancel_output_to_file(Widget , XtPointer,XtPointer);
                    879: static void generate_psfile(struct canvas *can, FILE *fp);
                    880: static void set_printing_method(Widget,XtPointer,XtPointer);
                    881: static void method_is_not_available();
                    882: static Widget PrintDialog;
                    883: static Widget PrintDialog_lp;
                    884: static Widget W;
                    885: static char *Fname = NULL;
                    886: static char *PrinterName = NULL;
                    887: #define PRINTING_METHOD_BITMAP 0
                    888: #define PRINTING_METHOD_VECTOR 1
                    889: static int PrintingMethod = PRINTING_METHOD_BITMAP;
                    890: static String Printing_methods[]={
                    891:   "bitMap","vector",
                    892: };
                    893: static int N_printing_methods = 2;
                    894:
                    895: /*
                    896: static Widget create_printing_method_bar(Widget parent) {
                    897:   Widget panel, button;
                    898:   int i,n;
                    899:   Arg wargs[1];
                    900:   panel = XtCreateManagedWidget("printing methods",panedWidgetClass,
                    901:                                                                parent,NULL,0);
                    902:   for (i=0; i<N_printing_methods; i++) {
                    903:        button = XtCreateManagedWidget(Printing_methods[i],menuButtonWidgetClass,
                    904:                                                                   panel,NULL,0);
                    905:        fprintf(stderr,"button=%x\n",(int) button);
                    906:        XtAddCallback(button,XtNcallback,set_printing_method,(XtPointer) i);
                    907:   }
                    908:   return(panel);
                    909: }
                    910: */
                    911:
                    912:
                    913: static void print_canvas(w,can,calldata)
                    914:         Widget w;
                    915:         struct canvas *can;
                    916:         XtPointer calldata;
                    917: {
                    918:   Widget fshell,fdialog;
                    919:   extern struct canvas *Can;
                    920:   extern Widget W;
                    921:   Widget entry;
                    922:   int i;
                    923:   Arg arg[1];
                    924:   static void output_to_printer();
                    925:   static void print_canvas_to_file();
                    926:   static void printing_method();
                    927:
                    928:   W = w;
                    929:   Can = can;
                    930:   create_popup(can->shell,"Print/Output PS file","",&fshell,&fdialog);
                    931:   XawDialogAddButton(fdialog,"print",output_to_printer,w);
                    932:   XawDialogAddButton(fdialog,"output PS file",print_canvas_to_file,w);
                    933:   XawDialogAddButton(fdialog,"method",printing_method,w);
                    934:   XawDialogAddButton(fdialog,"dismiss",cancel_output_to_file,w);
                    935:   XtSetSensitive(w,False); XtPopup(fshell,XtGrabNone);
                    936: }
                    937:
                    938: static void set_printing_method(Widget w,XtPointer number,XtPointer call_data) {
                    939:   Widget shell;
                    940:   extern int PrintingMethod;
                    941:   PrintingMethod = (int) number;
                    942:   fprintf(stderr,"PrintingMethod=%d\n",number);
                    943:   shell = XtParent(XtParent(w));
                    944:   XtPopdown(shell); XtDestroyWidget(shell);
                    945: }
                    946:
                    947: static void printing_method(w,can,calldata)
                    948:         Widget w;
                    949:         struct canvas *can;
                    950:         XtPointer calldata;
                    951: {
                    952:   Arg arg[10];
                    953:   int i,n;
                    954:   Widget fshell,fdialog;
                    955:   extern struct canvas *Can;
                    956:   extern int PrintingMethod;
                    957:
                    958:   w = W;
                    959:   can = Can;
                    960:   create_popup(can->shell,"Printing method",Printing_methods[PrintingMethod],
                    961:                           &fshell,&fdialog);
                    962:   n = 0;
                    963:   XtSetArg(arg[n], XtNlabel, "Method: "); n++;
                    964:   XtSetArg(arg[n], XtNvalue, Printing_methods[PrintingMethod]); n++;
                    965:   XtSetValues(fdialog,arg,n);
                    966:   for (i=0; i<N_printing_methods; i++) {
                    967:        XawDialogAddButton(fdialog,Printing_methods[i],set_printing_method,(XtPointer) i);
                    968:   }
                    969:   XtSetSensitive(w,False); XtPopup(fshell,XtGrabNone);
                    970: }
                    971: static void print_canvas_to_file(w,can,calldata)
                    972:         Widget w;
                    973:         struct canvas *can;
                    974:         XtPointer calldata;
                    975: {
                    976:   FILE *fp;
                    977:   Arg arg[10];
                    978:   int n;
                    979:   static char *psfile = NULL;
                    980:   Widget fshell,fdialog;
                    981:   extern struct canvas *Can;
                    982:   extern Widget PrintDialog;
                    983:   extern char *Fname;
                    984:
                    985:   w = W;
                    986:   can = Can;
                    987:   if (psfile == NULL || Fname == NULL) psfile = "ox_plot.eps";
                    988:   else psfile = Fname;
                    989:   create_popup(can->shell,"Output as PS file",psfile,&fshell,&fdialog);
                    990:   n = 0;
                    991:   XtSetArg(arg[n], XtNlabel, "File : "); n++;
                    992:   XtSetArg(arg[n], XtNvalue, psfile); n++;
                    993:   XtSetValues(fdialog,arg,n);
                    994:   XawDialogAddButton(fdialog,"output to file",output_to_file,w);
                    995:   XawDialogAddButton(fdialog,"cancel",cancel_output_to_file,w);
                    996:   PrintDialog = fdialog;
                    997:   XtSetSensitive(w,False); XtPopup(fshell,XtGrabNone);
                    998: }
                    999: static void output_to_printer(w,can,calldata)
                   1000:         Widget w;
                   1001:         struct canvas *can;
                   1002:         XtPointer calldata;
                   1003: {
                   1004:   FILE *fp;
                   1005:   Arg arg[10];
                   1006:   int n;
                   1007:   static char *psfile = NULL;
                   1008:   Widget fshell,fdialog;
                   1009:   extern struct canvas *Can;
                   1010:   extern Widget PrintDialog_lp;
                   1011:   extern char *PrinterName;
                   1012:
                   1013:   w = W;
                   1014:   can = Can;
                   1015:   if (psfile  == NULL || PrinterName == NULL) psfile = "lp";
                   1016:   else psfile = PrinterName;
                   1017:   create_popup(can->shell,"Output PS file to printer",psfile,&fshell,&fdialog);
                   1018:   n = 0;
                   1019:   XtSetArg(arg[n], XtNlabel, "PS Printer Name : "); n++;
                   1020:   XtSetArg(arg[n], XtNvalue, psfile); n++;
                   1021:   XtSetValues(fdialog,arg,n);
                   1022:   XawDialogAddButton(fdialog,"output to PS printer",output_to_ps_printer,w);
                   1023:   XawDialogAddButton(fdialog,"cancel",cancel_output_to_file,w);
                   1024:   PrintDialog_lp = fdialog;
                   1025:   XtSetSensitive(w,False); XtPopup(fshell,XtGrabNone);
                   1026: }
                   1027:
                   1028: static void cancel_output_to_file(w,fbutton,call)
                   1029:         Widget w;
                   1030:         XtPointer fbutton, call;
                   1031: {
                   1032:   Widget shell = XtParent(XtParent(w));
                   1033:   XtPopdown(shell); XtDestroyWidget(shell);
                   1034:   XtSetSensitive(fbutton,True);
                   1035: }
                   1036:
                   1037: static void output_to_file(w,fbutton,call)
                   1038:         Widget w;
                   1039:         XtPointer fbutton, call;
                   1040: {
                   1041:   char *fname;
                   1042:   FILE *fp;
                   1043:   int i;
                   1044:   char *m;
                   1045:   extern struct canvas *Can;
                   1046:   extern Widget PrintDialog;
                   1047:   extern int PrintingMethod;
                   1048:   Widget shell = XtParent(XtParent(w));
                   1049:
                   1050:   if (PrintingMethod == PRINTING_METHOD_BITMAP) {
                   1051:   }else{
                   1052:        method_is_not_available();
                   1053:        XtPopdown(shell); XtDestroyWidget(shell);
                   1054:        XtSetSensitive(fbutton,True);
                   1055:        return;
                   1056:   }
                   1057:
                   1058:   fname = XawDialogGetValueString(PrintDialog);
                   1059:   Fname = (char *)malloc(sizeof(char)*strlen(fname)+1);
                   1060:   strcpy(Fname,fname);
                   1061:   for (i=0; i<strlen(Fname); i++) {
                   1062:        if (Fname[i] == 0xd || Fname[i] == 0xa) {
                   1063:          Fname[i] = 0; break;
                   1064:        }
                   1065:   }
                   1066:   fprintf(stderr,"fname=%s\n",Fname); fflush(NULL);
                   1067:   fp = fopen(Fname,"w");
                   1068:   if (fp == NULL) {
                   1069:        warning(Can,"Could not open the output file.");
                   1070:   }else{
                   1071:        generate_psfile(Can,fp);
                   1072:        fclose(fp);
                   1073:   }
                   1074:
                   1075:   XtPopdown(shell); XtDestroyWidget(shell);
                   1076:   XtSetSensitive(fbutton,True);
                   1077: }
                   1078:
                   1079: static void output_to_ps_printer(w,fbutton,call)
                   1080:         Widget w;
                   1081:         XtPointer fbutton, call;
                   1082: {
                   1083:   char *printerName;
                   1084:   FILE *fp;
                   1085:   extern struct canvas *Can;
                   1086:   extern Widget PrintDialog_lp;
                   1087:   char fname[256];
                   1088:   char cmd[512];
                   1089:   static int id = 0;
                   1090:   int i;
                   1091:   Widget shell = XtParent(XtParent(w));
                   1092:
                   1093:   if (PrintingMethod == PRINTING_METHOD_BITMAP) {
                   1094:   }else{
                   1095:        method_is_not_available();
                   1096:        XtPopdown(shell); XtDestroyWidget(shell);
                   1097:        XtSetSensitive(fbutton,True);
                   1098:        return;
                   1099:   }
                   1100:
                   1101:   sprintf(fname,"/tmp/ox_plot_%d.eps",(int) getpid(),id++);
                   1102:
                   1103:   printerName = XawDialogGetValueString(PrintDialog_lp);
                   1104:   PrinterName = (char *)malloc(sizeof(char)*strlen(printerName)+1);
                   1105:   strcpy(PrinterName,printerName);
                   1106:   for (i=0; i<strlen(PrinterName); i++) {
                   1107:        if (PrinterName[i] == 0xd || PrinterName[i] == 0xa) {
                   1108:          PrinterName[i] = 0; break;
                   1109:        }
                   1110:   }
                   1111:   fprintf(stderr,"printerName=%s\n",PrinterName); fflush(NULL);
                   1112:   fp = fopen(fname,"w");
                   1113:   if (fp == NULL) {
                   1114:        warning(Can,"Could not open the output file.");
                   1115:   }else{
                   1116:        generate_psfile(Can,fp);
                   1117:        fclose(fp);
                   1118:   }
                   1119:
                   1120:   sprintf(cmd,"lpr -P%s %s",PrinterName,fname);
                   1121:   if (system(cmd)) {
                   1122:        warning(Can,"Unknown printer?");
                   1123:   }
                   1124:   sprintf(cmd,"rm -f %s",fname);
                   1125:   system(cmd);
                   1126:   XtPopdown(shell); XtDestroyWidget(shell);
                   1127:   XtSetSensitive(fbutton,True);
                   1128: }
                   1129:
                   1130:
                   1131: /* test sequence
                   1132:    ox_launch(0,"ox_plot");
                   1133:    ifplot(x^2-y^3);
                   1134:    drawcircle(0,0,100,0,0);
                   1135:    */
1.15      takayama 1136: static int getColorSizeOfImageForPS(int xsize,int ysize,XImage *image,
                   1137:                                     struct xcolorForPS **tableOfxcolorForPS);
                   1138:
1.8       takayama 1139: static void generate_psfile(can,fp)
                   1140:         struct canvas *can;
                   1141:         FILE *fp;
                   1142: {
                   1143:   int x,y;
                   1144:   XImage *image;
                   1145:   int color[1];
                   1146:   int colorSize = 1;
                   1147:   char *m;
1.15      takayama 1148:   struct xcolorForPS *tableOfxcolorForPS;
1.8       takayama 1149:   extern int PrintingMethod;
                   1150:   fprintf(stderr,"generate_psfile\n");
                   1151:   if (PrintingMethod == PRINTING_METHOD_BITMAP) {
                   1152:        if ( display ) {
                   1153:          fprintf(stderr,"generate_psfile: output to a file.\n");
                   1154:          image = XGetImage(display,can->pix,
                   1155:                                                0,0,can->width,can->height,-1,ZPixmap);
1.15      takayama 1156:          colorSize =
                   1157:                getColorSizeOfImageForPS(can->width,can->height,image,&tableOfxcolorForPS);
                   1158:          color[0] = 0; /* black line */
1.18      noro     1159:          generatePS_from_image(fp,image,can->width,can->height,color,colorSize,can,tableOfxcolorForPS,upsidedown);
1.8       takayama 1160:        }else{
                   1161:          fprintf(stderr,"Cannot print on this system\n");
                   1162:        }
                   1163:   }else{
                   1164:        method_is_not_available();
                   1165:   }
                   1166:   fflush(NULL);
                   1167: }
                   1168:
                   1169: static void method_is_not_available() {
                   1170:   char *m;
                   1171: #define MSG1 "Printing method \""
                   1172: #define MSG2 "\" is not available for this picture."
                   1173:   m = (char *)malloc(strlen(MSG1)+strlen(MSG2)+strlen(Printing_methods[PrintingMethod])+1);
                   1174:   strcpy(m,MSG1);
                   1175:   strcat(m,Printing_methods[PrintingMethod]);
                   1176:   strcat(m,MSG2);
                   1177:   warning(Can,m);
1.9       noro     1178: }
                   1179:
                   1180: clear_pixmap(can)
                   1181: struct canvas *can;
                   1182: {
                   1183:        XFillRectangle(display,can->pix,clearGC,0,0,can->width,can->height);
                   1184:        XFlush(display);
1.1       noro     1185: }
1.15      takayama 1186:
                   1187: /*
                   1188:    The following functions are used to generate color postscript file.
                   1189: */
                   1190: /* In order to count colorSize, binary tree (sm_btree) is used. */
                   1191: static struct sm_btree *sm_newNode(unsigned long v);
                   1192: static int sm_insert(struct sm_btree *node,unsigned long v);
                   1193: static int sm_count(struct sm_btree *rootp);
1.17      takayama 1194:
1.15      takayama 1195: struct sm_btree {
                   1196:   unsigned long p;
                   1197:   struct sm_btree * left;
                   1198:   struct sm_btree * right;
                   1199: };
                   1200: static struct sm_btree *sm_newNode(unsigned long v) {
                   1201:   struct sm_btree * n;
                   1202:   n = (struct sm_btree *)MALLOC(sizeof(struct sm_btree));
                   1203:   if (n == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
                   1204:   n->p = v;
                   1205:   n->left = NULL;
                   1206:   n->right = NULL;
                   1207:   return n;
                   1208: }
                   1209: static int sm_insert(struct sm_btree *node,unsigned long v)
                   1210: {
                   1211:   if (node->p == v) return;
                   1212:   if (node->p > v) {
                   1213:     if (node->left == NULL) {
                   1214:       node->left = sm_newNode(v);
                   1215:       return;
                   1216:     }
                   1217:     sm_insert(node->left,v);
                   1218:   }
                   1219:   if (node->p < v) {
                   1220:     if (node->right == NULL) {
                   1221:       node->right = sm_newNode(v);
                   1222:       return;
                   1223:     }
                   1224:     sm_insert(node->right,v);
                   1225:   }
                   1226: }
                   1227: static int sm_count(struct sm_btree *rootp)
                   1228: {
                   1229:   if (rootp == NULL) return 0;
                   1230:   return (1+sm_count(rootp->left)+sm_count(rootp->right));
                   1231: }
                   1232:
1.16      takayama 1233: static int setTableOfxcolorForPS(struct sm_btree *rootp,
                   1234:                                  struct xcolorForPS *table,int k,int size)
                   1235: {
                   1236:   int m;
                   1237:   m = k;
                   1238:   if (rootp == NULL) return;
                   1239:   if (k >= size) {
                   1240:     warning(Can,"internal error of setTableOfxcolorForPS");
                   1241:   }
                   1242:   if (rootp->left != NULL) {
                   1243:     m = setTableOfxcolorForPS(rootp->left,table,k,size);
                   1244:   }
                   1245:
                   1246:   (table[m]).pixel = rootp->p;
                   1247:   m++;
                   1248:   if (rootp->right != NULL) {
                   1249:     m = setTableOfxcolorForPS(rootp->right,table,m,size);
                   1250:   }
                   1251:   return m;
                   1252: }
                   1253:
                   1254:
1.15      takayama 1255: static int getColorSizeOfImageForPS(int xsize,int ysize,XImage *image,
                   1256:                                     struct xcolorForPS **tableOfxcolorForPS)
                   1257: {
                   1258:   int x,y;
                   1259:   int size;
                   1260:   struct sm_btree root;
                   1261:   struct xcolorForPS *table;
1.16      takayama 1262:   XStandardColormap scm;
                   1263:   Colormap cm;
                   1264:   XColor color;
                   1265:   XColor white;
                   1266:   int screen,i;
                   1267:
1.15      takayama 1268:   root.p = 0;
                   1269:   root.left = NULL; root.right=NULL;
                   1270:   /* get color size */
                   1271:   for (x=0; x<xsize; x++) {
                   1272:     for (y=0; y<ysize; y++) {
                   1273:       sm_insert(&root,XGetPixel(image,x,y));
                   1274:     }
                   1275:   }
                   1276:   size=sm_count(&root);
                   1277:
                   1278:   table = (struct xcolorForPS *)MALLOC((size+1)*sizeof(struct xcolorForPS));
                   1279:   if (table == NULL) {
                   1280:        fprintf(stderr,"No more memory in getColorSizeOfImageForPS.\n");
                   1281:        return 0;
                   1282:   }
                   1283:   /* Set rgb values standing for the pixel values.
                   1284:   */
1.16      takayama 1285:   if (setTableOfxcolorForPS(&root,table,0,size) != size) {
                   1286:        warning(Can,"internal error.");
                   1287:        return ;
                   1288:   }
                   1289:
                   1290:   screen = DefaultScreen(display);
                   1291:   cm = DefaultColormap(display,screen);
                   1292:   /*  BUG: it does not work.
                   1293:   if (!XGetStandardColormap(display,RootWindow(display,DefaultScreen(display)),&scm,XA_RGB_DEFAULT_MAP)) {
                   1294:        warning(Can,"failed to open the X Standard Colormap.");
                   1295:        scm.red_max = 0xffff;
                   1296:        scm.green_max = 0xffff;
                   1297:        scm.blue_max = 0xffff;
                   1298:   }
                   1299:   */
                   1300:   /* Set by hand. */
                   1301:   scm.red_max = 0xffff;
                   1302:   scm.green_max = 0xffff;
                   1303:   scm.blue_max = 0xffff;
                   1304:   XParseColor(display,cm,"White",&white);
                   1305:   for (i=0; i<size; i++) {
                   1306:     color.pixel=(table[i]).pixel;
                   1307:     /*
                   1308:     {
                   1309:       char s[254];
                   1310:       sprintf(s,"%ld",color.pixel);
                   1311:       warning(Can,s);
                   1312:     }
                   1313:     */
                   1314:     XQueryColor(display,cm,&color);
                   1315:     (table[i]).r = ((double) color.red)/((double) scm.red_max);
                   1316:     (table[i]).g = ((double) color.green)/((double) scm.green_max);
                   1317:     (table[i]).b = ((double) color.blue)/((double) scm.blue_max);
                   1318:     if ((table[i]).r > 1.0) (table[i]).r = 1.0;
                   1319:     if ((table[i]).g > 1.0) (table[i]).g = 1.0;
                   1320:     if ((table[i]).b > 1.0) (table[i]).b = 1.0;
                   1321:     if (color.red == white.red && color.green == white.green
                   1322:         && color.blue == white.blue) {
                   1323:       (table[i]).print = 0;
                   1324:     }else{
                   1325:       (table[i]).print = 1;
                   1326:     }
                   1327:   }
1.15      takayama 1328:
                   1329:   *tableOfxcolorForPS = table;
                   1330:   return size;
                   1331: }
                   1332:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>