NEURON
x.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #include <stdio.h>
3 #include <string.h>
4 #if defined(__TURBOC__) || defined(__linux__)
5 #ifndef NRNOC_X11
6 #define NRNOC_X11 0
7 #endif
8 #endif
9 
10 #if defined(__alpha)
11 #undef USG
12 #endif
13 
14 #if NRNOC_X11
15 
16 #if defined(IVX11_DYNAM)
17 #include <IV-X11/ivx11_declare.h>
18 #include <IV-X11/ivx11_redef.h>
19 extern int hoc_usegui;
20 #define return_if_no_x \
21  { \
22  if (!hoc_usegui) { \
23  return; \
24  } \
25  }
26 #else
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <X11/Xos.h>
30 #define return_if_no_x \
31  { ; }
32 #endif
33 
34 /* initial position of window */
35 #define WX 100
36 #define WY 100
37 
38 /* size of window */
39 #define WIDTH 500
40 #define HEIGHT 390
41 #define MARGIN 0
42 
43 /* size of icon */
44 #define IWIDTH 64
45 #define IHEIGHT 20
46 
47 #define Plot(x, y) XDrawPoint(display, win, gc, (x), (y))
48 #define Line(x1, y1, x2, y2) XDrawLine(display, win, gc, (x1), (y1), (x2), (y2))
49 #define LAST \
50  xold = xnew; \
51  yold = ynew
52 
53 static int fast; /* don't flush until plt(-1), use drawlines*/
54 static int nlinept;
55 static XPoint polyline[200];
56 static int maxnlinept = 200;
57 static Display* display;
58 static Window win;
59 static GC gc;
60 static int screen;
61 static XEvent report;
62 
63 /*static XFontStruct *font;*/
64 
65 static int D;
66 #define Color (D > 1)
67 #define Ncolors 11
68 
69 static unsigned long colors[Ncolors];
70 
71 extern void x11_open_window();
72 extern void x11_draw_vec();
73 int x11_init_done;
74 static int xnew, ynew;
75 static int xold, yold;
76 static double xscale, yscale;
77 #define TEKX 1000.
78 #define TEKY 780.
79 
80 static void set_colors(void);
81 
82 void x11_fast(int mode) {
83  fast = mode;
84 }
85 
86 void x11flush(void) {
87  return_if_no_x;
88  if (fast && nlinept) {
89  x11_draw_vec();
90  }
91  XFlush(display);
92 }
93 
94 static void getscale(void) {
95  int x, y;
96  unsigned int width, height, border_width, depth;
97  Window root;
98  return_if_no_x;
99  XGetGeometry(display, win, &root, &x, &y, &width, &height, &border_width, &depth);
100  xscale = ((double) width) / TEKX;
101  yscale = ((double) height) / TEKY;
102 }
103 
104 void x11_coord(double x, double y) {
105  xnew = (int) (xscale * x);
106  ynew = (int) (yscale * (TEKY - y));
107 }
108 
109 void x11_draw_vec(void) {
110  return_if_no_x;
111  if (nlinept > 1) {
112  XDrawLines(display, win, gc, polyline, nlinept, CoordModeOrigin);
113  }
114  nlinept = 0;
115 }
116 
117 void x11_vector(void) {
118  return_if_no_x;
119  if (fast) {
120  if (nlinept == 0) {
121  polyline[0].x = xold;
122  polyline[0].y = yold;
123  ++nlinept;
124  }
125  if (nlinept >= maxnlinept) {
126  x11_draw_vec();
127  }
128  polyline[nlinept].x = xnew;
129  polyline[nlinept].y = ynew;
130  ++nlinept;
131  } else {
132  Line(xold, yold, xnew, ynew);
133  XFlush(display);
134  }
135  LAST;
136 }
137 
138 void x11_point(void) {
139  return_if_no_x;
140  Plot(xnew, ynew);
141  LAST;
142  if (!fast) {
143  XFlush(display);
144  }
145 }
146 void x11_move(void) {
147  return_if_no_x;
148  if (fast) {
149  if (nlinept && (xnew != xold || ynew != yold)) {
150  x11_draw_vec();
151  }
152  }
153  LAST;
154 }
155 void x11_clear(void) {
156  return_if_no_x;
157  XClearWindow(display, win);
158  XFlush(display);
159  getscale();
160 }
161 
162 void x11_cleararea(void) {
163  int w, h, x, y;
164  return_if_no_x;
165  w = xnew - xold;
166  h = ynew - yold;
167  if (w < 0) {
168  w = -w;
169  x = xnew;
170  } else {
171  x = xold;
172  }
173  if (h < 0) {
174  h = -h;
175  y = ynew;
176  } else {
177  y = yold;
178  }
179  XClearArea(display, win, x, y, w, h, False);
180  if (!fast) {
181  XFlush(display);
182  }
183 }
184 
185 void x11_put_text(const char* s) {
186  return_if_no_x;
187  if (fast && nlinept) {
188  x11_draw_vec();
189  }
190  XDrawString(display, win, gc, xold, yold, s, strlen(s));
191  if (!fast) {
192  XFlush(display);
193  }
194 }
195 void x11_setcolor(int c) {
196  return_if_no_x;
197  if (!x11_init_done) {
198  x11_open_window();
199  }
200  x11_draw_vec();
201  if (c == 0) {
202  XSetForeground(display, gc, BlackPixel(display, screen));
203  } else if (!Color) {
204  XSetForeground(display, gc, WhitePixel(display, screen));
205  } else {
206  XSetForeground(display, gc, colors[c % Ncolors]);
207  }
208  if (!fast) {
209  XFlush(display);
210  }
211 }
212 
213 void x11_open_window(void) {
214  char* window_name = "Xhocplot";
215  char* display_name = NULL;
216  XSizeHints size_hints;
217  XWindowAttributes attr;
218  return_if_no_x;
219 
220  if (x11_init_done) {
221  return;
222  }
223  if ((display = XOpenDisplay(display_name)) == NULL) {
224  (void) fprintf(stderr, "cannot connect to X server %s\n", XDisplayName(display_name));
225  }
226 
227  screen = DefaultScreen(display);
228 
229  win = XCreateSimpleWindow(display,
230  RootWindow(display, screen),
231  WX,
232  WY,
233  WIDTH,
234  HEIGHT,
235  0,
236  BlackPixel(display, screen),
237  WhitePixel(display, screen));
238 
239  XGetWindowAttributes(display, win, &attr);
240  D = attr.depth;
241  if (Color)
242  set_colors();
243 
244  size_hints.flags = USPosition | USSize;
245  size_hints.x = WX;
246  size_hints.y = WY;
247  size_hints.width = WIDTH;
248  size_hints.height = HEIGHT;
249  XSetStandardProperties(display, win, window_name, NULL, 0, NULL, 0, &size_hints);
250 
251  gc = XCreateGC(display, win, 0, NULL);
252  XSetWindowBackground(display, win, BlackPixel(display, screen));
253  XSetForeground(display, gc, WhitePixel(display, screen));
254  XSetBackground(display, gc, BlackPixel(display, screen));
255  XMapWindow(display, win);
256 
257  /*
258  font = XLoadQueryFont(display, "9x15");
259  XSetFont(display, gc, font->fid);
260  */
261 
262  XSelectInput(display, win, ExposureMask);
263  XNextEvent(display, &report);
264  XSelectInput(display, win, 0L);
265  getscale();
266  x11_init_done = 1;
267 }
268 
269 void x11_close_window(void) {
270  if (x11_init_done) {
271  XFreeGC(display, gc);
272  XCloseDisplay(display);
273  }
274  x11_init_done = 0;
275 }
276 /*-----------------------------------------------------------------------------
277  * set_colors - set colors from user resources or defaults
278  *---------------------------------------------------------------------------*/
279 char* color_names[Ncolors] = {"black",
280  "white",
281  "yellow",
282  "red",
283  "green",
284  "blue",
285  "magenta",
286  "cyan",
287  "sienna",
288  "orange",
289  "coral"};
290 
291 static void set_colors(void) {
292  int n;
293  XColor used, exact;
294  return_if_no_x;
295 
296  for (n = 0; n < Ncolors; n++) {
297  if (XAllocNamedColor(display, DefaultColormap(display, 0), color_names[n], &used, &exact)) {
298  colors[n] = used.pixel;
299  } else {
300  fprintf(stderr, "xhocplot: assuming %s:white\n", color_names[n]);
301  colors[n] = WhitePixel(display, 0);
302  }
303  }
304 }
305 
306 #endif
#define Window
Definition: _defines.h:333
#define Color
Definition: _defines.h:74
#define Line
Definition: _defines.h:11
#define Display
Definition: _defines.h:97
#define c
static void display(int imenu)
Definition: fmenu.cpp:400
ColorPalette * colors
int hoc_usegui
Definition: hoc.cpp:148
void
#define GC
Definition: md1redef.h:7
#define D(i)
Definition: multisplit.cpp:65
#define fprintf
Definition: mwprefix.h:30
int const size_t const size_t n
Definition: nrngsl.h:11
#define WIDTH
Definition: axis.cpp:307
#define xscale
Definition: axis.cpp:158
#define yscale
Definition: axis.cpp:159
#define HEIGHT
Definition: axis.cpp:308
#define root
Definition: rbtqueue.cpp:53
#define NULL
Definition: sptree.h:16