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