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