1 /* $Id: inputbox.c,v 1.6 2005-02-26 09:50:36 chris Exp $ */
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.
16 static char rcsid[] = "$Id: inputbox.c,v 1.6 2005-02-26 09:50:36 chris Exp $";
33 // insert character c into string s at position p.
34 void strcins(char *s, int p, char c)
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
42 // delete n character from string s starting at position p
44 void strndel(char *s, int p, int n)
46 for (; (*(s+p) = *(s+p+n)) != '\0'; s++ )
47 *(s+p+n) = '\0'; // Delete and zero fill
50 void ui_draw_inputbox( UI_GADGET_INPUTBOX * inputbox )
54 if ((inputbox->status==1) || (inputbox->position != inputbox->oldposition))
57 gr_set_current_canvas( inputbox->canvas );
59 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)inputbox)
61 if (inputbox->first_time)
62 gr_set_fontcolor( CBLACK, CRED );
64 gr_set_fontcolor( CRED, CBLACK );
67 gr_set_fontcolor( CWHITE, CBLACK );
71 gr_string( 2, 2, inputbox->text );
72 gr_get_string_size(inputbox->text, &w, &h, &aw );
74 gr_setcolor( CBLACK );
75 gr_rect( 2+w, 0, inputbox->width-1, inputbox->height-1 );
77 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)inputbox && !inputbox->first_time )
80 Vline( 2,inputbox->height-3, 2+w+1 );
81 Vline( 2,inputbox->height-3, 2+w+2 );
88 UI_GADGET_INPUTBOX * ui_add_gadget_inputbox( UI_WINDOW * wnd, short x, short y, short length, short slength, char * text )
91 UI_GADGET_INPUTBOX * inputbox;
93 gr_get_string_size( NULL, &w, &h, &aw );
95 inputbox = (UI_GADGET_INPUTBOX *)ui_gadget_add( wnd, 6, x, y, x+aw*slength-1, y+h-1+4 );
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;
110 gr_set_current_canvas( inputbox->canvas );
111 gr_setcolor( CBLACK );
112 gr_rect( 0, 0, inputbox->width-1, inputbox->height-1 );
119 void ui_inputbox_do( UI_GADGET_INPUTBOX * inputbox, int keypress )
122 inputbox->oldposition = inputbox->position;
126 if (CurWindow->keyboard_focus_gadget==(UI_GADGET *)inputbox)
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;
142 inputbox->status = 1;
143 if (inputbox->first_time) inputbox->first_time = 0;
146 ascii = key_to_ascii(keypress);
147 if ((ascii < 255 ) && (inputbox->position < inputbox->length-2))
149 if (inputbox->first_time) {
150 inputbox->first_time = 0;
151 inputbox->position = 0;
153 inputbox->text[inputbox->position++] = ascii;
154 inputbox->text[inputbox->position] = 0;
156 inputbox->status = 1;
160 inputbox->first_time = 1;
165 ui_draw_inputbox( inputbox );