get rid of compiler warnings
[btb/d2x.git] / ui / inputbox.c
1 /* $Id: inputbox.c,v 1.6 2005-02-26 09:50:36 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: inputbox.c,v 1.6 2005-02-26 09:50:36 chris Exp $";
17 #endif
18
19 #ifdef HAVE_CONFIG_H
20 #include "conf.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "u_mem.h"
27 #include "fix.h"
28 #include "pstypes.h"
29 #include "gr.h"
30 #include "ui.h"
31 #include "key.h"
32
33 // insert character c into string s at position p.
34 void strcins(char *s, int p, char c)
35 {
36         int n;
37         for (n=strlen(s)-p; n>=0; n-- )
38                 *(s+p+n+1) = *(s+p+n);   // Move everything over
39         *(s+p) = c;         // then insert the character
40 }
41
42 // delete n character from string s starting at position p
43
44 void strndel(char *s, int p, int n)
45 {
46         for (; (*(s+p) = *(s+p+n)) != '\0'; s++ )
47                 *(s+p+n) = '\0';    // Delete and zero fill
48 }
49
50 void ui_draw_inputbox( UI_GADGET_INPUTBOX * inputbox )
51 {
52         int w, h, aw;
53
54         if ((inputbox->status==1) || (inputbox->position != inputbox->oldposition))
55         {
56                 ui_mouse_hide();
57                 gr_set_current_canvas( inputbox->canvas );
58
59                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)inputbox)
60                 {
61                         if (inputbox->first_time)
62                                 gr_set_fontcolor( CBLACK, CRED );
63                         else
64                                 gr_set_fontcolor( CRED, CBLACK );
65                 }
66                 else
67                         gr_set_fontcolor( CWHITE, CBLACK );
68
69                 inputbox->status = 0;
70
71                 gr_string( 2, 2, inputbox->text );
72                 gr_get_string_size(inputbox->text, &w, &h, &aw  );
73
74                 gr_setcolor( CBLACK );
75                 gr_rect( 2+w, 0, inputbox->width-1, inputbox->height-1 );
76
77                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)inputbox  && !inputbox->first_time )
78                 {
79                         gr_setcolor(CRED);
80                         Vline( 2,inputbox->height-3, 2+w+1 );
81                         Vline( 2,inputbox->height-3, 2+w+2 );
82                 }
83
84                 ui_mouse_show();
85         }
86 }
87
88 UI_GADGET_INPUTBOX * ui_add_gadget_inputbox( UI_WINDOW * wnd, short x, short y, short length, short slength, char * text )
89 {
90         int h, w, aw, f;
91         UI_GADGET_INPUTBOX * inputbox;
92
93         gr_get_string_size( NULL, &w, &h, &aw );
94
95         inputbox = (UI_GADGET_INPUTBOX *)ui_gadget_add( wnd, 6, x, y, x+aw*slength-1, y+h-1+4 );
96
97         f = 0;
98
99         inputbox->text = d_malloc(length + 1);
100         strncpy( inputbox->text, text, length );
101         inputbox->position = strlen(inputbox->text);
102         inputbox->oldposition = inputbox->position;
103         inputbox->width = aw*slength;
104         inputbox->height = h+4;
105         inputbox->length = length;
106         inputbox->slength = slength;
107         inputbox->pressed = 0;
108         inputbox->first_time = 1;
109
110         gr_set_current_canvas( inputbox->canvas );
111         gr_setcolor( CBLACK );
112         gr_rect( 0, 0, inputbox->width-1, inputbox->height-1 );
113
114         return inputbox;
115
116 }
117
118
119 void ui_inputbox_do( UI_GADGET_INPUTBOX * inputbox, int keypress )
120 {
121         unsigned char ascii;
122         inputbox->oldposition = inputbox->position;
123
124         inputbox->pressed=0;
125
126         if (CurWindow->keyboard_focus_gadget==(UI_GADGET *)inputbox)
127         {
128                 switch( keypress )
129                 {
130                 case 0:
131                         break;
132                 case (KEY_LEFT):
133                 case (KEY_BACKSP):
134                         if (inputbox->position > 0)
135                                 inputbox->position--;
136                         inputbox->text[inputbox->position] = 0;
137                         inputbox->status = 1;
138                         if (inputbox->first_time) inputbox->first_time = 0;
139                         break;
140                 case (KEY_ENTER):
141                         inputbox->pressed=1;
142                         inputbox->status = 1;
143                         if (inputbox->first_time) inputbox->first_time = 0;
144                         break;
145                 default:
146                         ascii = key_to_ascii(keypress);
147                         if ((ascii < 255 ) && (inputbox->position < inputbox->length-2))
148                         {
149                                 if (inputbox->first_time) {
150                                         inputbox->first_time = 0;
151                                         inputbox->position = 0;
152                                 }
153                                 inputbox->text[inputbox->position++] = ascii;
154                                 inputbox->text[inputbox->position] = 0;
155                         }
156                         inputbox->status = 1;
157                         break;
158                 }
159         } else {
160                 inputbox->first_time = 1;
161         }
162
163         last_keypress=0;
164         
165         ui_draw_inputbox( inputbox );
166
167 }