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