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