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