Debian Codegraph @

texk/web2c/window/x11-Xt.c

raw
1 /* x11.c: X11 window interface for Metafont, using Xt. Original by
2 [email protected]. */
3
4 #define EXTERN extern
5 #include "../mfd.h"
6
7 #ifdef X11WIN /* almost whole file */
8
9 /* For wchar_t et al., that the X files might want. */
10 #include <kpathsea/systypes.h>
11
12 #undef input /* the XWMHints structure has a field named `input' */
13 #undef output
14 #include <X11/Xlib.h>
15 #include <X11/Intrinsic.h>
16 #include <X11/StringDefs.h>
17 #undef wchar_t
18
19 #define PLANE 0
20
21 static unsigned int mf_defwidth = 0;
22 static unsigned int mf_defheight = 0;
23
24 static Display *mf_display;
25 static Window mf_window;
26
27 static Pixmap mf_pixmap;
28
29 static XtAppContext mf_app;
30
31 static GC mf_dgc; /* draw gc */
32 static GC mf_egc; /* erase gc */
33 static GC mf_cgc; /* copy plane gc */
34
35 typedef struct
36 {
37 unsigned int mf_width, mf_height;
38 Pixel mf_fg, mf_bg;
39 } mf_resources_struct;
40
41 static mf_resources_struct mf_x11_resources;
42
43
44 /* Don't paint anything until we're mapped. */
45 static Boolean mf_mapped = False;
46 static Boolean mf_awaiting_expose = True;
47
48 #ifdef MF_XT_DEBUG
49 static int mf_max_x, mf_max_y;
50 static int mf_min_x, mf_min_y;
51
52 static void mf_checkextent ();
53 #endif
54
55 static XtResource mf_resources[]
56 = { { "width", "Width", XtRInt, sizeof (int),
57 XtOffset (mf_resources_struct *, mf_width), XtRInt,
58 (XtPointer) & mf_defwidth },
59
60 { "height", "Height", XtRInt, sizeof (int),
61 XtOffset (mf_resources_struct *, mf_height), XtRInt,
62 (XtPointer) &mf_defheight },
63
64 { "foreground", "Foreground", XtRPixel, sizeof (Pixel),
65 XtOffset (mf_resources_struct *, mf_fg),
66 XtRString, (XtPointer) "Black" },
67
68 { "background", "Background", XtRPixel, sizeof (Pixel),
69 XtOffset (mf_resources_struct *, mf_bg), XtRString,
70 (XtPointer) "White" },
71 };
72
73 /* Maybe someday we'll read options, until then, this is just here for
74 the resources. */
75 static XrmOptionDescRec mf_optiondesclist[]
76 = { { "-width", "width", XrmoptionSepArg, (XPointer) NULL },
77 { "-height", "height", XrmoptionSepArg, (XPointer) NULL },
78 { "-fg", "foreground", XrmoptionSepArg, (XPointer) NULL },
79 { "-bg", "background", XrmoptionSepArg, (XPointer) NULL },
80 };
81
82 static void mf_events(void);
83 static void mf_mapstatus(Widget w, XtPointer data, XEvent *ev);
84 static void mf_newpixmap(unsigned int width, unsigned int height);
85 static void mf_redraw(void);
86 static void mf_repaint(Widget w, XtPointer data, XEvent *ev);
87
88 #include <mfdisplay.h>
89
90 /* Return 1 if display opened successfully, else 0. */
91
92 int
93 mf_x11_initscreen (void)
94 {
95 XSetWindowAttributes xwa;
96 Widget mf_toplevel;
97 Widget mf_canvas;
98 XGCValues gcv;
99 Arg args[1];
100 int mf_argc;
101 char *mf_argv[2];
102
103 mf_argv[0] = "mf";
104 mf_argv[1] = NULL;
105 mf_argc = 1;
106
107 mf_toplevel = XtInitialize ("mf", "Metafont",
108 mf_optiondesclist,
109 XtNumber (mf_optiondesclist),
110 &mf_argc, mf_argv);
111
112 XtGetApplicationResources (mf_toplevel, (XtPointer) & mf_x11_resources,
113 mf_resources, XtNumber (mf_resources),
114 NULL, 0);
115
116 if (mf_argc != 1)
117 {
118 fprintf (stderr, "Usage: %s\n", mf_argv[0]);
119 return 0;
120 }
121
122 /* If nothing specified in their resources (e.g., .Xdefaults)
123 then use the values of Metafont's "screen". */
124 if (mf_x11_resources.mf_width == 0)
125 mf_x11_resources.mf_width = screenwidth;
126 if (mf_x11_resources.mf_height == 0)
127 mf_x11_resources.mf_height = screendepth;
128
129 mf_canvas = XtCreateManagedWidget ("canvas", widgetClass, mf_toplevel,
130 NULL, 0);
131
132 XtSetArg (args[0], XtNwidth, mf_x11_resources.mf_width);
133 XtSetValues (mf_canvas, args, 1);
134 XtSetArg (args[0], XtNheight, mf_x11_resources.mf_height);
135 XtSetValues (mf_canvas, args, 1);
136
137 /* for mf_x11_updatescreen() */
138 mf_app = XtWidgetToApplicationContext (mf_canvas);
139
140 XtAddEventHandler (mf_canvas, (Cardinal) ExposureMask, True,
141 (XtEventHandler) mf_repaint, NULL);
142 XtAddEventHandler (mf_canvas, (Cardinal) StructureNotifyMask, True,
143 (XtEventHandler) mf_mapstatus, NULL);
144
145 XtRealizeWidget (mf_toplevel);
146
147 mf_display = XtDisplay (mf_canvas);
148 mf_window = XtWindow (mf_canvas);
149
150 /* Since Metafont isn't your typical x window program that
151 sits in XTMainLoop, if the server supports backing store
152 and save unders this will help keep the output looking
153 nice. */
154 xwa.backing_store = WhenMapped;
155 XChangeWindowAttributes (mf_display, mf_window, CWBackingStore, &xwa);
156
157 gcv.background = mf_x11_resources.mf_bg;
158 gcv.foreground = mf_x11_resources.mf_fg;
159 gcv.function = GXcopy;
160
161 /* copy plane gc */
162 mf_cgc = XCreateGC (mf_display, mf_window,
163 GCForeground | GCBackground | GCFunction, &gcv);
164
165 mf_newpixmap (screenwidth > mf_x11_resources.mf_width
166 ? screenwidth : mf_x11_resources.mf_width,
167 screendepth > mf_x11_resources.mf_height
168 ? screendepth : mf_x11_resources.mf_height);
169
170 return 1;
171 }
172
173 void
174 mf_x11_updatescreen (void)
175 {
176 XEvent event;
177
178 mf_events ();
179 while (mf_awaiting_expose)
180 {
181 XtAppNextEvent (mf_app, &event);
182 XtDispatchEvent (&event);
183 }
184 mf_redraw ();
185
186 #ifdef MF_XT_DEBUG
187 printf ("max_x=%d, min_x=%d, max_y=%d, min_y=%d\n",
188 mf_max_x, mf_min_x,
189 mf_max_y, mf_min_y);
190 #endif
191 }
192
193
194 void
195 mf_x11_blankrectangle(screencol left,
196 screencol right,
197 screenrow top,
198 screenrow bottom)
199 {
200 XFillRectangle (mf_display, mf_pixmap, mf_egc, (int) left, (int) top,
201 (int) (right - left + 1), (int) (bottom - top + 1));
202 mf_events ();
203 }
204
205 void
206 mf_x11_paintrow(screenrow row,
207 pixelcolor init_color,
208 transspec tvect,
209 screencol vector_size)
210 {
211 GC gc;
212 int col;
213
214 gc = (init_color == 0) ? mf_egc : mf_dgc;
215
216 do
217 {
218 col = *tvect++;
219
220 #ifdef MF_XT_DEBUG
221 mf_checkextent (col, *tvect, row);
222 #endif /* MF_XT_DEBUG */
223
224 XDrawLine (mf_display, mf_pixmap, gc, col, (int) row,
225 (int) *tvect, (int) row);
226
227 gc = (gc == mf_egc) ? mf_dgc : mf_egc;
228 }
229 while (--vector_size > 0);
230
231 mf_events ();
232 }
233
234 #ifdef MF_XT_DEBUG
235 static void
236 mf_checkextent(int x1, int x2, int y)
237 {
238 if (x1 < mf_min_x)
239 mf_min_x = x1;
240 if (x1 > mf_max_x)
241 mf_max_x = x1;
242
243 if (x2 < mf_min_x)
244 mf_min_x = x2;
245 if (x2 > mf_max_x)
246 mf_max_x = x2;
247
248 if (y > mf_max_y)
249 mf_max_y = y;
250 if (y < mf_min_y)
251 mf_min_y = y;
252 }
253 #endif /* MF_XT_DEBUG */
254
255 static void
256 mf_events (void)
257 {
258 XEvent event;
259
260 if (XtAppPending (mf_app) != 0)
261 {
262 while (XtAppPending (mf_app) != 0)
263 {
264 XtAppNextEvent (mf_app, &event);
265 XtDispatchEvent (&event);
266 }
267 }
268 }
269
270 static void
271 mf_newpixmap(unsigned int width, unsigned int height)
272 {
273 XGCValues gcv;
274 Pixmap newpixmap;
275
276 /* width == mf_width and height == mf_height
277 the first time mf_newpixmap() is called.
278 */
279 if (width < mf_x11_resources.mf_width && height < mf_x11_resources.mf_height)
280 return;
281
282 newpixmap = XCreatePixmap (mf_display, mf_window, width, height, 1);
283
284 gcv.background = 0;
285 gcv.foreground = 1;
286
287 if (mf_dgc != 0)
288 XFreeGC (mf_display, mf_dgc);
289
290 /* draw gc */
291 gcv.line_width = 1;
292 mf_dgc = XCreateGC (mf_display, newpixmap,
293 GCForeground | GCBackground | GCLineWidth, &gcv);
294
295 if (mf_egc != 0)
296 XFreeGC (mf_display, mf_egc);
297
298 /* erase gc */
299 gcv.foreground = 0;
300 mf_egc = XCreateGC (mf_display, newpixmap,
301 GCForeground | GCBackground | GCLineWidth, &gcv);
302
303 XFillRectangle (mf_display, newpixmap, mf_egc, 0, 0, width, height);
304
305 if (mf_pixmap != 0)
306 {
307 XCopyArea (mf_display, mf_pixmap, newpixmap, mf_dgc, 0, 0,
308 mf_x11_resources.mf_width,
309 mf_x11_resources.mf_height, 0, 0);
310
311 XFreePixmap (mf_display, mf_pixmap);
312 }
313
314 mf_pixmap = newpixmap;
315
316 mf_x11_resources.mf_width = width;
317 mf_x11_resources.mf_height = height;
318 }
319
320 static void
321 mf_repaint(Widget w, XtPointer data, XEvent *ev)
322 {
323 if (!mf_mapped || !ev || ev->type != Expose)
324 return;
325
326 /* We are a ``simple application''. */
327 if (ev->xexpose.count == 0)
328 {
329 XEvent event;
330
331 /* skip all excess redraws */
332 while (XCheckTypedEvent (mf_display, Expose, &event) != False)
333 continue;
334
335 mf_redraw ();
336 mf_awaiting_expose = False;
337 }
338 }
339
340
341 static void
342 mf_mapstatus(Widget w, XtPointer data, XEvent *ev)
343 {
344 switch (ev->type)
345 {
346 case MapNotify:
347 mf_mapped = True;
348 break;
349
350 case UnmapNotify:
351 mf_mapped = False;
352 break;
353
354 case ConfigureNotify:
355 mf_newpixmap (ev->xconfigure.width, ev->xconfigure.height);
356 mf_redraw ();
357 break;
358 }
359 }
360
361
362 static void
363 mf_redraw (void)
364 {
365 XCopyPlane (mf_display, mf_pixmap, mf_window, mf_cgc, 0, 0,
366 mf_x11_resources.mf_width, mf_x11_resources.mf_height,
367 0, 0, (unsigned long) 1);
368
369 XFlush (mf_display);
370 }
371
372 #else
373 int x11_dummy;
374 #endif /* X11WIN */