CMake checks for regular expression support.
[gromacs/qmmm-gamess-us.git] / src / ngmx / showcol.c
blob8272c3c82113436cb69b55685c6ab90754087109
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * Gyas ROwers Mature At Cryogenic Speed
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <math.h>
40 #include <smalloc.h>
41 #include <sysstuff.h>
42 #include <string2.h>
43 #include <Xstuff.h>
44 #include "xutil.h"
46 typedef struct {
47 XColor xc;
48 t_windata wd;
49 } t_col;
51 typedef struct {
52 t_windata wd;
53 t_windata but;
54 int ncol;
55 t_col *col;
56 } t_sc;
58 static bool ColCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
60 t_col *col;
61 XColor *xc;
62 int r,g,b;
63 t_windata *wd;
65 col=(t_col *)data;
66 xc=&(col->xc);
67 wd=&(col->wd);
68 switch (event->type) {
69 case ButtonPress:
70 r=xc->red >> 8;
71 g=xc->green >> 8;
72 b=xc->blue >> 8;
73 printf("%6d%6d%6d\t\t%-20s\n",r,g,b,wd->text);
74 break;
76 return FALSE;
79 static bool BCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
81 t_sc *sc;
83 sc=(t_sc *)data;
84 switch (event->type) {
85 case Expose:
86 XSetForeground(x11->disp,x11->gc,sc->col[sc->ncol-1].xc.pixel);
87 TextInWin(x11,&sc->but,sc->but.text,eXCenter,eYCenter);
88 break;
89 case ConfigureNotify:
90 sc->but.width=event->xconfigure.width;
91 sc->but.height=event->xconfigure.height;
92 break;
93 case EnterNotify:
94 LightBorder(x11->disp,sc->but.self,sc->col[sc->ncol-1].xc.pixel);
95 break;
96 case LeaveNotify:
97 LightBorder(x11->disp,sc->but.self,sc->col[0].xc.pixel);
98 break;
99 case ButtonPress:
100 x11->UnRegisterCallback(x11,sc->wd.self);
101 x11->UnRegisterCallback(x11,sc->but.self);
102 return TRUE;
104 return FALSE;
107 static bool scCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
109 t_sc *sc;
110 int i;
111 int nx,ny,X,Y,Dx,Dy,th;
112 float x,y,dx,dy;
114 th=XTextHeight(x11->font)+6;
115 sc=(t_sc *)data;
116 switch (event->type) {
117 case ConfigureNotify:
118 x=sc->wd.width=event->xconfigure.width;
119 y=sc->wd.height=event->xconfigure.height-th;
120 if ((x >= 100) && (y >= 100)) {
121 #ifdef DEBUG
122 printf("Disp: %x, But: %x, Font: %x x: %d, th: %d\n",
123 x11->disp,sc->but.self,x11->font,(int)x,th);
124 #endif
125 XResizeWindow(x11->disp,sc->but.self,(int)x-4,th-4);
126 nx=sqrt(sc->ncol);
127 ny=sc->ncol/nx;
128 while (nx*ny < sc->ncol)
129 ny++;
130 dx=((float)x) / nx;
131 dy=((float)y) / ny;
132 for(i=0; (i<sc->ncol); i++) {
133 X=x=(i%nx) * dx;
134 Y=y=(i/nx) * dy;
135 Dx=(int)(x+dx)-X;
136 Dy=(int)(y+dy)-Y;
137 XMoveWindow(x11->disp,sc->col[i].wd.self,X,th+Y);
138 XResizeWindow(x11->disp,sc->col[i].wd.self,Dx,Dy);
141 break;
143 return FALSE;
146 static int col_comp(const void *c1,const void *c2)
148 XColor *x1,*x2;
149 int dr,dg;
150 int diff;
152 x1=&(((t_col *)c1)->xc);
153 x2=&(((t_col *)c2)->xc);
155 /* sort on intensity */
156 diff=(x1->red+x1->green+x1->blue)-(x2->red+x2->green+x2->blue);
158 if (diff==0) {
159 if ((dr=(x1->red-x2->red)) != 0)
160 return dr;
161 if ((dg=(x1->green-x2->green)) != 0)
162 return dg;
163 return x1->blue-x2->blue;
165 else
166 return diff;
169 static void read_col(t_x11 *x11,t_sc *sc,char *rgb)
171 FILE *fp;
172 char buf[STRLEN],name[STRLEN],*dummy;
173 int i,j;
174 int r,g,b;
175 XColor xc,exact;
176 t_col *cols;
178 if ((fp=fopen(rgb,"r"))==NULL) {
179 perror(rgb);
180 exit(1);
182 do {
183 dummy=NULL;
184 if (fscanf(fp,"%d%d%d",&r,&g,&b)==3) {
185 dummy=fgets2(buf,STRLEN-1,fp);
186 if (dummy) {
187 trim(buf);
188 /* Filter out colours with names of two words */
189 sscanf(buf,"%s",name);
190 /* Filter out duplicate colours (grey and gray) */
191 if (strstr(name,"gray")==0) {
192 if (XAllocNamedColor(x11->disp,x11->cmap,name,&xc,&exact)) {
193 srenew(sc->col,++sc->ncol);
194 sc->col[sc->ncol-1].xc=xc;
195 #ifdef DEBUG
196 printf("color %3d: %s\n",sc->ncol,buf);
197 #endif
198 InitWin(&(sc->col[sc->ncol-1].wd),0,0,1,1,0,name);
203 } while (dummy);
204 fclose(fp);
205 if (sc->ncol)
206 qsort(sc->col,sc->ncol,sizeof(sc->col[0]),col_comp);
207 /* Now filter out doubles */
208 cols=&(sc->col[0]);
209 for(i=1,j=0; (i<sc->ncol); i++) {
210 if ((cols[i].xc.red != cols[j].xc.red) ||
211 (cols[i].xc.green != cols[j].xc.green) ||
212 (cols[i].xc.blue != cols[j].xc.blue)) {
213 j++;
214 cols[j]=cols[i];
217 sc->ncol=j;
220 static t_sc *init_sc(t_x11 *x11,Window Parent,char *rgb)
222 t_sc *sc;
223 Window w;
224 int i;
226 snew(sc,1);
227 InitWin(&sc->wd,0,0,400,300,1,"Show Colours");
228 sc->wd.self=XCreateSimpleWindow(x11->disp,Parent,sc->wd.x,sc->wd.y,
229 sc->wd.width,sc->wd.height,
230 sc->wd.bwidth,WHITE,BLACK);
231 x11->RegisterCallback(x11,sc->wd.self,Parent,scCallBack,sc);
232 x11->SetInputMask(x11,sc->wd.self,ButtonPressMask | ExposureMask |
233 StructureNotifyMask);
234 InitWin(&sc->but,0,0,sc->wd.width-2,XTextHeight(x11->font)+2,1,"Quit");
235 sc->but.self=XCreateSimpleWindow(x11->disp,sc->wd.self,sc->but.x,sc->but.y,
236 sc->but.width,sc->but.height,
237 sc->but.bwidth,BLACK,BLACK);
238 x11->RegisterCallback(x11,sc->but.self,sc->but.self,BCallBack,sc);
239 x11->SetInputMask(x11,sc->but.self,ButtonPressMask | ExposureMask |
240 StructureNotifyMask | EnterWindowMask |
241 LeaveWindowMask);
243 read_col(x11,sc,rgb);
244 fprintf(stderr,"%d colors found\n",sc->ncol);
245 fprintf(stderr,"%6s%6s%6s\t\t%-20s\n","Red","Green","Blue","name");
246 for(i=0; (i<sc->ncol); i++) {
247 sc->col[i].wd.self=w=XCreateSimpleWindow(x11->disp,sc->wd.self,0,0,1,1,0,
248 BLACK,sc->col[i].xc.pixel);
249 x11->RegisterCallback(x11,w,sc->wd.self,ColCallBack,&(sc->col[i]));
250 x11->SetInputMask(x11,w,ButtonPressMask);
253 return sc;
257 main(int argc, char *argv[])
259 t_x11 *x11;
260 t_sc *sc;
261 char *fn;
263 x11=GetX11(&argc,argv);
264 if (argc > 1)
265 fn=argv[1];
266 else
267 fn="/usr/lib/X11/rgb.txt";
268 sc=init_sc(x11,x11->root,fn);
269 XMapWindow(x11->disp,sc->wd.self);
270 XMapSubwindows(x11->disp,sc->wd.self);
271 x11->MainLoop(x11);
272 x11->CleanUp(x11);