2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Ui/INPUTBOX.cpp $
15 * Code to implement input boxes
18 * Revision 1.3 2004/09/20 01:31:45 theoddone33
21 * Revision 1.2 2002/06/09 04:41:29 relnev
22 * added copyright header
24 * Revision 1.1.1.1 2002/05/03 03:28:11 root
28 * 8 9/07/99 4:01p Dave
29 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
30 * does everything properly (setting up address when binding). Remove
31 * black rectangle background from UI_INPUTBOX.
33 * 7 7/15/99 7:14p Jefff
35 * 6 6/25/99 11:59a Dave
36 * Multi options screen.
38 * 5 2/11/99 3:08p Dave
39 * PXO refresh button. Very preliminary squad war support.
41 * 4 12/02/98 5:47p Dave
42 * Put in interface xstr code. Converted barracks screen to new format.
44 * 3 10/13/98 9:29a Dave
45 * Started neatening up freespace.h. Many variables renamed and
46 * reorganized. Added AlphaColors.[h,cpp]
48 * 2 10/07/98 10:54a Dave
51 * 1 10/07/98 10:51a Dave
53 * 43 5/14/98 6:29p Hoffoss
54 * Fixed some warnings a release rebuild all turned up.
56 * 42 4/14/98 5:06p Dave
57 * Don't load or send invalid pilot pics. Fixed chatbox graphic errors.
58 * Made chatbox display team icons in a team vs. team game. Fixed up pause
59 * and endgame sequencing issues.
61 * 41 4/12/98 5:31p Lawrance
62 * use timer_get_milliseconds() instead of gettime()
64 * 40 4/10/98 5:36p Dave
65 * Put in user notification of illegal values in multi host options
66 * screen. Fixed server respawn ship class problem.
68 * 39 4/09/98 3:10p Mike
69 * Fix shockingly stupid typo.
71 * 38 4/09/98 2:46p Mike
72 * Fix bug preventing first letter in pilot name being capitalized.
74 * 37 4/09/98 12:12p Mike
75 * Separate versioning for demo and full versions.
78 * 36 4/06/98 5:14p Hoffoss
79 * Added some needed asserts.
81 * 35 4/06/98 4:25p Allender
82 * Fix strncpy bug in inputbox
84 * 34 4/06/98 3:58p Frank
85 * added initialization to try and fix a bug.
87 * 33 4/01/98 10:26a Hoffoss
88 * Changed input box code to not draw a cursor if the input box is
91 * 32 3/27/98 4:01p Hoffoss
92 * Fixed bug where input box text wsn't being null terminated! Bad!
94 * 31 3/27/98 3:04p Mitri
95 * Fixed bug in set_text(). 'length' is the number of characters allowed,
96 * EXCLUDING the null terminator.
98 * 30 3/27/98 11:20a Hoffoss
99 * Changed input to use bright white text instead of bright green.
101 * 29 3/10/98 11:29a Hoffoss
102 * Make first_time ignored and text not disappear after a return is
105 * 28 2/26/98 4:21p Dave
106 * More robust multiplayer voice.
108 * 27 1/23/98 5:43p Dave
109 * Finished bringing standalone up to speed. Coded in new host options
112 * 26 1/20/98 10:36a Hoffoss
113 * Fixed optimized warnings.
115 * 25 1/17/98 5:51p Dave
116 * Bug fixes for bugs generated by multiplayer testing.
118 * 24 1/16/98 7:57p Lawrance
119 * support animating input box cursor
121 * 23 1/15/98 5:12p Hoffoss
122 * Fixed inputbox so clicking on it gives it focus.
124 * 22 1/15/98 5:10p Allender
125 * ton of interface changes. chatbox in multiplayer now behaves
126 * differently than before. It's always active in any screen that uses
127 * it. Only non-printatble characters will get passed back out from
130 * 21 1/14/98 6:43p Hoffoss
131 * Massive changes to UI code. A lot cleaner and better now. Did all
132 * this to get the new UI_DOT_SLIDER to work properly, which the old code
133 * wasn't flexible enough to handle.
135 * 20 1/05/98 1:15p John
136 * Made inputbox skip the initial "selected" phase when you first tab to
139 * 19 12/22/97 5:08p Hoffoss
140 * Changed inputbox class to be able to accept only certain keys, changed
141 * pilot screens to utilize this feature. Added to assert with pilot file
144 * 18 12/11/97 8:15p Dave
145 * Put in network options screen. Xed out olf protocol selection screen.
147 * 17 12/08/97 6:22p Lawrance
148 * blink cursor on inputbox
150 * 16 12/06/97 4:27p Dave
151 * Another load of interface and multiplayer bug fixes.
153 * 15 11/25/97 3:51p Hoffoss
154 * Changed edit background rect position slightly.
156 * 14 10/24/97 10:58p Hoffoss
157 * Made some changes to the UI code to do some things I need it to do.
159 * 13 10/09/97 11:05a Allender
160 * ignore controled and alted keys in input box
162 * 12 10/01/97 4:39p Lawrance
163 * null out text when free'ed
165 * 11 9/09/97 3:39p Sandeep
166 * warning level 4 bugs
168 * 10 8/21/97 12:13p Dave
169 * Made it possible for input box to ignore esc to lose focus.
171 * 9 8/19/97 1:28p Dave
172 * Made it possible to limit characters by pixel width.
174 * 8 8/15/97 8:21p Dave
175 * Modified UI_INPUTBOX so that it is possible to draw it invisibly. That
176 * is, only the text is displayed.
178 * 7 8/11/97 9:48p Lawrance
179 * reset clip after drawing
181 * 6 7/01/97 2:10p Dave
182 * Fixed bug which caused the box to stop responding after user called
183 * set_text(...) with a 0 length string.
185 * 5 6/12/97 12:39p John
186 * made ui use freespace colors
188 * 4 6/11/97 1:13p John
189 * Started fixing all the text colors in the game.
191 * 3 12/03/96 3:46p Lawrance
192 * added ability to set contents of input box
194 * 2 11/15/96 11:43a John
196 * 1 11/15/96 8:21a John
206 #include "alphacolors.h"
210 #define INPUTBOX_PASSWD_CHAR '*' // the password protected char
212 // Retuen true if c is a letter, else return false.
213 int is_letter(char c)
215 return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
218 // insert character c into string s at position p.
219 void strcins(char *s, int p, char c)
222 for (n=strlen(s)-p; n>=0; n-- )
223 *(s+p+n+1) = *(s+p+n); // Move everything over
224 *(s+p) = c; // then insert the character
227 // delete n character from string s starting at position p
229 void strndel(char *s, int p, int n)
231 for (; (*(s+p) = *(s+p+n)) != '\0'; s++ )
232 *(s+p+n) = '\0'; // Delete and zero fill
235 void UI_INPUTBOX::init_cursor()
237 cursor_first_frame = bm_load_animation("cursor1", &cursor_nframes, &cursor_fps);
238 if ( cursor_first_frame < 0 ) {
239 Warning(LOCATION,"Cannot load input box cursor: cursor1.ani\n");
242 cursor_elapsed_time=0;
243 cursor_current_frame=0;
246 void UI_INPUTBOX::create(UI_WINDOW *wnd, int _x, int _y, int _w, int _text_len, const char *_text, int _flags, int pixel_lim, color *clr)
250 SDL_assert(_text_len >= 0);
251 SDL_assert((int) strlen(_text) <= _text_len);
252 gr_set_font(wnd->f_id);
253 gr_get_string_size( &tw, &th, "*" );
255 // check to see if the user passed in a text color otherwise use the default green color
259 text_color = &CBRIGHT;
262 base_create( wnd, UI_KIND_INPUTBOX, _x, _y, _w, th+4 );
263 text = (char *) malloc( _text_len + 1);
265 // input boxes no longer use background
266 _flags |= UI_INPUTBOX_FLAG_NO_BACK;
268 // if its in "password" mode, allocate a second string
270 if (_flags & UI_INPUTBOX_FLAG_PASSWD) {
271 passwd_text = (char *) malloc(_text_len + 1);
272 memset(passwd_text, INPUTBOX_PASSWD_CHAR, strlen(_text));
273 passwd_text[strlen(_text)] = 0;
281 if ( _text_len > 0 ) {
282 SDL_strlcpy( text, _text, _text_len+1 );
285 position = strlen(_text);
286 oldposition = position;
292 pixel_limit = pixel_lim;
295 invalid_chars = NULL;
298 void UI_INPUTBOX::set_valid_chars(const char *vchars)
300 // free up any existing string
301 if(valid_chars != NULL){
306 valid_chars = strdup(vchars);
309 void UI_INPUTBOX::set_invalid_chars(const char *ichars)
311 // free up any existing string
312 if(invalid_chars != NULL){
314 invalid_chars = NULL;
317 invalid_chars = strdup(ichars);
320 void UI_INPUTBOX::destroy()
327 // free any valid chars
328 if(valid_chars != NULL){
333 // free any invalid chars
334 if(invalid_chars != NULL){
336 invalid_chars = NULL;
339 if ((flags & UI_INPUTBOX_FLAG_PASSWD) && passwd_text) {
344 UI_GADGET::destroy();
347 void UI_INPUTBOX::draw()
349 int invis, w1, h1, tw, th;
358 invis = flags & UI_INPUTBOX_FLAG_INVIS;
360 gr_set_font(my_wnd->f_id);
362 if (!invis && !(flags & UI_INPUTBOX_FLAG_NO_BACK)) {
363 // draw the entire text box region
364 ui_draw_sunken_border( x-2, y-2, x+w+1, y+h+1 );
365 gr_set_color_fast( &CBLACK );
366 gr_rect( 0, 0, w, h );
369 gr_set_clip( x + 1, y + 1, w1 + 1, h1 + 1 );
371 gr_set_clip( x - 1, y - 1, w1 + 1, h1 + 1 );
374 if (flags & UI_INPUTBOX_FLAG_PASSWD){
375 gr_get_string_size(&tw, &th, passwd_text);
377 gr_get_string_size(&tw, &th, text);
380 // If first_time is set, that means this input box got
381 // focus, but nothing is typed yet, so all the text is
382 // selected, if you type a character it will replace the
383 // text, if you type an arrow it will unselect it.
384 // So it needs to be colored differently to show this.
385 if (!disabled_flag && !(flags & UI_INPUTBOX_FLAG_NO_BACK)) {
386 // if ( (my_wnd->selected_gadget == this) && first_time ) {
387 // gr_set_color_fast( text_color );
390 gr_set_color_fast( &CBLACK );
393 // color the background behind the text
394 gr_rect( 0, 0, tw + 1, th );
397 if ( (my_wnd->selected_gadget == this) || disabled_flag ) {
398 gr_set_color_fast(text_color);
400 gr_set_color_fast(&CWHITE);
403 // coords of where to draw the text
406 if(flags & UI_INPUTBOX_FLAG_TEXT_CEN){
407 // if we fit within the text area, draw it centered
409 text_x += (w1 - tw)/2;
414 if (flags & UI_INPUTBOX_FLAG_PASSWD){
415 gr_string(text_x, text_y, passwd_text);
417 gr_string(text_x, text_y, text);
421 if (!disabled_flag) {
422 if (my_wnd->selected_gadget == this) {
423 if (cursor_first_frame == -1) {
424 gr_set_color_fast(text_color);
425 ui_vline(1, h1, text_x + tw + 4);
426 ui_vline(1, h1, text_x + tw + 5);
428 // draw animating cursor
429 int time_delta = timer_get_milliseconds() - cursor_elapsed_time;
431 if ( (time_delta / 1000.0f) > (1.0f / cursor_fps) ) {
433 cursor_elapsed_time += time_delta;
434 cursor_current_frame++;
435 if (cursor_current_frame >= cursor_nframes) {
436 cursor_current_frame = 0;
440 // draw current frame
441 gr_set_bitmap(cursor_first_frame + cursor_current_frame, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
442 gr_bitmap(text_x + tw + 4, 1);
450 int UI_INPUTBOX::validate_input(int chr)
452 if (chr < 32) { // weed out control characters
456 // if we're disallowing letters altogether
457 if((flags & UI_INPUTBOX_FLAG_NO_LETTERS) && isalpha(chr)){
461 // if we're disallowing numbers altogether
462 if((flags & UI_INPUTBOX_FLAG_NO_NUMERALS) && isdigit(chr)){
466 // otherwise allow numbers and alpha chars by
467 if (isdigit(chr) || isalpha(chr)){
471 // if we have specified no valid or invalid chars, accept everything
472 if(!valid_chars && !invalid_chars){
476 // otherwise compare against the valid chars list
477 if((valid_chars) && SDL_strchr(valid_chars, chr)){
481 // otherwise compare against the invalid chars list0
482 if((invalid_chars) && !SDL_strchr(invalid_chars,chr)){
489 void UI_INPUTBOX::process(int focus)
491 int ascii, clear_lastkey, key, key_used, key_check;
493 // check if mouse is pressed
494 if (B1_PRESSED && is_mouse_on()) {
502 if (my_wnd->selected_gadget == this)
509 oldposition = position;
511 clear_lastkey = (flags & UI_INPUTBOX_FLAG_KEYTHRU) ? 0 : 1;
514 key = my_wnd->keypress;
525 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
526 passwd_text[position] = 0;
548 if (flags & UI_INPUTBOX_FLAG_ESC_CLR){
559 if (flags & UI_INPUTBOX_FLAG_ESC_FOC) {
567 // MWA -- determine if alt or ctrl held down on this key and don't process if it is. We
568 // need to be able to pass these keys back to the top level. (And anyway -- ctrl-a shouldn't
569 // print out an A in the input window
570 if ( key & (KEY_ALTED | KEY_CTRLED) ) {
575 // get an ascii char from the input if possible
576 key_check = my_wnd->keypress_text;
578 ascii = validate_input(key_check);
579 if ((ascii > 0) && (ascii < 255)) {
580 if (flags & UI_INPUTBOX_FLAG_LETTER_FIRST) {
581 if ((position == 0) && !is_letter((char) ascii))
586 // if (should_reset) {
596 if ( position < length ) {
597 text[position] = (char) ascii;
598 text[position + 1] = 0;
600 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
601 passwd_text[position] = (char) INPUTBOX_PASSWD_CHAR;
602 passwd_text[position + 1] = 0;
607 // check to see if we should limit by pixel width
608 if (pixel_limit > -1) {
611 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
612 gr_get_string_size(&w, NULL, passwd_text);
615 gr_get_string_size(&w, NULL, text);
618 if (w > pixel_limit) {
623 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
624 passwd_text[position] = 0;
637 if (clear_lastkey || (key_used && (flags & UI_INPUTBOX_FLAG_EAT_USED)) )
638 my_wnd->last_keypress=0;
645 int UI_INPUTBOX::changed()
650 int UI_INPUTBOX::pressed()
655 void UI_INPUTBOX::get_text(char *out)
657 SDL_strlcpy(out, text, length+1);
660 void UI_INPUTBOX::set_text(const char *in)
665 SDL_strlcpy(text, "", length+1);
671 in_length = strlen(in);
672 if (in_length > length)
673 SDL_assert(0); // tried to force text into an input box that won't fit into allocated memory
675 SDL_strlcpy(text, in, length+1);
677 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
678 memset(passwd_text, INPUTBOX_PASSWD_CHAR, strlen(text));
679 passwd_text[strlen(text)] = 0;
682 position = in_length; // fixes the zero-length-I-don't-think-so bug