Tagged files with gromacs 3.0 header and added some license info
[alexxy/gromacs.git] / src / ngmx / xmb.c
1 /*
2  * $Id$
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.0
11  * 
12  * Copyright (c) 1991-2001
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
32  * 
33  * And Hey:
34  * Good gRace! Old Maple Actually Chews Slate
35  */
36 static char *SRCID_xmb_c = "$Id$";
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "typedefs.h"
42 #include "macros.h"
43 #include "Xstuff.h"
44 #include "x11.h"
45 #include "xdlg.h"
46 #include "xmb.h"
47 #include "fatal.h"
48 #include "gromacs.bm"
49 #include "stop.bm"
50 #include "info.bm"
51 #include "alert.bm"
52
53 #define ID_BOX     -3
54 #define ID_ICON    -2
55 #define ID_TEXT    -1
56
57 static bmchar *icon_bits=NULL;
58 static int     icon_width=0;
59 static int     icon_height=0;
60 static unsigned long   icon_fg=0;
61 static unsigned long   icon_bg=0;
62
63 void SetIcon(unsigned char *bits, int w, int h, unsigned long fg, unsigned long bg)
64 {
65   icon_bits=(bmchar *)bits;
66   icon_width=w;
67   icon_height=h;
68   icon_fg=fg;
69   icon_bg=bg;
70 }
71
72 t_dlg *MessageBox(t_x11 *x11, Window Parent, char *title,
73                   int nlines, char *lines[], unsigned long Flags,
74                   DlgCallback *cb, void *data)
75 {
76   t_dlg         *dlg;
77   int           width,nicon;
78   int           x,y,x0;
79   unsigned long         nFlag;
80   unsigned long         bg;
81   
82   /* Check flags for inconsistencies */
83   if (((Flags & MB_OK) && (Flags & MB_YES))     ||
84       ((Flags & MB_NO) && (Flags & MB_CANCEL))  ||
85       (!(Flags & MB_OK) && !(Flags & MB_YES))) {
86     fprintf(stderr,"Invalid button selection in MessageBox\n");
87     exit(1);
88   }
89   nicon=0;
90   if (Flags & MB_ICONSTOP) nicon++;
91   if (Flags & MB_ICONINFORMATION) nicon++;
92   if (Flags & MB_ICONEXCLAMATION) nicon++;
93   if (Flags & MB_ICONGMX) nicon++;
94   if (nicon > 1) 
95     fatal_error(0,"More than one (%d) icon selected in MessageBox",nicon);
96   /* Input seems ok */
97   bg=x11->bg;
98   if (nicon > 0) {
99     if (Flags & MB_ICONSTOP)
100       SetIcon(stop_bits,stop_width,stop_height,RED,bg);
101     if (Flags & MB_ICONINFORMATION)
102       SetIcon(info_bits,info_width,info_height,BLUE,bg);
103     if (Flags & MB_ICONEXCLAMATION)
104       SetIcon(alert_bits,alert_width,alert_height,GREEN,bg);
105     if (Flags & MB_ICONGMX)
106       SetIcon(gromacs_bits,gromacs_width,gromacs_height,BLUE,bg);
107   }
108   
109   dlg=CreateDlg(x11,Parent,title,0,0,0,0,3,x11->fg,bg,cb,data);
110   x=2*OFFS_X;
111   if (nicon > 0) {
112     AddDlgItem(dlg,CreatePixmap
113                (x11,XCreatePixmapFromBitmapData
114                 (x11->disp,dlg->win.self,icon_bits,icon_width,icon_height,
115                  icon_fg,icon_bg,x11->depth),
116                 ID_ICON,ID_BOX,2*OFFS_X,2*OFFS_Y,icon_width,icon_height,0));
117     x+=QueryDlgItemW(dlg,ID_ICON)+2*OFFS_X;
118   }
119   
120   AddDlgItem(dlg,CreateStaticText(x11,nlines,lines,ID_TEXT,ID_BOX,
121                                   x,2*OFFS_Y,0,0,0));
122
123   y=QueryDlgItemY(dlg,ID_TEXT)+QueryDlgItemH(dlg,ID_TEXT);
124   if (nicon > 0) {
125     int yi;
126     yi=QueryDlgItemY(dlg,ID_ICON)+QueryDlgItemH(dlg,ID_ICON);
127     if (yi > y)
128       SetDlgItemPos(dlg,ID_TEXT,x,2*OFFS_Y+(yi-y)/2);
129     else
130       SetDlgItemPos(dlg,ID_ICON,2*OFFS_X,2*OFFS_Y+(y-yi)/2);
131     y=max(y,yi);
132   }
133   x+=QueryDlgItemW(dlg,ID_TEXT)+2*OFFS_X;
134   y+=2*OFFS_Y;
135   width=(x-8*OFFS_X)/2;
136   
137   if (((Flags & MB_OKCANCEL) == MB_OKCANCEL) ||
138       ((Flags & MB_YESNO) == MB_YESNO))
139     x0=2*OFFS_X;
140   else
141     x0=(x-width)/2;
142
143 #define CB(name,butx,id) AddDlgItem(dlg,CreateButton(x11,name,\
144                                                      TRUE,id,ID_BOX,\
145                                                      butx,y,width,0,0))
146   if (Flags & MB_OK) CB("OK",x0,MB_OK);
147   if (Flags & MB_CANCEL) CB("Cancel",x/2+2*OFFS_X,MB_CANCEL);
148   if (Flags & MB_YES) CB("Yes",x0,MB_YES);
149   if (Flags & MB_NO) CB("No",x/2+2*OFFS_X,MB_NO);
150
151   SetDlgSize(dlg,x,y+2*OFFS_Y+
152              QueryDlgItemH(dlg,(Flags & MB_OK) ? MB_OK : MB_YES),TRUE);
153
154   if (Flags & MB_SYSTEMMODAL)
155     nFlag=DLG_SYSTEMMODAL;
156   else if (Flags & MB_APPLMODAL)
157     nFlag=DLG_APPLMODAL;
158   else
159     nFlag=0;
160   nFlag=nFlag | DLG_FREEONBUTTON;
161   dlg->flags=nFlag;
162
163   if (!(Flags & MB_DONTSHOW))
164     ShowDlg(dlg);
165
166   return dlg;
167 }