2 * $Logfile: /Freespace2/code/Ui/INPUTBOX.cpp $
7 * Code to implement input boxes
10 * Revision 1.1 2002/05/03 03:28:11 root
14 * 8 9/07/99 4:01p Dave
15 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
16 * does everything properly (setting up address when binding). Remove
17 * black rectangle background from UI_INPUTBOX.
19 * 7 7/15/99 7:14p Jefff
21 * 6 6/25/99 11:59a Dave
22 * Multi options screen.
24 * 5 2/11/99 3:08p Dave
25 * PXO refresh button. Very preliminary squad war support.
27 * 4 12/02/98 5:47p Dave
28 * Put in interface xstr code. Converted barracks screen to new format.
30 * 3 10/13/98 9:29a Dave
31 * Started neatening up freespace.h. Many variables renamed and
32 * reorganized. Added AlphaColors.[h,cpp]
34 * 2 10/07/98 10:54a Dave
37 * 1 10/07/98 10:51a Dave
39 * 43 5/14/98 6:29p Hoffoss
40 * Fixed some warnings a release rebuild all turned up.
42 * 42 4/14/98 5:06p Dave
43 * Don't load or send invalid pilot pics. Fixed chatbox graphic errors.
44 * Made chatbox display team icons in a team vs. team game. Fixed up pause
45 * and endgame sequencing issues.
47 * 41 4/12/98 5:31p Lawrance
48 * use timer_get_milliseconds() instead of gettime()
50 * 40 4/10/98 5:36p Dave
51 * Put in user notification of illegal values in multi host options
52 * screen. Fixed server respawn ship class problem.
54 * 39 4/09/98 3:10p Mike
55 * Fix shockingly stupid typo.
57 * 38 4/09/98 2:46p Mike
58 * Fix bug preventing first letter in pilot name being capitalized.
60 * 37 4/09/98 12:12p Mike
61 * Separate versioning for demo and full versions.
64 * 36 4/06/98 5:14p Hoffoss
65 * Added some needed asserts.
67 * 35 4/06/98 4:25p Allender
68 * Fix strncpy bug in inputbox
70 * 34 4/06/98 3:58p Frank
71 * added initialization to try and fix a bug.
73 * 33 4/01/98 10:26a Hoffoss
74 * Changed input box code to not draw a cursor if the input box is
77 * 32 3/27/98 4:01p Hoffoss
78 * Fixed bug where input box text wsn't being null terminated! Bad!
80 * 31 3/27/98 3:04p Mitri
81 * Fixed bug in set_text(). 'length' is the number of characters allowed,
82 * EXCLUDING the null terminator.
84 * 30 3/27/98 11:20a Hoffoss
85 * Changed input to use bright white text instead of bright green.
87 * 29 3/10/98 11:29a Hoffoss
88 * Make first_time ignored and text not disappear after a return is
91 * 28 2/26/98 4:21p Dave
92 * More robust multiplayer voice.
94 * 27 1/23/98 5:43p Dave
95 * Finished bringing standalone up to speed. Coded in new host options
98 * 26 1/20/98 10:36a Hoffoss
99 * Fixed optimized warnings.
101 * 25 1/17/98 5:51p Dave
102 * Bug fixes for bugs generated by multiplayer testing.
104 * 24 1/16/98 7:57p Lawrance
105 * support animating input box cursor
107 * 23 1/15/98 5:12p Hoffoss
108 * Fixed inputbox so clicking on it gives it focus.
110 * 22 1/15/98 5:10p Allender
111 * ton of interface changes. chatbox in multiplayer now behaves
112 * differently than before. It's always active in any screen that uses
113 * it. Only non-printatble characters will get passed back out from
116 * 21 1/14/98 6:43p Hoffoss
117 * Massive changes to UI code. A lot cleaner and better now. Did all
118 * this to get the new UI_DOT_SLIDER to work properly, which the old code
119 * wasn't flexible enough to handle.
121 * 20 1/05/98 1:15p John
122 * Made inputbox skip the initial "selected" phase when you first tab to
125 * 19 12/22/97 5:08p Hoffoss
126 * Changed inputbox class to be able to accept only certain keys, changed
127 * pilot screens to utilize this feature. Added to assert with pilot file
130 * 18 12/11/97 8:15p Dave
131 * Put in network options screen. Xed out olf protocol selection screen.
133 * 17 12/08/97 6:22p Lawrance
134 * blink cursor on inputbox
136 * 16 12/06/97 4:27p Dave
137 * Another load of interface and multiplayer bug fixes.
139 * 15 11/25/97 3:51p Hoffoss
140 * Changed edit background rect position slightly.
142 * 14 10/24/97 10:58p Hoffoss
143 * Made some changes to the UI code to do some things I need it to do.
145 * 13 10/09/97 11:05a Allender
146 * ignore controled and alted keys in input box
148 * 12 10/01/97 4:39p Lawrance
149 * null out text when free'ed
151 * 11 9/09/97 3:39p Sandeep
152 * warning level 4 bugs
154 * 10 8/21/97 12:13p Dave
155 * Made it possible for input box to ignore esc to lose focus.
157 * 9 8/19/97 1:28p Dave
158 * Made it possible to limit characters by pixel width.
160 * 8 8/15/97 8:21p Dave
161 * Modified UI_INPUTBOX so that it is possible to draw it invisibly. That
162 * is, only the text is displayed.
164 * 7 8/11/97 9:48p Lawrance
165 * reset clip after drawing
167 * 6 7/01/97 2:10p Dave
168 * Fixed bug which caused the box to stop responding after user called
169 * set_text(...) with a 0 length string.
171 * 5 6/12/97 12:39p John
172 * made ui use freespace colors
174 * 4 6/11/97 1:13p John
175 * Started fixing all the text colors in the game.
177 * 3 12/03/96 3:46p Lawrance
178 * added ability to set contents of input box
180 * 2 11/15/96 11:43a John
182 * 1 11/15/96 8:21a John
192 #include "alphacolors.h"
195 #define INPUTBOX_PASSWD_CHAR '*' // the password protected char
197 // Retuen true if c is a letter, else return false.
198 int is_letter(char c)
200 return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
203 // if the passed key is keypad number, return the ascii value, otherwise -1
204 int keypad_to_ascii(int c)
208 return key_to_ascii(KEY_0);
211 return key_to_ascii(KEY_1);
214 return key_to_ascii(KEY_2);
217 return key_to_ascii(KEY_3);
220 return key_to_ascii(KEY_4);
223 return key_to_ascii(KEY_5);
226 return key_to_ascii(KEY_6);
229 return key_to_ascii(KEY_7);
232 return key_to_ascii(KEY_8);
235 return key_to_ascii(KEY_9);
238 return key_to_ascii(KEY_PERIOD);
246 // insert character c into string s at position p.
247 void strcins(char *s, int p, char c)
250 for (n=strlen(s)-p; n>=0; n-- )
251 *(s+p+n+1) = *(s+p+n); // Move everything over
252 *(s+p) = c; // then insert the character
255 // delete n character from string s starting at position p
257 void strndel(char *s, int p, int n)
259 for (; (*(s+p) = *(s+p+n)) != '\0'; s++ )
260 *(s+p+n) = '\0'; // Delete and zero fill
263 void UI_INPUTBOX::init_cursor()
265 cursor_first_frame = bm_load_animation("cursor1", &cursor_nframes, &cursor_fps);
266 if ( cursor_first_frame < 0 ) {
267 Warning(LOCATION,"Cannot load input box cursor: cursor1.ani\n");
270 cursor_elapsed_time=0;
271 cursor_current_frame=0;
274 void UI_INPUTBOX::create(UI_WINDOW *wnd, int _x, int _y, int _w, int _text_len, char *_text, int _flags, int pixel_lim, color *clr)
278 Assert(_text_len >= 0);
279 Assert((int) strlen(_text) <= _text_len);
280 gr_set_font(wnd->f_id);
281 gr_get_string_size( &tw, &th, "*" );
283 // check to see if the user passed in a text color otherwise use the default green color
287 text_color = &CBRIGHT;
290 base_create( wnd, UI_KIND_INPUTBOX, _x, _y, _w, th+4 );
291 text = (char *) malloc( _text_len + 1);
293 // input boxes no longer use background
294 _flags |= UI_INPUTBOX_FLAG_NO_BACK;
296 // if its in "password" mode, allocate a second string
298 if (_flags & UI_INPUTBOX_FLAG_PASSWD) {
299 passwd_text = (char *) malloc(_text_len + 1);
300 memset(passwd_text, INPUTBOX_PASSWD_CHAR, strlen(_text));
301 passwd_text[strlen(_text)] = 0;
309 if ( _text_len > 0 ) {
310 strncpy( text, _text, _text_len );
313 position = strlen(_text);
314 oldposition = position;
320 pixel_limit = pixel_lim;
323 invalid_chars = NULL;
326 void UI_INPUTBOX::set_valid_chars(char *vchars)
328 // free up any existing string
329 if(valid_chars != NULL){
334 valid_chars = strdup(vchars);
337 void UI_INPUTBOX::set_invalid_chars(char *ichars)
339 // free up any existing string
340 if(invalid_chars != NULL){
342 invalid_chars = NULL;
345 invalid_chars = strdup(ichars);
348 void UI_INPUTBOX::destroy()
355 // free any valid chars
356 if(valid_chars != NULL){
361 // free any invalid chars
362 if(invalid_chars != NULL){
364 invalid_chars = NULL;
367 if ((flags & UI_INPUTBOX_FLAG_PASSWD) && passwd_text) {
372 UI_GADGET::destroy();
375 void UI_INPUTBOX::draw()
377 int invis, w1, h1, tw, th;
386 invis = flags & UI_INPUTBOX_FLAG_INVIS;
388 gr_set_font(my_wnd->f_id);
390 if (!invis && !(flags & UI_INPUTBOX_FLAG_NO_BACK)) {
391 // draw the entire text box region
392 ui_draw_sunken_border( x-2, y-2, x+w+1, y+h+1 );
393 gr_set_color_fast( &CBLACK );
394 gr_rect( 0, 0, w, h );
397 gr_set_clip( x + 1, y + 1, w1 + 1, h1 + 1 );
399 gr_set_clip( x - 1, y - 1, w1 + 1, h1 + 1 );
402 if (flags & UI_INPUTBOX_FLAG_PASSWD){
403 gr_get_string_size(&tw, &th, passwd_text);
405 gr_get_string_size(&tw, &th, text);
408 // If first_time is set, that means this input box got
409 // focus, but nothing is typed yet, so all the text is
410 // selected, if you type a character it will replace the
411 // text, if you type an arrow it will unselect it.
412 // So it needs to be colored differently to show this.
413 if (!disabled_flag && !(flags & UI_INPUTBOX_FLAG_NO_BACK)) {
414 // if ( (my_wnd->selected_gadget == this) && first_time ) {
415 // gr_set_color_fast( text_color );
418 gr_set_color_fast( &CBLACK );
421 // color the background behind the text
422 gr_rect( 0, 0, tw + 1, th );
425 if ( (my_wnd->selected_gadget == this) || disabled_flag ) {
426 gr_set_color_fast(text_color);
428 gr_set_color_fast(&CWHITE);
431 // coords of where to draw the text
434 if(flags & UI_INPUTBOX_FLAG_TEXT_CEN){
435 // if we fit within the text area, draw it centered
437 text_x += (w1 - tw)/2;
442 if (flags & UI_INPUTBOX_FLAG_PASSWD){
443 gr_string(text_x, text_y, passwd_text);
445 gr_string(text_x, text_y, text);
449 if (!disabled_flag) {
450 if (my_wnd->selected_gadget == this) {
451 if (cursor_first_frame == -1) {
452 gr_set_color_fast(text_color);
453 ui_vline(1, h1, text_x + tw + 4);
454 ui_vline(1, h1, text_x + tw + 5);
456 // draw animating cursor
457 int time_delta = timer_get_milliseconds() - cursor_elapsed_time;
459 if ( (time_delta / 1000.0f) > (1.0f / cursor_fps) ) {
461 cursor_elapsed_time += time_delta;
462 cursor_current_frame++;
463 if (cursor_current_frame >= cursor_nframes) {
464 cursor_current_frame = 0;
468 // draw current frame
469 gr_set_bitmap(cursor_first_frame + cursor_current_frame);
470 gr_bitmap(text_x + tw + 4, 1);
478 int UI_INPUTBOX::validate_input(int chr)
480 if (chr < 32) { // weed out control characters
484 // if we're disallowing letters altogether
485 if((flags & UI_INPUTBOX_FLAG_NO_LETTERS) && isalpha(chr)){
489 // if we're disallowing numbers altogether
490 if((flags & UI_INPUTBOX_FLAG_NO_NUMERALS) && isdigit(chr)){
494 // otherwise allow numbers and alpha chars by
495 if (isdigit(chr) || isalpha(chr)){
499 // if we have specified no valid or invalid chars, accept everything
500 if(!valid_chars && !invalid_chars){
504 // otherwise compare against the valid chars list
505 if((valid_chars) && strchr(valid_chars, chr)){
509 // otherwise compare against the invalid chars list0
510 if((invalid_chars) && !strchr(invalid_chars,chr)){
517 void UI_INPUTBOX::process(int focus)
519 int ascii, clear_lastkey, key, key_used, key_check;
521 // check if mouse is pressed
522 if (B1_PRESSED && is_mouse_on()) {
530 if (my_wnd->selected_gadget == this)
537 oldposition = position;
539 clear_lastkey = (flags & UI_INPUTBOX_FLAG_KEYTHRU) ? 0 : 1;
542 key = my_wnd->keypress;
553 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
554 passwd_text[position] = 0;
576 if (flags & UI_INPUTBOX_FLAG_ESC_CLR){
587 if (flags & UI_INPUTBOX_FLAG_ESC_FOC) {
595 // MWA -- determine if alt or ctrl held down on this key and don't process if it is. We
596 // need to be able to pass these keys back to the top level. (And anyway -- ctrl-a shouldn't
597 // print out an A in the input window
598 if ( key & (KEY_ALTED | KEY_CTRLED) ) {
603 // get an ascii char from the input if possible
604 key_check = keypad_to_ascii(key);
606 key_check = key_to_ascii(key);
609 ascii = validate_input(key_check);
610 if ((ascii > 0) && (ascii < 255)) {
611 if (flags & UI_INPUTBOX_FLAG_LETTER_FIRST) {
612 if ((position == 0) && !is_letter((char) ascii))
617 // if (should_reset) {
627 if ( position < length ) {
628 text[position] = (char) ascii;
629 text[position + 1] = 0;
631 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
632 passwd_text[position] = (char) INPUTBOX_PASSWD_CHAR;
633 passwd_text[position + 1] = 0;
638 // check to see if we should limit by pixel width
639 if (pixel_limit > -1) {
642 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
643 gr_get_string_size(&w, NULL, passwd_text);
646 gr_get_string_size(&w, NULL, text);
649 if (w > pixel_limit) {
654 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
655 passwd_text[position] = 0;
668 if (clear_lastkey || (key_used && (flags & UI_INPUTBOX_FLAG_EAT_USED)) )
669 my_wnd->last_keypress=0;
676 int UI_INPUTBOX::changed()
681 int UI_INPUTBOX::pressed()
686 void UI_INPUTBOX::get_text(char *out)
688 strncpy(out, text, length);
692 void UI_INPUTBOX::set_text(char *in)
696 in_length = strlen(in);
697 if (in_length > length)
698 Assert(0); // tried to force text into an input box that won't fit into allocated memory
702 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
703 memset(passwd_text, INPUTBOX_PASSWD_CHAR, strlen(text));
704 passwd_text[strlen(text)] = 0;
707 position = in_length; // fixes the zero-length-I-don't-think-so bug