Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
textinputdisplay.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #include "textinputdisplay.h"
23 #include "lin_time.h"
24 #include "guidefs.h"
25 using std::vector;
26 using std::string;
27 
28 TextInputDisplay::TextInputDisplay( vector< unsigned int > *keyboard_input_queue, const char *disallowed )
29 {
30  isFocused = false;
31  if (keyboard_input_queue)
32  this->keyboard_queue = keyboard_input_queue;
33  else
34  this->keyboard_queue = &local_keyboard_queue;
35  passwordChar = '\0';
36  keyboard_input_queue->clear();
37  this->disallowed = new char[strlen( disallowed )+1];
38  strcpy( this->disallowed, disallowed );
39 }
40 
42 {
43  if (event.code != WHEELUP_MOUSE_BUTTON && event.code != WHEELDOWN_MOUSE_BUTTON)
44  //If click is on me, set me focused... otherwise, clear my focus.
45  this->isFocused = ( hitTest( event.loc ) );
46  return StaticDisplay::processMouseDown( event );
47 }
48 
50 {
51  if (event.code != WHEELUP_MOUSE_BUTTON && event.code != WHEELDOWN_MOUSE_BUTTON)
52  //If click is on me, set me focused... otherwise, clear my focus.
53  this->isFocused = false;
55 }
56 
58 {
59  return true;
60 }
61 
63 {
64  string text = this->text();
65  if (!this->isFocused) {
66  if (passwordChar) {
67  string text1;
68  text1.insert( 0u, text.length(), passwordChar );
69  this->setText( text1 );
70  }
71  this->StaticDisplay::draw();
72  if (passwordChar)
73  this->setText( text );
74  return;
75  }
76  size_t LN = keyboard_queue->size();
77  for (size_t i = 0; i < LN; ++i) {
78  unsigned int c = (*keyboard_queue)[i];
79  if ( !processKeypress( c ) ) continue;
80  if (c == 8 || c == 127) {
81  text = text.substr( 0, text.length()-1 );
82  } else if (c != '\0' && c < 256) {
83  bool allowed = true;
84  for (int j = 0; disallowed[j]; ++j)
85  if ( c == static_cast<unsigned int>(disallowed[j]) ) {
86  allowed = false;
87  break;
88  }
89  if (allowed) {
90  char tmp[2] = {0, 0};
91  tmp[0] = (char) c;
92  text += tmp;
93  }
94  }
95  }
96  keyboard_queue->clear();
97  unsigned int x = (unsigned int) getNewTime();
98  string text1;
99  if (passwordChar)
100  text1.insert( 0u, text.length(), passwordChar );
101  else
102  text1 = text;
103  if (x%2)
104  text1 += "|";
105  this->setText( text1 );
106  this->StaticDisplay::draw();
107  this->setText( text );
108 }
109 
111 {
112  delete[] this->disallowed;
113 }
114