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