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