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