]> icculus.org git repositories - btb/d2x.git/blob - ui/message.c
dbc9590cca5c249c484b6378a97a004d5d51f199
[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 RCS
15 static char rcsid[] = "$Id: message.c,v 1.2 2004-12-19 14:10:33 btb Exp $";
16 #endif
17 #include <stdio.h>
18 #include <stdarg.h>
19
20 #include "fix.h"
21 #include "types.h"
22 #include "gr.h"
23 #include "ui.h"
24 #include "key.h"
25
26 // ts = total span
27 // w = width of each item
28 // n = number of items
29 // i = item number (0 based)
30 #define EVEN_DIVIDE(ts,w,n,i) ((((ts)-((w)*(n)))*((i)+1))/((n)+1))+((w)*(i))
31
32 #define BUTTON_HORZ_SPACING 20
33 #define TEXT_EXTRA_HEIGHT 5
34
35 int MessageBoxN( short xc, short yc, int NumButtons, char * text, char * Button[] )
36 {
37         grs_font * temp_font;
38         UI_WINDOW * wnd;
39         UI_GADGET_BUTTON * ButtonG[10];
40
41         int i, width, height, avg, x, y;
42         int button_width, button_height, text_height, text_width;
43         int w, h;
44
45         int choice;
46
47         if ((NumButtons < 1) || (NumButtons>10)) return -1;
48
49         button_width = button_height = 0;
50
51         gr_set_current_canvas( &grd_curscreen->sc_canvas );
52         w = grd_curscreen->sc_w;
53         h = grd_curscreen->sc_h;
54         temp_font = grd_curscreen->sc_canvas.cv_font;
55
56         if ( w < 640 )  {
57                 grd_curscreen->sc_canvas.cv_font = ui_small_font;
58         }
59
60         for (i=0; i<NumButtons; i++ )
61         {
62                 ui_get_button_size( Button[i], &width, &height );
63
64                 if ( width > button_width ) button_width = width;
65                 if ( height > button_height ) button_height = height;
66         }
67
68         gr_get_string_size(text, &text_width, &text_height, &avg );
69
70         width = button_width*NumButtons;
71         width += BUTTON_HORZ_SPACING*(NumButtons+1);
72         width ++;
73
74         text_width += avg*6;
75         text_width += 10;
76
77         if (text_width > width )
78                 width = text_width;
79
80         height = text_height;
81         height += button_height;
82         height += 4*TEXT_EXTRA_HEIGHT;
83         height += 2;  // For line in middle
84
85         if ( xc == -1 )
86                 xc = Mouse.x;
87
88         if ( yc == -1 )
89                 yc = Mouse.y - button_height/2;
90
91         if ( xc == -2 )
92                 xc = w/2;
93
94         if ( yc == -2 )
95                 yc = h/2;
96
97         x = xc - width/2;
98         y = yc - height/2;
99
100         if (x < 0 ) {
101                 x = 0;
102         }
103
104         if ( (x+width-1) >= w ) {
105                 x = w - width;
106         }
107
108         if (y < 0 ) {
109                 y = 0;
110         }
111
112         if ( (y+height-1) >= h ) {
113                 y = h - height;
114         }
115
116         wnd = ui_open_window( x, y, width, height, WIN_DIALOG );
117
118         //ui_draw_line_in( MESSAGEBOX_BORDER, MESSAGEBOX_BORDER, width-MESSAGEBOX_BORDER, height-MESSAGEBOX_BORDER );
119
120         y = TEXT_EXTRA_HEIGHT + text_height/2 - 1;
121
122         ui_string_centered( width/2, y, text );
123
124         y = 2*TEXT_EXTRA_HEIGHT + text_height;
125
126         gr_setcolor( CGREY );
127         Hline(1, width-2, y+1 );
128
129         gr_setcolor( CBRIGHT );
130         Hline(2, width-2, y+2 );
131
132         y = height - TEXT_EXTRA_HEIGHT - button_height;
133
134         for (i=0; i<NumButtons; i++ )
135         {
136
137                 x = EVEN_DIVIDE(width,button_width,NumButtons,i);
138
139                 ButtonG[i] = ui_add_gadget_button( wnd, x, y, button_width, button_height, Button[i], NULL );
140         }
141
142         ui_gadget_calc_keys(wnd);
143
144         //key_flush();
145
146         wnd->keyboard_focus_gadget = (UI_GADGET *)ButtonG[0];
147
148         choice = 0;
149
150         while(choice==0)
151         {
152                 ui_mega_process();
153                 ui_window_do_gadgets(wnd);
154
155                 for (i=0; i<NumButtons; i++ )
156                 {
157                         if (ButtonG[i]->pressed)   {
158                                 choice = i+1;
159                                 break;
160                         }
161                 }
162
163         }
164
165         ui_close_window(wnd);
166         
167         grd_curscreen->sc_canvas.cv_font = temp_font;
168
169         return choice;
170
171 }
172
173
174 int MessageBox( short xc, short yc, int NumButtons, char * text, ... )
175 {
176         va_list marker;
177         char * Button[10];
178
179         short i;
180
181         if ((NumButtons < 1) || (NumButtons>10)) return -1;
182
183         va_start( marker, text );
184         for (i=0; i<NumButtons; i++ )
185         {
186                 Button[i] = va_arg( marker, char * );
187
188         }
189         va_end( marker );
190
191
192         return MessageBoxN( xc, yc, NumButtons, text, Button );
193         
194 }