Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / ngmx / nleg.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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, 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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <ctype.h>
43 #include <string.h>
44 #include <smalloc.h>
45 #include <macros.h>
46 #include "buttons.h"
47 #include "nleg.h"
48 #include "writeps.h"
49
50 typedef struct {
51   const char    *tp;
52   unsigned long *col;
53   t_rgb rgb;
54 } t_atomcolor;
55
56 static t_atomcolor ac[] = {
57   { "O",  &LIGHTRED,     { 1,  0,  0   } },
58   { "N",  &LIGHTCYAN,    { 0,  0,  1   } },
59   { "NA", &LIGHTGREY,    { 0.6,0.6,0.6 } },
60   { "S",  &YELLOW,       { 1,  1,  0   } },
61   { "C",  &LIGHTGREEN,   { 0,  1,  0   } },
62   { "CL", &VIOLET,       { 1,  0,  1   } },
63   { "F",  &LIGHTGREY,    { 0.6,0.6,0.6 } },
64   { "Z",  &LIGHTGREY,    { 0.6,0.6,0.6 } },
65   { "P",  &LIGHTBLUE,    { 0.4,0.4,1.0 } },
66   { "H",  &WHITE,        { 0.8,0.8,0.8 } }
67 };
68 #define NAC asize(ac)
69
70 int search_ac(const char *type)
71 {
72   int i,nb,mij,best,besti;
73
74   best=0;
75   besti=0;
76   if (type) {
77     for(i=0; (i<NAC); i++) {
78       mij=min((int)strlen(type),(int)strlen(ac[i].tp));
79       for(nb=0; (nb<mij); nb++)
80         if (type[nb] != ac[i].tp[nb])
81           break;
82       if (nb > best) {
83         best=nb;
84         besti=i;
85       }
86     }
87   }
88   return besti;
89 }
90
91 unsigned long Type2Color(const char *type)
92 {
93   int i;
94   
95   i=search_ac(type);
96   
97   return *(ac[i].col);
98 }
99
100 t_rgb *Type2RGB(const char *type)
101 {
102   int i;
103   
104   i=search_ac(type);
105   
106   return &(ac[i].rgb);
107 }
108
109 void DrawLegend(t_x11 *x11,t_windata *Win)
110 {
111 #define NLAB 6
112 #define COLS 3
113   static const char *lab[NLAB] = { "C", "O", "H", "S", "N", "P" };
114   int  i,i0,dh,dw,w,y,x1,x0;
115   unsigned long cind;
116   real h_2;
117   
118   XClearWindow(x11->disp, Win->self);
119   w=Win->width;
120   h_2=Win->height/(2.0*NLAB/COLS);
121   dh=h_2-2;
122   dw=dh;
123
124   for (i=0; (i<NLAB); i++) {
125     i0=i % (NLAB/COLS);
126     x0=(i / (NLAB/COLS))*(Win->width/COLS)+AIR;
127     x1=x0+2*dw+AIR;
128     cind=Type2Color(lab[i]);
129     XSetForeground(x11->disp,x11->gc,cind);
130     y=((2*i0+1)*h_2);
131     XFillRectangle (x11->disp,Win->self,x11->gc,x0,y-dh,2*dw,2*dh);
132     XSetForeground(x11->disp,x11->gc,WHITE);
133     TextInRect(x11,Win->self,lab[i],x1,y-dh,w-x1,2*dh,
134                eXLeft,eYCenter);
135   }
136   XSetForeground(x11->disp,x11->gc,x11->fg);
137 }
138
139 static gmx_bool LegWCallBack(t_x11 *x11,XEvent *event, Window w, void *data)
140 {
141   t_legendwin *lw;
142
143   lw=(t_legendwin *)data;
144   switch(event->type) {
145   case Expose:
146     DrawLegend(x11,&lw->wd);
147     break;
148   default:
149     break;
150   }
151   return FALSE;
152 }
153
154 t_legendwin *init_legw(t_x11 *x11,Window Parent,
155                        int x,int y,int width,int height,
156                        unsigned long fg,unsigned long bg)
157 {
158   t_legendwin *lw;
159   
160   snew(lw,1);
161   InitWin(&lw->wd,x,y,width,height,1,"Legend Window");
162   lw->wd.self=XCreateSimpleWindow(x11->disp,Parent,x,y,1,1,1,fg,bg);
163   x11->RegisterCallback(x11,lw->wd.self,Parent,LegWCallBack,lw);
164   x11->SetInputMask(x11,lw->wd.self,ExposureMask);
165
166   return lw;
167 }
168
169 void map_legw(t_x11 *x11,t_legendwin *lw)
170 {
171   XMapWindow(x11->disp,lw->wd.self);
172 }
173
174
175 void done_legw(t_x11 *x11,t_legendwin *lw)
176 {
177   x11->UnRegisterCallback(x11,lw->wd.self);
178   sfree(lw);
179 }
180