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