]> icculus.org git repositories - btb/d2x.git/blob - ui/menu.c
header fixes
[btb/d2x.git] / ui / menu.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 <stdlib.h>
19
20 #include "u_mem.h"
21 #include "fix.h"
22 #include "pstypes.h"
23 #include "gr.h"
24 #include "ui.h"
25 #include "vid.h"
26
27
28 #define MENU_BORDER 2
29 #define MENU_VERT_SPACING 2
30
31 int MenuX( int x, int y, int NumButtons, char * text[] )
32 {
33         UI_WINDOW * wnd;
34         UI_GADGET_BUTTON ** ButtonG;
35         char ** Button;
36
37         int button_width, button_height, width, height;
38
39         int i;
40         int w, h;
41
42         int choice;
43
44         ButtonG = (UI_GADGET_BUTTON **) d_malloc(sizeof(UI_GADGET_BUTTON *)*NumButtons);
45         Button = (char **) d_malloc(sizeof(char *)*NumButtons);
46
47         button_width = button_height = 0;
48
49         for (i=0; i<NumButtons; i++ )
50         {
51                 Button[i] = text[i];
52
53                 ui_get_button_size( Button[i], &width, &height );
54
55                 if ( width > button_width ) button_width = width;
56                 if ( height > button_height ) button_height = height;
57         }
58
59         width = button_width + 2*(MENU_BORDER+3);
60
61         height = (button_height*NumButtons) + (MENU_VERT_SPACING*(NumButtons-1)) ;
62         height += (MENU_BORDER+3) * 2;
63
64         w = grd_curscreen->sc_w;
65         h = grd_curscreen->sc_h;
66
67         if ( x == -1 ) x = Mouse.x - width/2;
68         if ( y == -1 ) y = Mouse.y;
69
70         if (x < 0 ) {
71                 x = 0;
72         }
73
74         if ( (x+width-1) >= w ) {
75                 x = w - width;
76         }
77
78         if (y < 0 ) {
79                 y = 0;
80         }
81
82         if ( (y+height-1) >= h ) {
83                 y = h - height;
84         }
85
86         wnd = ui_open_window( x, y, width, height, WIN_FILLED | WIN_SAVE_BG );
87
88         x = MENU_BORDER+3;
89         y = MENU_BORDER+3;
90
91         for (i=0; i<NumButtons; i++ )
92         {
93                 ButtonG[i] = ui_add_gadget_button( wnd, x, y, button_width, button_height, Button[i], NULL );
94                 y += button_height+MENU_VERT_SPACING;
95         }
96
97         choice = 0;
98
99         while(choice==0)
100         {
101                 ui_mega_process();
102                 ui_window_do_gadgets(wnd);
103
104                 for (i=0; i<NumButtons; i++ )
105                 {
106                         if (ButtonG[i]->pressed)   {
107                                 choice = i+1;
108                                 break;
109                         }
110                 }
111
112                 if ( (choice==0) && B1_JUST_RELEASED )  {
113                         choice = -1;
114                         break;
115                 }
116
117                 vid_update();
118         }
119
120         ui_close_window(wnd);
121         d_free(Button);
122         d_free(ButtonG);
123
124         return choice;
125
126 }
127