d229fe323f2aa3c4c0769a2068fdd83d771a8063
[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 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please refer to:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Great Red Oystrich Makes All Chemists Sane
28  */
29 static char *SRCID_xmb_c = "$Id$";
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "typedefs.h"
36 #include "macros.h"
37 #include "Xstuff.h"
38 #include "x11.h"
39 #include "xdlg.h"
40 #include "xmb.h"
41 #include "fatal.h"
42 #include "gromacs.bm"
43 #include "stop.bm"
44 #include "info.bm"
45 #include "alert.bm"
46
47 #define ID_BOX     -3
48 #define ID_ICON    -2
49 #define ID_TEXT    -1
50
51 static bmchar *icon_bits=NULL;
52 static int     icon_width=0;
53 static int     icon_height=0;
54 static unsigned long   icon_fg=0;
55 static unsigned long   icon_bg=0;
56
57 void SetIcon(unsigned char *bits, int w, int h, unsigned long fg, unsigned long bg)
58 {
59   icon_bits=(bmchar *)bits;
60   icon_width=w;
61   icon_height=h;
62   icon_fg=fg;
63   icon_bg=bg;
64 }
65
66 t_dlg *MessageBox(t_x11 *x11, Window Parent, char *title,
67                   int nlines, char *lines[], unsigned long Flags,
68                   DlgCallback *cb, void *data)
69 {
70   t_dlg         *dlg;
71   int           width,nicon;
72   int           x,y,x0;
73   unsigned long         nFlag;
74   unsigned long         bg;
75   
76   /* Check flags for inconsistencies */
77   if (((Flags & MB_OK) && (Flags & MB_YES))     ||
78       ((Flags & MB_NO) && (Flags & MB_CANCEL))  ||
79       (!(Flags & MB_OK) && !(Flags & MB_YES))) {
80     fprintf(stderr,"Invalid button selection in MessageBox\n");
81     exit(1);
82   }
83   nicon=0;
84   if (Flags & MB_ICONSTOP) nicon++;
85   if (Flags & MB_ICONINFORMATION) nicon++;
86   if (Flags & MB_ICONEXCLAMATION) nicon++;
87   if (Flags & MB_ICONGMX) nicon++;
88   if (nicon > 1) 
89     fatal_error(0,"More than one (%d) icon selected in MessageBox",nicon);
90   /* Input seems ok */
91   bg=x11->bg;
92   if (nicon > 0) {
93     if (Flags & MB_ICONSTOP)
94       SetIcon(stop_bits,stop_width,stop_height,RED,bg);
95     if (Flags & MB_ICONINFORMATION)
96       SetIcon(info_bits,info_width,info_height,BLUE,bg);
97     if (Flags & MB_ICONEXCLAMATION)
98       SetIcon(alert_bits,alert_width,alert_height,GREEN,bg);
99     if (Flags & MB_ICONGMX)
100       SetIcon(gromacs_bits,gromacs_width,gromacs_height,BLUE,bg);
101   }
102   
103   dlg=CreateDlg(x11,Parent,title,0,0,0,0,3,x11->fg,bg,cb,data);
104   x=2*OFFS_X;
105   if (nicon > 0) {
106     AddDlgItem(dlg,CreatePixmap
107                (x11,XCreatePixmapFromBitmapData
108                 (x11->disp,dlg->win.self,icon_bits,icon_width,icon_height,
109                  icon_fg,icon_bg,x11->depth),
110                 ID_ICON,ID_BOX,2*OFFS_X,2*OFFS_Y,icon_width,icon_height,0));
111     x+=QueryDlgItemW(dlg,ID_ICON)+2*OFFS_X;
112   }
113   
114   AddDlgItem(dlg,CreateStaticText(x11,nlines,lines,ID_TEXT,ID_BOX,
115                                   x,2*OFFS_Y,0,0,0));
116
117   y=QueryDlgItemY(dlg,ID_TEXT)+QueryDlgItemH(dlg,ID_TEXT);
118   if (nicon > 0) {
119     int yi;
120     yi=QueryDlgItemY(dlg,ID_ICON)+QueryDlgItemH(dlg,ID_ICON);
121     if (yi > y)
122       SetDlgItemPos(dlg,ID_TEXT,x,2*OFFS_Y+(yi-y)/2);
123     else
124       SetDlgItemPos(dlg,ID_ICON,2*OFFS_X,2*OFFS_Y+(y-yi)/2);
125     y=max(y,yi);
126   }
127   x+=QueryDlgItemW(dlg,ID_TEXT)+2*OFFS_X;
128   y+=2*OFFS_Y;
129   width=(x-8*OFFS_X)/2;
130   
131   if (((Flags & MB_OKCANCEL) == MB_OKCANCEL) ||
132       ((Flags & MB_YESNO) == MB_YESNO))
133     x0=2*OFFS_X;
134   else
135     x0=(x-width)/2;
136
137 #define CB(name,butx,id) AddDlgItem(dlg,CreateButton(x11,name,\
138                                                      TRUE,id,ID_BOX,\
139                                                      butx,y,width,0,0))
140   if (Flags & MB_OK) CB("OK",x0,MB_OK);
141   if (Flags & MB_CANCEL) CB("Cancel",x/2+2*OFFS_X,MB_CANCEL);
142   if (Flags & MB_YES) CB("Yes",x0,MB_YES);
143   if (Flags & MB_NO) CB("No",x/2+2*OFFS_X,MB_NO);
144
145   SetDlgSize(dlg,x,y+2*OFFS_Y+
146              QueryDlgItemH(dlg,(Flags & MB_OK) ? MB_OK : MB_YES),TRUE);
147
148   if (Flags & MB_SYSTEMMODAL)
149     nFlag=DLG_SYSTEMMODAL;
150   else if (Flags & MB_APPLMODAL)
151     nFlag=DLG_APPLMODAL;
152   else
153     nFlag=0;
154   nFlag=nFlag | DLG_FREEONBUTTON;
155   dlg->flags=nFlag;
156
157   if (!(Flags & MB_DONTSHOW))
158     ShowDlg(dlg);
159
160   return dlg;
161 }