]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/inputbox.cpp
get rid of some platform specific stuff
[taylor/freespace2.git] / src / ui / inputbox.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Ui/INPUTBOX.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code to implement input boxes
16  *
17  * $Log$
18  * Revision 1.3  2004/09/20 01:31:45  theoddone33
19  * GCC 3.4 fixes.
20  *
21  * Revision 1.2  2002/06/09 04:41:29  relnev
22  * added copyright header
23  *
24  * Revision 1.1.1.1  2002/05/03 03:28:11  root
25  * Initial import.
26  *
27  * 
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.
32  * 
33  * 7     7/15/99 7:14p Jefff
34  * 
35  * 6     6/25/99 11:59a Dave
36  * Multi options screen.
37  * 
38  * 5     2/11/99 3:08p Dave
39  * PXO refresh button. Very preliminary squad war support.
40  * 
41  * 4     12/02/98 5:47p Dave
42  * Put in interface xstr code. Converted barracks screen to new format.
43  * 
44  * 3     10/13/98 9:29a Dave
45  * Started neatening up freespace.h. Many variables renamed and
46  * reorganized. Added AlphaColors.[h,cpp]
47  * 
48  * 2     10/07/98 10:54a Dave
49  * Initial checkin.
50  * 
51  * 1     10/07/98 10:51a Dave
52  * 
53  * 43    5/14/98 6:29p Hoffoss
54  * Fixed some warnings a release rebuild all turned up.
55  * 
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.
60  * 
61  * 41    4/12/98 5:31p Lawrance
62  * use timer_get_milliseconds() instead of gettime()
63  * 
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.
67  * 
68  * 39    4/09/98 3:10p Mike
69  * Fix shockingly stupid typo.
70  * 
71  * 38    4/09/98 2:46p Mike
72  * Fix bug preventing first letter in pilot name being capitalized.
73  * 
74  * 37    4/09/98 12:12p Mike
75  * Separate versioning for demo and full versions.
76  * Fix inputbox bugs.
77  * 
78  * 36    4/06/98 5:14p Hoffoss
79  * Added some needed asserts.
80  * 
81  * 35    4/06/98 4:25p Allender
82  * Fix strncpy bug in inputbox
83  * 
84  * 34    4/06/98 3:58p Frank
85  * added initialization to try and fix a bug.
86  * 
87  * 33    4/01/98 10:26a Hoffoss
88  * Changed input box code to not draw a cursor if the input box is
89  * disabled.
90  * 
91  * 32    3/27/98 4:01p Hoffoss
92  * Fixed bug where input box text wsn't being null terminated!  Bad!
93  * 
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.
97  * 
98  * 30    3/27/98 11:20a Hoffoss
99  * Changed input to use bright white text instead of bright green.
100  * 
101  * 29    3/10/98 11:29a Hoffoss
102  * Make first_time ignored and text not disappear after a return is
103  * pressed.
104  * 
105  * 28    2/26/98 4:21p Dave
106  * More robust multiplayer voice.
107  * 
108  * 27    1/23/98 5:43p Dave
109  * Finished bringing standalone up to speed. Coded in new host options
110  * screen.
111  * 
112  * 26    1/20/98 10:36a Hoffoss
113  * Fixed optimized warnings.
114  * 
115  * 25    1/17/98 5:51p Dave
116  * Bug fixes for bugs generated by multiplayer testing.
117  * 
118  * 24    1/16/98 7:57p Lawrance
119  * support animating input box cursor
120  * 
121  * 23    1/15/98 5:12p Hoffoss
122  * Fixed inputbox so clicking on it gives it focus.
123  * 
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
128  * chatbox
129  * 
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.
134  * 
135  * 20    1/05/98 1:15p John
136  * Made inputbox skip the initial "selected" phase when you first tab to
137  * it.
138  * 
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
142  * saving.
143  * 
144  * 18    12/11/97 8:15p Dave
145  * Put in network options screen. Xed out olf protocol selection screen.
146  * 
147  * 17    12/08/97 6:22p Lawrance
148  * blink cursor on inputbox
149  * 
150  * 16    12/06/97 4:27p Dave
151  * Another load of interface and multiplayer bug fixes.
152  * 
153  * 15    11/25/97 3:51p Hoffoss
154  * Changed edit background rect position slightly.
155  * 
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.
158  * 
159  * 13    10/09/97 11:05a Allender
160  * ignore controled and alted keys in input box
161  * 
162  * 12    10/01/97 4:39p Lawrance
163  * null out text when free'ed
164  * 
165  * 11    9/09/97 3:39p Sandeep
166  * warning level 4 bugs
167  * 
168  * 10    8/21/97 12:13p Dave
169  * Made it possible for input box to ignore esc to lose focus.
170  * 
171  * 9     8/19/97 1:28p Dave
172  * Made it possible to limit characters by pixel width.
173  * 
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.
177  * 
178  * 7     8/11/97 9:48p Lawrance
179  * reset clip after drawing
180  * 
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.
184  * 
185  * 5     6/12/97 12:39p John
186  * made ui use freespace colors
187  * 
188  * 4     6/11/97 1:13p John
189  * Started fixing all the text colors in the game.
190  * 
191  * 3     12/03/96 3:46p Lawrance
192  * added ability to set contents of input box
193  * 
194  * 2     11/15/96 11:43a John
195  * 
196  * 1     11/15/96 8:21a John
197  *
198  * $NoKeywords: $
199  */
200
201 #include <ctype.h>
202 #include "uidefs.h"
203 #include "ui.h"
204 #include "bmpman.h"
205 #include "timer.h"
206 #include "alphacolors.h"
207
208
209 #define INPUTBOX_PASSWD_CHAR        '*'   // the password protected char
210
211 //      Retuen true if c is a letter, else return false.
212 int is_letter(char c)
213 {
214         return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
215 }
216
217 // insert character c into string s at position p.
218 void strcins(char *s, int p, char c)
219 {
220         int n;
221         for (n=strlen(s)-p; n>=0; n-- )
222                 *(s+p+n+1) = *(s+p+n);   // Move everything over        
223         *(s+p) = c;         // then insert the character
224 }
225
226 // delete n character from string s starting at position p
227
228 void strndel(char *s, int p, int n)
229 {
230         for (; (*(s+p) = *(s+p+n)) != '\0'; s++ )
231                 *(s+p+n) = '\0';    // Delete and zero fill
232 }
233
234 void UI_INPUTBOX::init_cursor()
235 {
236         cursor_first_frame = bm_load_animation("cursor1", &cursor_nframes, &cursor_fps);
237         if ( cursor_first_frame < 0 ) {
238                 Warning(LOCATION,"Cannot load input box cursor: cursor1.ani\n");
239                 return;
240         }
241         cursor_elapsed_time=0;
242         cursor_current_frame=0;
243 }
244
245 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)
246 {
247         int tw, th;
248
249         SDL_assert(_text_len >= 0);
250         SDL_assert((int) strlen(_text) <= _text_len);
251         gr_set_font(wnd->f_id);
252         gr_get_string_size( &tw, &th, "*" );
253
254         // check to see if the user passed in a text color otherwise use the default green color
255         if (clr){
256                 text_color = clr;
257         } else {
258                 text_color = &CBRIGHT;
259         }
260
261         base_create( wnd, UI_KIND_INPUTBOX, _x, _y, _w, th+4 );
262         text = (char *) malloc( _text_len + 1);
263
264         // input boxes no longer use background
265         _flags |= UI_INPUTBOX_FLAG_NO_BACK;
266         
267         // if its in "password" mode, allocate a second string
268         // and copy it
269         if (_flags & UI_INPUTBOX_FLAG_PASSWD) {
270                 passwd_text = (char *) malloc(_text_len + 1);
271                 memset(passwd_text, INPUTBOX_PASSWD_CHAR, strlen(_text));
272                 passwd_text[strlen(_text)] = 0;
273
274         } else {
275                 passwd_text = NULL;
276         }
277
278         init_cursor();
279
280         if ( _text_len > 0 ) {
281                 strncpy( text, _text, _text_len );
282         }
283         text[_text_len] = 0;
284         position = strlen(_text);
285         oldposition = position;
286         length = _text_len;
287         pressed_down = 0;
288 //      first_time = 1;
289         changed_flag = 0;
290         flags = _flags;
291         pixel_limit = pixel_lim;
292         locked = 0;
293         valid_chars = NULL;
294         invalid_chars = NULL;
295 };
296
297 void UI_INPUTBOX::set_valid_chars(const char *vchars)
298 {
299         // free up any existing string
300         if(valid_chars != NULL){
301                 free(valid_chars);
302                 valid_chars = NULL;
303         }
304
305         valid_chars = strdup(vchars);
306 }
307
308 void UI_INPUTBOX::set_invalid_chars(const char *ichars)
309 {
310         // free up any existing string
311         if(invalid_chars != NULL){
312                 free(invalid_chars);
313                 invalid_chars = NULL;
314         }
315         
316         invalid_chars = strdup(ichars);
317 }
318
319 void UI_INPUTBOX::destroy()
320 {
321         if (text) {
322                 free(text);
323                 text = NULL;
324         }
325
326         // free any valid chars
327         if(valid_chars != NULL){
328                 free(valid_chars);
329                 valid_chars = NULL;
330         }
331
332         // free any invalid chars
333         if(invalid_chars != NULL){
334                 free(invalid_chars);
335                 invalid_chars = NULL;
336         }
337
338         if ((flags & UI_INPUTBOX_FLAG_PASSWD) && passwd_text) {
339                 free(passwd_text);
340                 passwd_text = NULL;
341         }
342
343         UI_GADGET::destroy();
344 }
345
346 void UI_INPUTBOX::draw()
347 {
348         int invis, w1, h1, tw, th;
349         int text_x, text_y;
350
351         if (hidden){
352                 return;
353         }
354
355         w1 = w;
356         h1 = h;
357         invis = flags & UI_INPUTBOX_FLAG_INVIS;
358
359         gr_set_font(my_wnd->f_id);
360         gr_reset_clip();
361         if (!invis && !(flags & UI_INPUTBOX_FLAG_NO_BACK)) {
362                 // draw the entire text box region
363                 ui_draw_sunken_border( x-2, y-2, x+w+1, y+h+1 );
364                 gr_set_color_fast( &CBLACK );
365                 gr_rect( 0, 0, w, h );
366                 w1 -= 4;
367                 h1 -= 4;
368                 gr_set_clip( x + 1, y + 1, w1 + 1, h1 + 1 );
369         } else {
370                 gr_set_clip( x - 1, y - 1, w1 + 1, h1 + 1 );
371         }
372
373         if (flags & UI_INPUTBOX_FLAG_PASSWD){
374                 gr_get_string_size(&tw, &th, passwd_text);
375         } else {
376                 gr_get_string_size(&tw, &th, text);
377         }
378
379         // If first_time is set, that means this input box got
380         // focus, but nothing is typed yet, so all the text is
381         // selected, if you type a character it will replace the
382         // text, if you type an arrow it will unselect it.
383         // So it needs to be colored differently to show this.
384         if (!disabled_flag && !(flags & UI_INPUTBOX_FLAG_NO_BACK)) {
385 //              if ( (my_wnd->selected_gadget == this) && first_time ) {
386 //                      gr_set_color_fast( text_color );
387
388 //              } else {
389                         gr_set_color_fast( &CBLACK );
390 //              }
391
392                 // color the background behind the text 
393                 gr_rect( 0, 0, tw + 1, th );
394         }
395
396         if      ( (my_wnd->selected_gadget == this) || disabled_flag ) {                
397                 gr_set_color_fast(text_color);
398         } else {
399                 gr_set_color_fast(&CWHITE);
400         }
401
402         // coords of where to draw the text
403         text_x = 1;
404         text_y = 1;
405         if(flags & UI_INPUTBOX_FLAG_TEXT_CEN){
406                 // if we fit within the text area, draw it centered
407                 if(tw <= w1 - 5){
408                         text_x += (w1 - tw)/2;
409                 }               
410         }
411
412         // draw the text
413         if (flags & UI_INPUTBOX_FLAG_PASSWD){
414                 gr_string(text_x, text_y, passwd_text);
415         } else {
416                 gr_string(text_x, text_y, text);
417         }
418
419         // draw the "cursor"
420         if (!disabled_flag) {
421                 if (my_wnd->selected_gadget == this) {
422                         if (cursor_first_frame == -1) {
423                                 gr_set_color_fast(text_color);
424                                 ui_vline(1, h1, text_x + tw + 4);
425                                 ui_vline(1, h1, text_x + tw + 5);
426                         } else {
427                                 // draw animating cursor
428                                 int time_delta = timer_get_milliseconds() - cursor_elapsed_time;
429
430                                 if ( (time_delta / 1000.0f) > (1.0f / cursor_fps) ) {
431                                         // advance frame
432                                         cursor_elapsed_time += time_delta;
433                                         cursor_current_frame++;
434                                         if (cursor_current_frame >= cursor_nframes) {
435                                                 cursor_current_frame = 0;
436                                         }
437                                 }
438
439                                 // draw current frame
440                                 gr_set_bitmap(cursor_first_frame + cursor_current_frame, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
441                                 gr_bitmap(text_x + tw + 4, 1);
442                         }
443                 }
444         }
445
446         gr_reset_clip();
447 }
448
449 int UI_INPUTBOX::validate_input(int chr)
450 {
451         if (chr < 32) {  // weed out control characters
452                 return 0;
453         }
454
455         // if we're disallowing letters altogether
456         if((flags & UI_INPUTBOX_FLAG_NO_LETTERS) && isalpha(chr)){
457                 return 0;
458         }
459
460         // if we're disallowing numbers altogether
461         if((flags & UI_INPUTBOX_FLAG_NO_NUMERALS) && isdigit(chr)){
462                 return 0;
463         }
464
465         // otherwise allow numbers and alpha chars by
466         if (isdigit(chr) || isalpha(chr)){
467                 return chr;
468         }
469
470         // if we have specified no valid or invalid chars, accept everything
471         if(!valid_chars && !invalid_chars){
472                 return chr;
473         }
474
475         // otherwise compare against the valid chars list
476         if((valid_chars) && strchr(valid_chars, chr)){
477                 return chr;
478         }
479
480         // otherwise compare against the invalid chars list0
481         if((invalid_chars) && !strchr(invalid_chars,chr)){
482                 return chr;
483         }
484
485         return 0;
486 }
487
488 void UI_INPUTBOX::process(int focus)
489 {
490         int ascii, clear_lastkey, key, key_used, key_check;     
491
492         // check if mouse is pressed
493         if (B1_PRESSED && is_mouse_on()) {
494                 set_focus();
495 //              first_time = 1;
496         }
497
498         if (disabled_flag)
499                 return;
500
501         if (my_wnd->selected_gadget == this)
502                 focus = 1;
503 //      else
504 //              first_time = 0;
505
506         key_used = 0;
507         changed_flag = 0;
508         oldposition = position;
509         pressed_down = 0;
510         clear_lastkey = (flags & UI_INPUTBOX_FLAG_KEYTHRU) ? 0 : 1;
511
512         if (focus) {
513                 key = my_wnd->keypress;
514                 switch (key) {
515                         case 0:
516                                 break;
517
518                         //case SDLK_LEFT:
519                         case SDLK_BACKSPACE:
520                                 if (position > 0)
521                                         position--;
522
523                                 text[position] = 0;
524                                 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
525                                         passwd_text[position] = 0;
526                                 }
527
528                                 changed_flag = 1;
529                                 key_used = 1;
530 //                              if (first_time)
531 //                                      first_time = 0;
532
533                                 break;
534
535                         case SDLK_RETURN:
536                                 pressed_down = 1;
537                                 locked = 0;
538                                 changed_flag = 1;
539                                 key_used = 1;
540 //                              if (first_time)
541 //                                      first_time = 0;
542
543 //                              should_reset = 1;
544                                 break;
545
546                         case SDLK_ESCAPE:
547                                 if (flags & UI_INPUTBOX_FLAG_ESC_CLR){
548                                         if (position > 0) {
549                                                 set_text("");
550                                                 key_used = 1;                                           
551
552                                         } else {
553                                                 key_used = 0;
554                                                 clear_lastkey = 0;
555                                         }
556                                 }
557
558                                 if (flags & UI_INPUTBOX_FLAG_ESC_FOC) {
559                                         clear_focus();
560                                 }
561
562                                 break;
563
564                         default:
565                                 if (!locked) {
566                                         // MWA -- determine if alt or ctrl held down on this key and don't process if it is.  We
567                                         // need to be able to pass these keys back to the top level.  (And anyway -- ctrl-a shouldn't
568                                         // print out an A in the input window
569                                         if ( key & (KEY_ALTED | KEY_CTRLED) ) {
570                                                 clear_lastkey = 0;
571                                                 break;
572                                         }
573
574                                         // get an ascii char from the input if possible
575                                         key_check = my_wnd->keypress_text;
576
577                                         ascii = validate_input(key_check);
578                                         if ((ascii > 0) && (ascii < 255)) {
579                                                 if (flags & UI_INPUTBOX_FLAG_LETTER_FIRST) {
580                                                         if ((position == 0) && !is_letter((char) ascii))
581                                                                 break;
582                                                 }
583
584                                                 key_used = 1;
585 //                                              if (should_reset) {
586 //                                                      should_reset = 0;
587 //                                                      position = 0;
588 //                                              }
589
590 //                                              if (first_time) {
591 //                                                      first_time = 0;
592 //                                                      position = 0;
593 //                                              }
594
595                                                 if ( position < length ) {
596                                                         text[position] = (char) ascii;
597                                                         text[position + 1] = 0;
598
599                                                         if (flags & UI_INPUTBOX_FLAG_PASSWD) {
600                                                                 passwd_text[position] = (char) INPUTBOX_PASSWD_CHAR;
601                                                                 passwd_text[position + 1] = 0;
602                                                         }
603
604                                                         position++;
605
606                                                         // check to see if we should limit by pixel width
607                                                         if (pixel_limit > -1) {
608                                                                 int w;
609
610                                                                 if (flags & UI_INPUTBOX_FLAG_PASSWD) {
611                                                                         gr_get_string_size(&w, NULL, passwd_text);                                                                      
612
613                                                                 } else {
614                                                                         gr_get_string_size(&w, NULL, text);                                                             
615                                                                 }
616
617                                                                 if (w > pixel_limit) {
618                                                                         position--;
619                                                                         locked = 1;
620                                                                         text[position] = 0;
621
622                                                                         if (flags & UI_INPUTBOX_FLAG_PASSWD) {
623                                                                                 passwd_text[position] = 0;
624                                                                         }
625                                                                 }
626                                                         }
627                                                 }
628
629                                                 changed_flag = 1;
630                                         }
631                                 }
632
633                                 break;
634                 }
635
636                 if (clear_lastkey || (key_used && (flags & UI_INPUTBOX_FLAG_EAT_USED)) )
637                         my_wnd->last_keypress=0;
638
639 //      } else {
640 //              first_time = 1;
641         }       
642 }
643
644 int UI_INPUTBOX::changed()
645 {               
646         return changed_flag;
647 }
648
649 int UI_INPUTBOX::pressed()
650 {       
651         return pressed_down;
652 }
653
654 void UI_INPUTBOX::get_text(char *out)
655 {
656         strncpy(out, text, length);
657         out[length] = 0;
658 }
659
660 void UI_INPUTBOX::set_text(const char *in)
661 {
662         int in_length;
663         
664         in_length = strlen(in);
665         if (in_length > length)
666                 SDL_assert(0);  // tried to force text into an input box that won't fit into allocated memory
667
668         strcpy(text, in);
669         
670         if (flags & UI_INPUTBOX_FLAG_PASSWD) {
671                 memset(passwd_text, INPUTBOX_PASSWD_CHAR, strlen(text));
672                 passwd_text[strlen(text)] = 0;
673         }
674
675         position = in_length;  // fixes the zero-length-I-don't-think-so bug
676 }
677
678