]> icculus.org git repositories - btb/d2x.git/blob - ui/popup.c
header fixes
[btb/d2x.git] / ui / popup.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 "fix.h"
19 #include "pstypes.h"
20 #include "gr.h"
21 #include "ui.h"
22 #include "mouse.h"
23 #include "vid.h"
24
25
26 #define MENU_BORDER 2
27 #define MENU_VERT_SPACING 2
28
29 extern void ui_mouse_flip_buttons();
30
31 int PopupMenu( int NumButtons, char * text[] )
32 {
33         UI_WINDOW * wnd;
34         UI_GADGET_BUTTON * ButtonG[10];
35
36         short SavedMouseX, SavedMouseY;
37         char * Button[10];
38
39         int button_width, button_height, width, height;
40
41         short i, x, y;
42         short w, h;
43
44         int choice;
45
46         ui_mouse_flip_buttons();
47
48         //ui_mouse_process();
49
50         if ( B1_RELEASED )
51         {
52                 ui_mouse_flip_buttons();
53                 return -1;
54         }
55
56         if ((NumButtons < 1) || (NumButtons>10))
57         {
58                 ui_mouse_flip_buttons();
59                 return -1;
60         }
61
62         SavedMouseX = Mouse.x; SavedMouseY = Mouse.y;
63
64         button_width = button_height = 0;
65
66         gr_set_current_canvas( &grd_curscreen->sc_canvas );
67
68         for (i=0; i<NumButtons; i++ )
69         {
70                 Button[i] = text[i];
71
72                 ui_get_button_size( Button[i], &width, &height );
73
74                 if ( width > button_width ) button_width = width;
75                 if ( height > button_height ) button_height = height;
76         }
77
78         width = button_width + 2*(MENU_BORDER+3);
79
80         height = (button_height*NumButtons) + (MENU_VERT_SPACING*(NumButtons-1)) ;
81         height += (MENU_BORDER+3) * 2;
82
83         x = Mouse.x - width/2;
84         y = Mouse.y - (MENU_BORDER+3) - button_height/2;
85
86         w = grd_curscreen->sc_w;
87         h = grd_curscreen->sc_h;
88
89         if (x < 0 ) {
90                 x = 0;
91                 Mouse.x = x + width / 2;
92         }
93
94         if ( (x+width-1) >= w ) {
95                 x = w - width;
96                 Mouse.x = x + width / 2;
97         }
98
99         if (y < 0 ) {
100                 y = 0;
101                 Mouse.y = y + (MENU_BORDER+3) + button_height/2;
102         }
103
104         if ( (y+height-1) >= h ) {
105                 y = h - height;
106                 Mouse.y = y + (MENU_BORDER+3) + button_height/2;
107         }
108
109         wnd = ui_open_window( x, y, width, height, WIN_DIALOG );
110
111         //mouse_set_pos(Mouse.x, Mouse.y);
112
113         x = MENU_BORDER+3;
114         y = MENU_BORDER+3;
115
116         for (i=0; i<NumButtons; i++ )
117         {
118                 ButtonG[i] = ui_add_gadget_button( wnd, x, y, button_width, button_height, Button[i], NULL );
119                 y += button_height+MENU_VERT_SPACING;
120         }
121
122         choice = 0;
123
124         while(choice==0)
125         {
126                 ui_mega_process();
127                 ui_window_do_gadgets(wnd);
128
129                 for (i=0; i<NumButtons; i++ )
130                 {
131                         if (ButtonG[i]->pressed)   {
132                                 choice = i+1;
133                                 break;
134                         }
135                 }
136
137                 if ( (choice==0) && B1_JUST_RELEASED )  {
138                         choice = -1;
139                         break;
140                 }
141
142                 vid_update();
143         }
144
145         ui_close_window(wnd);
146
147         ui_mouse_flip_buttons();
148
149         return choice;
150
151 }
152