]> icculus.org git repositories - btb/d2x.git/blob - ui/number.c
divide negative window x-coordinates properly, fixing random crashes
[btb/d2x.git] / ui / number.c
1 /* $Id: number.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: number.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 <stdio.h>
24 #include <stdarg.h>
25
26 #include "fix.h"
27 #include "pstypes.h"
28 #include "gr.h"
29 #include "ui.h"
30
31 #define TEXT_EXTRA_HEIGHT 5
32
33 double ui_input_number( short xc, short yc, char * text, double OrgNumber )
34 {
35         UI_WINDOW * wnd;
36         UI_GADGET_INPUTBOX * InputBox;
37
38         int text_width, text_height, avg;
39         short box_width, box_height, width, height, x, y;
40         short w, h;
41         char string[100];
42
43         sprintf( string, "%f", OrgNumber );
44
45         box_width = box_height = 0;
46
47         gr_set_current_canvas( &grd_curscreen->sc_canvas );
48
49         box_width = 8*20;
50         box_height = 20;
51
52         gr_get_string_size(text, &text_width, &text_height, &avg );
53
54         width = box_width + 50;
55
56         text_width += avg*6;
57         text_width += 10;
58
59         if (text_width > width )
60                 width = text_width;
61
62         height = text_height;
63         height += box_height;
64         height += 4*5;
65
66         // Center X and Y
67         w = grd_curscreen->sc_w;
68         h = grd_curscreen->sc_h;
69
70         if ( xc == -1 ) xc = Mouse.x;
71         if ( yc == -1 ) yc = Mouse.y - box_height/2;
72         if ( xc == -2 ) xc = w/2;
73         if ( yc == -2 ) yc = h/2;
74
75         x = xc - width/2;
76         y = yc - height/2;
77
78         // Make sure that we're onscreen
79         if (x < 0 ) x = 0;
80         if ( (x+width-1) >= w ) x = w - width;
81         if (y < 0 ) y = 0;
82         if ( (y+height-1) >= h ) y = h - height;
83
84
85         wnd = ui_open_window( x, y, width, height, WIN_DIALOG );
86
87         y = TEXT_EXTRA_HEIGHT + text_height/2 - 1;
88
89         ui_string_centered( width/2, y, text );
90
91         y = 2*TEXT_EXTRA_HEIGHT + text_height;
92
93         y = height - TEXT_EXTRA_HEIGHT - box_height-10;
94
95         InputBox = ui_add_gadget_inputbox( wnd, 10, y, 20, 20, string );
96
97         ui_gadget_calc_keys(wnd);
98
99         //key_flush();
100
101         wnd->keyboard_focus_gadget = (UI_GADGET *)InputBox;
102
103         while(1)
104         {
105                 ui_mega_process();
106                 ui_window_do_gadgets(wnd);
107
108                 if (InputBox->pressed) break;
109
110                 gr_update();
111         }
112
113         ui_close_window(wnd);
114
115         OrgNumber = atof(InputBox->text);
116
117         return OrgNumber;
118
119 }
120
121