Fix detection of i386 in tmpi
[gromacs/AngularHB.git] / src / ngmx / g_showcol.c
blob44bf0f5815f0492152af8d367ffaadb8b1e74ad2
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
42 #include <math.h>
43 #include <smalloc.h>
44 #include <sysstuff.h>
45 #include <string2.h>
46 #include <Xstuff.h>
47 #include "xutil.h"
48 #include "futil.h"
50 typedef struct {
51 XColor xc;
52 t_windata wd;
53 } t_col;
55 typedef struct {
56 t_windata wd;
57 t_windata but;
58 int ncol;
59 t_col *col;
60 } t_sc;
62 static gmx_bool ColCallBack(struct t_x11 *x11, XEvent *event, Window w, void *data)
64 t_col *col;
65 XColor *xc;
66 int r, g, b;
67 t_windata *wd;
69 col = (t_col *)data;
70 xc = &(col->xc);
71 wd = &(col->wd);
72 switch (event->type)
74 case ButtonPress:
75 r = xc->red >> 8;
76 g = xc->green >> 8;
77 b = xc->blue >> 8;
78 printf("%6d%6d%6d\t\t%-20s\n", r, g, b, wd->text);
79 break;
81 return FALSE;
84 static gmx_bool BCallBack(struct t_x11 *x11, XEvent *event, Window w, void *data)
86 t_sc *sc;
88 sc = (t_sc *)data;
89 switch (event->type)
91 case Expose:
92 XSetForeground(x11->disp, x11->gc, sc->col[sc->ncol-1].xc.pixel);
93 TextInWin(x11, &sc->but, sc->but.text, eXCenter, eYCenter);
94 break;
95 case ConfigureNotify:
96 sc->but.width = event->xconfigure.width;
97 sc->but.height = event->xconfigure.height;
98 break;
99 case EnterNotify:
100 LightBorder(x11->disp, sc->but.self, sc->col[sc->ncol-1].xc.pixel);
101 break;
102 case LeaveNotify:
103 LightBorder(x11->disp, sc->but.self, sc->col[0].xc.pixel);
104 break;
105 case ButtonPress:
106 x11->UnRegisterCallback(x11, sc->wd.self);
107 x11->UnRegisterCallback(x11, sc->but.self);
108 return TRUE;
110 return FALSE;
113 static gmx_bool scCallBack(struct t_x11 *x11, XEvent *event, Window w, void *data)
115 t_sc *sc;
116 int i;
117 int nx, ny, X, Y, Dx, Dy, th;
118 float x, y, dx, dy;
120 th = XTextHeight(x11->font)+6;
121 sc = (t_sc *)data;
122 switch (event->type)
124 case ConfigureNotify:
125 x = sc->wd.width = event->xconfigure.width;
126 y = sc->wd.height = event->xconfigure.height-th;
127 if ((x >= 100) && (y >= 100))
129 #ifdef DEBUG
130 printf("Disp: %x, But: %x, Font: %x x: %d, th: %d\n",
131 x11->disp, sc->but.self, x11->font, (int)x, th);
132 #endif
133 XResizeWindow(x11->disp, sc->but.self, (int)x-4, th-4);
134 nx = sqrt(sc->ncol);
135 ny = sc->ncol/nx;
136 while (nx*ny < sc->ncol)
138 ny++;
140 dx = ((float)x) / nx;
141 dy = ((float)y) / ny;
142 for (i = 0; (i < sc->ncol); i++)
144 X = x = (i%nx) * dx;
145 Y = y = (i/nx) * dy;
146 Dx = (int)(x+dx)-X;
147 Dy = (int)(y+dy)-Y;
148 XMoveWindow(x11->disp, sc->col[i].wd.self, X, th+Y);
149 XResizeWindow(x11->disp, sc->col[i].wd.self, Dx, Dy);
152 break;
154 return FALSE;
157 static int col_comp(const void *c1, const void *c2)
159 XColor *x1, *x2;
160 int dr, dg;
161 int diff;
163 x1 = &(((t_col *)c1)->xc);
164 x2 = &(((t_col *)c2)->xc);
166 /* sort on intensity */
167 diff = (x1->red+x1->green+x1->blue)-(x2->red+x2->green+x2->blue);
169 if (diff == 0)
171 if ((dr = (x1->red-x2->red)) != 0)
173 return dr;
175 if ((dg = (x1->green-x2->green)) != 0)
177 return dg;
179 return x1->blue-x2->blue;
181 else
183 return diff;
187 static void read_col(t_x11 *x11, t_sc *sc, char *rgb)
189 FILE *fp;
190 char buf[STRLEN], name[STRLEN], *dummy;
191 int i, j;
192 int r, g, b;
193 XColor xc, exact;
194 t_col *cols;
196 if ((fp = fopen(rgb, "r")) == NULL)
198 perror(rgb);
199 exit(1);
203 dummy = NULL;
204 if (fscanf(fp, "%d%d%d", &r, &g, &b) == 3)
206 dummy = fgets2(buf, STRLEN-1, fp);
207 if (dummy)
209 trim(buf);
210 /* Filter out colours with names of two words */
211 sscanf(buf, "%s", name);
212 /* Filter out duplicate colours (grey and gray) */
213 if (strstr(name, "gray") == 0)
215 if (XAllocNamedColor(x11->disp, x11->cmap, name, &xc, &exact))
217 srenew(sc->col, ++sc->ncol);
218 sc->col[sc->ncol-1].xc = xc;
219 #ifdef DEBUG
220 printf("color %3d: %s\n", sc->ncol, buf);
221 #endif
222 InitWin(&(sc->col[sc->ncol-1].wd), 0, 0, 1, 1, 0, name);
228 while (dummy);
229 fclose(fp);
230 if (sc->ncol)
232 qsort(sc->col, sc->ncol, sizeof(sc->col[0]), col_comp);
234 /* Now filter out doubles */
235 cols = &(sc->col[0]);
236 for (i = 1, j = 0; (i < sc->ncol); i++)
238 if ((cols[i].xc.red != cols[j].xc.red) ||
239 (cols[i].xc.green != cols[j].xc.green) ||
240 (cols[i].xc.blue != cols[j].xc.blue))
242 j++;
243 cols[j] = cols[i];
246 sc->ncol = j;
249 static t_sc *init_sc(t_x11 *x11, Window Parent, char *rgb)
251 t_sc *sc;
252 Window w;
253 int i;
255 snew(sc, 1);
256 InitWin(&sc->wd, 0, 0, 400, 300, 1, "Show Colours");
257 sc->wd.self = XCreateSimpleWindow(x11->disp, Parent, sc->wd.x, sc->wd.y,
258 sc->wd.width, sc->wd.height,
259 sc->wd.bwidth, WHITE, BLACK);
260 x11->RegisterCallback(x11, sc->wd.self, Parent, scCallBack, sc);
261 x11->SetInputMask(x11, sc->wd.self, ButtonPressMask | ExposureMask |
262 StructureNotifyMask);
263 InitWin(&sc->but, 0, 0, sc->wd.width-2, XTextHeight(x11->font)+2, 1, "Quit");
264 sc->but.self = XCreateSimpleWindow(x11->disp, sc->wd.self, sc->but.x, sc->but.y,
265 sc->but.width, sc->but.height,
266 sc->but.bwidth, BLACK, BLACK);
267 x11->RegisterCallback(x11, sc->but.self, sc->but.self, BCallBack, sc);
268 x11->SetInputMask(x11, sc->but.self, ButtonPressMask | ExposureMask |
269 StructureNotifyMask | EnterWindowMask |
270 LeaveWindowMask);
272 read_col(x11, sc, rgb);
273 fprintf(stderr, "%d colors found\n", sc->ncol);
274 fprintf(stderr, "%6s%6s%6s\t\t%-20s\n", "Red", "Green", "Blue", "name");
275 for (i = 0; (i < sc->ncol); i++)
277 sc->col[i].wd.self = w = XCreateSimpleWindow(x11->disp, sc->wd.self, 0, 0, 1, 1, 0,
278 BLACK, sc->col[i].xc.pixel);
279 x11->RegisterCallback(x11, w, sc->wd.self, ColCallBack, &(sc->col[i]));
280 x11->SetInputMask(x11, w, ButtonPressMask);
283 return sc;
287 main(int argc, char *argv[])
289 t_x11 *x11;
290 t_sc *sc;
291 char *fn;
293 x11 = GetX11(&argc, argv);
294 if (argc > 1)
296 fn = argv[1];
298 else
300 fn = "/usr/lib/X11/rgb.txt";
302 if (!gmx_fexist(fn))
304 fprintf(stderr, "Usage: %s rgb.txt\n", argv[0]);
305 fprintf(stderr, "rgb.txt is usually somewhere in your X windows directories.\n");
306 exit(1);
308 sc = init_sc(x11, x11->root, fn);
309 XMapWindow(x11->disp, sc->wd.self);
310 XMapSubwindows(x11->disp, sc->wd.self);
311 x11->MainLoop(x11);
312 x11->CleanUp(x11);
314 return 0;