]> icculus.org git repositories - btb/d2x.git/blob - ui/message.c
remove rcs tags
[btb/d2x.git] / ui / message.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include "conf.h"
16 #endif
17
18 #include <stdio.h>
19 #include <stdarg.h>
20
21 #include "fix.h"
22 #include "pstypes.h"
23 #include "gr.h"
24 #include "ui.h"
25 #include "key.h"
26
27 // ts = total span
28 // w = width of each item
29 // n = number of items
30 // i = item number (0 based)
31 #define EVEN_DIVIDE(ts,w,n,i) ((((ts)-((w)*(n)))*((i)+1))/((n)+1))+((w)*(i))
32
33 #define BUTTON_HORZ_SPACING 20
34 #define TEXT_EXTRA_HEIGHT 5
35
36 int MessageBoxN( short xc, short yc, int NumButtons, char * text, char * Button[] )
37 {
38         grs_font * temp_font;
39         UI_WINDOW * wnd;
40         UI_GADGET_BUTTON * ButtonG[10];
41
42         int i, width, height, avg, x, y;
43         int button_width, button_height, text_height, text_width;
44         int w, h;
45
46         int choice;
47
48         if ((NumButtons < 1) || (NumButtons>10)) return -1;
49
50         button_width = button_height = 0;
51
52         gr_set_current_canvas( &grd_curscreen->sc_canvas );
53         w = grd_curscreen->sc_w;
54         h = grd_curscreen->sc_h;
55         temp_font = grd_curscreen->sc_canvas.cv_font;
56
57         if ( w < 640 )  {
58                 grd_curscreen->sc_canvas.cv_font = ui_small_font;
59         }
60
61         for (i=0; i<NumButtons; i++ )
62         {
63                 ui_get_button_size( Button[i], &width, &height );
64
65                 if ( width > button_width ) button_width = width;
66                 if ( height > button_height ) button_height = height;
67         }
68
69         gr_get_string_size(text, &text_width, &text_height, &avg );
70
71         width = button_width*NumButtons;
72         width += BUTTON_HORZ_SPACING*(NumButtons+1);
73         width ++;
74
75         text_width += avg*6;
76         text_width += 10;
77
78         if (text_width > width )
79                 width = text_width;
80
81         height = text_height;
82         height += button_height;
83         height += 4*TEXT_EXTRA_HEIGHT;
84         height += 2;  // For line in middle
85
86         if ( xc == -1 )
87                 xc = Mouse.x;
88
89         if ( yc == -1 )
90                 yc = Mouse.y - button_height/2;
91
92         if ( xc == -2 )
93                 xc = w/2;
94
95         if ( yc == -2 )
96                 yc = h/2;
97
98         x = xc - width/2;
99         y = yc - height/2;
100
101         if (x < 0 ) {
102                 x = 0;
103         }
104
105         if ( (x+width-1) >= w ) {
106                 x = w - width;
107         }
108
109         if (y < 0 ) {
110                 y = 0;
111         }
112
113         if ( (y+height-1) >= h ) {
114                 y = h - height;
115         }
116
117         wnd = ui_open_window( x, y, width, height, WIN_DIALOG );
118
119         //ui_draw_line_in( MESSAGEBOX_BORDER, MESSAGEBOX_BORDER, width-MESSAGEBOX_BORDER, height-MESSAGEBOX_BORDER );
120
121         y = TEXT_EXTRA_HEIGHT + text_height/2 - 1;
122
123         ui_string_centered( width/2, y, text );
124
125         y = 2*TEXT_EXTRA_HEIGHT + text_height;
126
127         gr_setcolor( CGREY );
128         Hline(1, width-2, y+1 );
129
130         gr_setcolor( CBRIGHT );
131         Hline(2, width-2, y+2 );
132
133         y = height - TEXT_EXTRA_HEIGHT - button_height;
134
135         for (i=0; i<NumButtons; i++ )
136         {
137
138                 x = EVEN_DIVIDE(width,button_width,NumButtons,i);
139
140                 ButtonG[i] = ui_add_gadget_button( wnd, x, y, button_width, button_height, Button[i], NULL );
141         }
142
143         ui_gadget_calc_keys(wnd);
144
145         //key_flush();
146
147         wnd->keyboard_focus_gadget = (UI_GADGET *)ButtonG[0];
148
149         choice = 0;
150
151         while(choice==0)
152         {
153                 ui_mega_process();
154                 ui_window_do_gadgets(wnd);
155
156                 for (i=0; i<NumButtons; i++ )
157                 {
158                         if (ButtonG[i]->pressed)   {
159                                 choice = i+1;
160                                 break;
161                         }
162                 }
163
164                 gr_update();
165         }
166
167         ui_close_window(wnd);
168         
169         grd_curscreen->sc_canvas.cv_font = temp_font;
170
171         return choice;
172
173 }
174
175
176 int MessageBox( short xc, short yc, int NumButtons, char * text, ... )
177 {
178         va_list marker;
179         char * Button[10];
180
181         short i;
182
183         if ((NumButtons < 1) || (NumButtons>10)) return -1;
184
185         va_start( marker, text );
186         for (i=0; i<NumButtons; i++ )
187         {
188                 Button[i] = va_arg( marker, char * );
189
190         }
191         va_end( marker );
192
193
194         return MessageBoxN( xc, yc, NumButtons, text, Button );
195         
196 }