Sort includes outside src/gromacs
[alexxy/gromacs.git] / src / programs / view / xmb.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 #include "gmxpre.h"
38
39 #include "xmb.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "gromacs/legacyheaders/macros.h"
46 #include "gromacs/legacyheaders/typedefs.h"
47 #include "gromacs/utility/fatalerror.h"
48
49 #include "Xstuff.h"
50 #include "alert.bm"
51 #include "gromacs.bm"
52 #include "info.bm"
53 #include "stop.bm"
54 #include "x11.h"
55 #include "xdlg.h"
56 #include "xutil.h"
57
58 #define ID_BOX     -3
59 #define ID_ICON    -2
60 #define ID_TEXT    -1
61
62 static bmchar         *icon_bits   = NULL;
63 static int             icon_width  = 0;
64 static int             icon_height = 0;
65 static unsigned long   icon_fg     = 0;
66 static unsigned long   icon_bg     = 0;
67
68 void SetIcon(unsigned char *bits, int w, int h, unsigned long fg, unsigned long bg)
69 {
70     icon_bits   = (bmchar *)bits;
71     icon_width  = w;
72     icon_height = h;
73     icon_fg     = fg;
74     icon_bg     = bg;
75 }
76
77 t_dlg *MessageBox(t_x11 *x11, Window Parent, const char *title,
78                   int nlines, const char * const * lines, unsigned long Flags,
79                   DlgCallback *cb, void *data)
80 {
81     t_dlg                *dlg;
82     int                   width, nicon;
83     int                   x, y, x0;
84     unsigned long         nFlag;
85     unsigned long         bg;
86
87     /* Check flags for inconsistencies */
88     if (((Flags & MB_OK) && (Flags & MB_YES))     ||
89         ((Flags & MB_NO) && (Flags & MB_CANCEL))  ||
90         (!(Flags & MB_OK) && !(Flags & MB_YES)))
91     {
92         fprintf(stderr, "Invalid button selection in MessageBox\n");
93         exit(1);
94     }
95     nicon = 0;
96     if (Flags & MB_ICONSTOP)
97     {
98         nicon++;
99     }
100     if (Flags & MB_ICONINFORMATION)
101     {
102         nicon++;
103     }
104     if (Flags & MB_ICONEXCLAMATION)
105     {
106         nicon++;
107     }
108     if (Flags & MB_ICONGMX)
109     {
110         nicon++;
111     }
112     if (nicon > 1)
113     {
114         gmx_fatal(FARGS, "More than one (%d) icon selected in MessageBox", nicon);
115     }
116     /* Input seems ok */
117     bg = x11->bg;
118     if (nicon > 0)
119     {
120         if (Flags & MB_ICONSTOP)
121         {
122             SetIcon(stop_bits, stop_width, stop_height, RED, bg);
123         }
124         if (Flags & MB_ICONINFORMATION)
125         {
126             SetIcon(info_bits, info_width, info_height, BLUE, bg);
127         }
128         if (Flags & MB_ICONEXCLAMATION)
129         {
130             SetIcon(alert_bits, alert_width, alert_height, GREEN, bg);
131         }
132         if (Flags & MB_ICONGMX)
133         {
134             SetIcon(gromacs_bits, gromacs_width, gromacs_height, BLUE, bg);
135         }
136     }
137
138     dlg = CreateDlg(x11, Parent, title, 0, 0, 0, 0, 3, cb, data);
139     x   = 2*OFFS_X;
140     if (nicon > 0)
141     {
142         AddDlgItem(dlg, CreatePixmap
143                        (XCreatePixmapFromBitmapData
144                            (x11->disp, dlg->win.self, icon_bits, icon_width, icon_height,
145                            icon_fg, icon_bg, x11->depth),
146                        ID_ICON, ID_BOX, 2*OFFS_X, 2*OFFS_Y, icon_width, icon_height, 0));
147         x += QueryDlgItemW(dlg, ID_ICON)+2*OFFS_X;
148     }
149
150     AddDlgItem(dlg, CreateStaticText(x11, nlines, lines, ID_TEXT, ID_BOX,
151                                      x, 2*OFFS_Y, 0, 0, 0));
152
153     y = QueryDlgItemY(dlg, ID_TEXT)+QueryDlgItemH(dlg, ID_TEXT);
154     if (nicon > 0)
155     {
156         int yi;
157         yi = QueryDlgItemY(dlg, ID_ICON)+QueryDlgItemH(dlg, ID_ICON);
158         if (yi > y)
159         {
160             SetDlgItemPos(dlg, ID_TEXT, x, 2*OFFS_Y+(yi-y)/2);
161         }
162         else
163         {
164             SetDlgItemPos(dlg, ID_ICON, 2*OFFS_X, 2*OFFS_Y+(y-yi)/2);
165         }
166         if (yi > y)
167         {
168             y = yi;
169         }
170     }
171     x    += QueryDlgItemW(dlg, ID_TEXT)+2*OFFS_X;
172     y    += 2*OFFS_Y;
173     width = (x-8*OFFS_X)/2;
174
175     if (((Flags & MB_OKCANCEL) == MB_OKCANCEL) ||
176         ((Flags & MB_YESNO) == MB_YESNO))
177     {
178         x0 = 2*OFFS_X;
179     }
180     else
181     {
182         x0 = (x-width)/2;
183     }
184
185 #define CB(name, butx, id) AddDlgItem(dlg, CreateButton(x11, name, \
186                                                         true, id, ID_BOX, \
187                                                         butx, y, width, 0, 0))
188     if (Flags & MB_OK)
189     {
190         CB("OK", x0, MB_OK);
191     }
192     if (Flags & MB_CANCEL)
193     {
194         CB("Cancel", x/2+2*OFFS_X, MB_CANCEL);
195     }
196     if (Flags & MB_YES)
197     {
198         CB("Yes", x0, MB_YES);
199     }
200     if (Flags & MB_NO)
201     {
202         CB("No", x/2+2*OFFS_X, MB_NO);
203     }
204
205     SetDlgSize(dlg, x, y+2*OFFS_Y+
206                QueryDlgItemH(dlg, (Flags & MB_OK) ? MB_OK : MB_YES), true);
207
208     if (Flags & MB_SYSTEMMODAL)
209     {
210         nFlag = DLG_SYSTEMMODAL;
211     }
212     else if (Flags & MB_APPLMODAL)
213     {
214         nFlag = DLG_APPLMODAL;
215     }
216     else
217     {
218         nFlag = 0;
219     }
220     nFlag      = nFlag | DLG_FREEONBUTTON;
221     dlg->flags = nFlag;
222
223     if (!(Flags & MB_DONTSHOW))
224     {
225         ShowDlg(dlg);
226     }
227
228     return dlg;
229 }