Merge release-4-6 into master
[alexxy/gromacs.git] / src / programs / view / nleg.cpp
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-2013, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <ctype.h>
42 #include <string.h>
43 #include <smalloc.h>
44 #include <macros.h>
45 #include "buttons.h"
46 #include "nleg.h"
47
48 typedef struct {
49     const char    *tp;
50     unsigned long *col;
51     t_rgb          rgb;
52 } t_atomcolor;
53
54 static t_atomcolor ac[] = {
55     { "O",  &LIGHTRED,     { 1,  0,  0   } },
56     { "N",  &LIGHTCYAN,    { 0,  0,  1   } },
57     { "NA", &LIGHTGREY,    { 0.6, 0.6, 0.6 } },
58     { "S",  &YELLOW,       { 1,  1,  0   } },
59     { "C",  &LIGHTGREEN,   { 0,  1,  0   } },
60     { "CL", &VIOLET,       { 1,  0,  1   } },
61     { "F",  &LIGHTGREY,    { 0.6, 0.6, 0.6 } },
62     { "Z",  &LIGHTGREY,    { 0.6, 0.6, 0.6 } },
63     { "P",  &LIGHTBLUE,    { 0.4, 0.4, 1.0 } },
64     { "H",  &WHITE,        { 0.8, 0.8, 0.8 } }
65 };
66 #define NAC asize(ac)
67
68 int search_ac(const char *type)
69 {
70     unsigned int i, nb, mij, best, besti;
71
72     best  = 0;
73     besti = 0;
74     if (NULL != type)
75     {
76         for (i = 0; (i < NAC); i++)
77         {
78             mij = std::min((int)strlen(type), (int)strlen(ac[i].tp));
79             for (nb = 0; (nb < mij); nb++)
80             {
81                 if (type[nb] != ac[i].tp[nb])
82                 {
83                     break;
84                 }
85             }
86             if (nb > best)
87             {
88                 best  = nb;
89                 besti = i;
90             }
91         }
92     }
93     return besti;
94 }
95
96 unsigned long Type2Color(const char *type)
97 {
98     int i;
99
100     i = search_ac(type);
101
102     return *(ac[i].col);
103 }
104
105 t_rgb *Type2RGB(const char *type)
106 {
107     int i;
108
109     i = search_ac(type);
110
111     return &(ac[i].rgb);
112 }
113
114 void DrawLegend(t_x11 *x11, t_windata *Win)
115 {
116 #define NLAB 6
117 #define COLS 3
118     static const char *lab[NLAB] = { "C", "O", "H", "S", "N", "P" };
119     int                i, i0, dh, dw, w, y, x1, x0;
120     unsigned long      cind;
121     real               h_2;
122
123     XClearWindow(x11->disp, Win->self);
124     w   = Win->width;
125     h_2 = Win->height/(2.0*NLAB/COLS);
126     dh  = h_2-2;
127     dw  = dh;
128
129     for (i = 0; (i < NLAB); i++)
130     {
131         i0   = i % (NLAB/COLS);
132         x0   = (i / (NLAB/COLS))*(Win->width/COLS)+AIR;
133         x1   = x0+2*dw+AIR;
134         cind = Type2Color(lab[i]);
135         XSetForeground(x11->disp, x11->gc, cind);
136         y = ((2*i0+1)*h_2);
137         XFillRectangle (x11->disp, Win->self, x11->gc, x0, y-dh, 2*dw, 2*dh);
138         XSetForeground(x11->disp, x11->gc, WHITE);
139         TextInRect(x11, Win->self, lab[i], x1, y-dh, w-x1, 2*dh,
140                    eXLeft, eYCenter);
141     }
142     XSetForeground(x11->disp, x11->gc, x11->fg);
143 }
144
145 static bool LegWCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
146 {
147     t_legendwin *lw;
148
149     lw = (t_legendwin *)data;
150     switch (event->type)
151     {
152         case Expose:
153             DrawLegend(x11, &lw->wd);
154             break;
155         default:
156             break;
157     }
158     return false;
159 }
160
161 t_legendwin *init_legw(t_x11 *x11, Window Parent,
162                        int x, int y, int width, int height,
163                        unsigned long fg, unsigned long bg)
164 {
165     t_legendwin *lw;
166
167     snew(lw, 1);
168     InitWin(&lw->wd, x, y, width, height, 1, "Legend Window");
169     lw->wd.self = XCreateSimpleWindow(x11->disp, Parent, x, y, 1, 1, 1, fg, bg);
170     x11->RegisterCallback(x11, lw->wd.self, Parent, LegWCallBack, lw);
171     x11->SetInputMask(x11, lw->wd.self, ExposureMask);
172
173     return lw;
174 }
175
176 void map_legw(t_x11 *x11, t_legendwin *lw)
177 {
178     XMapWindow(x11->disp, lw->wd.self);
179 }
180
181
182 void done_legw(t_x11 *x11, t_legendwin *lw)
183 {
184     x11->UnRegisterCallback(x11, lw->wd.self);
185     sfree(lw);
186 }