Initial release of Maemo 5 port of gnuplot
[gnuplot] / term / sun.trm
1 /* Hello, Emacs, this is -*-C-*-
2  * $Id: sun.trm,v 1.13.2.1 2008/04/20 00:38:45 sfeam Exp $
3  *
4  */
5
6 /* GNUPLOT - sun.trm */
7
8 /*[
9  * Copyright 1990 - 1993, 1998, 2004
10  *
11  * Permission to use, copy, and distribute this software and its
12  * documentation for any purpose with or without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and
14  * that both that copyright notice and this permission notice appear
15  * in supporting documentation.
16  *
17  * Permission to modify the software is granted, but not the right to
18  * distribute the complete modified source code.  Modifications are to
19  * be distributed as patches to the released version.  Permission to
20  * distribute binaries produced by compiling modified sources is granted,
21  * provided you
22  *   1. distribute the corresponding source modifications from the
23  *    released version in the form of a patch file along with the binaries,
24  *   2. add special version identification to distinguish your version
25  *    in addition to the base release version number,
26  *   3. provide your name and address as the primary contact for the
27  *    support of your modified version, and
28  *   4. retain our contact information in regard to use of the base
29  *    software.
30  * Permission to distribute the released version of the source code along
31  * with corresponding source modifications in the form of a patch file is
32  * granted with same provisions 2 through 4 for binary distributions.
33  *
34  * This software is provided "as is" without express or implied warranty
35  * to the extent permitted by applicable law.
36 ]*/
37
38 /*
39  * This file is included by ../term.c.
40  *
41  * This terminal driver supports:
42  *   SUNview windowing system
43  *
44  * AUTHORS
45  *  Maurice Castro
46  *
47  * send your comments or suggestions to (gnuplot-info@lists.sourceforge.net).
48  *
49  */
50 /*
51  * adapted to the new terminal layout by Stefan Bodewig (Dec. 1995)
52  */
53
54 #include "driver.h"
55
56 #ifdef TERM_REGISTER
57 register_term(sun)
58 #endif
59
60 #ifdef TERM_PROTO
61 TERM_PUBLIC void SUN_init __PROTO((void));
62 TERM_PUBLIC void SUN_graphics __PROTO((void));
63 TERM_PUBLIC void SUN_text __PROTO((void));
64 TERM_PUBLIC void SUN_linetype __PROTO((int linetype));
65 TERM_PUBLIC void SUN_move __PROTO((unsigned int x, unsigned int y));
66 TERM_PUBLIC void SUN_vector __PROTO((unsigned int x, unsigned int y));
67 TERM_PUBLIC void SUN_put_text __PROTO((unsigned int x, unsigned int y, const char *str));
68 TERM_PUBLIC int SUN_justify_text __PROTO((enum JUSTIFY mode));
69 TERM_PUBLIC void SUN_reset __PROTO((void));
70 #define SUN_XMAX 600
71 #define SUN_YMAX 512
72
73 #define SUN_VCHAR (12)          /* default, will be changed */
74 #define SUN_HCHAR (8)           /* default, will be changed */
75 #define SUN_VTIC (SUN_YMAX/80)
76 #define SUN_HTIC (SUN_XMAX/80)
77 #endif /* TERM_PROTO */
78
79 #ifndef TERM_PROTO_ONLY
80 #ifdef TERM_BODY
81 #include <suntool/sunview.h>
82 #include <suntool/canvas.h>
83 #include <suntool/scrollbar.h>
84 #include <suntool/panel.h>
85 #include <pixrect/pixrect_hs.h>
86
87 void sun_setmaskpixel __PROTO((unsigned int x, unsigned int y, unsigned int value));
88 void sun_line __PROTO((unsigned int x1, unsigned int x2, unsigned int y1, unsigned int y2));
89 static Notify_value local_notice_destroy __PROTO((Frame frame, Destroy_status status));
90
91 #define MARGIN 5
92 #define MINWIN 128
93
94 static Frame frame;
95 static Canvas SUN_canvas;
96 static Pixwin *pw;
97 static struct pixfont *sun_font = NULL;
98
99 static enum JUSTIFY sun_justify = LEFT;
100
101 static Notify_value local_notice_destroy();
102
103 extern Notify_error notify_dispatch();
104
105 /* dotted line generator */
106 unsigned int sun_value = 1;     /* this can be used for colour */
107 unsigned int sun_line_mask = 0xffff;    /* 16 bit mask for dotted lines */
108 static unsigned int sun_pattern[] =
109 {0xffff, 0x1111,
110  0xffff, 0x5555, 0x3333, 0x7777, 0x3f3f, 0x0f0f, 0x5f5f};
111 int sun_mask_count = 0;
112 unsigned int sun_lastx, sun_lasty;      /* last pixel set - used by sun_line */
113
114
115 TERM_PUBLIC void
116 SUN_init()
117 {
118     struct termentry *t = term;
119     struct pr_subregion bound;
120
121     frame = window_create(NULL, FRAME,
122                           FRAME_LABEL, "Gnuplot",
123                           0);
124     notify_interpose_destroy_func(frame, local_notice_destroy);
125     SUN_canvas = window_create(frame, CANVAS,
126                            CANVAS_AUTO_EXPAND, TRUE,
127                            CANVAS_AUTO_SHRINK, TRUE,
128                            CANVAS_MARGIN, MARGIN,
129                            0);
130     notify_do_dispatch();
131     pw = canvas_pixwin(SUN_canvas);
132     window_set(frame, WIN_SHOW, TRUE, 0);
133
134     /* figure out font and rough size */
135     sun_font = pf_default();
136     pf_textbound(&bound, 1, sun_font, "M");
137     t->v_char = bound.size.y;
138     t->h_char = bound.size.x;
139
140     return;
141 }
142
143 TERM_PUBLIC void
144 SUN_graphics()
145 {
146     term->xmax = (int) window_get(SUN_canvas, CANVAS_WIDTH);
147     term->ymax = (int) window_get(SUN_canvas, CANVAS_HEIGHT);
148     pw_writebackground(pw, 0, 0, term->xmax, term->ymax, PIX_SRC);
149     notify_dispatch();
150     /* do not let the user make the window too small */
151     if ((term->xmax) < MINWIN) {
152         window_set(frame,
153                    WIN_WIDTH, MINWIN + 2 * MARGIN + 24,
154                    0);
155         notify_dispatch();
156         SUN_graphics();
157     }
158     if ((term->ymax) < MINWIN) {
159         window_set(frame,
160                    WIN_HEIGHT, MINWIN + 2 * MARGIN + 24,
161                    0);
162         notify_dispatch();
163         SUN_graphics();
164     }
165     notify_dispatch();
166     return;
167 }
168
169 TERM_PUBLIC void
170 SUN_text()
171 {
172     notify_dispatch();
173     return;                     /* enter text from another window!!! */
174 }
175
176 TERM_PUBLIC void
177 SUN_linetype(int linetype)
178 {
179     if (linetype < -2)
180         linetype = LT_BLACK;
181     if (linetype >= 7)
182         linetype %= 7;
183     sun_line_mask = sun_pattern[linetype + 2];
184     sun_mask_count = 0;
185 }
186
187
188 TERM_PUBLIC void
189 SUN_move(unsigned int x, unsigned int y)
190 {
191     sun_lastx = x;
192     sun_lasty = y;
193     notify_dispatch();
194     return;
195 }
196
197 TERM_PUBLIC void
198 SUN_vector(unsigned int x, unsigned int y)
199 {
200     if ((x >= term->xmax) || (y >= term->ymax))
201         return;
202     sun_line(sun_lastx, x, sun_lasty, y);
203     canvas_pixwin(SUN_canvas);
204     notify_dispatch();
205     return;
206 }
207
208 TERM_PUBLIC void
209 SUN_put_text(unsigned int x, unsigned int y, const char *str)
210 {
211     struct pr_subregion bound;
212
213     if ((x >= term->xmax) || (y >= term->ymax))
214         return;
215
216     pf_textbound(&bound, strlen(str), sun_font, str);
217     y = term->ymax - 1 - y + bound.size.y / 3;  /* vertical centering */
218
219     switch (sun_justify) {
220     case LEFT:
221         break;
222     case CENTRE:
223         x -= bound.size.x / 2;
224         break;
225     case RIGHT:
226         x -= bound.size.x;
227         break;
228     }
229     pw_text(pw, x, y, PIX_SRC | PIX_DST, 0, str);
230     canvas_pixwin(SUN_canvas);
231     notify_dispatch();
232     return;
233 }
234
235
236 TERM_PUBLIC int
237 SUN_justify_text(enum JUSTIFY mode)
238 {
239     sun_justify = mode;
240     return (TRUE);
241 }
242
243
244
245
246 TERM_PUBLIC void
247 SUN_reset()
248 {
249
250     term->xmax = SUN_XMAX;
251     term->ymax = SUN_YMAX;
252     window_set(frame, WIN_SHOW, FALSE, 0);
253     return;
254 }
255
256
257
258 void
259 sun_setmaskpixel(unsigned int x, unsigned int y, unsigned int value)
260 {
261     /* dotted line generator */
262     if ((sun_line_mask >> sun_mask_count) & (unsigned int) (1)) {
263         pw_put(pw, x, term->ymax - 1 - y, sun_value);
264     }
265     sun_mask_count = (sun_mask_count + 1) % 16;
266     sun_lastx = x;              /* last pixel set with mask */
267     sun_lasty = y;
268 }
269
270
271
272
273 void
274 sun_line(unsigned int x1, unsigned int x2, unsigned int y1, unsigned int y2)
275 {
276     int runcount;
277     int dx, dy;
278     int xinc, yinc;
279     unsigned int xplot, yplot;
280
281     runcount = 0;
282     dx = ABS((int) (x1) - (int) (x2));
283     if (x2 > x1)
284         xinc = 1;
285     if (x2 == x1)
286         xinc = 0;
287     if (x2 < x1)
288         xinc = -1;
289     dy = ABS((int) (y1) - (int) (y2));
290     if (y2 > y1)
291         yinc = 1;
292     if (y2 == y1)
293         yinc = 0;
294     if (y2 < y1)
295         yinc = -1;
296     xplot = x1;
297     yplot = y1;
298     if (dx > dy) {
299         /* iterate x */
300         if ((sun_line_mask == 0xffff) ||
301             ((xplot != sun_lastx) && (yplot != sun_lasty)))
302             sun_setmaskpixel(xplot, yplot, sun_value);
303         while (xplot != x2) {
304             xplot += xinc;
305             runcount += dy;
306             if (runcount >= (dx - runcount)) {
307                 yplot += yinc;
308                 runcount -= dx;
309             }
310             sun_setmaskpixel(xplot, yplot, sun_value);
311         }
312     } else {
313         /* iterate y */
314         if ((sun_line_mask == 0xffff) ||
315             ((xplot != sun_lastx) && (yplot != sun_lasty)))
316             sun_setmaskpixel(xplot, yplot, sun_value);
317         while (yplot != y2) {
318             yplot += yinc;
319             runcount += dx;
320             if (runcount >= (dy - runcount)) {
321                 xplot += xinc;
322                 runcount -= dy;
323             }
324             sun_setmaskpixel(xplot, yplot, sun_value);
325         }
326     }
327 }
328
329
330 static Notify_value
331 local_notice_destroy(Frame frame, Destroy_status status)
332 {
333     if (status != DESTROY_CHECKING) {
334         SUN_reset();
335         /* extern TBOOLEAN term_init is gone; is it sufficient just */
336         /* to comment it out? -lh */
337         /* term_init = FALSE; */
338     }
339     return (NOTIFY_DONE);
340 }
341
342 #endif /* TERM_BODY */
343
344 #ifdef TERM_TABLE
345
346 TERM_TABLE_START(sun_driver)
347     "sun", "SunView window system",
348     SUN_XMAX, SUN_YMAX, SUN_VCHAR, SUN_HCHAR,
349     SUN_VTIC, SUN_HTIC, options_null, SUN_init, SUN_reset,
350     SUN_text, null_scale, SUN_graphics, SUN_move, SUN_vector,
351     SUN_linetype, SUN_put_text, null_text_angle,
352     SUN_justify_text, line_and_point, do_arrow, set_font_null
353 TERM_TABLE_END(sun_driver)
354
355 #undef LAST_TERM
356 #define LAST_TERM sun_driver
357
358 #endif /* TERM_TABLE */
359 #endif /* TERM_PROTO_ONLY */
360
361 #ifdef TERM_HELP
362 START_HELP(sun)
363 "1 sun",
364 "?commands set terminal sun",
365 "?set terminal sun",
366 "?set term sun",
367 "?terminal sun",
368 "?term sun",
369 "?sun",
370 " The `sun` terminal driver supports the SunView window system.  It has no",
371 " options."
372 END_HELP(sun)
373 #endif