Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / ngmx / xmb.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 <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "typedefs.h"
47 #include "macros.h"
48 #include "Xstuff.h"
49 #include "x11.h"
50 #include "xdlg.h"
51 #include "xmb.h"
52 #include "gmx_fatal.h"
53 #include "gromacs.bm"
54 #include "stop.bm"
55 #include "info.bm"
56 #include "alert.bm"
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, char ** 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     fprintf(stderr,"Invalid button selection in MessageBox\n");
92     exit(1);
93   }
94   nicon=0;
95   if (Flags & MB_ICONSTOP) nicon++;
96   if (Flags & MB_ICONINFORMATION) nicon++;
97   if (Flags & MB_ICONEXCLAMATION) nicon++;
98   if (Flags & MB_ICONGMX) nicon++;
99   if (nicon > 1) 
100     gmx_fatal(FARGS,"More than one (%d) icon selected in MessageBox",nicon);
101   /* Input seems ok */
102   bg=x11->bg;
103   if (nicon > 0) {
104     if (Flags & MB_ICONSTOP)
105       SetIcon(stop_bits,stop_width,stop_height,RED,bg);
106     if (Flags & MB_ICONINFORMATION)
107       SetIcon(info_bits,info_width,info_height,BLUE,bg);
108     if (Flags & MB_ICONEXCLAMATION)
109       SetIcon(alert_bits,alert_width,alert_height,GREEN,bg);
110     if (Flags & MB_ICONGMX)
111       SetIcon(gromacs_bits,gromacs_width,gromacs_height,BLUE,bg);
112   }
113   
114   dlg=CreateDlg(x11,Parent,title,0,0,0,0,3,x11->fg,bg,cb,data);
115   x=2*OFFS_X;
116   if (nicon > 0) {
117     AddDlgItem(dlg,CreatePixmap
118                (x11,XCreatePixmapFromBitmapData
119                 (x11->disp,dlg->win.self,icon_bits,icon_width,icon_height,
120                  icon_fg,icon_bg,x11->depth),
121                 ID_ICON,ID_BOX,2*OFFS_X,2*OFFS_Y,icon_width,icon_height,0));
122     x+=QueryDlgItemW(dlg,ID_ICON)+2*OFFS_X;
123   }
124   
125   AddDlgItem(dlg,CreateStaticText(x11,nlines,lines,ID_TEXT,ID_BOX,
126                                   x,2*OFFS_Y,0,0,0));
127
128   y=QueryDlgItemY(dlg,ID_TEXT)+QueryDlgItemH(dlg,ID_TEXT);
129   if (nicon > 0) {
130     int yi;
131     yi=QueryDlgItemY(dlg,ID_ICON)+QueryDlgItemH(dlg,ID_ICON);
132     if (yi > y)
133       SetDlgItemPos(dlg,ID_TEXT,x,2*OFFS_Y+(yi-y)/2);
134     else
135       SetDlgItemPos(dlg,ID_ICON,2*OFFS_X,2*OFFS_Y+(y-yi)/2);
136     y=max(y,yi);
137   }
138   x+=QueryDlgItemW(dlg,ID_TEXT)+2*OFFS_X;
139   y+=2*OFFS_Y;
140   width=(x-8*OFFS_X)/2;
141   
142   if (((Flags & MB_OKCANCEL) == MB_OKCANCEL) ||
143       ((Flags & MB_YESNO) == MB_YESNO))
144     x0=2*OFFS_X;
145   else
146     x0=(x-width)/2;
147
148 #define CB(name,butx,id) AddDlgItem(dlg,CreateButton(x11,name,\
149                                                      TRUE,id,ID_BOX,\
150                                                      butx,y,width,0,0))
151   if (Flags & MB_OK) CB("OK",x0,MB_OK);
152   if (Flags & MB_CANCEL) CB("Cancel",x/2+2*OFFS_X,MB_CANCEL);
153   if (Flags & MB_YES) CB("Yes",x0,MB_YES);
154   if (Flags & MB_NO) CB("No",x/2+2*OFFS_X,MB_NO);
155
156   SetDlgSize(dlg,x,y+2*OFFS_Y+
157              QueryDlgItemH(dlg,(Flags & MB_OK) ? MB_OK : MB_YES),TRUE);
158
159   if (Flags & MB_SYSTEMMODAL)
160     nFlag=DLG_SYSTEMMODAL;
161   else if (Flags & MB_APPLMODAL)
162     nFlag=DLG_APPLMODAL;
163   else
164     nFlag=0;
165   nFlag=nFlag | DLG_FREEONBUTTON;
166   dlg->flags=nFlag;
167
168   if (!(Flags & MB_DONTSHOW))
169     ShowDlg(dlg);
170
171   return dlg;
172 }