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